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