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 apache_site new_resource.name do
129 template "apache.erb"
130 directory site_directory
131 variables :aliases => Array(new_resource.aliases),
132 :urls => new_resource.urls,
133 :ssl_enabled => new_resource.ssl_enabled,
134 :ssl_certificate => new_resource.ssl_certificate,
135 :ssl_certificate_chain => new_resource.ssl_certificate_chain
139 http_request "http://#{new_resource.name}/wp-admin/upgrade.php" do
141 url "http://#{new_resource.name}/wp-admin/upgrade.php?step=1"
142 subscribes :get, "subversion[#{site_directory}]"
145 wordpress_plugin "wp-fail2ban" do
146 site new_resource.name
150 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
157 @include "wp-config.php";
158 @include_once "wp-includes/functions.php";
159 @include_once "wp-admin/includes/plugin.php";
160 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
163 subscribes :run, "wordpress_plugin[wp-fail2ban]"
168 wordpress_plugin "wp-fail2ban" do
170 site new_resource.name
174 apache_site new_resource.name do
179 directory site_directory do
184 mysql_database new_resource.database_name do
188 mysql_user "#{new_resource.database_user}@localhost" do
194 new_resource.directory || "/srv/#{new_resource.name}"