5 # Copyright:: 2011, OpenStreetMap Foundation
 
   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
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  20 include_recipe "apache"
 
  21 include_recipe "geoipupdate"
 
  22 include_recipe "mysql"
 
  23 include_recipe "php::fpm"
 
  25 passwords = data_bag_item("matomo", "passwords")
 
  39 apache_module "expires"
 
  41 apache_module "proxy_fcgi"
 
  42 apache_module "rewrite"
 
  44 version = node[:matomo][:version]
 
  46 geoip_directory = node[:geoipupdate][:directory]
 
  48 remote_file "#{Chef::Config[:file_cache_path]}/matomo-#{version}.zip" do
 
  49   source "https://builds.matomo.org/matomo-#{version}.zip"
 
  52 archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{version}.zip" do
 
  53   destination "/opt/matomo-#{version}"
 
  54   notifies :run, "notify_group[matomo-updated]"
 
  57 node[:matomo][:plugins].each do |plugin_name, plugin_version|
 
  58   next if plugin_version.nil?
 
  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}"
 
  64   archive_file "#{Chef::Config[:file_cache_path]}/matomo-#{plugin_name}-#{plugin_version}.zip" do
 
  65     destination "/opt/matomo-#{plugin_name}-#{plugin_version}"
 
  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]"
 
  74 directory "/opt/matomo-#{version}/matomo/config" do
 
  80 template "/opt/matomo-#{version}/matomo/config/config.ini.php" do
 
  85   variables :passwords => passwords,
 
  86             :directory => "/opt/matomo-#{version}/matomo",
 
  87             :plugins => node[:matomo][:plugins].keys.sort
 
  88   notifies :run, "notify_group[matomo-updated]"
 
  91 directory "/opt/matomo-#{version}/matomo/tmp" do
 
  97 directory "/opt/matomo-#{version}/matomo/tmp/assets" do
 
 103 directory "/opt/matomo-#{version}/matomo/tmp/cache" do
 
 109 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-ASN.mmdb" do
 
 110   to "#{geoip_directory}/GeoLite2-ASN.mmdb"
 
 113 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-City.mmdb" do
 
 114   to "#{geoip_directory}/GeoLite2-City.mmdb"
 
 117 link "/opt/matomo-#{version}/matomo/misc/GeoLite2-Country.mmdb" do
 
 118   to "#{geoip_directory}/GeoLite2-Country.mmdb"
 
 121 mysql_user "piwik@localhost" do
 
 122   password passwords["database"]
 
 125 mysql_database "piwik" do
 
 126   permissions "piwik@localhost" => :all
 
 129 notify_group "matomo-updated"
 
 131 if File.symlink?("/srv/matomo.openstreetmap.org")
 
 132   execute "core:update" do
 
 134     command "/opt/matomo-#{version}/matomo/console core:update --yes"
 
 137     subscribes :run, "notify_group[matomo-updated]"
 
 140   execute "custom-matomo-js:update" do
 
 142     command "/opt/matomo-#{version}/matomo/console custom-matomo-js:update"
 
 145     subscribes :run, "execute[core:update]"
 
 148   execute "/opt/matomo-#{version}/matomo/matomo.br" do
 
 150     command "brotli --keep --force --best /opt/matomo-#{version}/matomo/matomo.js"
 
 151     cwd "/opt/matomo-#{version}"
 
 154     subscribes :run, "execute[custom-matomo-js:update]"
 
 157   execute "/opt/matomo-#{version}/matomo/matomo.js" do
 
 159     command "gzip --keep --force --best /opt/matomo-#{version}/matomo/matomo.js"
 
 160     cwd "/opt/matomo-#{version}"
 
 163     subscribes :run, "execute[custom-matomo-js:update]"
 
 166   execute "/opt/matomo-#{version}/matomo/piwik.br" do
 
 168     command "brotli --keep --force --best /opt/matomo-#{version}/matomo/piwik.js"
 
 169     cwd "/opt/matomo-#{version}"
 
 172     subscribes :run, "execute[custom-matomo-js:update]"
 
 175   execute "/opt/matomo-#{version}/matomo/piwik.js" do
 
 177     command "gzip --keep --force --best /opt/matomo-#{version}/matomo/piwik.js"
 
 178     cwd "/opt/matomo-#{version}"
 
 181     subscribes :run, "execute[custom-matomo-js:update]"
 
 185 link "/srv/matomo.openstreetmap.org" do
 
 186   to "/opt/matomo-#{version}/matomo"
 
 187   notifies :restart, "service[php#{node[:php][:version]}-fpm]"
 
 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]"
 
 196 php_fpm "matomo.openstreetmap.org" do
 
 200 apache_site "matomo.openstreetmap.org" do
 
 201   template "apache.erb"
 
 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/"
 
 210   memory_deny_write_execute false
 
 211   restrict_address_families "AF_UNIX"
 
 212   read_write_paths "/opt/matomo-#{version}/matomo/tmp"
 
 215 systemd_timer "matomo-archive" do
 
 216   description "Matomo report archiving"
 
 218   on_unit_inactive_sec "30m"
 
 221 service "matomo-archive.timer" do
 
 222   action [:enable, :start]