Skip to content

Commit

Permalink
Merge pull request #509 from khaefeli/tasks
Browse files Browse the repository at this point in the history
Add new Docker Swarm Tasks (node ls, rm, update; service scale)
  • Loading branch information
sheenaajay authored Aug 7, 2019
2 parents 03f5178 + bf8dc5c commit 869748b
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tasks/node_ls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{
"description": "List nodes in the swarm",
"input_method": "stdin",
"parameters": {
"filter": {
"description": "Filter output based on conditions provided",
"type": "Optional[String[1]]"
},
"quiet": {
"description": "Only display IDs",
"type": "Optional[Boolean]"
}
}
}
29 changes: 29 additions & 0 deletions tasks/node_ls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

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

def node_ls(filter, quiet)
cmd_string = 'docker node ls'
cmd_string += " --filter=#{filter}" unless filter.nil?
cmd_string += ' --quiet' unless quiet.nil?

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

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

begin
result = node_ls(filter, quiet)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
15 changes: 15 additions & 0 deletions tasks/node_rm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{
"description": "Update a node",
"input_method": "stdin",
"parameters": {
"force": {
"description": "Force remove a node from the swarm",
"type": "Optional[Boolean]"
},
"node": {
"description": "Hostname or ID of the node in the swarm",
"type": "String[1]"
}
}
}
29 changes: 29 additions & 0 deletions tasks/node_rm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

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

def node_rm(force, node)
cmd_string = 'docker node rm'
cmd_string += ' --force' unless force.nil?
cmd_string += " #{node}" unless node.nil?

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

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

begin
result = node_rm(force, node)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
19 changes: 19 additions & 0 deletions tasks/node_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

{
"description": "Update a node",
"input_method": "stdin",
"parameters": {
"availability": {
"description": "Availability of the node",
"type": "Optional[Enum['active', 'pause', 'drain']]"
},
"role": {
"description": "Role of the node",
"type": "Optional[Enum['manager', 'worker']]"
},
"node": {
"description": "Hostname or ID of the node in the swarm",
"type": "String[1]"
}
}
}
31 changes: 31 additions & 0 deletions tasks/node_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

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

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

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

params = JSON.parse(STDIN.read)
availability = params['availability']
role = params['role']
node = params['node']

begin
result = node_update(availability, role, node)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end
19 changes: 19 additions & 0 deletions tasks/service_scale.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

{
"description": "Scale one replicated service",
"input_method": "stdin",
"parameters": {
"service": {
"description": "Name or ID of the service",
"type": "String[1]"
},
"scale": {
"description": "Number of replicas",
"type": "Integer"
},
"detatch": {
"description": "Exit immediately instead of waiting for the service to converge",
"type": "Optional[Boolean]"
}
}
}
31 changes: 31 additions & 0 deletions tasks/service_scale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

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

def service_scale(service, scale, detach)
cmd_string = 'docker service scale'
cmd_string += " #{service}" unless service.nil?
cmd_string += "=#{scale}" unless scale.nil?
cmd_string += ' -d' unless detach.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']
scale = params['scale']
detach = params['detach']

begin
result = service_scale(service, scale, detach)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

0 comments on commit 869748b

Please sign in to comment.