forked from puppetlabs/puppetlabs-apache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated: Add Docker service (create, remote, scale) tasks (puppetlabs…
…#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
1 parent
d498423
commit 7620f86
Showing
18 changed files
with
234 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"description": "List nodes in the swarm", | ||
"input_method": "stdin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"description": "Update a node", | ||
"input_method": "stdin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"description": "Initializes a swarm", | ||
"input_method": "stdin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"description": "Join a swarm", | ||
"input_method": "stdin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"description": "Leave a swarm", | ||
"input_method": "stdin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters