From 7aee24e5c1a770694a5f4d2da7721aaa4f6003f5 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Tue, 22 Dec 2020 21:18:38 -0800 Subject: [PATCH] Clean up and optimize the node and profile sets --- internal/app/wwctl/node/set/main.go | 163 ++++--------------------- internal/app/wwctl/profile/set/main.go | 134 +++----------------- 2 files changed, 41 insertions(+), 256 deletions(-) diff --git a/internal/app/wwctl/node/set/main.go b/internal/app/wwctl/node/set/main.go index 81dd00a7..22298a56 100644 --- a/internal/app/wwctl/node/set/main.go +++ b/internal/app/wwctl/node/set/main.go @@ -66,175 +66,87 @@ func CobraRunE(cmd *cobra.Command, args []string) error { if SetComment != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting comment to: %s\n", n.Id.Get(), SetComment) - n.Comment.Set(SetComment) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetContainer != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting container name to: %s\n", n.Id.Get(), SetContainer) - n.ContainerName.Set(SetContainer) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetInit != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting init command to: %s\n", n.Id.Get(), SetInit) - n.Init.Set(SetInit) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetRoot != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting root to: %s\n", n.Id.Get(), SetRoot) - n.Root.Set(SetRoot) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetKernel != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting kernel to: %s\n", n.Id.Get(), SetKernel) - n.KernelVersion.Set(SetKernel) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetKernelArgs != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting kernel args to: %s\n", n.Id.Get(), SetKernelArgs) - n.KernelArgs.Set(SetKernelArgs) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetClusterName != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting cluster name to: %s\n", n.Id.Get(), SetClusterName) - n.ClusterName.Set(SetClusterName) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpxe != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting iPXE template to: %s\n", n.Id.Get(), SetIpxe) - n.Ipxe.Set(SetIpxe) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetRuntimeOverlay != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting runtime overlay to: %s\n", n.Id.Get(), SetRuntimeOverlay) - n.RuntimeOverlay.Set(SetRuntimeOverlay) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetSystemOverlay != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting system overlay to: %s\n", n.Id.Get(), SetSystemOverlay) - n.SystemOverlay.Set(SetSystemOverlay) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiIpaddr != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting IPMI IP address to: %s\n", n.Id.Get(), SetIpmiIpaddr) - n.IpmiIpaddr.Set(SetIpmiIpaddr) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiNetmask != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting IPMI netmask to: %s\n", n.Id.Get(), SetIpmiNetmask) - n.IpmiNetmask.Set(SetIpmiNetmask) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiGateway != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting IPMI gateway to: %s\n", n.Id.Get(), SetIpmiGateway) - n.IpmiGateway.Set(SetIpmiGateway) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiUsername != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting IPMI IP username to: %s\n", n.Id.Get(), SetIpmiUsername) - n.IpmiUserName.Set(SetIpmiUsername) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiPassword != "" { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting IPMI IP password to: %s\n", n.Id.Get(), SetIpmiPassword) - n.IpmiPassword.Set(SetIpmiPassword) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetDiscoverable == true { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting node to discoverable\n", n.Id.Get()) - n.Discoverable.SetB(true) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetUndiscoverable == true { wwlog.Printf(wwlog.VERBOSE, "Node: %s, Setting node to undiscoverable\n", n.Id.Get()) - n.Discoverable.SetB(false) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if len(SetAddProfile) > 0 { @@ -242,22 +154,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Printf(wwlog.VERBOSE, "Node: %s, adding profile to '%s'\n", n.Id.Get(), p) n.Profiles = util.SliceAddUniqueElement(n.Profiles, p) } - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if len(SetDelProfile) > 0 { for _, p := range SetDelProfile { wwlog.Printf(wwlog.VERBOSE, "Node: %s, deleting profile from '%s'\n", n.Id.Get(), p) n.Profiles = util.SliceRemoveElement(n.Profiles, p) } - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetNetDevDel == true { @@ -272,13 +175,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Node: %s, Deleting network device: %s\n", n.Id.Get(), SetNetDev) - delete(n.NetDevs, SetNetDev) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetIpaddr != "" { if SetNetDev == "" { @@ -292,13 +189,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Node: %s:%s, Setting Ipaddr to: %s\n", n.Id.Get(), SetNetDev, SetIpaddr) - n.NetDevs[SetNetDev].Ipaddr.Set(SetIpaddr) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetNetmask != "" { if SetNetDev == "" { @@ -312,13 +203,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Node: %s:%s, Setting netmask to: %s\n", n.Id.Get(), SetNetDev, SetNetmask) - n.NetDevs[SetNetDev].Netmask.Set(SetNetmask) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetGateway != "" { if SetNetDev == "" { @@ -332,13 +217,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Node: %s:%s, Setting gateway to: %s\n", n.Id.Get(), SetNetDev, SetGateway) - n.NetDevs[SetNetDev].Gateway.Set(SetGateway) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetHwaddr != "" { if SetNetDev == "" { @@ -352,13 +231,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Node: %s:%s, Setting HW address to: %s\n", n.Id.Get(), SetNetDev, SetHwaddr) - n.NetDevs[SetNetDev].Hwaddr.Set(SetHwaddr) - err := nodeDB.NodeUpdate(n) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } + } + + err := nodeDB.NodeUpdate(n) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) } } diff --git a/internal/app/wwctl/profile/set/main.go b/internal/app/wwctl/profile/set/main.go index 551e6e28..5025a3ff 100644 --- a/internal/app/wwctl/profile/set/main.go +++ b/internal/app/wwctl/profile/set/main.go @@ -70,145 +70,72 @@ func CobraRunE(cmd *cobra.Command, args []string) error { if SetComment != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting comment to: %s\n", p.Id, SetComment) - p.Comment.Set(SetComment) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetClusterName != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting cluster name to: %s\n", p.Id, SetClusterName) - p.ClusterName.Set(SetClusterName) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetContainer != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting Container name to: %s\n", p.Id, SetContainer) - p.ContainerName.Set(SetContainer) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetInit != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting init command to: %s\n", p.Id, SetInit) - p.Init.Set(SetInit) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetRoot != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting root to: %s\n", p.Id, SetRoot) - p.Root.Set(SetRoot) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetKernel != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting Kernel to: %s\n", p.Id, SetKernel) - p.KernelVersion.Set(SetKernel) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetKernelArgs != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting Kernel args to: %s\n", p.Id, SetKernelArgs) - p.KernelArgs.Set(SetKernelArgs) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetIpxe != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting iPXE template to: %s\n", p.Id, SetIpxe) - p.Ipxe.Set(SetIpxe) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetRuntimeOverlay != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting runtime overlay to: %s\n", p.Id, SetRuntimeOverlay) - p.RuntimeOverlay.Set(SetRuntimeOverlay) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetSystemOverlay != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting system overlay to: %s\n", p.Id, SetSystemOverlay) - p.SystemOverlay.Set(SetSystemOverlay) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiNetmask != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting IPMI username to: %s\n", p.Id, SetIpmiNetmask) - p.IpmiNetmask.Set(SetIpmiNetmask) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiGateway != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting IPMI username to: %s\n", p.Id, SetIpmiGateway) - p.IpmiGateway.Set(SetIpmiGateway) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiUsername != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting IPMI username to: %s\n", p.Id, SetIpmiUsername) - p.IpmiUserName.Set(SetIpmiUsername) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpmiPassword != "" { wwlog.Printf(wwlog.VERBOSE, "Profile: %s, Setting IPMI username to: %s\n", p.Id, SetIpmiPassword) - p.IpmiPassword.Set(SetIpmiPassword) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } if SetNetDevDel == true { @@ -223,14 +150,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Profile %s: Deleting network device: %s\n", p.Id.Get(), SetNetDev) - delete(p.NetDevs, SetNetDev) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetIpaddr != "" { if SetNetDev == "" { wwlog.Printf(wwlog.ERROR, "You must include the '--netdev' option\n") @@ -243,14 +165,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Profile '%s': Setting IP address to: %s:%s\n", p.Id.Get(), SetNetDev, SetHwaddr) - p.NetDevs[SetNetDev].Ipaddr.Set(SetIpaddr) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetNetmask != "" { if SetNetDev == "" { wwlog.Printf(wwlog.ERROR, "You must include the '--netdev' option\n") @@ -263,14 +180,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Profile '%s': Setting netmask to: %s:%s\n", p.Id.Get(), SetNetDev, SetHwaddr) - p.NetDevs[SetNetDev].Netmask.Set(SetNetmask) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetGateway != "" { if SetNetDev == "" { wwlog.Printf(wwlog.ERROR, "You must include the '--netdev' option\n") @@ -283,14 +195,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Profile '%s': Setting gateway to: %s:%s\n", p.Id.Get(), SetNetDev, SetHwaddr) - p.NetDevs[SetNetDev].Gateway.Set(SetGateway) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + if SetHwaddr != "" { if SetNetDev == "" { wwlog.Printf(wwlog.ERROR, "You must include the '--netdev' option\n") @@ -303,15 +210,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.VERBOSE, "Profile '%s': Setting HW address to: %s:%s\n", p.Id.Get(), SetNetDev, SetHwaddr) - p.NetDevs[SetNetDev].Hwaddr.Set(SetHwaddr) - err := nodeDB.ProfileUpdate(p) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } } + err := nodeDB.ProfileUpdate(p) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } } if len(profiles) > 0 {