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 (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote"
)
@ -39,33 +40,16 @@ func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Co
baremetalRootCmd := &cobra.Command{
Use: "baremetal",
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()
},
}
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
ejectMediaCmd := NewEjectMediaCommand(rootSettings)
baremetalRootCmd.AddCommand(ejectMediaCmd)
powerOffCmd := NewPowerOffCommand(rootSettings)
baremetalRootCmd.AddCommand(powerOffCmd)
powerOnCmd := NewPowerOnCommand(rootSettings)
baremetalRootCmd.AddCommand(powerOnCmd)
powerStatusCmd := NewPowerStatusCommand(rootSettings)
baremetalRootCmd.AddCommand(powerStatusCmd)
rebootCmd := NewRebootCommand(rootSettings)
baremetalRootCmd.AddCommand(rebootCmd)
remoteDirectCmd := NewRemoteDirectCommand(rootSettings)
baremetalRootCmd.AddCommand(remoteDirectCmd)
baremetalRootCmd.AddCommand(NewEjectMediaCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerOffCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerOnCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerStatusCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewRebootCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewRemoteDirectCommand(cfgFactory))
return baremetalRootCmd
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,17 +19,21 @@ import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote"
)
// 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{
Use: "remotedirect",
Short: "Bootstrap the ephemeral host",
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,
remote.ByLabel(document.EphemeralHostSelector))
if err != nil {
@ -41,7 +45,7 @@ func NewRemoteDirectCommand(rootSettings *environment.AirshipCTLSettings) *cobra
}
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/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/remote/power"
"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
// out-of-band management on multiple hosts.
func NewManager(settings *environment.AirshipCTLSettings, phase string, hosts ...HostSelector) (*Manager, error) {
managementCfg, err := settings.Config.CurrentContextManagementConfig()
func NewManager(cfg *config.Config, phase string, hosts ...HostSelector) (*Manager, error) {
managementCfg, err := cfg.CurrentContextManagementConfig()
if err != nil {
return nil, err
}
@ -126,7 +125,7 @@ func NewManager(settings *environment.AirshipCTLSettings, phase string, hosts ..
return nil, err
}
entrypoint, err := settings.Config.CurrentContextEntryPoint(phase)
entrypoint, err := cfg.CurrentContextEntryPoint(phase)
if err != nil {
return nil, err
}

View File

@ -23,39 +23,36 @@ import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote/redfish"
redfishdell "opendev.org/airship/airshipctl/pkg/remote/redfish/vendors/dell"
"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
// manipulate configuration sections.
func initSettings(t *testing.T, configs ...Configuration) *environment.AirshipCTLSettings {
func initSettings(t *testing.T, configs ...Configuration) *config.Config {
t.Helper()
settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
config := testutil.DummyConfig()
for _, cfg := range configs {
cfg(settings)
cfg(config)
}
return settings
return config
}
// withManagementConfig initializes the management config when used as an argument to initSettings.
func withManagementConfig(cfg *config.ManagementConfiguration) Configuration {
return func(settings *environment.AirshipCTLSettings) {
settings.Config.ManagementConfiguration["dummy_management_config"] = cfg
return func(settings *config.Config) {
settings.ManagementConfiguration["dummy_management_config"] = cfg
}
}
// withTestDataPath sets the test data path when used as an argument to initSettings.
func withTestDataPath(path string) Configuration {
return func(settings *environment.AirshipCTLSettings) {
manifest, err := settings.Config.CurrentContextManifest()
return func(settings *config.Config) {
manifest, err := settings.CurrentContextManifest()
if err != nil {
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"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/remote/power"
)
// DoRemoteDirect bootstraps the ephemeral node.
func (b baremetalHost) DoRemoteDirect(settings *environment.AirshipCTLSettings) error {
cfg := settings.Config
func (b baremetalHost) DoRemoteDirect(cfg *config.Config) error {
root, err := cfg.CurrentContextEntryPoint(config.BootstrapPhase)
if err != nil {
return err