2 # Resource:: wordpress_site
4 # Copyright:: 2015, OpenStreetMap Foundation
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # https://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 require "securerandom"
23 default_action :create
25 property :site, :kind_of => String, :name_property => true
26 property :aliases, :kind_of => [String, Array]
27 property :title, :kind_of => String
28 property :admin_user, :kind_of => String, :default => "osm_admin"
29 property :admin_email, :kind_of => String, :default => "admins@openstreetmap.org"
30 property :directory, :kind_of => String
31 property :version, :kind_of => String
32 property :database_name, :kind_of => String, :required => true
33 property :database_user, :kind_of => String, :required => [:create]
34 property :database_password, :kind_of => String, :required => [:create]
35 property :database_prefix, :kind_of => String, :default => "wp_"
36 property :wp2fa_encrypt_key, :kind_of => String, :required => true
37 property :urls, :kind_of => Hash, :default => {}
38 property :fpm_max_children, :kind_of => Integer, :default => 10
39 property :fpm_start_servers, :kind_of => Integer, :default => 4
40 property :fpm_min_spare_servers, :kind_of => Integer, :default => 2
41 property :fpm_max_spare_servers, :kind_of => Integer, :default => 6
42 property :fpm_request_terminate_timeout, :kind_of => Integer, :default => 300
43 property :fpm_prometheus_port, :kind_of => Integer
44 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
47 version = new_resource.version || Chef::Wordpress.current_version
49 node.default[:wordpress][:sites][new_resource.site] = {
50 :directory => site_directory
53 auth_key = persistent_token("wordpress", new_resource.site, "auth_key")
54 secure_auth_key = persistent_token("wordpress", new_resource.site, "secure_auth_key")
55 logged_in_key = persistent_token("wordpress", new_resource.site, "logged_in_key")
56 nonce_key = persistent_token("wordpress", new_resource.site, "nonce_key")
57 auth_salt = persistent_token("wordpress", new_resource.site, "auth_salt")
58 secure_auth_salt = persistent_token("wordpress", new_resource.site, "secure_auth_salt")
59 logged_in_salt = persistent_token("wordpress", new_resource.site, "logged_in_salt")
60 nonce_salt = persistent_token("wordpress", new_resource.site, "nonce_salt")
62 mysql_user "#{new_resource.database_user}@localhost" do
63 password new_resource.database_password
66 mysql_database new_resource.database_name do
67 permissions "#{new_resource.database_user}@localhost" => :all
70 declare_resource :directory, site_directory do
71 owner node[:wordpress][:user]
72 group node[:wordpress][:group]
76 subversion site_directory do
78 repository "https://core.svn.wordpress.org/tags/#{version}"
79 user node[:wordpress][:user]
80 group node[:wordpress][:group]
84 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
85 line.gsub!(/database_name_here/, new_resource.database_name)
86 line.gsub!(/username_here/, new_resource.database_user)
87 line.gsub!(/password_here/, new_resource.database_password)
88 line.gsub!(/wp_/, new_resource.database_prefix)
90 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{auth_key}'")
91 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{secure_auth_key}'")
92 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{logged_in_key}'")
93 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{nonce_key}'")
94 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{auth_salt}'")
95 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{secure_auth_salt}'")
96 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{logged_in_salt}'")
97 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{nonce_salt}'")
99 if line =~ /Add any custom values between this line/
102 line += " * Don't allow file editing.\r\n"
104 line += "define( 'DISALLOW_FILE_EDIT', true);\r\n"
105 line += "define( 'DISALLOW_FILE_MODS', true);\r\n"
106 line += "define( 'AUTOMATIC_UPDATER_DISABLED', true);\r\n"
107 line += "define( 'FORCE_SSL_LOGIN', true);\r\n"
108 line += "define( 'FORCE_SSL_ADMIN', true);\r\n"
109 line += "define( 'WP_FAIL2BAN_SITE_HEALTH_SKIP_FILTERS', true);\r\n"
110 line += "define( 'WP_ENVIRONMENT_TYPE', 'production');\r\n"
111 line += "define( 'WP_MEMORY_LIMIT', '128M');\r\n"
112 line += "define( 'WP2FA_ENCRYPT_KEY', '#{new_resource.wp2fa_encrypt_key}');\r\n"
118 file "#{site_directory}/wp-config.php" do
119 owner node[:wordpress][:user]
120 group node[:wordpress][:group]
125 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
131 file "#{site_directory}/sitemap.xml" do
135 file "#{site_directory}/sitemap.xml.gz" do
139 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
141 owner node[:wordpress][:user]
142 group node[:wordpress][:group]
147 # Setup wordpress database and create admin user with random password
148 execute "wp core install" do
149 command "/opt/wp-cli/wp --path=#{site_directory} core install --url=#{new_resource.site} --title='#{new_resource.title}' --admin_user=#{new_resource.admin_user} --admin_email=#{new_resource.admin_email} --skip-email"
152 only_if { ::File.exist?("#{site_directory}/wp-config.php") }
153 not_if "/opt/wp-cli/wp --path=#{site_directory} core is-installed"
156 ssl_certificate new_resource.site do
157 domains [new_resource.site] + Array(new_resource.aliases)
160 php_fpm new_resource.site do
161 pm_max_children new_resource.fpm_max_children
162 pm_start_servers new_resource.fpm_start_servers
163 pm_min_spare_servers new_resource.fpm_min_spare_servers
164 pm_max_spare_servers new_resource.fpm_max_spare_servers
165 request_terminate_timeout new_resource.fpm_request_terminate_timeout
166 php_admin_values "open_basedir" => "#{site_directory}/:/usr/share/php/:/tmp/",
167 "disable_functions" => "exec,shell_exec,system,passthru,popen,proc_open"
168 php_values "upload_max_filesize" => "70M",
169 "post_max_size" => "100M",
170 "memory_limit" => "368M"
171 prometheus_port new_resource.fpm_prometheus_port
174 apache_site new_resource.site do
176 template "apache.erb"
177 directory site_directory
178 variables :aliases => Array(new_resource.aliases),
179 :urls => new_resource.urls
183 http_request "https://#{new_resource.site}/wp-admin/upgrade.php" do
185 url "https://#{new_resource.site}/wp-admin/upgrade.php?step=1"
186 subscribes :get, "subversion[#{site_directory}]"
189 wordpress_plugin "wp-fail2ban" do
190 site new_resource.site
194 wordpress_plugin "wp-2fa" do
195 site new_resource.site
199 wordpress_plugin "wp-last-login" do
200 site new_resource.site
206 wordpress_plugin "wp-last-login" do
208 site new_resource.site
212 wordpress_plugin "wp-2fa" do
214 site new_resource.site
218 wordpress_plugin "wp-fail2ban" do
220 site new_resource.site
224 apache_site new_resource.site do
229 declare_resource :directory, site_directory do
234 mysql_database new_resource.database_name do
238 mysql_user "#{new_resource.database_user}@localhost" do
244 include OpenStreetMap::Mixin::EditFile
245 include OpenStreetMap::Mixin::PersistentToken
248 new_resource.directory || "/srv/#{new_resource.site}"
253 notifies :reload, "service[apache2]" if reload_apache