Skip to content

Commit 8091b0a

Browse files
davejrtscotty-c
authored andcommitted
Cloud 1469 (#46)
* adding in tasks for swarm token * adding in tasks and plans for swarm token * adding in tasks for docker swarm * updates for tasks
1 parent 148ce5e commit 8091b0a

9 files changed

+261
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,58 @@ Auto pool extension threshold (in % of pool size).
10851085

10861086
Extends the pool by the specified percentage when the threshold is passed.
10871087

1088+
### Tasks
1089+
1090+
The docker module has an example task that allows a user to initialize, join and leave a swarm.
1091+
1092+
```puppet
1093+
bolt task run docker::swarm_init listen_addr=172.17.10.101 adverstise_addr=172.17.10.101 ---nodes swarm-master --user <user> --password <password> --modulepath <module_path>
1094+
1095+
docker swarm init --advertise-addr=172.17.10.101 --listen-addr=172.17.10.101
1096+
Swarm initialized: current node (w8syk0g286vd7d9kwzt7jl44z) is now a manager.
1097+
1098+
To add a worker to this swarm, run the following command:
1099+
1100+
docker swarm join --token SWMTKN-1-317gw63odq6w1foaw0xkibzqy34lga55aa5nbjlqekcrhg8utl-08vrg0913zken8h9vfo4t6k0t 172.17.10.101:2377
1101+
1102+
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
1103+
1104+
1105+
Ran on 1 node in 4.04 seconds
1106+
```
1107+
1108+
```puppet
1109+
bolt task run docker::swarm_token node_role=worker ---nodes swarm-master --user <user> --password <password> --modulepath <module_path>
1110+
1111+
SWMTKN-1-317gw63odq6w1foaw0xkibzqy34lga55aa5nbjlqekcrhg8utl-08vrg0913zken8h9vfo4t6k0t
1112+
1113+
1114+
Ran on 1 node in 4.02 seconds
1115+
1116+
```
1117+
```puppet
1118+
bolt task run docker::swarm_join listen_addr=172.17.10.102 adverstise_addr=172.17.10.102 token=<swarm_token> manager_ip=172.17.10.101:2377 --nodes swarm-02 --user root --password puppet --modulepath /tmp/modules
1119+
1120+
1121+
This node joined a swarm as a worker.
1122+
1123+
1124+
Ran on 1 node in 4.68 seconds
1125+
```
1126+
```puppet
1127+
bolt task run docker::swarm_leave --nodes swarm-02 --user root --password puppet --modulepath --modulepath <module_path>
1128+
1129+
Node left the swarm.
1130+
1131+
1132+
Ran on 1 node in 6.16 seconds
1133+
```
1134+
1135+
1136+
1137+
1138+
For further explanation please refer to the[PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task.
1139+
10881140
## Limitations
10891141

10901142
This module supports:

tasks/swarm_init.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{
3+
"description": "Initializes a swarm",
4+
"input_method": "stdin",
5+
"parameters": {
6+
"adveritse_addr": {
7+
"description": "Advertised address",
8+
"type": "Optional[String[1]]"
9+
},
10+
"autolock": {
11+
"description": "Enable manager autolocking",
12+
"type": "Optional[Boolean[1]]"
13+
},
14+
"cert_expiry": {
15+
"description": "Validity period for node certificates",
16+
"type": "Optional[String[1]]"
17+
},
18+
"dispatcher_heartbeat": {
19+
"description": "Dispatcher heartbeat period",
20+
"type": "Optional[String[1]]"
21+
},
22+
"external_ca": {
23+
"description": "Specifications of one or more certificate signing endpoints",
24+
"type": "Optional[String[1]]"
25+
},
26+
"force_new_cluster": {
27+
"description": "Force create a new cluster from current state",
28+
"type": "Optional[Boolean[1]]"
29+
},
30+
"listen_addr": {
31+
"description": "Listen address",
32+
"type": "Optional[String[1]]"
33+
},
34+
"max_snapshots": {
35+
"description": "Number of additional Raft snapshots to retain",
36+
"type": "Optional[Integer[1]]"
37+
},
38+
"snapshot_interval": {
39+
"description": "Number of log entries between Raft snapshots",
40+
"type": "Optional[Integer[1]]"
41+
}
42+
}
43+
}

tasks/swarm_init.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def swarm_init(advertise_addr, autolock, cert_expiry, dispatcher_heartbeat, external_ca, force_new_cluster, listen_addr, max_snapshots, snapshot_interval)
7+
cmd_string = "docker swarm init"
8+
cmd_string << " --advertise-addr=#{advertise_addr}" unless advertise_addr.nil?
9+
cmd_string << " --autolock" unless autolock.nil?
10+
cmd_string << " --cert-expiry" unless cert_expiry.nil?
11+
cmd_string << " --dispatcher-heartbeat=#{dispatcher_heartbeat}" unless dispatcher_heartbeat.nil?
12+
cmd_string << " --external-ca=#{external_ca}" unless external_ca.nil?
13+
cmd_string << " --force-new-cluster" unless force_new_cluster.nil?
14+
cmd_string << " --listen-addr=#{listen_addr}" unless listen_addr.nil?
15+
cmd_string << " --max-snapshots=#{max_snapshots}" unless max_snapshots.nil?
16+
cmd_string << " --snapshot-interval=#{snapshot_interval}" unless snapshot_interval.nil?
17+
18+
stdout, stderr, status = Open3.capture3(cmd_string)
19+
raise Puppet::Error, ("stderr: '#{stderr}'") if status != 0
20+
stdout.strip
21+
end
22+
23+
params = JSON.parse(STDIN.read)
24+
advertise_addr = params['advertise_addr']
25+
autolock = params['autolock']
26+
cert_expiry = params['cert_expiry']
27+
dispatcher_heartbeat = params['dispatcher_heartbeat']
28+
external_ca = params['external_ca']
29+
force_new_cluster = params['force_new_cluster']
30+
listen_addr = params['listen_addr']
31+
max_snapshots = params['max_snapshots']
32+
snapshot_interval = params['snapshot_interval']
33+
34+
begin
35+
result = swarm_init(advertise_addr, autolock, cert_expiry, dispatcher_heartbeat, external_ca, force_new_cluster, listen_addr, max_snapshots, snapshot_interval)
36+
puts result
37+
exit 0
38+
rescue Puppet::Error => e
39+
puts({ status: 'failure', error: e.message })
40+
exit 1
41+
end

tasks/swarm_join.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{
3+
"description": "Join a swarm",
4+
"input_method": "stdin",
5+
"parameters": {
6+
"adveritse_addr": {
7+
"description": "Advertised address",
8+
"type": "Optional[String[1]]"
9+
},
10+
"listen_addr": {
11+
"description": "Listen address",
12+
"type": "Optional[String[1]]"
13+
},
14+
"token": {
15+
"description": "Join token for the swarm",
16+
"type": "String[1]"
17+
},
18+
"manager_ip": {
19+
"description": "IP Address of the swarm manager",
20+
"type": "String[1]"
21+
}
22+
}
23+
}

tasks/swarm_join.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def swarm_join(advertise_addr, listen_addr, token, manager_ip)
7+
cmd_string = "docker swarm join"
8+
cmd_string << " --advertise-addr=#{advertise_addr}" unless advertise_addr.nil?
9+
cmd_string << " --listen-addr=#{listen_addr}" unless listen_addr.nil?
10+
cmd_string << " --token=#{token}" unless token.nil?
11+
cmd_string << " #{manager_ip}" unless manager_ip.nil?
12+
13+
14+
stdout, stderr, status = Open3.capture3(cmd_string)
15+
raise Puppet::Error, ("stderr: '#{stderr}'") if status != 0
16+
stdout.strip
17+
end
18+
19+
params = JSON.parse(STDIN.read)
20+
advertise_addr = params['advertise_addr']
21+
listen_addr = params['listen_addr']
22+
token = params['token']
23+
manager_ip = params['manager_ip']
24+
25+
begin
26+
result = swarm_join(advertise_addr, listen_addr, token, manager_ip)
27+
puts result
28+
exit 0
29+
rescue Puppet::Error => e
30+
puts({ status: 'failure', error: e.message })
31+
exit 1
32+
end

tasks/swarm_leave.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
{
3+
"description": "Leave a swarm",
4+
"input_method": "stdin",
5+
"parameters": {
6+
"force": {
7+
"description": "Force this node to leave the swarm, ignoring warnings",
8+
"type": "Optional[Boolean[1]]"
9+
}
10+
}
11+
}

tasks/swarm_leave.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def swarm_leave(force)
7+
cmd_string = "docker swarm leave "
8+
cmd_string << " -f" if force == "true"
9+
stdout, stderr, status = Open3.capture3(cmd_string)
10+
raise Puppet::Error, ("stderr: '#{stderr}'") if status != 0
11+
stdout.strip
12+
end
13+
14+
params = JSON.parse(STDIN.read)
15+
force = params['force']
16+
begin
17+
result = swarm_leave(force)
18+
puts result
19+
exit 0
20+
rescue Puppet::Error => e
21+
puts({ status: 'failure', error: e.message })
22+
exit 1
23+
end

tasks/swarm_token.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
{
3+
"description": "Gets the swarm token from the master",
4+
"input_method": "stdin",
5+
"parameters": {
6+
"node_role": {
7+
"description": "The role of the node joining the swarm",
8+
"type": "String[1]"
9+
}
10+
}
11+
}

tasks/swarm_token.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def swarm_token(node_role)
7+
cmd_string = "docker swarm join-token -q"
8+
cmd_string << " #{node_role}" unless node_role.nil?
9+
10+
stdout, stderr, status = Open3.capture3(cmd_string)
11+
raise Puppet::Error, ("stderr: '#{stderr}'") if status != 0
12+
stdout.strip
13+
end
14+
15+
params = JSON.parse(STDIN.read)
16+
node_role = params['node_role']
17+
18+
begin
19+
result = swarm_token(node_role)
20+
puts result
21+
exit 0
22+
rescue Puppet::Error => e
23+
puts({ status: 'failure', error: e.message })
24+
exit 1
25+
end

0 commit comments

Comments
 (0)