]> git.openstreetmap.org Git - chef.git/blob - cookbooks/dns/recipes/default.rb
Use a systemd timer for DNS updates
[chef.git] / cookbooks / dns / recipes / default.rb
1 #
2 # Cookbook:: dns
3 # Recipe:: default
4 #
5 # Copyright:: 2011, 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 include_recipe "accounts"
21 include_recipe "apache"
22 include_recipe "git"
23
24 geoservers = search(:node, "roles:geodns").collect(&:name).sort
25
26 passwords = data_bag_item("dns", "passwords")
27
28 package %w[
29   make
30   parallel
31   rsync
32   perl
33   libdigest-sha-perl
34   libjson-xs-perl
35   libwww-perl
36   libxml-treebuilder-perl
37   libxml-writer-perl
38   libyaml-libyaml-perl
39   lockfile-progs
40 ]
41
42 cache_dir = Chef::Config[:file_cache_path]
43
44 dnscontrol_version = "3.20.0"
45
46 dnscontrol_arch = if arm?
47                     "arm64"
48                   else
49                     "amd64"
50                   end
51
52 remote_file "#{cache_dir}/dnscontrol-#{dnscontrol_version}.deb" do
53   source "https://github.com/StackExchange/dnscontrol/releases/download/v#{dnscontrol_version}/dnscontrol-#{dnscontrol_version}.#{dnscontrol_arch}.deb"
54   owner "root"
55   group "root"
56   mode "644"
57   backup false
58 end
59
60 dpkg_package "dnscontrol" do
61   action :nothing
62   source "#{cache_dir}/dnscontrol-#{dnscontrol_version}.deb"
63   subscribes :install, "remote_file[#{cache_dir}/dnscontrol-#{dnscontrol_version}.deb]"
64 end
65
66 directory "/srv/dns.openstreetmap.org" do
67   owner "root"
68   group "root"
69   mode "755"
70 end
71
72 remote_directory "/srv/dns.openstreetmap.org/html" do
73   source "html"
74   owner "root"
75   group "root"
76   mode "755"
77   files_owner "root"
78   files_group "root"
79   files_mode "644"
80 end
81
82 zones = []
83
84 Dir.glob("/var/lib/dns/json/*.json").each do |kmlfile|
85   zone = File.basename(kmlfile, ".json")
86
87   template "/srv/dns.openstreetmap.org/html/#{zone}.html" do
88     source "zone.html.erb"
89     owner "root"
90     group "root"
91     mode "644"
92     variables :zone => zone
93   end
94
95   zones.push(zone)
96 end
97
98 template "/srv/dns.openstreetmap.org/html/index.html" do
99   source "index.html.erb"
100   owner "root"
101   group "root"
102   mode "644"
103   variables :zones => zones
104 end
105
106 ssl_certificate "dns.openstreetmap.org" do
107   domains ["dns.openstreetmap.org", "dns.osm.org"]
108   notifies :reload, "service[apache2]"
109 end
110
111 apache_site "dns.openstreetmap.org" do
112   template "apache.erb"
113   directory "/srv/dns.openstreetmap.org"
114   variables :aliases => ["dns.osm.org"]
115 end
116
117 template "/usr/local/bin/dns-update" do
118   source "dns-update.erb"
119   owner "root"
120   group "git"
121   mode "750"
122   variables :passwords => passwords, :geoservers => geoservers
123 end
124
125 execute "dns-update" do
126   action :nothing
127   command "/usr/local/bin/dns-update"
128   user "git"
129   group "git"
130 end
131
132 directory "/var/lib/dns" do
133   owner "git"
134   group "git"
135   mode "2775"
136   notifies :run, "execute[dns-update]"
137 end
138
139 template "/var/lib/dns/creds.json" do
140   source "creds.json.erb"
141   owner "git"
142   group "git"
143   mode "440"
144   variables :passwords => passwords
145 end
146
147 template "/var/lib/dns/include/geo.js" do
148   source "geo.js.erb"
149   owner "git"
150   group "git"
151   mode "440"
152   variables :geoservers => geoservers
153   only_if { ::Dir.exist?("/var/lib/dns/include") }
154 end
155
156 cookbook_file "#{node[:dns][:repository]}/hooks/post-receive" do
157   source "post-receive"
158   owner "git"
159   group "git"
160   mode "750"
161   only_if { ::Dir.exist?("#{node[:dns][:repository]}/hooks") }
162 end
163
164 template "/usr/local/bin/dns-check" do
165   source "dns-check.erb"
166   owner "root"
167   group "git"
168   mode "750"
169   variables :passwords => passwords, :geoservers => geoservers
170 end
171
172 systemd_service "dns-check" do
173   description "Rebuild DNS zones with GeoDNS changes"
174   exec_start "/usr/local/bin/dns-check"
175   user "git"
176   private_tmp true
177   private_devices true
178   protect_system "strict"
179   protect_home true
180   read_write_paths "/var/lib/dns"
181 end
182
183 systemd_timer "dns-check" do
184   description "Rebuild DNS zones with GeoDNS changes"
185   on_boot_sec "3m"
186   on_unit_active_sec "3m"
187 end
188
189 service "dns-check.timer" do
190   action [:enable, :start]
191 end