]> git.openstreetmap.org Git - chef.git/blob - cookbooks/matomo/recipes/default.rb
Rename piwik to matomo
[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   not_if { ::File.exist?("/opt/matomo-#{version}/matomo") }
53 end
54
55 archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{version}.zip" do
56   destination "/opt/matomo-#{version}"
57   overwrite true
58   owner "root"
59   group "root"
60   not_if { ::File.exist?("/opt/matomo-#{version}/matomo") }
61 end
62
63 node[:matomo][:plugins].each do |plugin_name, plugin_version|
64   next if plugin_version.nil?
65
66   remote_file "#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip" do
67     source "https://plugins.matomo.org/api/2.0/plugins/#{plugin_name}/download/#{plugin_version}"
68   end
69
70   archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip" do
71     action :nothing
72     destination "/opt/matomo-#{version}/matomo/plugins"
73     overwrite true
74     owner "root"
75     group "root"
76     subscribes :extract, "remote_file[#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip]", :immediately
77   end
78 end
79
80 execute "/opt/matomo-#{version}/matomo/matomo.js" do
81   command "gzip -k -9 /opt/matomo-#{version}/matomo/matomo.js"
82   cwd "/opt/matomo-#{version}"
83   user "root"
84   group "root"
85   not_if { ::File.exist?("/opt/matomo-#{version}/matomo/matomo.js.gz") }
86 end
87
88 execute "/opt/matomo-#{version}/matomo/piwik.js" do
89   command "gzip -k -9 /opt/matomo-#{version}/matomo/piwik.js"
90   cwd "/opt/matomo-#{version}"
91   user "root"
92   group "root"
93   not_if { ::File.exist?("/opt/matomo-#{version}/matomo/piwik.js.gz") }
94 end
95
96 directory "/opt/matomo-#{version}/matomo/config" do
97   owner "www-data"
98   group "www-data"
99   mode "0755"
100 end
101
102 template "/opt/matomo-#{version}/matomo/config/config.ini.php" do
103   source "config.erb"
104   owner "root"
105   group "root"
106   mode "0644"
107   variables :passwords => passwords,
108             :directory => "/opt/matomo-#{version}/matomo",
109             :plugins => node[:matomo][:plugins].keys.sort
110 end
111
112 directory "/opt/matomo-#{version}/matomo/tmp" do
113   owner "www-data"
114   group "www-data"
115   mode "0755"
116 end
117
118 directory "/opt/matomo-#{version}/matomo/tmp/assets" do
119   owner "www-data"
120   group "mysql"
121   mode "0750"
122 end
123
124 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-ASN.mmdb" do
125   to "#{geoip_directory}/GeoLite2-ASN.mmdb"
126 end
127
128 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-City.mmdb" do
129   to "#{geoip_directory}/GeoLite2-City.mmdb"
130 end
131
132 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-Country.mmdb" do
133   to "#{geoip_directory}/GeoLite2-Country.mmdb"
134 end
135
136 link "/srv/matomo.openstreetmap.org" do
137   to "/opt/matomo-#{version}/matomo"
138   notifies :restart, "service[php#{node[:php][:version]}-fpm]"
139 end
140
141 mysql_user "piwik@localhost" do
142   password passwords["database"]
143 end
144
145 mysql_database "piwik" do
146   permissions "piwik@localhost" => :all
147 end
148
149 ssl_certificate "matomo.openstreetmap.org" do
150   domains ["matomo.openstreetmap.org", "matomo.osm.org",
151            "piwik.openstreetmap.org", "piwik.osm.org"]
152   notifies :reload, "service[apache2]"
153 end
154
155 php_fpm "matomo.openstreetmap.org" do
156   prometheus_port 9253
157 end
158
159 apache_site "matomo.openstreetmap.org" do
160   template "apache.erb"
161 end
162
163 cron_d "matomo" do
164   minute "5"
165   user "www-data"
166   command "/usr/bin/php /srv/matomo.openstreetmap.org/console core:archive --quiet --url=https://matomo.openstreetmap.org/"
167 end
168
169 cron_d "piwik" do
170   action :delete
171 end