]> git.openstreetmap.org Git - chef.git/blob - cookbooks/podman/resources/service.rb
d2ee9070562ad323fefb7ffddb81e8a96dd4bb9a
[chef.git] / cookbooks / podman / resources / service.rb
1 #
2 # Cookbook:: podman
3 # Resource:: podman_service
4 #
5 # Copyright:: 2023, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 unified_mode true
21
22 default_action :create
23
24 property :service, String, :name_property => true
25 property :description, String, :required => true
26 property :image, String, :required => true
27 property :ports, Hash
28 property :environment, Hash, :default => {}
29
30 action :create do
31   systemd_service new_resource.service do
32     description new_resource.description
33     type "notify"
34     notify_access "all"
35     environment "PODMAN_SYSTEMD_UNIT" => "%n"
36     exec_start_pre "/bin/rm --force %t/%n.ctr-id"
37     exec_start "/usr/bin/podman run --cidfile=%t/%n.ctr-id --cgroups=no-conmon --userns=auto --label=io.containers.autoupdate=registry --network=slirp4netns:mtu=1500 #{publish_options} #{environment_options} --rm --sdnotify=conmon --detach --replace --name=%N #{new_resource.image}"
38     exec_stop "/usr/bin/podman stop --ignore --time=10 --cidfile=%t/%n.ctr-id"
39     exec_stop_post "/usr/bin/podman rm --force --ignore --cidfile=%t/%n.ctr-id"
40     timeout_start_sec 180
41     timeout_stop_sec 70
42     restart "on-failure"
43   end
44
45   # No action :start here to avoid a start and then immediate :restart, due to subscribe, on first run
46   # FIXME: Ubuntu 22.04 podman/crun bug workaround "retries"
47   service new_resource.service do
48     action :enable
49     subscribes :restart, "systemd_service[#{new_resource.service}]", :immediately
50     retries 4 # Workaround https://github.com/containers/podman/issues/9752
51     retry_delay 5
52   end
53
54   # Ensure the service is started if not running, replies on status of service resource
55   notify_group new_resource.service do
56     action :run
57     notifies :start, "service[#{new_resource.service}]", :immediately
58   end
59 end
60
61 action :delete do
62   service new_resource.service do
63     action [:disable, :stop]
64   end
65
66   systemd_service new_resource.service do
67     action :delete
68   end
69 end
70
71 action_class do
72   def publish_options
73     new_resource.ports.collect do |host, guest|
74       "--publish=127.0.0.1:#{host}:#{guest}"
75     end.join(" ")
76   end
77
78   def environment_options
79     new_resource.environment.collect do |key, value|
80       "-e '#{key}=#{value}'"
81     end.join(" ")
82   end
83 end