@@ -30,6 +30,7 @@ type RunArgs struct {
3030 Volumes []string
3131 AutoRemove bool
3232 TmpSSHKey bool
33+ ShowBoot bool
3334 // DynamicIPRequired
3435 // Timeout
3536}
@@ -103,47 +104,99 @@ func Run(ctx CommandContext, args RunArgs) error {
103104 if args .Detach {
104105 fmt .Fprintln (ctx .Stdout , serverID )
105106 return nil
106- } else {
107- // Sync cache on disk
108- ctx .API .Sync ()
109107 }
108+ // Sync cache on disk
109+ ctx .API .Sync ()
110110
111- if args .Attach {
111+ if args .ShowBoot {
112112 // Attach to server serial
113113 logrus .Info ("Attaching to server console ..." )
114- err = utils .AttachToSerial (serverID , ctx .API .Token , true )
114+ gottycli , done , err : = utils .AttachToSerial (serverID , ctx .API .Token )
115115 if err != nil {
116116 return fmt .Errorf ("cannot attach to server serial: %v" , err )
117117 }
118- } else {
119- // Resolve gateway
120- gateway , err := api .ResolveGateway (ctx .API , args .Gateway )
118+ utils .Quiet (true )
119+ notif , gateway , err := waitSSHConnection (ctx , args , serverID )
121120 if err != nil {
122- return fmt . Errorf ( "cannot resolve Gateway '%s': %v" , args . Gateway , err )
121+ return err
123122 }
124-
125- // waiting for server to be ready
126- logrus .Debug ("Waiting for server to be ready" )
127- // We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
128- server , err := api .WaitForServerReady (ctx .API , serverID , gateway )
123+ sshConnection := <- notif
124+ gottycli .ExitLoop ()
125+ <- done
126+ utils .Quiet (false )
127+ if sshConnection .err != nil {
128+ return sshConnection .err
129+ }
130+ server := sshConnection .server
131+ logrus .Info ("Connecting to server ..." )
132+ if err = utils .SSHExec (server .PublicAddress .IP , server .PrivateIP , []string {}, false , gateway ); err != nil {
133+ return fmt .Errorf ("Connection to server failed: %v" , err )
134+ }
135+ } else if args .Attach {
136+ // Attach to server serial
137+ logrus .Info ("Attaching to server console ..." )
138+ gottycli , done , err := utils .AttachToSerial (serverID , ctx .API .Token )
129139 if err != nil {
130- return fmt .Errorf ("cannot get access to server %s : %v" , serverID , err )
140+ return fmt .Errorf ("cannot attach to server serial : %v" , err )
131141 }
132- logrus .Debugf ("SSH server is available: %s:22" , server .PublicAddress .IP )
133- logrus .Info ("Server is ready !" )
134-
142+ <- done
143+ gottycli .Close ()
144+ } else {
145+ notif , gateway , err := waitSSHConnection (ctx , args , serverID )
146+ if err != nil {
147+ return err
148+ }
149+ sshConnection := <- notif
150+ if sshConnection .err != nil {
151+ return sshConnection .err
152+ }
153+ server := sshConnection .server
135154 // exec -w SERVER COMMAND ARGS...
136155 if len (args .Command ) < 1 {
137156 logrus .Info ("Connecting to server ..." )
138- err = utils .SSHExec (server .PublicAddress .IP , server .PrivateIP , []string {}, false , gateway )
157+ if err = utils .SSHExec (server .PublicAddress .IP , server .PrivateIP , []string {}, false , gateway ); err != nil {
158+ return fmt .Errorf ("Connection to server failed: %v" , err )
159+ }
139160 } else {
140161 logrus .Infof ("Executing command: %s ..." , args .Command )
141- err = utils .SSHExec (server .PublicAddress .IP , server .PrivateIP , args .Command , false , gateway )
142- }
143- if err != nil {
144- return fmt . Errorf ( "command execution failed: %v" , err )
162+ if err = utils .SSHExec (server .PublicAddress .IP , server .PrivateIP , args .Command , false , gateway ); err != nil {
163+ return fmt . Errorf ( "command execution failed: %v" , err )
164+ }
165+ logrus . Info ( "Command successfuly executed" )
145166 }
146- logrus .Info ("Command successfuly executed" )
147167 }
148168 return nil
149169}
170+
171+ type notifSSHConnection struct {
172+ server * api.ScalewayServer
173+ err error
174+ }
175+
176+ func waitSSHConnection (ctx CommandContext , args RunArgs , serverID string ) (chan notifSSHConnection , string , error ) {
177+ notif := make (chan notifSSHConnection )
178+ // Resolve gateway
179+ gateway , err := api .ResolveGateway (ctx .API , args .Gateway )
180+ if err != nil {
181+ return nil , "" , fmt .Errorf ("cannot resolve Gateway '%s': %v" , args .Gateway , err )
182+ }
183+
184+ // waiting for server to be ready
185+ logrus .Debug ("Waiting for server to be ready" )
186+ // We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
187+ go func () {
188+ server , err := api .WaitForServerReady (ctx .API , serverID , gateway )
189+ if err != nil {
190+ notif <- notifSSHConnection {
191+ err : fmt .Errorf ("cannot get access to server %s: %v" , serverID , err ),
192+ }
193+ return
194+ }
195+ logrus .Debugf ("SSH server is available: %s:22" , server .PublicAddress .IP )
196+ logrus .Info ("Server is ready !" )
197+ notif <- notifSSHConnection {
198+ server : server ,
199+ }
200+ }()
201+ return notif , gateway , nil
202+ }
0 commit comments