diff --git a/tasks/node_ls.json b/tasks/node_ls.json new file mode 100644 index 00000000..f62075e9 --- /dev/null +++ b/tasks/node_ls.json @@ -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]" + } + } +} diff --git a/tasks/node_ls.rb b/tasks/node_ls.rb new file mode 100755 index 00000000..54294a28 --- /dev/null +++ b/tasks/node_ls.rb @@ -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 diff --git a/tasks/node_rm.json b/tasks/node_rm.json new file mode 100644 index 00000000..10127fc0 --- /dev/null +++ b/tasks/node_rm.json @@ -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]" + } + } +} diff --git a/tasks/node_rm.rb b/tasks/node_rm.rb new file mode 100755 index 00000000..9b92a576 --- /dev/null +++ b/tasks/node_rm.rb @@ -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 diff --git a/tasks/node_update.json b/tasks/node_update.json new file mode 100644 index 00000000..4f5a775f --- /dev/null +++ b/tasks/node_update.json @@ -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]" + } + } +} diff --git a/tasks/node_update.rb b/tasks/node_update.rb new file mode 100755 index 00000000..69340177 --- /dev/null +++ b/tasks/node_update.rb @@ -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 diff --git a/tasks/service_scale.json b/tasks/service_scale.json new file mode 100644 index 00000000..5b0c84c2 --- /dev/null +++ b/tasks/service_scale.json @@ -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]" + } + } +} diff --git a/tasks/service_scale.rb b/tasks/service_scale.rb new file mode 100755 index 00000000..f954316a --- /dev/null +++ b/tasks/service_scale.rb @@ -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