Skip to content
This repository was archived by the owner on Feb 27, 2018. It is now read-only.

Commit 22b11f1

Browse files
Sven DowideitSven Dowideit
authored andcommitted
Merge pull request #229 from SvenDowideit/add-more-sleep-loops
Add a sleep-loop to get the Docker daemon's tcp socket - the pid file is...
2 parents f6ab692 + 4b80b04 commit 22b11f1

2 files changed

Lines changed: 86 additions & 59 deletions

File tree

cmds.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,51 @@ func cmdUp() error {
4040
return fmt.Errorf("Failed to start machine %q (run again with -v for details)", B2D.VM)
4141
}
4242

43-
fmt.Println("Waiting for VM to be started...")
43+
fmt.Println("Waiting for VM and Docker daemon to start...")
4444
//give the VM a little time to start, so we don't kill the Serial Pipe/Socket
4545
time.Sleep(600 * time.Millisecond)
4646
natSSH := fmt.Sprintf("localhost:%d", m.GetSSHPort())
4747
IP := ""
4848
for i := 1; i < 30; i++ {
49+
print(".")
4950
if B2D.Serial && runtime.GOOS != "windows" {
50-
if IP = RequestIPFromSerialPort(m.GetSerialFile()); IP != "" {
51+
if IP, err = RequestIPFromSerialPort(m.GetSerialFile()); err == nil {
5152
break
5253
}
5354
}
54-
if err := read(natSSH, 1, 2*time.Second); err == nil {
55-
IP = RequestIPFromSSH(m)
56-
break
55+
if err := read(natSSH, 1, 300*time.Millisecond); err == nil {
56+
if IP, err = RequestIPFromSSH(m); err == nil {
57+
break
58+
}
5759
}
60+
}
61+
if B2D.Verbose {
62+
fmt.Printf("VM Host-only IP address: %s", IP)
63+
fmt.Printf("\nWaiting for Docker daemon to start...\n")
64+
}
5865

66+
time.Sleep(300 * time.Millisecond)
67+
socket := ""
68+
for i := 1; i < 30; i++ {
5969
print(".")
70+
if socket, err = RequestSocketFromSSH(m); err == nil {
71+
break
72+
}
73+
if B2D.Verbose {
74+
fmt.Printf("Error requesting socket: %s\n", err)
75+
}
76+
time.Sleep(300 * time.Millisecond)
6077
}
61-
print("\n")
78+
fmt.Printf("\nStarted.\n")
6279

63-
fmt.Printf("Started.\n")
64-
65-
if IP == "" {
80+
if socket == "" {
6681
// lets try one more time
6782
time.Sleep(600 * time.Millisecond)
68-
fmt.Printf(" Trying to get IP one more time\n")
83+
fmt.Printf(" Trying to get Docker socket one more time\n")
6984

70-
IP = RequestIPFromSSH(m)
85+
if socket, err = RequestSocketFromSSH(m); err != nil {
86+
fmt.Printf("Error requesting socket: %s\n", err)
87+
}
7188
}
7289
// Copying the certs here - someone might have have written a Windows API client.
7390
certPath, err := RequestCertsUsingSSH(m)
@@ -81,12 +98,11 @@ func cmdUp() error {
8198
fmt.Printf(" \"%s\" ssh\n", os.Args[0])
8299
fmt.Printf("to SSH into the VM instead.\n")
83100
default:
84-
if IP == "" {
85-
fmt.Fprintf(os.Stderr, "Auto detection of the VM's IP address failed.\n")
101+
if socket == "" {
102+
fmt.Fprintf(os.Stderr, "Auto detection of the VM's Docker socket failed.\n")
86103
fmt.Fprintf(os.Stderr, "Please run `boot2docker -v up` to diagnose.\n")
87104
} else {
88105
// Check if $DOCKER_HOST ENV var is properly configured.
89-
socket := RequestSocketFromSSH(m)
90106
if os.Getenv("DOCKER_HOST") != socket || os.Getenv("DOCKER_CERT_PATH") != certPath {
91107
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
92108
printExport(socket, certPath)
@@ -110,7 +126,10 @@ func cmdShellInit() error {
110126
return fmt.Errorf("VM %q is not running.", B2D.VM)
111127
}
112128

113-
socket := RequestSocketFromSSH(m)
129+
socket, err := RequestSocketFromSSH(m)
130+
if err != nil {
131+
return fmt.Errorf("Error requesting socket: %s\n", err)
132+
}
114133

115134
certPath, err := RequestCertsUsingSSH(m)
116135
if err != nil && B2D.Verbose {
@@ -270,7 +289,10 @@ func cmdSocket() error {
270289
return fmt.Errorf("VM %q is not running.", B2D.VM)
271290
}
272291

273-
socket := RequestSocketFromSSH(m)
292+
socket, err := RequestSocketFromSSH(m)
293+
if err != nil {
294+
return fmt.Errorf("Error requesting socket: %s\n", err)
295+
}
274296

275297
fmt.Fprintf(os.Stderr, "\n\t export DOCKER_HOST=")
276298
fmt.Printf("%s", socket)
@@ -316,17 +338,21 @@ func cmdIP() error {
316338

317339
IP := ""
318340
if B2D.Serial {
319-
for i := 1; i < 20; i++ {
320-
if runtime.GOOS != "windows" {
321-
if IP = RequestIPFromSerialPort(m.GetSerialFile()); IP != "" {
322-
break
341+
if runtime.GOOS != "windows" {
342+
if IP, err = RequestIPFromSerialPort(m.GetSerialFile()); err != nil {
343+
if B2D.Verbose {
344+
fmt.Printf("Error getting IP via Serial: %s\n", err)
323345
}
324346
}
325347
}
326348
}
327349

328350
if IP == "" {
329-
IP = RequestIPFromSSH(m)
351+
if IP, err = RequestIPFromSSH(m); err != nil {
352+
if B2D.Verbose {
353+
fmt.Printf("Error getting IP via SSH: %s\n", err)
354+
}
355+
}
330356
}
331357
if IP != "" {
332358
fmt.Fprintf(os.Stderr, "\nThe VM's Host only interface IP address is: ")

util.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -189,60 +189,62 @@ func getSSHCommand(m driver.Machine, args ...string) *exec.Cmd {
189189
return cmd
190190
}
191191

192-
func RequestIPFromSSH(m driver.Machine) string {
192+
func RequestIPFromSSH(m driver.Machine) (string, error) {
193193
cmd := getSSHCommand(m, "ip addr show dev eth1")
194194

195195
b, err := cmd.Output()
196-
IP := ""
197196
if err != nil {
198-
fmt.Printf("%s", err)
199-
} else {
200-
out := string(b)
201-
if B2D.Verbose {
202-
fmt.Printf("SSH returned: %s\nEND SSH\n", out)
203-
}
204-
// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
205-
lines := strings.Split(out, "\n")
206-
for _, line := range lines {
207-
vals := strings.Split(strings.TrimSpace(line), " ")
208-
if len(vals) >= 2 && vals[0] == "inet" {
209-
IP = vals[1][:strings.Index(vals[1], "/")]
210-
break
211-
}
197+
return "", err
198+
}
199+
out := string(b)
200+
if B2D.Verbose {
201+
fmt.Printf("SSH returned: %s\nEND SSH\n", out)
202+
}
203+
// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
204+
lines := strings.Split(out, "\n")
205+
for _, line := range lines {
206+
vals := strings.Split(strings.TrimSpace(line), " ")
207+
if len(vals) >= 2 && vals[0] == "inet" {
208+
return vals[1][:strings.Index(vals[1], "/")], nil
212209
}
213210
}
214-
return IP
211+
212+
return "", fmt.Errorf("No IP address found %s", out)
215213
}
216214

217-
func RequestSocketFromSSH(m driver.Machine) string {
215+
func RequestSocketFromSSH(m driver.Machine) (string, error) {
218216
cmd := getSSHCommand(m, "grep tcp:// /proc/$(cat /var/run/docker.pid)/cmdline")
219217

220218
b, err := cmd.Output()
221219
if err != nil {
222-
fmt.Printf("%s", err)
223-
} else {
224-
out := string(b)
225-
if B2D.Verbose {
226-
fmt.Printf("SSH returned: %s\nEND SSH\n", out)
227-
}
228-
// Lets only use the first one - its possible to specify more than one...
229-
lines := strings.Split(out, "\n")
230-
tcpRE := regexp.MustCompile(`^(tcp://)(0.0.0.0)(:.*)`)
231-
if s := tcpRE.FindStringSubmatch(lines[0]); s != nil {
232-
IP := RequestIPFromSSH(m)
233-
return s[1] + IP + s[3]
220+
return "", err
221+
}
222+
out := string(b)
223+
if B2D.Verbose {
224+
fmt.Printf("SSH returned: %s\nEND SSH\n", out)
225+
}
226+
// Lets only use the first one - its possible to specify more than one...
227+
lines := strings.Split(out, "\n")
228+
tcpRE := regexp.MustCompile(`^(tcp://)(0.0.0.0)(:.*)`)
229+
if s := tcpRE.FindStringSubmatch(lines[0]); s != nil {
230+
IP, err := RequestIPFromSSH(m)
231+
if err != nil {
232+
return "", err
234233
}
235-
return lines[0]
234+
return s[1] + IP + s[3], nil
235+
}
236+
if !strings.HasPrefix(lines[0], "tcp://") {
237+
return "", fmt.Errorf("Error requesting Docker Socket: %s", lines[0])
236238
}
237-
return ""
239+
return lines[0], nil
238240
}
239241

240242
// use the serial port socket to ask what the VM's host only IP is
241-
func RequestIPFromSerialPort(socket string) string {
243+
func RequestIPFromSerialPort(socket string) (string, error) {
242244
c, err := net.Dial("unix", socket)
243245

244246
if err != nil {
245-
return ""
247+
return "", err
246248
}
247249
defer c.Close()
248250
c.SetDeadline(time.Now().Add(time.Second))
@@ -257,15 +259,14 @@ func RequestIPFromSerialPort(socket string) string {
257259
for IP == "" {
258260
_, err := c.Write([]byte("ip addr show dev eth1\r"))
259261
if err != nil {
260-
println(err)
261-
break
262+
return "", err
262263
}
263264
time.Sleep(1 * time.Second)
264265
buf := make([]byte, 1024)
265266
for {
266267
n, err := c.Read(buf[:])
267268
if err != nil {
268-
return IP
269+
return "", err
269270
}
270271
line = line + string(buf[0:n])
271272
fullLog += string(buf[0:n])
@@ -293,7 +294,7 @@ func RequestIPFromSerialPort(socket string) string {
293294
fmt.Printf(fullLog)
294295
}
295296

296-
return IP
297+
return IP, nil
297298
}
298299

299300
// TODO: need to add or abstract to get a Serial coms version

0 commit comments

Comments
 (0)