Skip to content

baremetal: add install command with a wait flag #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ARGS:

FLAGS:
-h, --help help for install
-w, --wait wait until the server is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
1 change: 1 addition & 0 deletions internal/namespaces/baremetal/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func GetCommands() *core.Commands {
))

cmds.MustFind("baremetal", "server", "create").Override(serverCreateBuilder)
cmds.MustFind("baremetal", "server", "install").Override(serverInstallBuilder)

// Action commands
cmds.MustFind("baremetal", "server", "start").Override(serverStartBuilder)
Expand Down
22 changes: 22 additions & 0 deletions internal/namespaces/baremetal/v1alpha1/custom_server_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package baremetal

import (
"context"

"github.com/scaleway/scaleway-cli/internal/core"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
)

func serverInstallBuilder(c *core.Command) *core.Command {

c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
return api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: argsI.(*baremetal.InstallServerRequest).Zone,
ServerID: respI.(*baremetal.Server).ID,
Timeout: serverActionTimeout,
})
}

return c
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package baremetal

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
account "github.com/scaleway/scaleway-cli/internal/namespaces/account/v2alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func Test_InstallServer(t *testing.T) {
// All test below should succeed to create an instance.
t.Run("Simple", func(t *testing.T) {
// baremetal api requires that the key must be at least 1024 bits long. Regardless of the algorithm
sshKey := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCbJuYSOQc01zjHsMyn4OUsW61cqRvttKt3StJgbvt2WBuGpwi1/5RtSoMQpudYlZpdeivFb21S8QRas8zcOc+6WqgWa2nj/8yA+cauRlV6CMWY+hOTkkg39xaekstuQ+WR2/AP7O/9hjVx5735+9ZNIxxHsFjVYdBEuk9gEX+1Rw== foobar@foobar`
osID := `d859aa89-8b4a-4551-af42-ff7c0c27260a` // Ubuntu 18.04
cmds := GetCommands()
cmds.Merge(account.GetCommands())

t.Run("With ID", core.Test(&core.TestConfig{
BeforeFunc: core.BeforeFuncCombine(
addSSH("key", sshKey),
createServerAndWait("Server"),
),
Commands: cmds,
Cmd: "scw baremetal server install {{ .Server.ID }} hostname=test-install-server ssh-key-ids.0={{ .key.ID }} os-id=" + osID + " -w",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteSSH("key"),
deleteServer("Server"),
),
DefaultZone: scw.ZoneFrPar2,
}))
})
}
6 changes: 3 additions & 3 deletions internal/namespaces/baremetal/v1alpha1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func Test_StartServerErrors(t *testing.T) {
t.Run("Error: cannot be started while not delivered", core.Test(&core.TestConfig{
BeforeFunc: createServer("Server"),
BeforeFunc: createServerAndWait("Server"),
Commands: GetCommands(),
Cmd: "scw baremetal server start {{ .Server.ID }} -w",
Check: core.TestCheckExitCode(1),
Expand All @@ -20,7 +20,7 @@ func Test_StartServerErrors(t *testing.T) {

func Test_StopServerErrors(t *testing.T) {
t.Run("Error: cannot be stopped while not delivered", core.Test(&core.TestConfig{
BeforeFunc: createServer("Server"),
BeforeFunc: createServerAndWait("Server"),
Commands: GetCommands(),
Cmd: "scw baremetal server stop {{ .Server.ID }} -w",
Check: core.TestCheckExitCode(1),
Expand All @@ -31,7 +31,7 @@ func Test_StopServerErrors(t *testing.T) {

func Test_RebootServerErrors(t *testing.T) {
t.Run("Error: cannot be rebooted while not delivered", core.Test(&core.TestConfig{
BeforeFunc: createServer("Server"),
BeforeFunc: createServerAndWait("Server"),
Commands: GetCommands(),
Cmd: "scw baremetal server reboot {{ .Server.ID }} -w",
Check: core.TestCheckExitCode(1),
Expand Down
21 changes: 18 additions & 3 deletions internal/namespaces/baremetal/v1alpha1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ import (
"github.com/scaleway/scaleway-cli/internal/core"
)

// createServer creates a baremetal instance
// createServerAndWait creates a baremetal instance
// register it in the context Meta at metaKey.
func createServer(metaKey string) core.BeforeFunc {
return core.ExecStoreBeforeCmd(metaKey, "scw baremetal server create")
func createServerAndWait(metaKey string) core.BeforeFunc {
return core.ExecStoreBeforeCmd(metaKey, "scw baremetal server create -w")
}

// deleteServer deletes a server
// previously registered in the context Meta at metaKey.
func deleteServer(metaKey string) core.AfterFunc {
return core.ExecAfterCmd("scw baremetal server delete {{ ." + metaKey + ".ID }}")
}

// add an ssh key with a given meta key
func addSSH(metaKey string, key string) core.BeforeFunc {
return func(ctx *core.BeforeFuncCtx) error {
ctx.Meta[metaKey] = ctx.ExecuteCmd([]string{
"scw", "account", "ssh-key", "add", "public-key=" + key,
})
return nil
}
}

// delete an ssh key with a given meta key
func deleteSSH(metaKey string) core.AfterFunc {
return core.ExecAfterCmd("scw account ssh-key delete {{ ." + metaKey + ".ID }}")
}
Loading