]> git.openstreetmap.org Git - chef.git/blob - cookbooks/matomo/recipes/default.rb
Switch matomo archiver to use a systemd timer
[chef.git] / cookbooks / matomo / recipes / default.rb
1 #
2 # Cookbook:: matomo
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 "apache"
21 include_recipe "geoipupdate"
22 include_recipe "mysql"
23 include_recipe "php::fpm"
24
25 passwords = data_bag_item("matomo", "passwords")
26
27 package %w[
28   brotli
29   gzip
30   php-cli
31   php-curl
32   php-mbstring
33   php-mysql
34   php-gd
35   php-xml
36   php-apcu
37 ]
38
39 apache_module "expires"
40 apache_module "rewrite"
41
42 version = node[:matomo][:version]
43
44 geoip_directory = node[:geoipupdate][:directory]
45
46 remote_file "#{Chef::Config[:file_cache_path]}/matomo-#{version}.zip" do
47   source "https://builds.matomo.org/matomo-#{version}.zip"
48 end
49
50 archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{version}.zip" do
51   destination "/opt/matomo-#{version}"
52   notifies :run, "notify_group[matomo-updated]"
53 end
54
55 node[:matomo][:plugins].each do |plugin_name, plugin_version|
56   next if plugin_version.nil?
57
58   remote_file "#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip" do
59     source "https://plugins.matomo.org/api/2.0/plugins/#{plugin_name}/download/#{plugin_version}"
60   end
61
62   archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip" do
63     destination "/opt/matomo-#{plugin_name}-#{plugin_version}"
64   end
65
66   link "/opt/matomo-#{version}/matomo/plugins/#{plugin_name}" do
67     to "/opt/matomo-#{plugin_name}-#{plugin_version}/#{plugin_name}"
68     notifies :run, "notify_group[matomo-updated]"
69   end
70 end
71
72 directory "/opt/matomo-#{version}/matomo/config" do
73   owner "www-data"
74   group "www-data"
75   mode "0755"
76 end
77
78 template "/opt/matomo-#{version}/matomo/config/config.ini.php" do
79   source "config.erb"
80   owner "root"
81   group "root"
82   mode "0644"
83   variables :passwords => passwords,
84             :directory => "/opt/matomo-#{version}/matomo",
85             :plugins => node[:matomo][:plugins].keys.sort
86   notifies :run, "notify_group[matomo-updated]"
87 end
88
89 directory "/opt/matomo-#{version}/matomo/tmp" do
90   owner "www-data"
91   group "www-data"
92   mode "0755"
93 end
94
95 directory "/opt/matomo-#{version}/matomo/tmp/assets" do
96   owner "www-data"
97   group "mysql"
98   mode "0750"
99 end
100
101 directory "/opt/matomo-#{version}/matomo/tmp/cache" do
102   owner "www-data"
103   group "www-data"
104   mode "0750"
105 end
106
107 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-ASN.mmdb" do
108   to "#{geoip_directory}/GeoLite2-ASN.mmdb"
109 end
110
111 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-City.mmdb" do
112   to "#{geoip_directory}/GeoLite2-City.mmdb"
113 end
114
115 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-Country.mmdb" do
116   to "#{geoip_directory}/GeoLite2-Country.mmdb"
117 end
118
119 mysql_user "piwik@localhost" do
120   password passwords["database"]
121 end
122
123 mysql_database "piwik" do
124   permissions "piwik@localhost" => :all
125 end
126
127 notify_group "matomo-updated"
128
129 if File.symlink?("/srv/matomo.openstreetmap.org")
130   execute "core:update" do
131     action :nothing
132     command "/opt/matomo-#{version}/matomo/console core:update --yes"
133     user "www-data"
134     group "www-data"
135     subscribes :run, "notify_group[matomo-updated]"
136   end
137
138   execute "custom-matomo-js:update" do
139     action :nothing
140     command "/opt/matomo-#{version}/matomo/console custom-matomo-js:update"
141     user "root"
142     group "root"
143     subscribes :run, "execute[core:update]"
144   end
145
146   execute "/opt/matomo-#{version}/matomo/matomo.br" do
147     action :nothing
148     command "brotli --keep --force --best /opt/matomo-#{version}/matomo/matomo.js"
149     cwd "/opt/matomo-#{version}"
150     user "root"
151     group "root"
152     subscribes :run, "execute[custom-matomo-js:update]"
153   end
154
155   execute "/opt/matomo-#{version}/matomo/matomo.js" do
156     action :nothing
157     command "gzip --keep --force --best /opt/matomo-#{version}/matomo/matomo.js"
158     cwd "/opt/matomo-#{version}"
159     user "root"
160     group "root"
161     subscribes :run, "execute[custom-matomo-js:update]"
162   end
163
164   execute "/opt/matomo-#{version}/matomo/piwik.br" do
165     action :nothing
166     command "brotli --keep --force --best /opt/matomo-#{version}/matomo/piwik.js"
167     cwd "/opt/matomo-#{version}"
168     user "root"
169     group "root"
170     subscribes :run, "execute[custom-matomo-js:update]"
171   end
172
173   execute "/opt/matomo-#{version}/matomo/piwik.js" do
174     action :nothing
175     command "gzip --keep --force --best /opt/matomo-#{version}/matomo/piwik.js"
176     cwd "/opt/matomo-#{version}"
177     user "root"
178     group "root"
179     subscribes :run, "execute[custom-matomo-js:update]"
180   end
181 end
182
183 link "/srv/matomo.openstreetmap.org" do
184   to "/opt/matomo-#{version}/matomo"
185   notifies :restart, "service[php#{node[:php][:version]}-fpm]"
186 end
187
188 ssl_certificate "matomo.openstreetmap.org" do
189   domains ["matomo.openstreetmap.org", "matomo.osm.org",
190            "piwik.openstreetmap.org", "piwik.osm.org"]
191   notifies :reload, "service[apache2]"
192 end
193
194 php_fpm "matomo.openstreetmap.org" do
195   prometheus_port 9253
196 end
197
198 apache_site "matomo.openstreetmap.org" do
199   template "apache.erb"
200 end
201
202 systemd_service "matomo-archive" do
203   description "Matomo report archiving"
204   exec_start "/usr/bin/php /srv/matomo.openstreetmap.org/console core:archive --quiet --url=https://matomo.openstreetmap.org/"
205   user "www-data"
206   sandbox true
207   memory_deny_write_execute false
208   restrict_address_families "AF_UNIX"
209   read_write_paths "/opt/matomo-#{version}/matomo/tmp"
210 end
211
212 systemd_timer "matomo-archive" do
213   description "Matomo report archiving"
214   on_calendar "00:05"
215 end
216
217 service "matomo-archive.timer" do
218   action [:enable, :start]
219 end