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 :directory, :kind_of => String
28 property :version, :kind_of => String
29 property :database_name, :kind_of => String, :required => true
30 property :database_user, :kind_of => String, :required => [:create]
31 property :database_password, :kind_of => String, :required => [:create]
32 property :database_prefix, :kind_of => String, :default => "wp_"
33 property :urls, :kind_of => Hash, :default => {}
34 property :fpm_max_children, :kind_of => Integer, :default => 10
35 property :fpm_start_servers, :kind_of => Integer, :default => 4
36 property :fpm_min_spare_servers, :kind_of => Integer, :default => 2
37 property :fpm_max_spare_servers, :kind_of => Integer, :default => 6
38 property :fpm_request_terminate_timeout, :kind_of => Integer, :default => 300
39 property :fpm_prometheus_port, :kind_of => Integer
40 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
43 version = new_resource.version || Chef::Wordpress.current_version
45 node.default[:wordpress][:sites][new_resource.site] = {
46 :directory => site_directory
49 auth_key = persistent_token("wordpress", new_resource.site, "auth_key")
50 secure_auth_key = persistent_token("wordpress", new_resource.site, "secure_auth_key")
51 logged_in_key = persistent_token("wordpress", new_resource.site, "logged_in_key")
52 nonce_key = persistent_token("wordpress", new_resource.site, "nonce_key")
53 auth_salt = persistent_token("wordpress", new_resource.site, "auth_salt")
54 secure_auth_salt = persistent_token("wordpress", new_resource.site, "secure_auth_salt")
55 logged_in_salt = persistent_token("wordpress", new_resource.site, "logged_in_salt")
56 nonce_salt = persistent_token("wordpress", new_resource.site, "nonce_salt")
58 mysql_user "#{new_resource.database_user}@localhost" do
59 password new_resource.database_password
62 mysql_database new_resource.database_name do
63 permissions "#{new_resource.database_user}@localhost" => :all
66 declare_resource :directory, site_directory do
67 owner node[:wordpress][:user]
68 group node[:wordpress][:group]
72 subversion site_directory do
74 repository "https://core.svn.wordpress.org/tags/#{version}"
75 user node[:wordpress][:user]
76 group node[:wordpress][:group]
80 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
81 line.gsub!(/database_name_here/, new_resource.database_name)
82 line.gsub!(/username_here/, new_resource.database_user)
83 line.gsub!(/password_here/, new_resource.database_password)
84 line.gsub!(/wp_/, new_resource.database_prefix)
86 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{auth_key}'")
87 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{secure_auth_key}'")
88 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{logged_in_key}'")
89 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{nonce_key}'")
90 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{auth_salt}'")
91 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{secure_auth_salt}'")
92 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{logged_in_salt}'")
93 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{nonce_salt}'")
95 if line =~ /Add any custom values between this line/
98 line += " * Don't allow file editing.\r\n"
100 line += "define( 'DISALLOW_FILE_EDIT', true);\r\n"
101 line += "define( 'DISALLOW_FILE_MODS', true);\r\n"
102 line += "define( 'AUTOMATIC_UPDATER_DISABLED', true);\r\n"
103 line += "define( 'FORCE_SSL_LOGIN', true);\r\n"
104 line += "define( 'FORCE_SSL_ADMIN', true);\r\n"
105 line += "define( 'WP_FAIL2BAN_SITE_HEALTH_SKIP_FILTERS', true);\r\n"
111 file "#{site_directory}/wp-config.php" do
112 owner node[:wordpress][:user]
113 group node[:wordpress][:group]
118 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
124 file "#{site_directory}/sitemap.xml" do
128 file "#{site_directory}/sitemap.xml.gz" do
132 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
134 owner node[:wordpress][:user]
135 group node[:wordpress][:group]
140 ssl_certificate new_resource.site do
141 domains [new_resource.site] + Array(new_resource.aliases)
144 php_fpm new_resource.site do
145 pm_max_children new_resource.fpm_max_children
146 pm_start_servers new_resource.fpm_start_servers
147 pm_min_spare_servers new_resource.fpm_min_spare_servers
148 pm_max_spare_servers new_resource.fpm_max_spare_servers
149 request_terminate_timeout new_resource.fpm_request_terminate_timeout
150 php_admin_values "open_basedir" => "#{site_directory}/:/usr/share/php/:/tmp/",
151 "disable_functions" => "exec,shell_exec,system,passthru,popen,proc_open"
152 php_values "upload_max_filesize" => "70M",
153 "post_max_size" => "100M"
154 prometheus_port new_resource.fpm_prometheus_port
157 apache_site new_resource.site do
159 template "apache.erb"
160 directory site_directory
161 variables :aliases => Array(new_resource.aliases),
162 :urls => new_resource.urls
166 http_request "https://#{new_resource.site}/wp-admin/upgrade.php" do
168 url "https://#{new_resource.site}/wp-admin/upgrade.php?step=1"
169 subscribes :get, "subversion[#{site_directory}]"
172 wordpress_plugin "wp-fail2ban" do
173 site new_resource.site
177 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
184 @include "wp-config.php";
185 @include_once "wp-includes/functions.php";
186 @include_once "wp-admin/includes/plugin.php";
187 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
190 subscribes :run, "wordpress_plugin[wp-fail2ban]"
195 wordpress_plugin "wp-fail2ban" do
197 site new_resource.site
201 apache_site new_resource.site do
206 declare_resource :directory, site_directory do
211 mysql_database new_resource.database_name do
215 mysql_user "#{new_resource.database_user}@localhost" do
221 include OpenStreetMap::Mixin::EditFile
222 include OpenStreetMap::Mixin::PersistentToken
225 new_resource.directory || "/srv/#{new_resource.site}"
230 notifies :reload, "service[apache2]" if reload_apache