]> git.openstreetmap.org Git - chef.git/blob - cookbooks/php/resources/fpm.rb
nominatim: install secondary importance file
[chef.git] / cookbooks / php / resources / fpm.rb
1 #
2 # Cookbook:: php
3 # Resource:: php_fpm
4 #
5 # Copyright:: 2020, 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 unified_mode true
21
22 default_action :create
23
24 property :pool, :kind_of => String, :name_property => true
25 property :port, :kind_of => Integer
26 property :user, :kind_of => String, :default => "www-data"
27 property :group, :kind_of => String, :default => "www-data"
28 property :pm, :kind_of => String, :default => "dynamic"
29 property :pm_max_children, :kind_of => Integer, :default => 10
30 property :pm_start_servers, :kind_of => Integer, :default => 4
31 property :pm_min_spare_servers, :kind_of => Integer, :default => 2
32 property :pm_max_spare_servers, :kind_of => Integer, :default => 6
33 property :pm_max_requests, :kind_of => Integer, :default => 1000
34 property :request_terminate_timeout, :kind_of => Integer, :default => 1800
35 property :environment, :kind_of => Hash, :default => {}
36 property :php_values, :kind_of => Hash, :default => {}
37 property :php_admin_values, :kind_of => Hash, :default => {}
38 property :php_flags, :kind_of => Hash, :default => {}
39 property :php_admin_flags, :kind_of => Hash, :default => {}
40 property :reload_fpm, :kind_of => [TrueClass, FalseClass], :default => true
41 property :prometheus_port, :kind_of => Integer
42
43 action :create do
44   template conf_file do
45     cookbook "php"
46     source "pool.conf.erb"
47     owner "root"
48     group "root"
49     mode "644"
50     variables new_resource.to_hash.merge(:pool => new_resource.pool)
51   end
52
53   if new_resource.prometheus_port
54     prometheus_exporter "phpfpm" do
55       port new_resource.prometheus_port
56       restrict_address_families "AF_UNIX"
57       service service_name
58       group "www-data"
59       command "server"
60       options "--phpfpm.scrape-uri=#{scrape_uri} --phpfpm.fix-process-count"
61       labels "pool" => new_resource.pool
62     end
63   else
64     prometheus_exporter "phpfpm" do
65       action :delete
66       service service_name
67     end
68   end
69 end
70
71 action :delete do
72   file conf_file do
73     action :delete
74   end
75
76   prometheus_exporter "phpfpm" do
77     action :delete
78     service service_name
79   end
80 end
81
82 action_class do
83   def php_version
84     node[:php][:version]
85   end
86
87   def conf_file
88     "/etc/php/#{php_version}/fpm/pool.d/#{new_resource.pool}.conf"
89   end
90
91   def service_name
92     "phpfpm-#{new_resource.pool}"
93   end
94
95   def scrape_uri
96     if new_resource.port
97       "tcp://127.0.0.1:#{new_resource.port}/status"
98     else
99       "unix:///run/php/php-#{new_resource.pool}-fpm.sock;/status"
100     end
101   end
102 end
103
104 def after_created
105   notifies :reload, "service[php#{node[:php][:version]}-fpm]" if reload_fpm
106 end