-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(as): add ssh verb on the server resource to connect to a server (#…
- Loading branch information
Showing
10 changed files
with
574 additions
and
7 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
cmd/scw/testdata/test-all-usage-apple-silicon-server-ssh-usage.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
internal/namespaces/applesilicon/v1alpha1/custom_server_ssh.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
49 changes: 49 additions & 0 deletions
49
internal/namespaces/applesilicon/v1alpha1/custom_server_ssh_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})) | ||
} |
Oops, something went wrong.