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"
21 default_action :create
23 property :site, :kind_of => String, :name_property => true
24 property :aliases, :kind_of => [String, Array]
25 property :directory, :kind_of => String
26 property :version, :kind_of => String
27 property :database_name, :kind_of => String, :required => true
28 property :database_user, :kind_of => String, :required => [:create]
29 property :database_password, :kind_of => String, :required => [:create]
30 property :database_prefix, :kind_of => String, :default => "wp_"
31 property :urls, :kind_of => Hash, :default => {}
32 property :fpm_max_children, :kind_of => Integer, :default => 5
33 property :fpm_start_servers, :kind_of => Integer, :default => 2
34 property :fpm_min_spare_servers, :kind_of => Integer, :default => 1
35 property :fpm_max_spare_servers, :kind_of => Integer, :default => 3
36 property :fpm_request_terminate_timeout, :kind_of => Integer, :default => 300
37 property :fpm_prometheus_port, :kind_of => Integer
38 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
41 version = new_resource.version || Chef::Wordpress.current_version
43 node.default[:wordpress][:sites][new_resource.site] = {
44 :directory => site_directory
47 auth_key = persistent_token("wordpress", new_resource.site, "auth_key")
48 secure_auth_key = persistent_token("wordpress", new_resource.site, "secure_auth_key")
49 logged_in_key = persistent_token("wordpress", new_resource.site, "logged_in_key")
50 nonce_key = persistent_token("wordpress", new_resource.site, "nonce_key")
51 auth_salt = persistent_token("wordpress", new_resource.site, "auth_salt")
52 secure_auth_salt = persistent_token("wordpress", new_resource.site, "secure_auth_salt")
53 logged_in_salt = persistent_token("wordpress", new_resource.site, "logged_in_salt")
54 nonce_salt = persistent_token("wordpress", new_resource.site, "nonce_salt")
56 mysql_user "#{new_resource.database_user}@localhost" do
57 password new_resource.database_password
60 mysql_database new_resource.database_name do
61 permissions "#{new_resource.database_user}@localhost" => :all
64 declare_resource :directory, site_directory do
65 owner node[:wordpress][:user]
66 group node[:wordpress][:group]
70 subversion site_directory do
72 repository "https://core.svn.wordpress.org/tags/#{version}"
73 user node[:wordpress][:user]
74 group node[:wordpress][:group]
78 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
79 line.gsub!(/database_name_here/, new_resource.database_name)
80 line.gsub!(/username_here/, new_resource.database_user)
81 line.gsub!(/password_here/, new_resource.database_password)
82 line.gsub!(/wp_/, new_resource.database_prefix)
84 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{auth_key}'")
85 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{secure_auth_key}'")
86 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{logged_in_key}'")
87 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{nonce_key}'")
88 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{auth_salt}'")
89 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{secure_auth_salt}'")
90 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{logged_in_salt}'")
91 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{nonce_salt}'")
93 if line =~ /define\('WP_DEBUG'/
96 line += " * Don't allow file editing.\n"
98 line += "define('DISALLOW_FILE_EDIT', true);\n"
99 line += "define('FORCE_SSL_LOGIN', true);\n"
100 line += "define('FORCE_SSL_ADMIN', true);\n"
106 file "#{site_directory}/wp-config.php" do
107 owner node[:wordpress][:user]
108 group node[:wordpress][:group]
113 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
119 file "#{site_directory}/sitemap.xml" do
123 file "#{site_directory}/sitemap.xml.gz" do
127 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
129 owner node[:wordpress][:user]
130 group node[:wordpress][:group]
135 ssl_certificate new_resource.site do
136 domains [new_resource.site] + Array(new_resource.aliases)
139 php_fpm new_resource.site do
140 pm_max_children new_resource.fpm_max_children
141 pm_start_servers new_resource.fpm_start_servers
142 pm_min_spare_servers new_resource.fpm_min_spare_servers
143 pm_max_spare_servers new_resource.fpm_max_spare_servers
144 request_terminate_timeout new_resource.fpm_request_terminate_timeout
145 php_admin_values "open_basedir" => "#{site_directory}/:/usr/share/php/:/tmp/",
146 "disable_functions" => "exec,shell_exec,system,passthru,popen,proc_open"
147 php_values "upload_max_filesize" => "70M",
148 "post_max_size" => "100M"
149 prometheus_port new_resource.fpm_prometheus_port
152 apache_site new_resource.site do
154 template "apache.erb"
155 directory site_directory
156 variables :aliases => Array(new_resource.aliases),
157 :urls => new_resource.urls
161 http_request "https://#{new_resource.site}/wp-admin/upgrade.php" do
163 url "https://#{new_resource.site}/wp-admin/upgrade.php?step=1"
164 subscribes :get, "subversion[#{site_directory}]"
167 wordpress_plugin "wp-fail2ban" do
168 site new_resource.site
172 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
179 @include "wp-config.php";
180 @include_once "wp-includes/functions.php";
181 @include_once "wp-admin/includes/plugin.php";
182 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
185 subscribes :run, "wordpress_plugin[wp-fail2ban]"
190 wordpress_plugin "wp-fail2ban" do
192 site new_resource.site
196 apache_site new_resource.site do
201 declare_resource :directory, site_directory do
206 mysql_database new_resource.database_name do
210 mysql_user "#{new_resource.database_user}@localhost" do
216 include Chef::Mixin::EditFile
217 include Chef::Mixin::PersistentToken
220 new_resource.directory || "/srv/#{new_resource.site}"
225 notifies :reload, "service[apache2]" if reload_apache