feat: initial commit

This commit is contained in:
amy 2025-04-01 17:40:03 +00:00
commit 38f495e3f4
457 changed files with 40577 additions and 0 deletions

View file

@ -0,0 +1,14 @@
{
"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]"
}
}
}

View file

@ -0,0 +1,30 @@
#!/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

View file

@ -0,0 +1,14 @@
{
"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]"
}
}
}

View file

@ -0,0 +1,30 @@
#!/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

View file

@ -0,0 +1,27 @@
{
"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']]"
},
"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": "ID of the node in the swarm",
"type": "String[1]"
}
}
}

View file

@ -0,0 +1,47 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
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)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end
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, label_add, label_rm, node)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

View file

@ -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]"
}
}
}

View file

@ -0,0 +1,56 @@
#!/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

View file

@ -0,0 +1,10 @@
{
"description": "Remove one replicated service",
"input_method": "stdin",
"parameters": {
"service": {
"description": "Name or ID of the service",
"type": "String[1]"
}
}
}

View file

@ -0,0 +1,28 @@
#!/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

View file

@ -0,0 +1,18 @@
{
"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"
},
"detach": {
"description": "Exit immediately instead of waiting for the service to converge",
"type": "Optional[Boolean]"
}
}
}

View file

@ -0,0 +1,32 @@
#!/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

View file

@ -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]"
}
}
}

View file

@ -0,0 +1,45 @@
#!/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

View file

@ -0,0 +1,42 @@
{
"description": "Initializes a swarm",
"input_method": "stdin",
"parameters": {
"advertise_addr": {
"description": "Advertised address",
"type": "Optional[String[1]]"
},
"autolock": {
"description": "Enable manager autolocking",
"type": "Optional[Boolean]"
},
"cert_expiry": {
"description": "Validity period for node certificates",
"type": "Optional[String[1]]"
},
"dispatcher_heartbeat": {
"description": "Dispatcher heartbeat period",
"type": "Optional[String[1]]"
},
"external_ca": {
"description": "Specifications of one or more certificate signing endpoints",
"type": "Optional[String[1]]"
},
"force_new_cluster": {
"description": "Force create a new cluster from current state",
"type": "Optional[Boolean]"
},
"listen_addr": {
"description": "Listen address",
"type": "Optional[String[1]]"
},
"max_snapshots": {
"description": "Number of additional Raft snapshots to retain",
"type": "Optional[Integer[1]]"
},
"snapshot_interval": {
"description": "Number of log entries between Raft snapshots",
"type": "Optional[Integer[1]]"
}
}
}

View file

@ -0,0 +1,44 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
def swarm_init(advertise_addr, autolock, cert_expiry, dispatcher_heartbeat, external_ca, force_new_cluster, listen_addr, max_snapshots, snapshot_interval)
cmd_string = 'docker swarm init'
cmd_string += " --advertise-addr=#{advertise_addr}" unless advertise_addr.nil?
cmd_string += ' --autolock' unless autolock.nil?
cmd_string += ' --cert-expiry' unless cert_expiry.nil?
cmd_string += " --dispatcher-heartbeat=#{dispatcher_heartbeat}" unless dispatcher_heartbeat.nil?
cmd_string += " --external-ca=#{external_ca}" unless external_ca.nil?
cmd_string += ' --force-new-cluster' unless force_new_cluster.nil?
cmd_string += " --listen-addr=#{listen_addr}" unless listen_addr.nil?
cmd_string += " --max-snapshots=#{max_snapshots}" unless max_snapshots.nil?
cmd_string += " --snapshot-interval=#{snapshot_interval}" unless snapshot_interval.nil?
stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end
params = JSON.parse($stdin.read)
advertise_addr = params['advertise_addr']
autolock = params['autolock']
cert_expiry = params['cert_expiry']
dispatcher_heartbeat = params['dispatcher_heartbeat']
external_ca = params['external_ca']
force_new_cluster = params['force_new_cluster']
listen_addr = params['listen_addr']
max_snapshots = params['max_snapshots']
snapshot_interval = params['snapshot_interval']
begin
result = swarm_init(advertise_addr, autolock, cert_expiry, dispatcher_heartbeat, external_ca, force_new_cluster, listen_addr, max_snapshots, snapshot_interval)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

View file

@ -0,0 +1,22 @@
{
"description": "Join a swarm",
"input_method": "stdin",
"parameters": {
"advertise_addr": {
"description": "Advertised address",
"type": "Optional[String[1]]"
},
"listen_addr": {
"description": "Listen address",
"type": "Optional[String[1]]"
},
"token": {
"description": "Join token for the swarm",
"type": "String[1]"
},
"manager_ip": {
"description": "IP Address of the swarm manager",
"type": "String[1]"
}
}
}

View file

@ -0,0 +1,34 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
def swarm_join(advertise_addr, listen_addr, token, manager_ip)
cmd_string = 'docker swarm join'
cmd_string += " --advertise-addr=#{advertise_addr}" unless advertise_addr.nil?
cmd_string += " --listen-addr=#{listen_addr}" unless listen_addr.nil?
cmd_string += " --token=#{token}" unless token.nil?
cmd_string += " #{manager_ip}" unless manager_ip.nil?
stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end
params = JSON.parse($stdin.read)
advertise_addr = params['advertise_addr']
listen_addr = params['listen_addr']
token = params['token']
manager_ip = params['manager_ip']
begin
result = swarm_join(advertise_addr, listen_addr, token, manager_ip)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

View file

@ -0,0 +1,10 @@
{
"description": "Leave a swarm",
"input_method": "stdin",
"parameters": {
"force": {
"description": "Force this node to leave the swarm, ignoring warnings",
"type": "Optional[Boolean]"
}
}
}

View file

@ -0,0 +1,26 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
def swarm_leave(force)
cmd_string = 'docker swarm leave '
cmd_string += ' -f' if force == 'true'
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']
begin
result = swarm_leave(force)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

View file

@ -0,0 +1,10 @@
{
"description": "Gets the swarm token from the server",
"input_method": "stdin",
"parameters": {
"node_role": {
"description": "The role of the node joining the swarm",
"type": "String[1]"
}
}
}

View file

@ -0,0 +1,28 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
def swarm_token(node_role)
cmd_string = 'docker swarm join-token -q'
cmd_string += " #{node_role}" unless node_role.nil?
stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, "stderr: '#{stderr}'" if status != 0
stdout.strip
end
params = JSON.parse($stdin.read)
node_role = params['node_role']
begin
result = swarm_token(node_role)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end

View file

@ -0,0 +1,15 @@
{
"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]"
}
}
}

View file

@ -0,0 +1,20 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'puppet'
params = JSON.parse($stdin.read)
image = params['image']
service = params['service']
begin
puts 'Deprecated: use docker::service_update instead'
result = service_update(image, service)
puts result
exit 0
rescue Puppet::Error => e
puts(status: 'failure', error: e.message)
exit 1
end