Refactor baremetal* commands

All the baremetal commands was refactored for usage with new config
factory. Config object now is being initialized in pkg module on demand,
which provides more flexibility and cleaner code.

Change-Id: If75cd3f1a8bc22fc47ea132188bd9fec989ee434
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #327
This commit is contained in:
Ruslan Aliev 2020-08-27 18:24:51 -05:00
parent b63d107ec7
commit a2b8d45cb0
11 changed files with 66 additions and 65 deletions

View File

@ -17,6 +17,7 @@ package baremetal
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
@ -39,33 +40,16 @@ func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Co
baremetalRootCmd := &cobra.Command{ baremetalRootCmd := &cobra.Command{
Use: "baremetal", Use: "baremetal",
Short: "Perform actions on baremetal hosts", Short: "Perform actions on baremetal hosts",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil {
parentPreRun(cmd.Root(), args)
}
// Load or Initialize airship Config
rootSettings.InitConfig()
},
} }
ejectMediaCmd := NewEjectMediaCommand(rootSettings) cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
baremetalRootCmd.AddCommand(ejectMediaCmd)
powerOffCmd := NewPowerOffCommand(rootSettings) baremetalRootCmd.AddCommand(NewEjectMediaCommand(cfgFactory))
baremetalRootCmd.AddCommand(powerOffCmd) baremetalRootCmd.AddCommand(NewPowerOffCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerOnCommand(cfgFactory))
powerOnCmd := NewPowerOnCommand(rootSettings) baremetalRootCmd.AddCommand(NewPowerStatusCommand(cfgFactory))
baremetalRootCmd.AddCommand(powerOnCmd) baremetalRootCmd.AddCommand(NewRebootCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewRemoteDirectCommand(cfgFactory))
powerStatusCmd := NewPowerStatusCommand(rootSettings)
baremetalRootCmd.AddCommand(powerStatusCmd)
rebootCmd := NewRebootCommand(rootSettings)
baremetalRootCmd.AddCommand(rebootCmd)
remoteDirectCmd := NewRemoteDirectCommand(rootSettings)
baremetalRootCmd.AddCommand(remoteDirectCmd)
return baremetalRootCmd return baremetalRootCmd
} }

View File

@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipctl/cmd/baremetal" "opendev.org/airship/airshipctl/cmd/baremetal"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -28,7 +29,7 @@ func TestBaremetal(t *testing.T) {
{ {
Name: "baremetal-with-help", Name: "baremetal-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: baremetal.NewBaremetalCommand(nil), Cmd: baremetal.NewBaremetalCommand(&environment.AirshipCTLSettings{}),
}, },
{ {
Name: "baremetal-ejectmedia-with-help", Name: "baremetal-ejectmedia-with-help",

View File

@ -20,12 +20,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewEjectMediaCommand provides a command to eject media attached to a baremetal host. // NewEjectMediaCommand provides a command to eject media attached to a baremetal host.
func NewEjectMediaCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewEjectMediaCommand(cfgFactory config.Factory) *cobra.Command {
var labels string var labels string
var name string var name string
var phase string var phase string
@ -35,8 +34,13 @@ func NewEjectMediaCommand(rootSettings *environment.AirshipCTLSettings) *cobra.C
Short: "Eject media attached to a baremetal host", Short: "Eject media attached to a baremetal host",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
selectors := GetHostSelections(name, labels) selectors := GetHostSelections(name, labels)
m, err := remote.NewManager(rootSettings, phase, selectors...) m, err := remote.NewManager(cfg, phase, selectors...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,12 +20,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewPowerOffCommand provides a command to shutdown a remote host. // NewPowerOffCommand provides a command to shutdown a remote host.
func NewPowerOffCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewPowerOffCommand(cfgFactory config.Factory) *cobra.Command {
var labels string var labels string
var name string var name string
var phase string var phase string
@ -35,8 +34,13 @@ func NewPowerOffCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com
Short: "Shutdown a baremetal host", Short: "Shutdown a baremetal host",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
selectors := GetHostSelections(name, labels) selectors := GetHostSelections(name, labels)
m, err := remote.NewManager(rootSettings, phase, selectors...) m, err := remote.NewManager(cfg, phase, selectors...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,12 +20,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewPowerOnCommand provides a command with the capability to power on baremetal hosts. // NewPowerOnCommand provides a command with the capability to power on baremetal hosts.
func NewPowerOnCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewPowerOnCommand(cfgFactory config.Factory) *cobra.Command {
var labels string var labels string
var name string var name string
var phase string var phase string
@ -35,8 +34,13 @@ func NewPowerOnCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comm
Short: "Power on a host", Short: "Power on a host",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
selectors := GetHostSelections(name, labels) selectors := GetHostSelections(name, labels)
m, err := remote.NewManager(rootSettings, phase, selectors...) m, err := remote.NewManager(cfg, phase, selectors...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,12 +20,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewPowerStatusCommand provides a command to retrieve the power status of a baremetal host. // NewPowerStatusCommand provides a command to retrieve the power status of a baremetal host.
func NewPowerStatusCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewPowerStatusCommand(cfgFactory config.Factory) *cobra.Command {
var labels string var labels string
var name string var name string
var phase string var phase string
@ -35,8 +34,13 @@ func NewPowerStatusCommand(rootSettings *environment.AirshipCTLSettings) *cobra.
Short: "Retrieve the power status of a baremetal host", Short: "Retrieve the power status of a baremetal host",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
selectors := GetHostSelections(name, labels) selectors := GetHostSelections(name, labels)
m, err := remote.NewManager(rootSettings, phase, selectors...) m, err := remote.NewManager(cfg, phase, selectors...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,12 +20,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewRebootCommand provides a command with the capability to reboot baremetal hosts. // NewRebootCommand provides a command with the capability to reboot baremetal hosts.
func NewRebootCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewRebootCommand(cfgFactory config.Factory) *cobra.Command {
var labels string var labels string
var name string var name string
var phase string var phase string
@ -35,8 +34,13 @@ func NewRebootCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
Short: "Reboot a host", Short: "Reboot a host",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
selectors := GetHostSelections(name, labels) selectors := GetHostSelections(name, labels)
m, err := remote.NewManager(rootSettings, phase, selectors...) m, err := remote.NewManager(cfg, phase, selectors...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,17 +19,21 @@ import (
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
// NewRemoteDirectCommand provides a command with the capability to perform remote direct operations. // NewRemoteDirectCommand provides a command with the capability to perform remote direct operations.
func NewRemoteDirectCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewRemoteDirectCommand(cfgFactory config.Factory) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "remotedirect", Use: "remotedirect",
Short: "Bootstrap the ephemeral host", Short: "Bootstrap the ephemeral host",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
manager, err := remote.NewManager(rootSettings, cfg, err := cfgFactory()
if err != nil {
return err
}
manager, err := remote.NewManager(cfg,
config.BootstrapPhase, config.BootstrapPhase,
remote.ByLabel(document.EphemeralHostSelector)) remote.ByLabel(document.EphemeralHostSelector))
if err != nil { if err != nil {
@ -41,7 +45,7 @@ func NewRemoteDirectCommand(rootSettings *environment.AirshipCTLSettings) *cobra
} }
ephemeralHost := manager.Hosts[0] ephemeralHost := manager.Hosts[0]
return ephemeralHost.DoRemoteDirect(rootSettings) return ephemeralHost.DoRemoteDirect(cfg)
}, },
} }

View File

@ -20,7 +20,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/remote/power" "opendev.org/airship/airshipctl/pkg/remote/power"
"opendev.org/airship/airshipctl/pkg/remote/redfish" "opendev.org/airship/airshipctl/pkg/remote/redfish"
@ -116,8 +115,8 @@ func ByName(name string) HostSelector {
// NewManager provides a manager that exposes the capability to perform remote direct functionality and other // NewManager provides a manager that exposes the capability to perform remote direct functionality and other
// out-of-band management on multiple hosts. // out-of-band management on multiple hosts.
func NewManager(settings *environment.AirshipCTLSettings, phase string, hosts ...HostSelector) (*Manager, error) { func NewManager(cfg *config.Config, phase string, hosts ...HostSelector) (*Manager, error) {
managementCfg, err := settings.Config.CurrentContextManagementConfig() managementCfg, err := cfg.CurrentContextManagementConfig()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -126,7 +125,7 @@ func NewManager(settings *environment.AirshipCTLSettings, phase string, hosts ..
return nil, err return nil, err
} }
entrypoint, err := settings.Config.CurrentContextEntryPoint(phase) entrypoint, err := cfg.CurrentContextEntryPoint(phase)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -23,39 +23,36 @@ import (
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote/redfish" "opendev.org/airship/airshipctl/pkg/remote/redfish"
redfishdell "opendev.org/airship/airshipctl/pkg/remote/redfish/vendors/dell" redfishdell "opendev.org/airship/airshipctl/pkg/remote/redfish/vendors/dell"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
type Configuration func(*environment.AirshipCTLSettings) type Configuration func(*config.Config)
// initSettings initializes the global airshipctl settings with test data by accepting functions as arguments that // initSettings initializes the global airshipctl settings with test data by accepting functions as arguments that
// manipulate configuration sections. // manipulate configuration sections.
func initSettings(t *testing.T, configs ...Configuration) *environment.AirshipCTLSettings { func initSettings(t *testing.T, configs ...Configuration) *config.Config {
t.Helper() t.Helper()
settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()} config := testutil.DummyConfig()
for _, cfg := range configs { for _, cfg := range configs {
cfg(settings) cfg(config)
} }
return config
return settings
} }
// withManagementConfig initializes the management config when used as an argument to initSettings. // withManagementConfig initializes the management config when used as an argument to initSettings.
func withManagementConfig(cfg *config.ManagementConfiguration) Configuration { func withManagementConfig(cfg *config.ManagementConfiguration) Configuration {
return func(settings *environment.AirshipCTLSettings) { return func(settings *config.Config) {
settings.Config.ManagementConfiguration["dummy_management_config"] = cfg settings.ManagementConfiguration["dummy_management_config"] = cfg
} }
} }
// withTestDataPath sets the test data path when used as an argument to initSettings. // withTestDataPath sets the test data path when used as an argument to initSettings.
func withTestDataPath(path string) Configuration { func withTestDataPath(path string) Configuration {
return func(settings *environment.AirshipCTLSettings) { return func(settings *config.Config) {
manifest, err := settings.Config.CurrentContextManifest() manifest, err := settings.CurrentContextManifest()
if err != nil { if err != nil {
panic(fmt.Sprintf("Unable to initialize management tests. Current Context error %q", err)) panic(fmt.Sprintf("Unable to initialize management tests. Current Context error %q", err))
} }

View File

@ -18,16 +18,12 @@ import (
api "opendev.org/airship/airshipctl/pkg/api/v1alpha1" api "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/remote/power" "opendev.org/airship/airshipctl/pkg/remote/power"
) )
// DoRemoteDirect bootstraps the ephemeral node. // DoRemoteDirect bootstraps the ephemeral node.
func (b baremetalHost) DoRemoteDirect(settings *environment.AirshipCTLSettings) error { func (b baremetalHost) DoRemoteDirect(cfg *config.Config) error {
cfg := settings.Config
root, err := cfg.CurrentContextEntryPoint(config.BootstrapPhase) root, err := cfg.CurrentContextEntryPoint(config.BootstrapPhase)
if err != nil { if err != nil {
return err return err