forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_swarm.go
120 lines (99 loc) · 2.82 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
package provision
import (
"bytes"
"fmt"
"net/url"
"strings"
"text/template"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/log"
)
type SwarmCommandContext struct {
ContainerName string
DockerDir string
DockerPort int
Ip string
Port string
AuthOptions auth.AuthOptions
SwarmOptions swarm.SwarmOptions
SwarmImage string
}
// Wrapper function to generate a docker run swarm command (manage or join)
// from a template/context and execute it.
func runSwarmCommandFromTemplate(p Provisioner, cmdTmpl string, swarmCmdContext SwarmCommandContext) error {
var (
executedCmdTmpl bytes.Buffer
)
parsedMasterCmdTemplate, err := template.New("swarmMasterCmd").Parse(cmdTmpl)
if err != nil {
return err
}
parsedMasterCmdTemplate.Execute(&executedCmdTmpl, swarmCmdContext)
log.Debugf("The swarm command being run is: %s", executedCmdTmpl.String())
if _, err := p.SSHCommand(executedCmdTmpl.String()); err != nil {
return err
}
return nil
}
func configureSwarm(p Provisioner, swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions) error {
if !swarmOptions.IsSwarm {
return nil
}
ip, err := p.GetDriver().GetIP()
if err != nil {
return err
}
u, err := url.Parse(swarmOptions.Host)
if err != nil {
return err
}
parts := strings.Split(u.Host, ":")
port := parts[1]
dockerDir := p.GetDockerOptionsDir()
swarmCmdContext := SwarmCommandContext{
ContainerName: "",
DockerDir: dockerDir,
DockerPort: 2376,
Ip: ip,
Port: port,
AuthOptions: authOptions,
SwarmOptions: swarmOptions,
SwarmImage: swarmOptions.Image,
}
// First things first, get the swarm image.
if _, err := p.SSHCommand(fmt.Sprintf("sudo docker pull %s", swarmOptions.Image)); err != nil {
return err
}
swarmMasterCmdTemplate := `sudo docker run -d \
--restart=always \
--name swarm-agent-master \
-p {{.Port}}:{{.Port}} \
-v {{.DockerDir}}:{{.DockerDir}} \
{{.SwarmImage}} \
manage \
--tlsverify \
--tlscacert={{.AuthOptions.CaCertRemotePath}} \
--tlscert={{.AuthOptions.ServerCertRemotePath}} \
--tlskey={{.AuthOptions.ServerKeyRemotePath}} \
-H {{.SwarmOptions.Host}} \
--strategy {{.SwarmOptions.Strategy}} {{range .SwarmOptions.ArbitraryFlags}} --{{.}}{{end}} {{.SwarmOptions.Discovery}}
`
swarmWorkerCmdTemplate := `sudo docker run -d \
--restart=always \
--name swarm-agent \
{{.SwarmImage}} \
join --addr {{.Ip}}:{{.DockerPort}} {{.SwarmOptions.Discovery}}
`
if swarmOptions.Master {
log.Debug("Launching swarm master")
if err := runSwarmCommandFromTemplate(p, swarmMasterCmdTemplate, swarmCmdContext); err != nil {
return err
}
}
log.Debug("Launch swarm worker")
if err := runSwarmCommandFromTemplate(p, swarmWorkerCmdTemplate, swarmCmdContext); err != nil {
return err
}
return nil
}