55 "encoding/json"
66 "errors"
77 "fmt"
8+
89 log "github.com/Sirupsen/logrus"
910 docker_types "github.com/docker/docker/api/types"
1011 "github.com/docker/docker/api/types/filters"
@@ -13,8 +14,8 @@ import (
1314 "github.com/docker/infrakit/pkg/plugin/group/util"
1415 "github.com/docker/infrakit/pkg/spi/flavor"
1516 "github.com/docker/infrakit/pkg/spi/instance"
17+ "github.com/docker/infrakit/pkg/template"
1618 "golang.org/x/net/context"
17- "text/template"
1819)
1920
2021type nodeType string
@@ -26,12 +27,13 @@ const (
2627)
2728
2829// 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 }
3132}
3233
3334type swarmFlavor struct {
34- client client.APIClient
35+ client client.APIClient
36+ initScript * template.Template
3537}
3638
3739type schema struct {
@@ -135,39 +137,20 @@ func (s swarmFlavor) Validate(flavorProperties json.RawMessage, allocation types
135137const (
136138 // associationTag is a machine tag added to associate machines with Swarm nodes.
137139 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- `
156140)
157141
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
161144 err := templ .Execute (& buffer , map [string ]string {
162145 "MY_IP" : joinIP ,
163146 "JOIN_TOKEN" : joinToken ,
164147 "ASSOCIATION_ID" : associationID ,
165148 "RESTART_DOCKER" : restartCommand ,
166149 })
167150 if err != nil {
168- panic ( err )
151+ return "" , err
169152 }
170- return buffer .String ()
153+ return buffer .String (), nil
171154}
172155
173156// 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
246229 }
247230}
248231
249- func (s swarmFlavor ) Prepare (
232+ func (s * swarmFlavor ) Prepare (
250233 flavorProperties json.RawMessage ,
251234 spec instance.Spec ,
252235 allocation types.AllocationMethod ) (instance.Spec , error ) {
@@ -281,24 +264,34 @@ func (s swarmFlavor) Prepare(
281264
282265 switch properties .Type {
283266 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
289278
290279 case manager :
291280 if spec .LogicalID == nil {
292281 return spec , errors .New ("Manager nodes require a LogicalID, " +
293282 "which will be used as an assigned private IP address" )
294283 }
295284
296- spec .Init = generateInitScript (
285+ initScript , err := generateInitScript (
286+ s .initScript ,
297287 self .ManagerStatus .Addr ,
298288 swarmStatus .JoinTokens .Manager ,
299289 associationID ,
300290 properties .DockerRestartCommand )
301-
291+ if err != nil {
292+ return spec , err
293+ }
294+ spec .Init = initScript
302295 default :
303296 return spec , errors .New ("Unsupported node type" )
304297 }
0 commit comments