Skip to content

Commit 7620f86

Browse files
FlaskkhaefeliDavidSmweibel
authored
Updated: Add Docker service (create, remote, scale) tasks (#582)
* 🚚 Use consistent naming for service commands * 📝 Updating documentation * 🐳 Add service rm task * 📝 Add reference to service_rm * 🐛 Fix missing specifications * ✨ Add Docker Service create to Puppet Tasks * 🐛 Fix parameter name typoe * 🐛 Fix wrong function names * 🐛 Use nodeid parameter instead of node * 🐛 Fix json syntax * 🐛 Fix wrong data types and iterations * 🐛 Fix file permissions * 📝 Update reference with new tasks * Apply rubocop autocorrections * fixes and extens docker node update to add/remove labels0 * fix: lint errors * add/remove service constraints Co-authored-by: Kevin Häfeli <[email protected]> Co-authored-by: Kevin Häfeli <[email protected]> Co-authored-by: David Schmitt <[email protected]> Co-authored-by: Michael Weibel <[email protected]>
1 parent d498423 commit 7620f86

18 files changed

+234
-28
lines changed

REFERENCE.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@
6262
* [`node_ls`](#node_ls): List nodes in the swarm
6363
* [`node_rm`](#node_rm): Update a node
6464
* [`node_update`](#node_update): Update a node
65+
* [`service_create`](#service_create): Create one service
66+
* [`service_rm`](#service_rm): Removes an existing service.
6567
* [`service_scale`](#service_scale): Scale one replicated service
68+
* [`service_update`](#service_update): Updates an existing service.
6669
* [`swarm_init`](#swarm_init): Initializes a swarm
6770
* [`swarm_join`](#swarm_join): Join a swarm
6871
* [`swarm_leave`](#swarm_leave): Leave a swarm
6972
* [`swarm_token`](#swarm_token): Gets the swarm token from the master
70-
* [`swarm_update`](#swarm_update): Updates an existing service.
7173

7274
## Classes
7375

tasks/node_ls.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "List nodes in the swarm",
43
"input_method": "stdin",

tasks/node_rm.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Update a node",
43
"input_method": "stdin",

tasks/node_update.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Update a node",
43
"input_method": "stdin",
@@ -11,9 +10,18 @@
1110
"description": "Role of the node",
1211
"type": "Optional[Enum['manager', 'worker']]"
1312
},
13+
"label_add": {
14+
"description": "Add or update a node label (key=value)",
15+
"type": "Optional[Array]"
16+
},
17+
"label_rm": {
18+
"description": "Remove a node label if exists.",
19+
"type": "Optional[Array]"
20+
},
1421
"node": {
15-
"description": "Hostname or ID of the node in the swarm",
22+
"description": "ID of the node in the swarm",
1623
"type": "String[1]"
1724
}
1825
}
1926
}
27+

tasks/node_update.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@
55
require 'open3'
66
require 'puppet'
77

8-
def node_update(availability, role, node)
8+
def node_update(availability, role, label_add, label_rm, node)
99
cmd_string = 'docker node update'
1010
cmd_string += " --availability #{availability}" unless availability.nil?
1111
cmd_string += " --role #{role}" unless role.nil?
12+
13+
if label_add.is_a? Array
14+
label_add.each do |param|
15+
cmd_string += " --label-add #{param}"
16+
end
17+
end
18+
19+
if label_rm.is_a? Array
20+
label_rm.each do |param|
21+
cmd_string += " --label-rm #{param}"
22+
end
23+
end
24+
1225
cmd_string += " #{node}" unless node.nil?
1326

1427
stdout, stderr, status = Open3.capture3(cmd_string)
@@ -19,10 +32,12 @@ def node_update(availability, role, node)
1932
params = JSON.parse(STDIN.read)
2033
availability = params['availability']
2134
role = params['role']
35+
label_add = params['label_add']
36+
label_rm = params['label_rm']
2237
node = params['node']
2338

2439
begin
25-
result = node_update(availability, role, node)
40+
result = node_update(availability, role, label_add, label_rm, node)
2641
puts result
2742
exit 0
2843
rescue Puppet::Error => e

tasks/service_create.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"description": "Create a new Docker service",
3+
"input_method": "stdin",
4+
"parameters": {
5+
"service": {
6+
"description": "The name of the service to create",
7+
"type": "String[1]"
8+
},
9+
"image": {
10+
"description": "The new image to use for the service",
11+
"type": "String[1]"
12+
},
13+
"replicas": {
14+
"description": "Number of replicas",
15+
"type": "Integer"
16+
},
17+
"expose": {
18+
"description": "Publish service ports externally to the swarm",
19+
"type": "Variant[String,Array,Undef]"
20+
},
21+
"env": {
22+
"description": "Set environment variables",
23+
"type": "Optional[Hash]"
24+
},
25+
"command": {
26+
"description": "Command to run on the container",
27+
"type": "Variant[String,Array,Undef]"
28+
},
29+
"extra_params": {
30+
"description": "Allows you to pass any other flag that the Docker service create supports.",
31+
"type": "Optional[Array]"
32+
},
33+
"detach": {
34+
"description": "Exit immediately instead of waiting for the service to converge",
35+
"type": "Optional[Boolean]"
36+
}
37+
}
38+
}

tasks/service_create.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'open3'
6+
require 'puppet'
7+
8+
def service_create(image, replicas, expose, env, command, extra_params, service, detach)
9+
cmd_string = 'docker service create'
10+
if extra_params.is_a? Array
11+
extra_params.each do |param|
12+
cmd_string += " #{param}"
13+
end
14+
end
15+
cmd_string += " --name #{service}" unless service.nil?
16+
cmd_string += " --replicas #{replicas}" unless replicas.nil?
17+
cmd_string += " --publish #{expose}" unless expose.nil?
18+
if env.is_a? Hash
19+
env.each do |key, value|
20+
cmd_string += " --env #{key}='#{value}'"
21+
end
22+
end
23+
24+
if command.is_a? Array
25+
cmd_string += command.join(' ')
26+
elsif command && command.to_s != 'undef'
27+
cmd_string += command.to_s
28+
end
29+
30+
cmd_string += ' -d' unless detach.nil?
31+
cmd_string += " #{image}" unless image.nil?
32+
33+
stdout, stderr, status = Open3.capture3(cmd_string)
34+
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
35+
stdout.strip
36+
end
37+
38+
params = JSON.parse(STDIN.read)
39+
image = params['image']
40+
replicas = params['replicas']
41+
expose = params['expose']
42+
env = params['env']
43+
command = params['command']
44+
extra_params = params['extra_params']
45+
service = params['service']
46+
detach = params['detach']
47+
48+
begin
49+
result = service_create(image, replicas, expose, env, command, extra_params, service, detach)
50+
puts result
51+
exit 0
52+
rescue Puppet::Error => e
53+
puts(status: 'failure', error: e.message)
54+
exit 1
55+
end

tasks/service_rm.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"description": "Remove one replicated service",
3+
"input_method": "stdin",
4+
"parameters": {
5+
"service": {
6+
"description": "Name or ID of the service",
7+
"type": "String[1]"
8+
}
9+
}
10+
}

tasks/service_rm.rb

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

tasks/service_scale.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Scale one replicated service",
43
"input_method": "stdin",
@@ -11,7 +10,7 @@
1110
"description": "Number of replicas",
1211
"type": "Integer"
1312
},
14-
"detatch": {
13+
"detach": {
1514
"description": "Exit immediately instead of waiting for the service to converge",
1615
"type": "Optional[Boolean]"
1716
}

tasks/service_update.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"description": "Updates an existing service.",
3+
"input_method": "stdin",
4+
"parameters": {
5+
"service": {
6+
"description": "The service to update",
7+
"type": "String[1]"
8+
},
9+
"image": {
10+
"description": "The new image to use for the service",
11+
"type": "String[1]"
12+
},
13+
"constraint_add": {
14+
"description": "Add or update a service constraint (selector==value, selector!=value)",
15+
"type": "Optional[Array]"
16+
},
17+
"constraint_rm": {
18+
"description": "Remove a service constraint if exists.",
19+
"type": "Optional[Array]"
20+
}
21+
}
22+
}

tasks/service_update.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'open3'
6+
require 'puppet'
7+
8+
def service_update(image, service, constraint_add, constraint_rm)
9+
cmd_string = 'docker service update'
10+
cmd_string += " --image #{image}" unless image.nil?
11+
12+
if constraint_add.is_a? Array
13+
constraint_add.each do |param|
14+
cmd_string += " --constraint-add #{param}"
15+
end
16+
end
17+
18+
if constraint_rm.is_a? Array
19+
constraint_rm.each do |param|
20+
cmd_string += " --constraint-rm #{param}"
21+
end
22+
end
23+
24+
cmd_string += " #{service}" unless service.nil?
25+
26+
stdout, stderr, status = Open3.capture3(cmd_string)
27+
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
28+
stdout.strip
29+
end
30+
31+
params = JSON.parse(STDIN.read)
32+
image = params['image']
33+
service = params['service']
34+
constraint_add = params['constraint_add']
35+
constraint_rm = params['constraint_rm']
36+
37+
begin
38+
result = service_update(image, service, constraint_add, constraint_rm)
39+
puts result
40+
exit 0
41+
rescue Puppet::Error => e
42+
puts(status: 'failure', error: e.message)
43+
exit 1
44+
end

tasks/swarm_init.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Initializes a swarm",
43
"input_method": "stdin",

tasks/swarm_join.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Join a swarm",
43
"input_method": "stdin",

tasks/swarm_leave.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Leave a swarm",
43
"input_method": "stdin",

tasks/swarm_token.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"description": "Gets the swarm token from the master",
43
"input_method": "stdin",

tasks/swarm_update.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"description": "The service to update",
77
"type": "String[1]"
88
},
9-
"image": {
10-
"description": "The new image to use for the service",
11-
"type": "String[1]"
12-
}
9+
"image": {
10+
"description": "The new image to use for the service",
11+
"type": "String[1]"
12+
}
1313
}
1414
}
15+

tasks/swarm_update.rb

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@
55
require 'open3'
66
require 'puppet'
77

8-
def swarm_update(image, service)
9-
cmd_string = 'docker service update'
10-
cmd_string += " --image #{image}" unless image.nil?
11-
cmd_string += " #{service}" unless service.nil?
12-
13-
stdout, stderr, status = Open3.capture3(cmd_string)
14-
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
15-
stdout.strip
16-
end
17-
188
params = JSON.parse(STDIN.read)
199
image = params['image']
2010
service = params['service']
2111

2212
begin
23-
result = swarm_update(image, service)
13+
puts 'Deprecated: use docker::service_update instead'
14+
result = service_update(image, service)
2415
puts result
2516
exit 0
2617
rescue Puppet::Error => e

0 commit comments

Comments
 (0)