Add baremetal host poweron command

This change introduces a command to power on baremetal hosts.

Closes: #5

Change-Id: Ie76cd7c044cc44629648ef0b02bb7e492ccccdcf
Signed-off-by: Drew Walters <andrew.walters@att.com>
This commit is contained in:
Drew Walters 2020-04-01 19:56:19 +00:00
parent fd65389f1a
commit 7ddb858135
8 changed files with 97 additions and 1 deletions

View File

@ -38,6 +38,9 @@ func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Co
powerOffCmd := NewPowerOffCommand(rootSettings)
cmd.AddCommand(powerOffCmd)
powerOnCmd := NewPowerOnCommand(rootSettings)
cmd.AddCommand(powerOnCmd)
powerStatusCmd := NewPowerStatusCommand(rootSettings)
cmd.AddCommand(powerStatusCmd)

View File

@ -38,6 +38,11 @@ func TestBaremetal(t *testing.T) {
CmdLine: "-h",
Cmd: baremetal.NewPowerOffCommand(nil),
},
{
Name: "baremetal-poweron-with-help",
CmdLine: "-h",
Cmd: baremetal.NewPowerOnCommand(nil),
},
{
Name: "baremetal-powerstatus-with-help",
CmdLine: "-h",

56
cmd/baremetal/poweron.go Normal file
View File

@ -0,0 +1,56 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package baremetal
import (
"fmt"
"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 {
var phase string
cmd := &cobra.Command{
Use: "poweron BAREMETAL_HOST_DOC_NAME",
Short: "Power on a host",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
m, err := remote.NewManager(rootSettings, phase, remote.ByName(args[0]))
if err != nil {
return err
}
for _, host := range m.Hosts {
if err := host.SystemPowerOn(host.Context); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Powered on remote host %s\n", args[0])
}
return nil
},
}
flags := cmd.Flags()
flags.StringVar(&phase, flagPhase, config.BootstrapPhase, flagPhaseDescription)
return cmd
}

View File

@ -0,0 +1,8 @@
Power on a host
Usage:
poweron BAREMETAL_HOST_DOC_NAME [flags]
Flags:
-h, --help help for poweron
--phase string airshipctl phase that contains the desired baremetal host document(s) (default "bootstrap")

View File

@ -7,6 +7,7 @@ Available Commands:
help Help about any command
isogen Generate baremetal host ISO image
poweroff Shutdown a baremetal host
poweron Power on a host
powerstatus Retrieve the power status of a baremetal host
reboot Reboot a host
remotedirect Bootstrap the ephemeral host

View File

@ -28,8 +28,8 @@ import (
// functions within client are used by power management commands and remote direct functionality.
type Client interface {
RebootSystem(context.Context) error
SystemPowerOff(context.Context) error
SystemPowerOn(context.Context) error
// TODO(drewwalters96): Should this be a string forever? We may want to define our own custom type, as the
// string format will be client dependent when we add new clients.

View File

@ -200,6 +200,16 @@ func (c *Client) SystemPowerOff(ctx context.Context) error {
return ScreenRedfishError(httpResp, err)
}
// SystemPowerOn powers on a host.
func (c *Client) SystemPowerOn(ctx context.Context) error {
resetReq := redfishClient.ResetRequestBody{}
resetReq.ResetType = redfishClient.RESETTYPE_ON
_, httpResp, err := c.RedfishAPI.ResetSystem(ctx, c.nodeID, resetReq)
return ScreenRedfishError(httpResp, err)
}
// SystemPowerStatus retrieves the power status of a host as a human-readable string.
func (c *Client) SystemPowerStatus(ctx context.Context) (string, error) {
computerSystem, httpResp, err := c.RedfishAPI.GetSystem(ctx, c.nodeID)

View File

@ -92,6 +92,19 @@ func (m *MockClient) SystemPowerOff(ctx context.Context) error {
return args.Error(0)
}
// SystemPowerOn provides a stubbed method that can be mocked to test functions that use the
// Redfish client without making any Redfish API calls or requiring the appropriate Redfish client settings.
//
// Example usage:
// client := redfishutils.NewClient()
// client.On("SystemPowerOn").Return(<return values>)
//
// err := client.SystemPowerOn(<args>)
func (m *MockClient) SystemPowerOn(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
// SystemPowerStatus provides a stubbed method that can be mocked to test functions that use the
// Redfish client without making any Redfish API calls or requiring the appropriate Redfish client settings.
//