|
4 | 4 | require 'json' |
5 | 5 | require 'open3' |
6 | 6 | require 'puppet' |
| 7 | +require 'shellwords' |
7 | 8 |
|
8 | 9 | def service_create(image, replicas, expose, env, command, extra_params, service, detach) |
9 | | - cmd_string = ['docker', 'service', 'create'] |
10 | | - if extra_params.is_a? Array |
11 | | - extra_params.each do |param| |
12 | | - cmd_string += [" #{param}"] |
13 | | - end |
14 | | - end |
15 | | - cmd_string += [" --name #{service}"] unless service.nil? |
16 | | - cmd_string += [" --replicas #{replicas}"] unless replicas.nil? |
17 | | - cmd_string += [" --publish #{expose}"] unless expose.nil? |
| 10 | + cmd = ['docker', 'service', 'create'] |
| 11 | + cmd.concat(extra_params) unless extra_params.nil? || extra_params.empty? |
| 12 | + |
| 13 | + cmd.concat(['--name', service]) unless service.nil? |
| 14 | + cmd.concat(['--replicas', replicas.to_s]) unless replicas.nil? |
| 15 | + cmd.concat(['--publish', Shellwords.join(expose)]) unless expose.nil? |
| 16 | + |
18 | 17 | if env.is_a? Hash |
19 | 18 | env.each do |key, value| |
20 | | - cmd_string += [" --env #{key}='#{value}'"] |
| 19 | + cmd.concat(['--env', Shellwords.escape("#{key}=#{value}")]) |
21 | 20 | end |
22 | 21 | end |
23 | 22 |
|
24 | | - if command.is_a? Array |
25 | | - cmd_string += command.join(' ') |
26 | | - elsif command && command.to_s != 'undef' |
27 | | - cmd_string += command.to_s |
28 | | - end |
29 | | - |
30 | | - cmd_string += [' -d'] unless detach.nil? |
31 | | - cmd_string += [" #{image}"] unless image.nil? |
| 23 | + cmd.append(image) unless image.nil? |
| 24 | + cmd.append('-d') unless !detach || detach.nil? |
| 25 | + cmd.concat(command) unless command.nil? || command.empty? |
32 | 26 |
|
33 | | - stdout, stderr, status = Open3.capture3(cmd_string) |
| 27 | + stdout, stderr, status = Open3.capture3(*cmd) |
34 | 28 | raise Puppet::Error, "stderr: '#{stderr}'" if status != 0 |
35 | 29 | stdout.strip |
36 | 30 | end |
|
0 commit comments