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