From d1f80662a66c6bb38dec59a053d339e60a617890 Mon Sep 17 00:00:00 2001 From: Niko Kivel Date: Thu, 28 Jan 2021 11:41:12 +0100 Subject: [PATCH] Fix configure hosts show emptying /etc/hosts and setpersist CLI variable in configure hosts - Only open `/etc/hosts` when write intended - Correct `SetPersist` CLI variable --- internal/app/wwctl/configure/hosts/main.go | 15 ++++++++------- internal/app/wwctl/configure/hosts/root.go | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/app/wwctl/configure/hosts/main.go b/internal/app/wwctl/configure/hosts/main.go index 2f8e4c6f..bde5159f 100644 --- a/internal/app/wwctl/configure/hosts/main.go +++ b/internal/app/wwctl/configure/hosts/main.go @@ -83,13 +83,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error { w.Close() - w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - defer w.Close() - nodes, _ := n.FindAllNodes() replace.AllNodes = nodes @@ -97,6 +90,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error { replace.Fqdn = controller.Fqdn if SetShow == false { + // only open "/etc/hosts" when intended to write, as 'os.O_TRUNC' will empty the file otherwise. + w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + defer w.Close() + err = tmpl.Execute(w, replace) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) diff --git a/internal/app/wwctl/configure/hosts/root.go b/internal/app/wwctl/configure/hosts/root.go index 5be05e85..958bf57d 100644 --- a/internal/app/wwctl/configure/hosts/root.go +++ b/internal/app/wwctl/configure/hosts/root.go @@ -16,7 +16,7 @@ var ( func init() { baseCmd.PersistentFlags().BoolVarP(&SetShow, "show", "s", false, "Show configuration (don't update)") - baseCmd.PersistentFlags().BoolVar(&SetShow, "persist", false, "Persist the configuration and initialize the service") + baseCmd.PersistentFlags().BoolVar(&SetPersist, "persist", false, "Persist the configuration and initialize the service") } // GetRootCommand returns the root cobra.Command for the application.