Skip to content

Commit

Permalink
feat(as): add ssh verb on the server resource to connect to a server (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Feb 8, 2021
1 parent 168d2e6 commit 5b4fefb
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Connect to distant server via the SSH protocol.

USAGE:
scw apple-silicon server ssh <server-id ...> [arg=value ...]

ARGS:
server-id Server ID to SSH into
[username=m1] Username used for the SSH connection
[port=22] Port used for the SSH connection
[command] Command to execute on the remote server
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config

FLAGS:
-h, --help help for ssh

GLOBAL FLAGS:
-c, --config string The path to the config file
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ AVAILABLE COMMANDS:
list List all servers
reboot Reboot a server
reinstall Reinstall a server
ssh SSH into a server
update Update a server
wait Wait for a server to reach a stable state

Expand Down
6 changes: 4 additions & 2 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ func createTestClient(t *testing.T, testConfig *TestConfig, httpClient *http.Cli
clientOpts = append(clientOpts, scw.WithEnv())
config, err := scw.LoadConfig()
if err == nil {
p, err := config.GetActiveProfile()
activeProfile, err := config.GetActiveProfile()
require.NoError(t, err)
clientOpts = append(clientOpts, scw.WithProfile(p))
envProfile := scw.LoadEnvProfile()
profile := scw.MergeProfiles(activeProfile, envProfile)
clientOpts = append(clientOpts, scw.WithProfile(profile))
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions internal/namespaces/applesilicon/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

cmds.Merge(
core.NewCommands(
serverWaitCommand(),
),
)
cmds.Merge(core.NewCommands(
serverSSHCommand(),
serverWaitCommand(),
))

human.RegisterMarshalerFunc(applesilicon.ServerTypeCPU{}, cpuMarshalerFunc)
human.RegisterMarshalerFunc(applesilicon.ServerTypeDisk{}, diskMarshalerFunc)
Expand Down
98 changes: 98 additions & 0 deletions internal/namespaces/applesilicon/v1alpha1/custom_server_ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package applesilicon

import (
"context"
"fmt"
"os/exec"
"reflect"

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

type serverSSHConnectRequest struct {
Zone scw.Zone
ServerID string
Username string
Port uint
Command string
}

func serverSSHCommand() *core.Command {
return &core.Command{
Short: `SSH into a server`,
Long: `Connect to distant server via the SSH protocol.`,
Namespace: "apple-silicon",
Verb: "ssh",
Resource: "server",
ArgsType: reflect.TypeOf(serverSSHConnectRequest{}),
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Short: "Server ID to SSH into",
Required: true,
Positional: true,
},
{
Name: "username",
Short: "Username used for the SSH connection",
Default: core.DefaultValueSetter("m1"),
},
{
Name: "port",
Short: "Port used for the SSH connection",
Default: core.DefaultValueSetter("22"),
},
{
Name: "command",
Short: "Command to execute on the remote server",
},
core.ZoneArgSpec(),
},
Run: serverSSHRun,
}
}

func serverSSHRun(ctx context.Context, argsI interface{}) (i interface{}, e error) {
args := argsI.(*serverSSHConnectRequest)

client := core.ExtractClient(ctx)
asAPI := applesilicon.NewAPI(client)
serverResp, err := asAPI.GetServer(&applesilicon.GetServerRequest{
Zone: args.Zone,
ServerID: args.ServerID,
})
if err != nil {
return nil, err
}

if serverResp.Status != applesilicon.ServerStatusReady {
return nil, &core.CliError{
Err: fmt.Errorf("server is not ready"),
Details: fmt.Sprintf("Server %s currently in %s", serverResp.Name, serverResp.Status),
}
}

sshArgs := []string{
serverResp.IP.String(),
"-p", fmt.Sprintf("%d", args.Port),
"-l", args.Username,
"-t",
}
if args.Command != "" {
sshArgs = append(sshArgs, args.Command)
}

sshCmd := exec.Command("ssh", sshArgs...)

exitCode, err := core.ExecCmd(ctx, sshCmd)
if err != nil {
return nil, err
}
if exitCode != 0 {
return nil, &core.CliError{Empty: true, Code: exitCode}
}

return &core.SuccessResult{Empty: true}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package applesilicon

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_ServerSSH(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd("Server", "scw apple-silicon server create --wait"),
),
Cmd: "scw apple-silicon server ssh {{ .Server.ID }}",
OverrideExec: core.OverrideExecSimple(
"ssh {{ .Server.IP }} -p 22 -l m1 -t",
0,
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd("scw apple-silicon server delete {{ .Server.ID }}"),
),
DisableParallel: true,
}))

t.Run("With-Exit-Code", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd("Server", "scw apple-silicon server create --wait"),
),
Cmd: "scw apple-silicon server ssh {{ .Server.ID }}",
OverrideExec: core.OverrideExecSimple(
"ssh {{ .Server.IP }} -p 22 -l m1 -t",
130,
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(130),
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd("scw apple-silicon server delete {{ .Server.ID }}"),
),
DisableParallel: true,
}))
}
Loading

0 comments on commit 5b4fefb

Please sign in to comment.