forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_swarm.go
138 lines (119 loc) · 3.24 KB
/
configure_swarm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package provision
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcndockerclient"
"github.com/docker/machine/libmachine/swarm"
"github.com/samalba/dockerclient"
)
func configureSwarm(p Provisioner, swarmOptions swarm.Options, authOptions auth.Options) error {
if !swarmOptions.IsSwarm {
return nil
}
log.Info("Configuring swarm...")
ip, err := p.GetDriver().GetIP()
if err != nil {
return err
}
u, err := url.Parse(swarmOptions.Host)
if err != nil {
return err
}
enginePort := engine.DefaultPort
engineURL, err := p.GetDriver().GetURL()
if err != nil {
return err
}
parts := strings.Split(engineURL, ":")
if len(parts) == 3 {
dPort, err := strconv.Atoi(parts[2])
if err != nil {
return err
}
enginePort = dPort
}
parts = strings.Split(u.Host, ":")
port := parts[1]
dockerDir := p.GetDockerOptionsDir()
dockerHost := &mcndockerclient.RemoteDocker{
HostURL: fmt.Sprintf("tcp://%s:%d", ip, enginePort),
AuthOption: &authOptions,
}
advertiseInfo := fmt.Sprintf("%s:%d", ip, enginePort)
if swarmOptions.Master {
advertiseMasterInfo := fmt.Sprintf("%s:%s", ip, "3376")
cmd := fmt.Sprintf("manage --tlsverify --tlscacert=%s --tlscert=%s --tlskey=%s -H %s --strategy %s --advertise %s",
authOptions.CaCertRemotePath,
authOptions.ServerCertRemotePath,
authOptions.ServerKeyRemotePath,
swarmOptions.Host,
swarmOptions.Strategy,
advertiseMasterInfo,
)
if swarmOptions.IsExperimental {
cmd = "--experimental " + cmd
}
cmdMaster := strings.Fields(cmd)
for _, option := range swarmOptions.ArbitraryFlags {
cmdMaster = append(cmdMaster, "--"+option)
}
//Discovery must be at end of command
cmdMaster = append(cmdMaster, swarmOptions.Discovery)
hostBind := fmt.Sprintf("%s:%s", dockerDir, dockerDir)
masterHostConfig := dockerclient.HostConfig{
RestartPolicy: dockerclient.RestartPolicy{
Name: "always",
MaximumRetryCount: 0,
},
Binds: []string{hostBind},
PortBindings: map[string][]dockerclient.PortBinding{
"3376/tcp": {
{
HostIp: "0.0.0.0",
HostPort: port,
},
},
},
}
swarmMasterConfig := &dockerclient.ContainerConfig{
Image: swarmOptions.Image,
Env: swarmOptions.Env,
ExposedPorts: map[string]struct{}{
"2375/tcp": {},
"3376/tcp": {},
},
Cmd: cmdMaster,
HostConfig: masterHostConfig,
}
err = mcndockerclient.CreateContainer(dockerHost, swarmMasterConfig, "swarm-agent-master")
if err != nil {
return err
}
}
workerHostConfig := dockerclient.HostConfig{
RestartPolicy: dockerclient.RestartPolicy{
Name: "always",
MaximumRetryCount: 0,
},
}
swarmWorkerConfig := &dockerclient.ContainerConfig{
Image: swarmOptions.Image,
Env: swarmOptions.Env,
Cmd: []string{
"join",
"--advertise",
advertiseInfo,
swarmOptions.Discovery,
},
HostConfig: workerHostConfig,
}
if swarmOptions.IsExperimental {
swarmWorkerConfig.Cmd = append([]string{"--experimental"}, swarmWorkerConfig.Cmd...)
}
return mcndockerclient.CreateContainer(dockerHost, swarmWorkerConfig, "swarm-agent")
}