X Tutup
Skip to content

Commit 3757dc5

Browse files
Add code to wait for Docker daemon on start
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
1 parent 2193bd7 commit 3757dc5

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

libmachine/engine/engine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package engine
22

3+
const (
4+
DefaultPort = 2376
5+
)
6+
37
type Options struct {
48
ArbitraryFlags []string
59
DNS []string `json:"Dns"`

libmachine/host/host.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ func (h *Host) Start() error {
111111
}
112112

113113
log.Infof("Machine %q was started.", h.Name)
114+
115+
provisioner, err := provision.DetectProvisioner(h.Driver)
116+
if err != nil {
117+
return err
118+
}
119+
120+
// TODO: Migrate away from using hardcoded daemon port.
121+
if err := provision.WaitForDocker(provisioner, engine.DefaultPort); err != nil {
122+
return err
123+
}
124+
114125
return nil
115126
}
116127

libmachine/host/host_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package host
33
import (
44
"testing"
55

6+
"github.com/docker/machine/drivers/fakedriver"
67
_ "github.com/docker/machine/drivers/none"
8+
"github.com/docker/machine/libmachine/provision"
9+
"github.com/docker/machine/libmachine/state"
710
)
811

912
func TestValidateHostnameValid(t *testing.T) {
@@ -35,3 +38,47 @@ func TestValidateHostnameInvalid(t *testing.T) {
3538
}
3639
}
3740
}
41+
42+
type NetstatProvisioner struct {
43+
*provision.FakeProvisioner
44+
}
45+
46+
func (p *NetstatProvisioner) SSHCommand(args string) (string, error) {
47+
return `Active Internet connections (servers and established)
48+
Proto Recv-Q Send-Q Local Address Foreign Address State
49+
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
50+
tcp 0 72 192.168.25.141:ssh 192.168.25.1:63235 ESTABLISHED
51+
tcp 0 0 :::2376 :::* LISTEN
52+
tcp 0 0 :::ssh :::* LISTEN
53+
Active UNIX domain sockets (servers and established)
54+
Proto RefCnt Flags Type State I-Node Path
55+
unix 2 [ ACC ] STREAM LISTENING 17990 /var/run/acpid.socket
56+
unix 2 [ ACC ] SEQPACKET LISTENING 14233 /run/udev/control
57+
unix 2 [ ACC ] STREAM LISTENING 19365 /var/run/docker.sock
58+
unix 3 [ ] STREAM CONNECTED 19774
59+
unix 3 [ ] STREAM CONNECTED 19775
60+
unix 3 [ ] DGRAM 14243
61+
unix 3 [ ] DGRAM 14242`, nil
62+
}
63+
64+
func NewNetstatProvisioner() provision.Provisioner {
65+
return &NetstatProvisioner{
66+
&provision.FakeProvisioner{},
67+
}
68+
}
69+
70+
func TestStart(t *testing.T) {
71+
provision.SetDetector(&provision.FakeDetector{
72+
NewNetstatProvisioner(),
73+
})
74+
75+
host := &Host{
76+
Driver: &fakedriver.Driver{
77+
MockState: state.Stopped,
78+
},
79+
}
80+
81+
if err := host.Start(); err != nil {
82+
t.Fatalf("Expected no error but got one: %s", err)
83+
}
84+
}

libmachine/provision/boot2docker.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,35 +194,31 @@ func (provisioner *Boot2DockerProvisioner) AttemptIPContact(dockerPort int) {
194194
}
195195

196196
if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, dockerPort), 5*time.Second); err != nil {
197-
log.Warn(`
197+
log.Warnf(`
198198
This machine has been allocated an IP address, but Docker Machine could not
199199
reach it successfully.
200200
201201
SSH for the machine should still work, but connecting to exposed ports, such as
202-
the Docker daemon port (usually <ip>:2376), may not work properly.
202+
the Docker daemon port (usually <ip>:%d), may not work properly.
203203
204204
You may need to add the route manually, or use another related workaround.
205205
206206
This could be due to a VPN, proxy, or host file configuration issue.
207207
208-
You also might want to clear any VirtualBox host only interfaces you are not using.`)
208+
You also might want to clear any VirtualBox host only interfaces you are not using.`, engine.DefaultPort)
209209
} else {
210210
conn.Close()
211211
}
212212
}
213213

214214
func (provisioner *Boot2DockerProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
215-
const (
216-
dockerPort = 2376
217-
)
218-
219215
var (
220216
err error
221217
)
222218

223219
defer func() {
224220
if err == nil {
225-
provisioner.AttemptIPContact(dockerPort)
221+
provisioner.AttemptIPContact(engine.DefaultPort)
226222
}
227223
}()
228224

@@ -241,7 +237,7 @@ func (provisioner *Boot2DockerProvisioner) Provision(swarmOptions swarm.Options,
241237

242238
// b2d hosts need to wait for the daemon to be up
243239
// before continuing with provisioning
244-
if err = waitForDocker(provisioner, dockerPort); err != nil {
240+
if err = WaitForDocker(provisioner, engine.DefaultPort); err != nil {
245241
return err
246242
}
247243

libmachine/provision/utils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/docker/machine/libmachine/auth"
1515
"github.com/docker/machine/libmachine/cert"
16+
"github.com/docker/machine/libmachine/engine"
1617
"github.com/docker/machine/libmachine/log"
1718
"github.com/docker/machine/libmachine/mcnutils"
1819
"github.com/docker/machine/libmachine/provision/serviceaction"
@@ -161,7 +162,7 @@ func ConfigureAuth(p Provisioner) error {
161162
if err != nil {
162163
return err
163164
}
164-
dockerPort := 2376
165+
dockerPort := engine.DefaultPort
165166
parts := strings.Split(u.Host, ":")
166167
if len(parts) == 2 {
167168
dPort, err := strconv.Atoi(parts[1])
@@ -186,7 +187,7 @@ func ConfigureAuth(p Provisioner) error {
186187
return err
187188
}
188189

189-
return waitForDocker(p, dockerPort)
190+
return WaitForDocker(p, dockerPort)
190191
}
191192

192193
func matchNetstatOut(reDaemonListening, netstatOut string) bool {
@@ -262,7 +263,7 @@ func checkDaemonUp(p Provisioner, dockerPort int) func() bool {
262263
}
263264
}
264265

265-
func waitForDocker(p Provisioner, dockerPort int) error {
266+
func WaitForDocker(p Provisioner, dockerPort int) error {
266267
if err := mcnutils.WaitForSpecific(checkDaemonUp(p, dockerPort), 10, 3*time.Second); err != nil {
267268
return NewErrDaemonAvailable(err)
268269
}

libmachine/provision/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestMachinePortBoot2Docker(t *testing.T) {
102102
p := &Boot2DockerProvisioner{
103103
Driver: &fakedriver.Driver{},
104104
}
105-
dockerPort := 2376
105+
dockerPort := engine.DefaultPort
106106
bindURL := fmt.Sprintf("tcp://0.0.0.0:%d", dockerPort)
107107
p.AuthOptions = auth.Options{
108108
CaCertRemotePath: "/test/ca-cert",

0 commit comments

Comments
 (0)
X Tutup