Skip to content

Commit

Permalink
Updated: Add Docker service (create, remote, scale) tasks (puppetlabs…
Browse files Browse the repository at this point in the history
…#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 <khaefeli@users.noreply.github.com>
Co-authored-by: Kevin Häfeli <kevin@helio.exchange>
Co-authored-by: David Schmitt <david.schmitt@puppet.com>
Co-authored-by: Michael Weibel <michael@helio.exchange>
  • Loading branch information
5 people authored Apr 6, 2020
1 parent d498423 commit 7620f86
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 28 deletions.
4 changes: 3 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
* [`node_ls`](#node_ls): List nodes in the swarm
* [`node_rm`](#node_rm): Update a node
* [`node_update`](#node_update): Update a node
* [`service_create`](#service_create): Create one service
* [`service_rm`](#service_rm): Removes an existing service.
* [`service_scale`](#service_scale): Scale one replicated service
* [`service_update`](#service_update): Updates an existing service.
* [`swarm_init`](#swarm_init): Initializes a swarm
* [`swarm_join`](#swarm_join): Join a swarm
* [`swarm_leave`](#swarm_leave): Leave a swarm
* [`swarm_token`](#swarm_token): Gets the swarm token from the master
* [`swarm_update`](#swarm_update): Updates an existing service.

## Classes

Expand Down
1 change: 0 additions & 1 deletion tasks/node_ls.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "List nodes in the swarm",
"input_method": "stdin",
Expand Down
1 change: 0 additions & 1 deletion tasks/node_rm.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Update a node",
"input_method": "stdin",
Expand Down
12 changes: 10 additions & 2 deletions tasks/node_update.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Update a node",
"input_method": "stdin",
Expand All @@ -11,9 +10,18 @@
"description": "Role of the node",
"type": "Optional[Enum['manager', 'worker']]"
},
"label_add": {
"description": "Add or update a node label (key=value)",
"type": "Optional[Array]"
},
"label_rm": {
"description": "Remove a node label if exists.",
"type": "Optional[Array]"
},
"node": {
"description": "Hostname or ID of the node in the swarm",
"description": "ID of the node in the swarm",
"type": "String[1]"
}
}
}

19 changes: 17 additions & 2 deletions tasks/node_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
require 'open3'
require 'puppet'

def node_update(availability, role, node)
def node_update(availability, role, label_add, label_rm, node)
cmd_string = 'docker node update'
cmd_string += " --availability #{availability}" unless availability.nil?
cmd_string += " --role #{role}" unless role.nil?

if label_add.is_a? Array
label_add.each do |param|
cmd_string += " --label-add #{param}"
end
end

if label_rm.is_a? Array
label_rm.each do |param|
cmd_string += " --label-rm #{param}"
end
end

cmd_string += " #{node}" unless node.nil?

stdout, stderr, status = Open3.capture3(cmd_string)
Expand All @@ -19,10 +32,12 @@ def node_update(availability, role, node)
params = JSON.parse(STDIN.read)
availability = params['availability']
role = params['role']
label_add = params['label_add']
label_rm = params['label_rm']
node = params['node']

begin
result = node_update(availability, role, node)
result = node_update(availability, role, label_add, label_rm, node)
puts result
exit 0
rescue Puppet::Error => e
Expand Down
38 changes: 38 additions & 0 deletions tasks/service_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"description": "Create a new Docker service",
"input_method": "stdin",
"parameters": {
"service": {
"description": "The name of the service to create",
"type": "String[1]"
},
"image": {
"description": "The new image to use for the service",
"type": "String[1]"
},
"replicas": {
"description": "Number of replicas",
"type": "Integer"
},
"expose": {
"description": "Publish service ports externally to the swarm",
"type": "Variant[String,Array,Undef]"
},
"env": {
"description": "Set environment variables",
"type": "Optional[Hash]"
},
"command": {
"description": "Command to run on the container",
"type": "Variant[String,Array,Undef]"
},
"extra_params": {
"description": "Allows you to pass any other flag that the Docker service create supports.",
"type": "Optional[Array]"
},
"detach": {
"description": "Exit immediately instead of waiting for the service to converge",
"type": "Optional[Boolean]"
}
}
}
55 changes: 55 additions & 0 deletions tasks/service_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'
require 'open3'
require 'puppet'

def service_create(image, replicas, expose, env, command, extra_params, service, detach)
cmd_string = 'docker service create'
if extra_params.is_a? Array
extra_params.each do |param|
cmd_string += " #{param}"
end
end
cmd_string += " --name #{service}" unless service.nil?
cmd_string += " --replicas #{replicas}" unless replicas.nil?
cmd_string += " --publish #{expose}" unless expose.nil?
if env.is_a? Hash
env.each do |key, value|
cmd_string += " --env #{key}='#{value}'"
end
end

if command.is_a? Array
cmd_string += command.join(' ')
elsif command && command.to_s != 'undef'
cmd_string += command.to_s
end

cmd_string += ' -d' unless detach.nil?
cmd_string += " #{image}" unless image.nil?

stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end

params = JSON.parse(STDIN.read)
image = params['image']
replicas = params['replicas']
expose = params['expose']
env = params['env']
command = params['command']
extra_params = params['extra_params']
service = params['service']
detach = params['detach']

begin
result = service_create(image, replicas, expose, env, command, extra_params, service, detach)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
10 changes: 10 additions & 0 deletions tasks/service_rm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"description": "Remove one replicated service",
"input_method": "stdin",
"parameters": {
"service": {
"description": "Name or ID of the service",
"type": "String[1]"
}
}
}
27 changes: 27 additions & 0 deletions tasks/service_rm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'
require 'open3'
require 'puppet'

def service_rm(service)
cmd_string = 'docker service rm'
cmd_string += " #{service}" unless service.nil?

stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end

params = JSON.parse(STDIN.read)
service = params['service']

begin
result = service_rm(service)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
3 changes: 1 addition & 2 deletions tasks/service_scale.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Scale one replicated service",
"input_method": "stdin",
Expand All @@ -11,7 +10,7 @@
"description": "Number of replicas",
"type": "Integer"
},
"detatch": {
"detach": {
"description": "Exit immediately instead of waiting for the service to converge",
"type": "Optional[Boolean]"
}
Expand Down
22 changes: 22 additions & 0 deletions tasks/service_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"description": "Updates an existing service.",
"input_method": "stdin",
"parameters": {
"service": {
"description": "The service to update",
"type": "String[1]"
},
"image": {
"description": "The new image to use for the service",
"type": "String[1]"
},
"constraint_add": {
"description": "Add or update a service constraint (selector==value, selector!=value)",
"type": "Optional[Array]"
},
"constraint_rm": {
"description": "Remove a service constraint if exists.",
"type": "Optional[Array]"
}
}
}
44 changes: 44 additions & 0 deletions tasks/service_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'
require 'open3'
require 'puppet'

def service_update(image, service, constraint_add, constraint_rm)
cmd_string = 'docker service update'
cmd_string += " --image #{image}" unless image.nil?

if constraint_add.is_a? Array
constraint_add.each do |param|
cmd_string += " --constraint-add #{param}"
end
end

if constraint_rm.is_a? Array
constraint_rm.each do |param|
cmd_string += " --constraint-rm #{param}"
end
end

cmd_string += " #{service}" unless service.nil?

stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end

params = JSON.parse(STDIN.read)
image = params['image']
service = params['service']
constraint_add = params['constraint_add']
constraint_rm = params['constraint_rm']

begin
result = service_update(image, service, constraint_add, constraint_rm)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
1 change: 0 additions & 1 deletion tasks/swarm_init.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Initializes a swarm",
"input_method": "stdin",
Expand Down
1 change: 0 additions & 1 deletion tasks/swarm_join.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Join a swarm",
"input_method": "stdin",
Expand Down
1 change: 0 additions & 1 deletion tasks/swarm_leave.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Leave a swarm",
"input_method": "stdin",
Expand Down
1 change: 0 additions & 1 deletion tasks/swarm_token.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"description": "Gets the swarm token from the master",
"input_method": "stdin",
Expand Down
9 changes: 5 additions & 4 deletions tasks/swarm_update.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"description": "The service to update",
"type": "String[1]"
},
"image": {
"description": "The new image to use for the service",
"type": "String[1]"
}
"image": {
"description": "The new image to use for the service",
"type": "String[1]"
}
}
}

13 changes: 2 additions & 11 deletions tasks/swarm_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
require 'open3'
require 'puppet'

def swarm_update(image, service)
cmd_string = 'docker service update'
cmd_string += " --image #{image}" unless image.nil?
cmd_string += " #{service}" unless service.nil?

stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end

params = JSON.parse(STDIN.read)
image = params['image']
service = params['service']

begin
result = swarm_update(image, service)
puts 'Deprecated: use docker::service_update instead'
result = service_update(image, service)
puts result
exit 0
rescue Puppet::Error => e
Expand Down

0 comments on commit 7620f86

Please sign in to comment.