2 # Cookbook Name:: wordpress
3 # Provider:: wordpress_site
5 # Copyright 2015, OpenStreetMap Foundation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 require "securerandom"
22 include Chef::Mixin::EditFile
31 version = new_resource.version || Chef::Wordpress.current_version
33 node.normal_unless[:wordpress][:sites][new_resource.name] = {}
35 node.normal[:wordpress][:sites][new_resource.name][:directory] = site_directory
37 node.normal_unless[:wordpress][:sites][new_resource.name][:auth_key] = SecureRandom.base64(48)
38 node.normal_unless[:wordpress][:sites][new_resource.name][:secure_auth_key] = SecureRandom.base64(48)
39 node.normal_unless[:wordpress][:sites][new_resource.name][:logged_in_key] = SecureRandom.base64(48)
40 node.normal_unless[:wordpress][:sites][new_resource.name][:nonce_key] = SecureRandom.base64(48)
41 node.normal_unless[:wordpress][:sites][new_resource.name][:auth_salt] = SecureRandom.base64(48)
42 node.normal_unless[:wordpress][:sites][new_resource.name][:secure_auth_salt] = SecureRandom.base64(48)
43 node.normal_unless[:wordpress][:sites][new_resource.name][:logged_in_salt] = SecureRandom.base64(48)
44 node.normal_unless[:wordpress][:sites][new_resource.name][:nonce_salt] = SecureRandom.base64(48)
46 mysql_user "#{new_resource.database_user}@localhost" do
47 password new_resource.database_password
50 mysql_database new_resource.database_name do
51 permissions "#{new_resource.database_user}@localhost" => :all
54 directory site_directory do
55 owner node[:wordpress][:user]
56 group node[:wordpress][:group]
60 subversion site_directory do
62 repository "http://core.svn.wordpress.org/tags/#{version}"
63 user node[:wordpress][:user]
64 group node[:wordpress][:group]
68 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
69 line.gsub!(/database_name_here/, new_resource.database_name)
70 line.gsub!(/username_here/, new_resource.database_user)
71 line.gsub!(/password_here/, new_resource.database_password)
72 line.gsub!(/wp_/, new_resource.database_prefix)
74 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:auth_key]}'")
75 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:secure_auth_key]}'")
76 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:logged_in_key]}'")
77 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:nonce_key]}'")
78 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:auth_salt]}'")
79 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:secure_auth_salt]}'")
80 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:logged_in_salt]}'")
81 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.name][:nonce_salt]}'")
83 if line =~ /define\('WP_DEBUG'/
86 line += " * Don't allow file editing.\n"
88 line += "define('DISALLOW_FILE_EDIT', true);\n"
89 if new_resource.ssl_enabled
90 line += "define('FORCE_SSL_LOGIN', true);\n"
91 line += "define('FORCE_SSL_ADMIN', true);\n"
98 file "#{site_directory}/wp-config.php" do
99 owner node[:wordpress][:user]
100 group node[:wordpress][:group]
105 directory "#{site_directory}/wp-content/uploads" do
111 file "#{site_directory}/sitemap.xml" do
115 file "#{site_directory}/sitemap.xml.gz" do
119 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
121 owner node[:wordpress][:user]
122 group node[:wordpress][:group]
127 ssl_certificate new_resource.name do
128 domains [new_resource.name] + Array(new_resource.aliases)
129 only_if { new_resource.ssl_enabled }
132 apache_site new_resource.name do
134 template "apache.erb"
135 directory site_directory
136 variables :aliases => Array(new_resource.aliases),
137 :urls => new_resource.urls,
138 :ssl_enabled => new_resource.ssl_enabled
142 http_request "http://#{new_resource.name}/wp-admin/upgrade.php" do
144 url "http://#{new_resource.name}/wp-admin/upgrade.php?step=1"
145 subscribes :get, "subversion[#{site_directory}]"
148 wordpress_plugin "wp-fail2ban" do
149 site new_resource.name
153 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
160 @include "wp-config.php";
161 @include_once "wp-includes/functions.php";
162 @include_once "wp-admin/includes/plugin.php";
163 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
166 subscribes :run, "wordpress_plugin[wp-fail2ban]"
171 wordpress_plugin "wp-fail2ban" do
173 site new_resource.name
177 apache_site new_resource.name do
182 directory site_directory do
187 mysql_database new_resource.database_name do
191 mysql_user "#{new_resource.database_user}@localhost" do
197 new_resource.directory || "/srv/#{new_resource.name}"