5
5
package commands
6
6
7
7
import (
8
+ "fmt"
8
9
"os"
9
10
"time"
10
11
@@ -16,7 +17,7 @@ import (
16
17
)
17
18
18
19
var cmdExec = & types.Command {
19
- Exec : runExec ,
20
+ Exec : cmdExecExec ,
20
21
UsageLine : "exec [OPTIONS] SERVER [COMMAND] [ARGS...]" ,
21
22
Description : "Run a command on a running server" ,
22
23
Help : "Run a command on a running server." ,
@@ -47,57 +48,90 @@ var execTimeout float64 // -T flag
47
48
var execHelp bool // -h, --help flag
48
49
var execGateway string // -g, --gateway flag
49
50
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 ) {
51
61
if execHelp {
52
62
cmd .PrintUsage ()
53
63
}
54
- if len (args ) < 1 {
64
+ if len (rawArgs ) < 1 {
55
65
cmd .PrintShortUsage ()
56
66
}
57
67
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 )
59
85
60
86
// Resolve gateway
61
- if execGateway == "" {
62
- execGateway = os .Getenv ("SCW_GATEWAY" )
87
+ if args . Gateway == "" {
88
+ args . Gateway = os .Getenv ("SCW_GATEWAY" )
63
89
}
64
90
var gateway string
65
91
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" )
67
94
gateway = ""
68
95
} else {
69
- gateway , err = api .ResolveGateway (cmd .API , execGateway )
96
+ gateway , err = api .ResolveGateway (ctx .API , args . Gateway )
70
97
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 )
72
99
}
100
+ log .Debugf ("The server will be accessed using the gateway '%s' as a SSH relay" , gateway )
73
101
}
74
102
75
103
var server * api.ScalewayServer
76
- if execW {
104
+ if args . Wait {
77
105
// --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 )
79
108
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 )
81
110
}
82
111
} else {
83
112
// 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 )
85
115
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 )
87
117
}
88
118
}
89
119
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
91
124
go func () {
92
- time .Sleep (time .Duration (execTimeout * 1000 ) * time .Millisecond )
125
+ time .Sleep (time .Duration (args . Timeout * 1000 ) * time .Millisecond )
93
126
log .Fatalf ("Operation timed out" )
94
127
}()
95
128
}
96
129
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 )
98
131
if err != nil {
99
- log .Fatalf ("%v" , err )
100
- os .Exit (1 )
132
+ return fmt .Errorf ("Failed to run the command: %v" , err )
101
133
}
134
+
102
135
log .Debugf ("Command successfuly executed" )
136
+ return nil
103
137
}
0 commit comments