12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/terrafying/components/ignition.rb', line 12
def self.container_unit(name, image, options = {})
options = {
volumes: [],
environment_variables: [],
arguments: [],
ports: [],
require_units: [],
host_networking: false,
privileged: false
}.merge(options)
if options[:require_units].count > 0
require_units = options[:require_units].join(' ')
require = <<~EOF
After=#{require_units}
Requires=#{require_units}
EOF
end
docker_options = []
docker_options += options[:environment_variables].map { |var| "-e #{var}" }
docker_options += options[:volumes].map { |volume| "-v #{volume}" }
docker_options += options[:ports].map { |port| "-p #{port}:#{port}" }
docker_options << '--net=host' if options[:host_networking]
docker_options << '--privileged' if options[:privileged]
docker_options_str = " \\\n" + docker_options.join(" \\\n")
if options[:arguments].count > 0
arguments = " \\\n" + options[:arguments].join(" \\\n")
end
{
name: "#{name}.service",
contents: <<~EOF
[Install]
WantedBy=multi-user.target
[Unit]
Description=#{name}
#{require}
[Service]
ExecStartPre=-/usr/bin/docker rm -f #{name}
ExecStart=/usr/bin/docker run --name #{name} #{docker_options_str} \
#{image} #{arguments}
Restart=always
RestartSec=30
EOF
}
end
|