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( 'WP_HOME', 'https://#{new_resource.site}');\r\n"
105 line += "define( 'WP_SITEURL', 'https://#{new_resource.site}');\r\n"
106 line += "define( 'DISALLOW_FILE_EDIT', true);\r\n"
107 line += "define( 'DISALLOW_FILE_MODS', true);\r\n"
108 line += "define( 'AUTOMATIC_UPDATER_DISABLED', true);\r\n"
109 line += "define( 'FORCE_SSL_LOGIN', true);\r\n"
110 line += "define( 'FORCE_SSL_ADMIN', true);\r\n"
111 line += "define( 'WP_FAIL2BAN_SITE_HEALTH_SKIP_FILTERS', true);\r\n"
112 line += "define( 'WP_ENVIRONMENT_TYPE', 'production');\r\n"
113 line += "define( 'WP_MEMORY_LIMIT', '128M');\r\n"
114 line += "define( 'WP2FA_ENCRYPT_KEY', '#{new_resource.wp2fa_encrypt_key}');\r\n"
120 file "#{site_directory}/wp-config.php" do
121 owner node[:wordpress][:user]
122 group node[:wordpress][:group]
127 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
133 file "#{site_directory}/sitemap.xml" do
137 file "#{site_directory}/sitemap.xml.gz" do
141 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
143 owner node[:wordpress][:user]
144 group node[:wordpress][:group]
149 # Setup wordpress database and create admin user with random password
150 execute "wp core install" do
151 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"
154 only_if { ::File.exist?("#{site_directory}/wp-config.php") }
155 not_if "/opt/wp-cli/wp --path='#{site_directory}' core is-installed"
158 execute "wp core update-db" do
159 command "/opt/wp-cli/wp --path='#{site_directory}' core update-db"
162 only_if { ::File.exist?("#{site_directory}/wp-config.php") }
163 subscribes :run, "subversion[#{site_directory}]"
166 ssl_certificate new_resource.site do
167 domains [new_resource.site] + Array(new_resource.aliases)
170 php_fpm new_resource.site do
171 pm_max_children new_resource.fpm_max_children
172 pm_start_servers new_resource.fpm_start_servers
173 pm_min_spare_servers new_resource.fpm_min_spare_servers
174 pm_max_spare_servers new_resource.fpm_max_spare_servers
175 request_terminate_timeout new_resource.fpm_request_terminate_timeout
176 php_admin_values "open_basedir" => "#{site_directory}/:/usr/share/php/:/tmp/",
177 "disable_functions" => "exec,shell_exec,system,passthru,popen,proc_open"
178 php_values "upload_max_filesize" => "70M",
179 "post_max_size" => "100M",
180 "memory_limit" => "368M"
181 prometheus_port new_resource.fpm_prometheus_port
184 apache_site new_resource.site do
186 template "apache.erb"
187 directory site_directory
188 variables :aliases => Array(new_resource.aliases),
189 :urls => new_resource.urls
193 wordpress_plugin "wp-fail2ban" do
194 site new_resource.site
198 wordpress_plugin "wp-2fa" do
199 site new_resource.site
203 wordpress_plugin "wp-last-login" do
204 site new_resource.site
210 wordpress_plugin "wp-last-login" do
212 site new_resource.site
216 wordpress_plugin "wp-2fa" do
218 site new_resource.site
222 wordpress_plugin "wp-fail2ban" do
224 site new_resource.site
228 apache_site new_resource.site do
233 declare_resource :directory, site_directory do
238 mysql_database new_resource.database_name do
242 mysql_user "#{new_resource.database_user}@localhost" do
248 include OpenStreetMap::Mixin::EditFile
249 include OpenStreetMap::Mixin::PersistentToken
252 new_resource.directory || "/srv/#{new_resource.site}"
257 notifies :reload, "service[apache2]" if reload_apache