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
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/terrafying/components/ignition.rb', line 13
def self.container_unit(name, image, options={})
options = {
volumes: [],
environment_variables: [],
arguments: [],
require_units: [],
host_networking: false,
privileged: false,
}.merge(options)
if options[:require_units].count > 0
require_units = options[:require_units].join(" ")
require = "After=\#{require_units}\nRequires=\#{require_units}\n"
end
docker_options = []
if options[:environment_variables].count > 0
docker_options += options[:environment_variables].map { |var|
"-e #{var}"
}
end
if options[:volumes].count > 0
docker_options += options[:volumes].map { |volume|
"-v #{volume}"
}
end
if options[:host_networking]
docker_options << "--net=host"
end
if options[:privileged]
docker_options << "--privileged"
end
docker_options_str = " \\\n" + docker_options.join(" \\\n")
if options[:arguments].count > 0
arguments = " \\\n" + options[:arguments].join(" \\\n")
end
{
name: "#{name}.service",
contents: "[Install]\nWantedBy=multi-user.target\n\n[Unit]\nDescription=\#{name}\n\#{require}\n\n[Service]\nExecStartPre=-/usr/bin/docker rm -f \#{name}\nExecStart=/usr/bin/docker run --name \#{name} \#{docker_options_str} \\\n\#{image} \#{arguments}\nRestart=always\nRestartSec=30\n\n"
}
end
|