@@ -24,6 +24,7 @@ var cmdRun = &types.Command{
24
24
$ scw run ubuntu-trusty
25
25
$ scw run --name=mydocker docker docker run moul/nyancat:armhf
26
26
$ scw run --bootscript=3.2.34 --env="boot=live rescue_image=http://j.mp/scaleway-ubuntu-trusty-tarball" 50GB bash
27
+ $ scw run attach alpine
27
28
` ,
28
29
}
29
30
@@ -33,6 +34,7 @@ func init() {
33
34
cmdRun .Flag .StringVar (& runCreateEnv , []string {"e" , "-env" }, "" , "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)" )
34
35
cmdRun .Flag .StringVar (& runCreateVolume , []string {"v" , "-volume" }, "" , "Attach additional volume (i.e., 50G)" )
35
36
cmdRun .Flag .BoolVar (& runHelpFlag , []string {"h" , "-help" }, false , "Print usage" )
37
+ cmdRun .Flag .BoolVar (& runAttachFlag , []string {"a" , "-attach" }, false , "Attach to serial console" )
36
38
// FIXME: handle start --timeout
37
39
}
38
40
@@ -42,6 +44,7 @@ var runCreateBootscript string // --bootscript flag
42
44
var runCreateEnv string // -e, --env flag
43
45
var runCreateVolume string // -v, --volume flag
44
46
var runHelpFlag bool // -h, --help flag
47
+ var runAttachFlag bool // -a, --attach flag
45
48
46
49
func runRun (cmd * types.Command , args []string ) {
47
50
if runHelpFlag {
@@ -50,8 +53,9 @@ func runRun(cmd *types.Command, args []string) {
50
53
if len (args ) < 1 {
51
54
cmd .PrintShortUsage ()
52
55
}
53
-
54
- //image := args[0]
56
+ if runAttachFlag && len (args ) > 1 {
57
+ log .Fatalf ("Cannot use '--attach' and 'COMMAND [ARG...]' at the same time. See 'scw run --help'" )
58
+ }
55
59
56
60
// create IMAGE
57
61
log .Debugf ("Creating a new server" )
@@ -69,26 +73,35 @@ func runRun(cmd *types.Command, args []string) {
69
73
}
70
74
log .Debugf ("Server is booting" )
71
75
72
- // waiting for server to be ready
73
- log .Debugf ("Waiting for server to be ready" )
74
- // We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
75
- time .Sleep (30 * time .Second )
76
- server , err := api .WaitForServerReady (cmd .API , serverID )
77
- if err != nil {
78
- log .Fatalf ("Cannot get access to server %s: %v" , serverID , err )
79
- }
80
- log .Debugf ("Server is ready: %s" , server .PublicAddress .IP )
81
-
82
- // exec -w SERVER COMMAND ARGS...
83
- log .Debugf ("Executing command" )
84
- if len (args ) < 2 {
85
- err = utils .SSHExec (server .PublicAddress .IP , []string {"if [ -x /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi" }, false )
76
+ if runAttachFlag {
77
+ // Attach to server serial
78
+ log .Debugf ("Attaching to server console" )
79
+ err = utils .AttachToSerial (serverID , cmd .API .Token )
80
+ if err != nil {
81
+ log .Fatalf ("Cannot attach to server serial: %v" , err )
82
+ }
86
83
} else {
87
- err = utils .SSHExec (server .PublicAddress .IP , args [1 :], false )
88
- }
89
- if err != nil {
90
- log .Debugf ("Command execution failed: %v" , err )
91
- os .Exit (1 )
84
+ // waiting for server to be ready
85
+ log .Debugf ("Waiting for server to be ready" )
86
+ // We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
87
+ time .Sleep (30 * time .Second )
88
+ server , err := api .WaitForServerReady (cmd .API , serverID )
89
+ if err != nil {
90
+ log .Fatalf ("Cannot get access to server %s: %v" , serverID , err )
91
+ }
92
+ log .Debugf ("Server is ready: %s" , server .PublicAddress .IP )
93
+
94
+ // exec -w SERVER COMMAND ARGS...
95
+ log .Debugf ("Executing command" )
96
+ if len (args ) < 2 {
97
+ err = utils .SSHExec (server .PublicAddress .IP , []string {"if [ -x /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi" }, false )
98
+ } else {
99
+ err = utils .SSHExec (server .PublicAddress .IP , args [1 :], false )
100
+ }
101
+ if err != nil {
102
+ log .Debugf ("Command execution failed: %v" , err )
103
+ os .Exit (1 )
104
+ }
105
+ log .Debugf ("Command successfuly executed" )
92
106
}
93
- log .Debugf ("Command successfuly executed" )
94
107
}
0 commit comments