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