Skip to content
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

baremetal: add install command with a wait flag #873

Merged
merged 13 commits into from
Apr 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Install an OS on the server associated with the given ID.
USAGE:
scw baremetal server install <server-id> [arg=value ...]

EXAMPLES:
Install an OS on a given server
scw baremetal server install 11111111-1111-1111-1111-111111111111 os-id=11111111-1111-1111-1111-111111111111

ARGS:
server-id Server ID to install
os-id ID of the OS to install on the server
Expand All @@ -12,8 +16,16 @@ ARGS:

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

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

SEE ALSO:
# List OS (useful to get all OS IDs)
scw baremetal os list

# Create a server
scw baremetal server create
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
39 changes: 39 additions & 0 deletions internal/namespaces/baremetal/v1alpha1/custom_server_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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.Examples = []*core.Example{
{
Short: "Install an OS on a given server",
Request: `{"os_id":"11111111-1111-1111-1111-111111111111","server_id":"11111111-1111-1111-1111-111111111111"}`,
},
}

c.SeeAlsos = []*core.SeeAlso{
{
Short: "List OS (useful to get all OS IDs)",
Command: "scw baremetal os list",
},
{
Short: "Create a server",
Command: "scw baremetal server create",
},
}

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 {
jerome-quere marked this conversation as resolved.
Show resolved Hide resolved
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