|
| 1 | +package applesilicon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "reflect" |
| 8 | + |
| 9 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 10 | + applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +type serverSSHConnectRequest struct { |
| 15 | + Zone scw.Zone |
| 16 | + ServerID string |
| 17 | + Username string |
| 18 | + Port uint |
| 19 | + Command string |
| 20 | +} |
| 21 | + |
| 22 | +func serverSSHCommand() *core.Command { |
| 23 | + return &core.Command{ |
| 24 | + Short: `SSH into a server`, |
| 25 | + Long: `Connect to distant server via the SSH protocol.`, |
| 26 | + Namespace: "apple-silicon", |
| 27 | + Verb: "ssh", |
| 28 | + Resource: "server", |
| 29 | + ArgsType: reflect.TypeOf(serverSSHConnectRequest{}), |
| 30 | + ArgSpecs: core.ArgSpecs{ |
| 31 | + { |
| 32 | + Name: "server-id", |
| 33 | + Short: "Server ID to SSH into", |
| 34 | + Required: true, |
| 35 | + Positional: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + Name: "username", |
| 39 | + Short: "Username used for the SSH connection", |
| 40 | + Default: core.DefaultValueSetter("m1"), |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "port", |
| 44 | + Short: "Port used for the SSH connection", |
| 45 | + Default: core.DefaultValueSetter("22"), |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "command", |
| 49 | + Short: "Command to execute on the remote server", |
| 50 | + }, |
| 51 | + core.ZoneArgSpec(), |
| 52 | + }, |
| 53 | + Run: serverSSHRun, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func serverSSHRun(ctx context.Context, argsI interface{}) (i interface{}, e error) { |
| 58 | + args := argsI.(*serverSSHConnectRequest) |
| 59 | + |
| 60 | + client := core.ExtractClient(ctx) |
| 61 | + asAPI := applesilicon.NewAPI(client) |
| 62 | + serverResp, err := asAPI.GetServer(&applesilicon.GetServerRequest{ |
| 63 | + Zone: args.Zone, |
| 64 | + ServerID: args.ServerID, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + if serverResp.Status != applesilicon.ServerStatusReady { |
| 71 | + return nil, &core.CliError{ |
| 72 | + Err: fmt.Errorf("server is not ready"), |
| 73 | + Details: fmt.Sprintf("Server %s currently in %s", serverResp.Name, serverResp.Status), |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + sshArgs := []string{ |
| 78 | + serverResp.IP.String(), |
| 79 | + "-p", fmt.Sprintf("%d", args.Port), |
| 80 | + "-l", args.Username, |
| 81 | + "-t", |
| 82 | + } |
| 83 | + if args.Command != "" { |
| 84 | + sshArgs = append(sshArgs, args.Command) |
| 85 | + } |
| 86 | + |
| 87 | + sshCmd := exec.Command("ssh", sshArgs...) |
| 88 | + |
| 89 | + exitCode, err := core.ExecCmd(ctx, sshCmd) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + if exitCode != 0 { |
| 94 | + return nil, &core.CliError{Empty: true, Code: exitCode} |
| 95 | + } |
| 96 | + |
| 97 | + return &core.SuccessResult{Empty: true}, nil |
| 98 | +} |
0 commit comments