From bfc48f7c725285f586067ba96079ab00ed3a8824 Mon Sep 17 00:00:00 2001 From: jcsiadal Date: Wed, 16 Feb 2022 16:26:15 +0000 Subject: [PATCH] Clean up show and add safe copy Signed-off-by: jcsiadal --- internal/pkg/configure/common.go | 32 +++++++---- internal/pkg/configure/dhcp.go | 36 +++++-------- internal/pkg/configure/nfs.go | 79 +++++++++++++-------------- internal/pkg/configure/ssh.go | 91 +++++++++++++++----------------- internal/pkg/configure/tftp.go | 50 ++++++++---------- internal/pkg/util/copyfile.go | 57 ++++++++++++++++++++ internal/pkg/util/util.go | 45 ---------------- 7 files changed, 197 insertions(+), 193 deletions(-) diff --git a/internal/pkg/configure/common.go b/internal/pkg/configure/common.go index 713e08be..fe6404b4 100644 --- a/internal/pkg/configure/common.go +++ b/internal/pkg/configure/common.go @@ -6,27 +6,39 @@ import ( "github.com/pkg/errors" ) -func Configure(s string, v bool) error { - if !v { +func Configure(serv string, show bool) error { + if !show { fmt.Printf("################################################################################\n") - fmt.Printf("Configuring: %s\n", s) + fmt.Printf("Configuring: %s\n", serv) } var err error - switch s { + switch serv { case "DHCP": - err = configureDHCP(v) + err = configureDHCP(show) case "hosts": - err = configureHosts(v) + err = configureHosts(show) case "NFS": - err = configureNFS(v) + if !show { + err = configureNFS() + } else { + showNFS() + } case "SSH": - err = configureSSH(v) + if !show { + err = configureSSH() + } else { + fmt.Printf("'ssh -s' is not yet implemented.\n") + } case "TFTP": - err = configureTFTP(v) + if !show { + err = configureTFTP() + } else { + fmt.Printf("'tftp -s' is not yet implemented.\n") + } } if err != nil { - return errors.Wrap(err, "Failed to configure "+s) + return errors.Wrap(err, "Failed to execute configure on "+serv) } return nil } diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index 1b7b2511..bda1fbf7 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -16,19 +16,21 @@ import ( "github.com/pkg/errors" ) -type dhcpTemplate struct { - Ipaddr string - Port string - RangeStart string - RangeEnd string - Network string - Netmask string - Nodes []node.NodeInfo -} - func configureDHCP(show bool) error { - var d dhcpTemplate - var templateFile string + + var ( + d struct { + Ipaddr string + Port string + RangeStart string + RangeEnd string + Network string + Netmask string + Nodes []node.NodeInfo + } + + templateFile string + ) nodeDB, err := node.New() if err != nil { @@ -42,16 +44,6 @@ func configureDHCP(show bool) error { os.Exit(1) } - if controller.Ipaddr == "" { - wwlog.Printf(wwlog.ERROR, "The Warewulf IP Address is not properly configured\n") - os.Exit(1) - } - - if controller.Netmask == "" { - wwlog.Printf(wwlog.ERROR, "The Warewulf Netmask is not properly configured\n") - os.Exit(1) - } - if !controller.Dhcp.Enabled { wwlog.Printf(wwlog.INFO, "This system is not configured as a Warewulf DHCP controller\n") os.Exit(1) diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index 6025f77b..6bdc7123 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -10,7 +10,7 @@ import ( "github.com/pkg/errors" ) -func configureNFS(show bool) error { +func configureNFS() error { controller, err := warewulfconf.New() if err != nil { @@ -18,52 +18,47 @@ func configureNFS(show bool) error { os.Exit(1) } - if controller.Network == "" { - wwlog.Printf(wwlog.ERROR, "Network must be defined in warewulf.conf to configure NFS\n") - os.Exit(1) - } - if controller.Netmask == "" { - wwlog.Printf(wwlog.ERROR, "Netmask must be defined in warewulf.conf to configure NFS\n") - os.Exit(1) - } - - if !show { - - if controller.Nfs.Enabled { - exports, err := os.OpenFile("/etc/exports", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - defer exports.Close() - - fmt.Fprintf(exports, "# This file was written by Warewulf (wwctl configure nfs)\n") - - for _, export := range controller.Nfs.ExportsExtended { - fmt.Fprintf(exports, "%s %s/%s(%s)\n", export.Path, controller.Network, controller.Netmask, export.ExportOptions) - } - - fmt.Printf("Enabling and restarting the NFS services\n") - if controller.Nfs.SystemdName == "" { - err := util.SystemdStart("nfs-server") - if err != nil { - return errors.Wrap(err, "failed to start nfs-server") - } - } else { - err := util.SystemdStart(controller.Nfs.SystemdName) - if err != nil { - return errors.Wrap(err, "failed to start") - } - } + if controller.Nfs.Enabled { + exports, err := os.OpenFile("/etc/exports", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) } - } else { - fmt.Printf("/etc/exports:\n") + defer exports.Close() + + fmt.Fprintf(exports, "# This file was written by Warewulf (wwctl configure nfs)\n") for _, export := range controller.Nfs.ExportsExtended { - fmt.Printf("%s %s/%s\n", export.Path, controller.Network, controller.Netmask) + fmt.Fprintf(exports, "%s %s/%s(%s)\n", export.Path, controller.Network, controller.Netmask, export.ExportOptions) + } + + fmt.Printf("Enabling and restarting the NFS services\n") + if controller.Nfs.SystemdName == "" { + err := util.SystemdStart("nfs-server") + if err != nil { + return errors.Wrap(err, "failed to start nfs-server") + } + } else { + err := util.SystemdStart(controller.Nfs.SystemdName) + if err != nil { + return errors.Wrap(err, "failed to start") + } } - fmt.Printf("\n") } return nil } + +func showNFS() { + controller, err := warewulfconf.New() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + fmt.Printf("/etc/exports:\n") + for _, export := range controller.Nfs.ExportsExtended { + fmt.Printf("%s %s/%s\n", export.Path, controller.Network, controller.Netmask) + } + fmt.Printf("\n") +} diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index cf699253..db6b810d 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -11,62 +11,59 @@ import ( "github.com/pkg/errors" ) -func configureSSH(show bool) error { - if !show { - if os.Getuid() == 0 { - fmt.Printf("Updating system keys\n") +func configureSSH() error { + if os.Getuid() == 0 { + fmt.Printf("Updating system keys\n") - wwkeydir := path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys") + "/" + wwkeydir := path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys") + "/" - err := os.MkdirAll(path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys"), 0755) - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not create base directory: %s\n", err) - os.Exit(1) - } - - for _, k := range [4]string{"rsa", "dsa", "ecdsa", "ed25519"} { - keytype := "ssh_host_" + k + "_key" - if !util.IsFile(path.Join(wwkeydir, keytype)) { - fmt.Printf("Setting up key: %s\n", keytype) - wwlog.Printf(wwlog.DEBUG, "Creating new %s key\n", keytype) - err = util.ExecInteractive("ssh-keygen", "-q", "-t", k, "-f", path.Join(wwkeydir, keytype), "-C", "", "-N", "") - if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed to exec ssh-keygen: %s\n", err) - return errors.Wrap(err, "failed to exec ssh-keygen command") - } - } else { - fmt.Printf("Skipping, key already exists: %s\n", keytype) - } - } - } else { - fmt.Printf("Updating user's keys\n") - } - - homeDir, err := os.UserHomeDir() + err := os.MkdirAll(path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys"), 0755) if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not obtain the user's home directory: %s\n", err) + wwlog.Printf(wwlog.ERROR, "Could not create base directory: %s\n", err) os.Exit(1) } - authorizedKeys := path.Join(homeDir, "/.ssh/authorized_keys") - rsaPriv := path.Join(homeDir, "/.ssh/id_rsa") - rsaPub := path.Join(homeDir, "/.ssh/id_rsa.pub") - - if !util.IsFile(authorizedKeys) { - fmt.Printf("Setting up: %s\n", authorizedKeys) - err = util.ExecInteractive("ssh-keygen", "-q", "-t", "rsa", "-f", rsaPriv, "-C", "", "-N", "") - if err != nil { - return errors.Wrap(err, "failed to exec ssh-keygen command") + for _, k := range [4]string{"rsa", "dsa", "ecdsa", "ed25519"} { + keytype := "ssh_host_" + k + "_key" + if !util.IsFile(path.Join(wwkeydir, keytype)) { + fmt.Printf("Setting up key: %s\n", keytype) + wwlog.Printf(wwlog.DEBUG, "Creating new %s key\n", keytype) + err = util.ExecInteractive("ssh-keygen", "-q", "-t", k, "-f", path.Join(wwkeydir, keytype), "-C", "", "-N", "") + if err != nil { + wwlog.Printf(wwlog.ERROR, "Failed to exec ssh-keygen: %s\n", err) + return errors.Wrap(err, "failed to exec ssh-keygen command") + } + } else { + fmt.Printf("Skipping, key already exists: %s\n", keytype) } - err := util.CopyFile(rsaPub, authorizedKeys) - if err != nil { - return errors.Wrap(err, "failed to copy keys") - } - } else { - fmt.Printf("Skipping, authorized_keys already exists: %s\n", authorizedKeys) } } else { - fmt.Printf("'ssh -s' is not yet implemented.\n") + fmt.Printf("Updating user's keys\n") } + + homeDir, err := os.UserHomeDir() + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not obtain the user's home directory: %s\n", err) + os.Exit(1) + } + + authorizedKeys := path.Join(homeDir, "/.ssh/authorized_keys") + rsaPriv := path.Join(homeDir, "/.ssh/id_rsa") + rsaPub := path.Join(homeDir, "/.ssh/id_rsa.pub") + + if !util.IsFile(authorizedKeys) { + fmt.Printf("Setting up: %s\n", authorizedKeys) + err = util.ExecInteractive("ssh-keygen", "-q", "-t", "rsa", "-f", rsaPriv, "-C", "", "-N", "") + if err != nil { + return errors.Wrap(err, "failed to exec ssh-keygen command") + } + err := util.CopyFile(rsaPub, authorizedKeys) + if err != nil { + return errors.Wrap(err, "failed to copy keys") + } + } else { + fmt.Printf("Skipping, authorized_keys already exists: %s\n", authorizedKeys) + } + return nil } diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 3f56db39..31faea0e 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -13,37 +13,33 @@ import ( var tftpdir string = path.Join(buildconfig.TFTPDIR(), "warewulf") -func configureTFTP(show bool) error { - if !show { - controller, err := warewulfconf.New() +func configureTFTP() error { + controller, err := warewulfconf.New() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + return err + } + + err = os.MkdirAll(tftpdir, 0755) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + return err + } + + fmt.Printf("Writing PXE files to: %s\n", tftpdir) + for _, f := range [4]string{"x86.efi", "i386.efi", "i386.kpxe", "arm64.efi"} { + err = util.CopyFileSafe(path.Join(buildconfig.DATADIR(), "warewulf", "ipxe", f), path.Join(tftpdir, f)) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) - return err } + return err + } - err = os.MkdirAll(tftpdir, 0755) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - return err - } - - fmt.Printf("Writing PXE files to: %s\n", tftpdir) - for _, f := range [4]string{"x86.efi", "i386.efi", "i386.kpxe", "arm64.efi"} { - err = util.CopyFile(path.Join(buildconfig.DATADIR(), "warewulf", "ipxe", f), path.Join(tftpdir, f)) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - return err - } - } - - fmt.Printf("Enabling and restarting the TFTP services\n") - err = util.SystemdStart(controller.Tftp.SystemdName) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - return err - } - } else { - fmt.Printf("'tftp -s' is not yet implemented.\n") + fmt.Printf("Enabling and restarting the TFTP services\n") + err = util.SystemdStart(controller.Tftp.SystemdName) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + return err } return nil diff --git a/internal/pkg/util/copyfile.go b/internal/pkg/util/copyfile.go index 2866710b..f0445c2a 100644 --- a/internal/pkg/util/copyfile.go +++ b/internal/pkg/util/copyfile.go @@ -3,6 +3,8 @@ package util import ( "io" "os" + "path" + "path/filepath" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) @@ -48,3 +50,58 @@ func CopyFile(src string, dst string) error { } return nil } + +func CopyFileSafe(src string, dst string) error { + var err error + // Don't overwrite existing files -- should add force overwrite switch + if _, err = os.Stat(dst); err == nil { + wwlog.Printf(wwlog.DEBUG, "Destination file %s exists.\n", dst) + } else { + err = CopyFile(src, dst) + } + return err +} + +func CopyFiles(source string, dest string) error { + err := filepath.Walk(source, func(location string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + wwlog.Printf(wwlog.DEBUG, "Creating directory: %s\n", location) + info, err := os.Stat(source) + if err != nil { + return err + } + + err = os.MkdirAll(path.Join(dest, location), info.Mode()) + if err != nil { + return err + } + err = CopyUIDGID(source, dest) + if err != nil { + return err + } + + } else { + wwlog.Printf(wwlog.DEBUG, "Writing file: %s\n", location) + + err := CopyFile(location, path.Join(dest, location)) + if err != nil { + return err + } + + } + + return nil + }) + + if err != nil { + return err + } + + return nil +} + +//TODO: func CopyRecursive ... diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 94ec4cb2..c4f121fa 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -9,7 +9,6 @@ import ( "net" "os" "os/exec" - "path" "path/filepath" "regexp" "syscall" @@ -73,50 +72,6 @@ func RandomString(n int) string { return string(b) } -func CopyFiles(source string, dest string) error { - err := filepath.Walk(source, func(location string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - wwlog.Printf(wwlog.DEBUG, "Creating directory: %s\n", location) - info, err := os.Stat(source) - if err != nil { - return err - } - - err = os.MkdirAll(path.Join(dest, location), info.Mode()) - if err != nil { - return err - } - err = CopyUIDGID(source, dest) - if err != nil { - return err - } - - } else { - wwlog.Printf(wwlog.DEBUG, "Writing file: %s\n", location) - - err := CopyFile(location, path.Join(dest, location)) - if err != nil { - return err - } - - } - - return nil - }) - - if err != nil { - return err - } - - return nil -} - -//TODO: func CopyRecursive ... - func IsDir(path string) bool { wwlog.Printf(wwlog.DEBUG, "Checking if path exists as a directory: %s\n", path)