2 # Cookbook Name:: wordpress
3 # Resource:: 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 default_action :create
24 property :site, :kind_of => String, :name_attribute => true
25 property :aliases, :kind_of => [String, Array]
26 property :directory, :kind_of => String
27 property :version, :kind_of => String
28 property :database_name, :kind_of => String, :required => true
29 property :database_user, :kind_of => String, :required => true
30 property :database_password, :kind_of => String, :required => true
31 property :database_prefix, :kind_of => String, :default => "wp_"
32 property :ssl_enabled, :kind_of => [TrueClass, FalseClass], :default => false
33 property :urls, :kind_of => Hash, :default => {}
34 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
37 version = new_resource.version || Chef::Wordpress.current_version
39 node.normal_unless[:wordpress][:sites][new_resource.site] = {}
41 node.normal[:wordpress][:sites][new_resource.site][:directory] = site_directory
43 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_key] = SecureRandom.base64(48)
44 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_key] = SecureRandom.base64(48)
45 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_key] = SecureRandom.base64(48)
46 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_key] = SecureRandom.base64(48)
47 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_salt] = SecureRandom.base64(48)
48 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_salt] = SecureRandom.base64(48)
49 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_salt] = SecureRandom.base64(48)
50 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_salt] = SecureRandom.base64(48)
52 mysql_user "#{new_resource.database_user}@localhost" do
53 password new_resource.database_password
56 mysql_database new_resource.database_name do
57 permissions "#{new_resource.database_user}@localhost" => :all
60 declare_resource :directory, site_directory do
61 owner node[:wordpress][:user]
62 group node[:wordpress][:group]
66 subversion site_directory do
68 repository "http://core.svn.wordpress.org/tags/#{version}"
69 user node[:wordpress][:user]
70 group node[:wordpress][:group]
74 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
75 line.gsub!(/database_name_here/, new_resource.database_name)
76 line.gsub!(/username_here/, new_resource.database_user)
77 line.gsub!(/password_here/, new_resource.database_password)
78 line.gsub!(/wp_/, new_resource.database_prefix)
80 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_key]}'")
81 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_key]}'")
82 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_key]}'")
83 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_key]}'")
84 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_salt]}'")
85 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_salt]}'")
86 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_salt]}'")
87 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_salt]}'")
89 if line =~ /define\('WP_DEBUG'/
92 line += " * Don't allow file editing.\n"
94 line += "define('DISALLOW_FILE_EDIT', true);\n"
95 if new_resource.ssl_enabled
96 line += "define('FORCE_SSL_LOGIN', true);\n"
97 line += "define('FORCE_SSL_ADMIN', true);\n"
104 file "#{site_directory}/wp-config.php" do
105 owner node[:wordpress][:user]
106 group node[:wordpress][:group]
111 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
117 file "#{site_directory}/sitemap.xml" do
121 file "#{site_directory}/sitemap.xml.gz" do
125 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
127 owner node[:wordpress][:user]
128 group node[:wordpress][:group]
133 ssl_certificate new_resource.site do
134 domains [new_resource.site] + Array(new_resource.aliases)
135 only_if { new_resource.ssl_enabled }
138 apache_site new_resource.site do
140 template "apache.erb"
141 directory site_directory
142 variables :aliases => Array(new_resource.aliases),
143 :urls => new_resource.urls,
144 :ssl_enabled => new_resource.ssl_enabled
148 http_request "http://#{new_resource.site}/wp-admin/upgrade.php" do
150 url "http://#{new_resource.site}/wp-admin/upgrade.php?step=1"
151 subscribes :get, "subversion[#{site_directory}]"
154 wordpress_plugin "wp-fail2ban" do
155 site new_resource.site
159 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
166 @include "wp-config.php";
167 @include_once "wp-includes/functions.php";
168 @include_once "wp-admin/includes/plugin.php";
169 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
172 subscribes :run, "wordpress_plugin[wp-fail2ban]"
177 wordpress_plugin "wp-fail2ban" do
179 site new_resource.site
183 apache_site new_resource.site do
188 declare_resource :directory, site_directory do
193 mysql_database new_resource.database_name do
197 mysql_user "#{new_resource.database_user}@localhost" do
203 include Chef::Mixin::EditFile
206 new_resource.directory || "/srv/#{new_resource.site}"
211 notifies :reload, "service[apache2]" if reload_apache