]> git.openstreetmap.org Git - chef.git/blob - cookbooks/podman/resources/site.rb
Add missing apache reload on ssl_certificate change
[chef.git] / cookbooks / podman / resources / site.rb
1 #
2 # Cookbook:: podman
3 # Resource:: podman_site
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 require "yaml"
21
22 unified_mode true
23
24 default_action :create
25
26 property :site, String, :name_property => true
27 property :image, String, :required => true
28 property :port, Integer, :default => 8080
29 property :aliases, :kind_of => Array, :default => []
30 property :environment, Hash, :default => {}
31
32 action :create do
33   podman_service new_resource.site do
34     description "Container service for #{new_resource.site}"
35     image new_resource.image
36     ports external_port => new_resource.port
37     environment new_resource.environment
38   end
39
40   ssl_certificate new_resource.site do
41     domains Array(new_resource.site) + new_resource.aliases
42     notifies :reload, "service[apache2]"
43   end
44
45   apache_site new_resource.site do
46     cookbook "podman"
47     template "apache.erb"
48     variables :port => external_port, :aliases => new_resource.aliases
49     notifies :reload, "service[apache2]"
50   end
51 end
52
53 action :delete do
54   apache_site new_resource.site do
55     action [:disable, :delete]
56     notifies :reload, "service[apache2]"
57   end
58
59   podman_service new_resource.site do
60     action :delete
61   end
62
63   node.rm_normal(:podman, :ports, new_resource.site)
64 end
65
66 action_class do
67   def ports_file
68     "#{Chef::Config[:file_cache_path]}/podman-ports.yml"
69   end
70
71   def ports
72     @ports ||= if ::File.exist?(ports_file)
73                  YAML.safe_load_file(ports_file)
74                else
75                  {}
76                end
77   end
78
79   def external_port
80     unless ports.include?(new_resource.site)
81       port = 40000
82
83       port += 1 while ports.values.include?(port)
84
85       ports[new_resource.site] = port
86
87       ::File.write(ports_file, YAML.dump(ports))
88     end
89
90     ports[new_resource.site]
91   end
92 end