3 # Resource:: podman_service
5 # Copyright:: 2023, OpenStreetMap Foundation
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
11 # https://www.apache.org/licenses/LICENSE-2.0
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.
22 default_action :create
24 property :service, String, :name_property => true
25 property :description, String, :required => true
26 property :image, String, :required => true
27 property :ports, Hash, :default => {}
28 property :environment, Hash, :default => {}
29 property :volume, Hash, :default => {}
30 property :command, String, :default => ""
33 systemd_service new_resource.service do
34 description new_resource.description
37 environment "PODMAN_SYSTEMD_UNIT" => "%n"
38 exec_start_pre "/bin/rm --force %t/%n.ctr-id"
39 exec_start "/usr/bin/podman run --cidfile=%t/%n.ctr-id --cgroups=no-conmon "\
40 "--userns=auto --label=io.containers.autoupdate=registry "\
41 "--pids-limit=-1 #{publish_options} #{environment_options} "\
42 "#{volume_options} --rm --sdnotify=conmon --detach --replace "\
43 "--name=%N #{new_resource.image} #{new_resource.command}"
44 exec_stop "/usr/bin/podman stop --ignore --time=10 --cidfile=%t/%n.ctr-id"
45 exec_stop_post "/usr/bin/podman rm --force --ignore --cidfile=%t/%n.ctr-id"
51 # No action :start here to avoid a start and then immediate :restart, due to subscribe, on first run
52 # FIXME: Ubuntu 22.04 podman/crun bug workaround "retries"
53 service new_resource.service do
55 subscribes :restart, "systemd_service[#{new_resource.service}]", :immediately
56 retries 4 # Workaround https://github.com/containers/podman/issues/9752
60 # Ensure the service is started if not running, replies on status of service resource
61 notify_group new_resource.service do
63 notifies :start, "service[#{new_resource.service}]", :immediately
68 service new_resource.service do
69 action [:disable, :stop]
72 systemd_service new_resource.service do
79 new_resource.ports.collect do |host, guest|
80 "--publish=127.0.0.1:#{host}:#{guest}"
84 def environment_options
85 new_resource.environment.collect do |key, value|
86 "-e '#{key}=#{value}'"
91 new_resource.volume.collect do |key, value|
92 "-v '#{key}:#{value}'"