5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
+
8
9
log "github.com/Sirupsen/logrus"
9
10
docker_types "github.com/docker/docker/api/types"
10
11
"github.com/docker/docker/api/types/filters"
@@ -13,8 +14,8 @@ import (
13
14
"github.com/docker/infrakit/pkg/plugin/group/util"
14
15
"github.com/docker/infrakit/pkg/spi/flavor"
15
16
"github.com/docker/infrakit/pkg/spi/instance"
17
+ "github.com/docker/infrakit/pkg/template"
16
18
"golang.org/x/net/context"
17
- "text/template"
18
19
)
19
20
20
21
type nodeType string
@@ -26,12 +27,13 @@ const (
26
27
)
27
28
28
29
// NewSwarmFlavor creates a flavor.Plugin that creates manager and worker nodes connected in a swarm.
29
- func NewSwarmFlavor (dockerClient client.APIClient ) flavor.Plugin {
30
- return & swarmFlavor {client : dockerClient }
30
+ func NewSwarmFlavor (dockerClient client.APIClient , templ * template. Template ) flavor.Plugin {
31
+ return & swarmFlavor {client : dockerClient , initScript : templ }
31
32
}
32
33
33
34
type swarmFlavor struct {
34
- client client.APIClient
35
+ client client.APIClient
36
+ initScript * template.Template
35
37
}
36
38
37
39
type schema struct {
@@ -135,39 +137,20 @@ func (s swarmFlavor) Validate(flavorProperties json.RawMessage, allocation types
135
137
const (
136
138
// associationTag is a machine tag added to associate machines with Swarm nodes.
137
139
associationTag = "swarm-association-id"
138
-
139
- // bootScript is used to generate node boot scripts.
140
- bootScript = `#!/bin/sh
141
- set -o errexit
142
- set -o nounset
143
- set -o xtrace
144
-
145
- mkdir -p /etc/docker
146
- cat << EOF > /etc/docker/daemon.json
147
- {
148
- "labels": ["swarm-association-id={{.ASSOCIATION_ID}}"]
149
- }
150
- EOF
151
-
152
- {{.RESTART_DOCKER}}
153
-
154
- docker swarm join {{.MY_IP}} --token {{.JOIN_TOKEN}}
155
- `
156
140
)
157
141
158
- func generateInitScript (joinIP , joinToken , associationID , restartCommand string ) string {
159
- buffer := bytes.Buffer {}
160
- templ := template .Must (template .New ("" ).Parse (bootScript ))
142
+ func generateInitScript (templ * template.Template , joinIP , joinToken , associationID , restartCommand string ) (string , error ) {
143
+ var buffer bytes.Buffer
161
144
err := templ .Execute (& buffer , map [string ]string {
162
145
"MY_IP" : joinIP ,
163
146
"JOIN_TOKEN" : joinToken ,
164
147
"ASSOCIATION_ID" : associationID ,
165
148
"RESTART_DOCKER" : restartCommand ,
166
149
})
167
150
if err != nil {
168
- panic ( err )
151
+ return "" , err
169
152
}
170
- return buffer .String ()
153
+ return buffer .String (), nil
171
154
}
172
155
173
156
// Healthy determines whether an instance is healthy. This is determined by whether it has successfully joined the
@@ -246,7 +229,7 @@ func (s swarmFlavor) Drain(flavorProperties json.RawMessage, inst instance.Descr
246
229
}
247
230
}
248
231
249
- func (s swarmFlavor ) Prepare (
232
+ func (s * swarmFlavor ) Prepare (
250
233
flavorProperties json.RawMessage ,
251
234
spec instance.Spec ,
252
235
allocation types.AllocationMethod ) (instance.Spec , error ) {
@@ -281,24 +264,34 @@ func (s swarmFlavor) Prepare(
281
264
282
265
switch properties .Type {
283
266
case worker :
284
- spec .Init = generateInitScript (
285
- self .ManagerStatus .Addr ,
286
- swarmStatus .JoinTokens .Worker ,
287
- associationID ,
288
- properties .DockerRestartCommand )
267
+ initScript , err :=
268
+ generateInitScript (
269
+ s .initScript ,
270
+ self .ManagerStatus .Addr ,
271
+ swarmStatus .JoinTokens .Worker ,
272
+ associationID ,
273
+ properties .DockerRestartCommand )
274
+ if err != nil {
275
+ return spec , err
276
+ }
277
+ spec .Init = initScript
289
278
290
279
case manager :
291
280
if spec .LogicalID == nil {
292
281
return spec , errors .New ("Manager nodes require a LogicalID, " +
293
282
"which will be used as an assigned private IP address" )
294
283
}
295
284
296
- spec .Init = generateInitScript (
285
+ initScript , err := generateInitScript (
286
+ s .initScript ,
297
287
self .ManagerStatus .Addr ,
298
288
swarmStatus .JoinTokens .Manager ,
299
289
associationID ,
300
290
properties .DockerRestartCommand )
301
-
291
+ if err != nil {
292
+ return spec , err
293
+ }
294
+ spec .Init = initScript
302
295
default :
303
296
return spec , errors .New ("Unsupported node type" )
304
297
}
0 commit comments