Skip to content

Commit 16a354f

Browse files
committed
Refactored 'exec' command (scaleway#80)
1 parent e07a1fb commit 16a354f

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

pkg/commands/exec.go

+53-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package commands
66

77
import (
8+
"fmt"
89
"os"
910
"time"
1011

@@ -16,7 +17,7 @@ import (
1617
)
1718

1819
var cmdExec = &types.Command{
19-
Exec: runExec,
20+
Exec: cmdExecExec,
2021
UsageLine: "exec [OPTIONS] SERVER [COMMAND] [ARGS...]",
2122
Description: "Run a command on a running server",
2223
Help: "Run a command on a running server.",
@@ -47,57 +48,90 @@ var execTimeout float64 // -T flag
4748
var execHelp bool // -h, --help flag
4849
var execGateway string // -g, --gateway flag
4950

50-
func runExec(cmd *types.Command, args []string) {
51+
// ExecArgs are flags for the `RunExec` function
52+
type ExecArgs struct {
53+
Timeout float64
54+
Wait bool
55+
Gateway string
56+
Server string
57+
Command []string
58+
}
59+
60+
func cmdExecExec(cmd *types.Command, rawArgs []string) {
5161
if execHelp {
5262
cmd.PrintUsage()
5363
}
54-
if len(args) < 1 {
64+
if len(rawArgs) < 1 {
5565
cmd.PrintShortUsage()
5666
}
5767

58-
serverID := cmd.API.GetServerID(args[0])
68+
args := ExecArgs{
69+
Timeout: execTimeout,
70+
Wait: execW,
71+
Gateway: execGateway,
72+
Server: rawArgs[0],
73+
Command: rawArgs[1:],
74+
}
75+
ctx := cmd.GetContext(rawArgs)
76+
err := RunExec(ctx, args)
77+
if err != nil {
78+
log.Fatalf("Cannot exec 'exec': %v", err)
79+
}
80+
}
81+
82+
// RunExec is the handler for 'scw exec'
83+
func RunExec(ctx types.CommandContext, args ExecArgs) error {
84+
serverID := ctx.API.GetServerID(args.Server)
5985

6086
// Resolve gateway
61-
if execGateway == "" {
62-
execGateway = os.Getenv("SCW_GATEWAY")
87+
if args.Gateway == "" {
88+
args.Gateway = os.Getenv("SCW_GATEWAY")
6389
}
6490
var gateway string
6591
var err error
66-
if execGateway == serverID || execGateway == args[0] {
92+
if args.Gateway == serverID || args.Gateway == args.Server {
93+
log.Debugf("The server and the gateway are the same host, using direct access to the server")
6794
gateway = ""
6895
} else {
69-
gateway, err = api.ResolveGateway(cmd.API, execGateway)
96+
gateway, err = api.ResolveGateway(ctx.API, args.Gateway)
7097
if err != nil {
71-
log.Fatalf("Cannot resolve Gateway '%s': %v", execGateway, err)
98+
return fmt.Errorf("Cannot resolve Gateway '%s': %v", args.Gateway, err)
7299
}
100+
log.Debugf("The server will be accessed using the gateway '%s' as a SSH relay", gateway)
73101
}
74102

75103
var server *api.ScalewayServer
76-
if execW {
104+
if args.Wait {
77105
// --wait
78-
server, err = api.WaitForServerReady(cmd.API, serverID, gateway)
106+
log.Debugf("Waiting for server to be ready")
107+
server, err = api.WaitForServerReady(ctx.API, serverID, gateway)
79108
if err != nil {
80-
log.Fatalf("Failed to wait for server to be ready, %v", err)
109+
return fmt.Errorf("Failed to wait for server to be ready, %v", err)
81110
}
82111
} else {
83112
// no --wait
84-
server, err = cmd.API.GetServer(serverID)
113+
log.Debugf("Won't wait for the server to be ready, if it is not, the command will fail")
114+
server, err = ctx.API.GetServer(serverID)
85115
if err != nil {
86-
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
116+
return fmt.Errorf("Failed to get server information for %s: %v", serverID, err)
87117
}
88118
}
89119

90-
if execTimeout > 0 {
120+
// --timeout
121+
if args.Timeout > 0 {
122+
log.Debugf("Setting up a global timeout of %d seconds", args.Timeout)
123+
// FIXME: avoid use of log.Fatalf here
91124
go func() {
92-
time.Sleep(time.Duration(execTimeout*1000) * time.Millisecond)
125+
time.Sleep(time.Duration(args.Timeout*1000) * time.Millisecond)
93126
log.Fatalf("Operation timed out")
94127
}()
95128
}
96129

97-
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], !execW, gateway)
130+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, !args.Wait, gateway)
98131
if err != nil {
99-
log.Fatalf("%v", err)
100-
os.Exit(1)
132+
return fmt.Errorf("Failed to run the command: %v", err)
101133
}
134+
102135
log.Debugf("Command successfuly executed")
136+
return nil
103137
}

0 commit comments

Comments
 (0)