@@ -24,6 +24,7 @@ var cmdRun = &types.Command{
2424 $ scw run ubuntu-trusty
2525 $ scw run --name=mydocker docker docker run moul/nyancat:armhf
2626 $ 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
2728` ,
2829}
2930
@@ -33,6 +34,7 @@ func init() {
3334 cmdRun .Flag .StringVar (& runCreateEnv , []string {"e" , "-env" }, "" , "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)" )
3435 cmdRun .Flag .StringVar (& runCreateVolume , []string {"v" , "-volume" }, "" , "Attach additional volume (i.e., 50G)" )
3536 cmdRun .Flag .BoolVar (& runHelpFlag , []string {"h" , "-help" }, false , "Print usage" )
37+ cmdRun .Flag .BoolVar (& runAttachFlag , []string {"a" , "-attach" }, false , "Attach to serial console" )
3638 // FIXME: handle start --timeout
3739}
3840
@@ -42,6 +44,7 @@ var runCreateBootscript string // --bootscript flag
4244var runCreateEnv string // -e, --env flag
4345var runCreateVolume string // -v, --volume flag
4446var runHelpFlag bool // -h, --help flag
47+ var runAttachFlag bool // -a, --attach flag
4548
4649func runRun (cmd * types.Command , args []string ) {
4750 if runHelpFlag {
@@ -50,8 +53,9 @@ func runRun(cmd *types.Command, args []string) {
5053 if len (args ) < 1 {
5154 cmd .PrintShortUsage ()
5255 }
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+ }
5559
5660 // create IMAGE
5761 log .Debugf ("Creating a new server" )
@@ -69,26 +73,35 @@ func runRun(cmd *types.Command, args []string) {
6973 }
7074 log .Debugf ("Server is booting" )
7175
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+ }
8683 } 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" )
92106 }
93- log .Debugf ("Command successfuly executed" )
94107}
0 commit comments