88 "io/ioutil"
99 "net"
1010 "os"
11- //"os/exec"
1211 "path/filepath"
1312 "runtime"
1413 "strings"
@@ -80,6 +79,7 @@ func cmdInit() int {
8079 m .OSType = "Linux26_64"
8180 m .CPUs = uint (runtime .NumCPU ())
8281 m .Memory = B2D .Memory
82+ m .SerialFile = B2D .SerialFile
8383
8484 m .Flag |= vbx .F_pae
8585 m .Flag |= vbx .F_longmode // important: use x86-64 processor
@@ -232,27 +232,50 @@ func cmdUp() int {
232232 return 1
233233 }
234234
235- logf ( "Waiting for SSH server to start..." )
236- addr := fmt . Sprintf ( "localhost:%d " , m . SSHPort )
237- const n = 10
238- // Try to connect to the SSH 10 times at 3 sec interval before giving up.
239- if err := read ( addr , n , 3 * time . Second ); err != nil {
240- logf ("Failed to connect to SSH port at %s after %d attempts. Last error: %v " , addr , n , err )
235+ if err := m . Refresh (); err != nil {
236+ logf ( "Failed to start machine %q: %s " , B2D . VM , err )
237+ return 1
238+ }
239+ if m . State != vbx . Running {
240+ logf ("Failed to start machine %q (run again with -v for details) " , B2D . VM )
241241 return 1
242242 }
243243
244+ logf ("Waiting for VM to be started..." )
245+ //give the VM a little time to start, so we don't kill the Serial Pipe/Socket
246+ time .Sleep (2 )
247+ natSSH := fmt .Sprintf ("localhost:%d" , B2D .SSHPort )
248+ IP := ""
249+ for i := 1 ; i < 30 ; i ++ {
250+ if B2D .Serial && runtime .GOOS != "windows" {
251+ if IP = RequestIPFromSerialPort (m .SerialFile ); IP != "" {
252+ break
253+ }
254+ }
255+ if err := read (natSSH , 1 , 1 * time .Second ); err == nil {
256+ IP = RequestIPFromSSH (m )
257+ break
258+ }
259+
260+ print ("." )
261+ }
262+ print ("\n " )
263+
244264 logf ("Started." )
245265
266+ if IP == "" {
267+ logf ("Auto detection of the VM's IP address." )
268+ }
246269 switch runtime .GOOS {
247270 case "windows" :
248271 logf ("Docker client does not run on Windows for now. Please use" )
249272 logf (" %s ssh" , os .Args [0 ])
250273 logf ("to SSH into the VM instead." )
251274 default :
252275 // Check if $DOCKER_HOST ENV var is properly configured.
253- if os .Getenv ("DOCKER_HOST" ) != fmt .Sprintf ("tcp://localhost :%d" , m .DockerPort ) {
276+ if os .Getenv ("DOCKER_HOST" ) != fmt .Sprintf ("tcp://%s :%d" , IP , m .DockerPort ) {
254277 logf ("To connect the Docker client to the Docker daemon, please set:" )
255- logf (" export DOCKER_HOST=tcp://localhost :%d" , m .DockerPort )
278+ logf (" export DOCKER_HOST=tcp://%s :%d" , IP , m .DockerPort )
256279 }
257280 }
258281 return 0
@@ -433,6 +456,33 @@ func cmdIP() int {
433456 return 1
434457 }
435458
459+ IP := ""
460+ if B2D .Serial {
461+ for i := 1 ; i < 20 ; i ++ {
462+ if runtime .GOOS != "windows" {
463+ if IP = RequestIPFromSerialPort (m .SerialFile ); IP != "" {
464+ break
465+ }
466+ }
467+ }
468+ }
469+
470+ if IP == "" {
471+ IP = RequestIPFromSSH (m )
472+ }
473+ if IP != "" {
474+ errf ("\n The VM's Host only interface IP address is: " )
475+ fmt .Printf ("%s" , IP )
476+ errf ("\n \n " )
477+ } else {
478+ errf ("\n Failed to get VM Host only IP address.\n " )
479+ errf ("\t Was the VM initilized using boot2docker?\n " )
480+ }
481+ return 0
482+ }
483+
484+ func RequestIPFromSSH (m * vbx.Machine ) string {
485+ // fall back to using the NAT port forwarded ssh
436486 out , err := cmd (B2D .SSH ,
437487 //"-vvv", //TODO: add if its boot2docker -v
438488 "-o" , "StrictHostKeyChecking=no" ,
@@ -442,25 +492,21 @@ func cmdIP() int {
442492 "docker@localhost" ,
443493 "ip addr show dev eth1" ,
444494 )
495+ IP := ""
445496 if err != nil {
446497 logf ("%s" , err )
447- return 1
448- }
449- // parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
450- lines := strings .Split (out , "\n " )
451- for _ , line := range lines {
452- vals := strings .Split (strings .TrimSpace (line ), " " )
453- if vals [0 ] == "inet" {
454- ip := vals [1 ][:strings .Index (vals [1 ], "/" )]
455- errf ("\n The VM's Host only interface IP address is: " )
456- fmt .Printf ("%s" , ip )
457- errf ("\n \n " )
458- return 0
498+ } else {
499+ // parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
500+ lines := strings .Split (out , "\n " )
501+ for _ , line := range lines {
502+ vals := strings .Split (strings .TrimSpace (line ), " " )
503+ if len (vals ) >= 2 && vals [0 ] == "inet" {
504+ IP = vals [1 ][:strings .Index (vals [1 ], "/" )]
505+ break
506+ }
459507 }
460508 }
461- errf ("\n Failed to get VM Host only IP address.\n " )
462- errf ("\t Was the VM initilized using boot2docker?\n " )
463- return 0
509+ return IP
464510}
465511
466512// Download the boot2docker ISO image.
0 commit comments