1 # Cookbook Name:: wordpress
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_attribute => 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 => true
29 property :database_password, :kind_of => String, :required => true
30 property :database_prefix, :kind_of => String, :default => "wp_"
31 property :urls, :kind_of => Hash, :default => {}
32 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
35 version = new_resource.version || Chef::Wordpress.current_version
37 node.normal_unless[:wordpress][:sites][new_resource.site] = {}
39 node.normal[:wordpress][:sites][new_resource.site][:directory] = site_directory
41 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_key] = SecureRandom.base64(48)
42 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_key] = SecureRandom.base64(48)
43 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_key] = SecureRandom.base64(48)
44 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_key] = SecureRandom.base64(48)
45 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_salt] = SecureRandom.base64(48)
46 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_salt] = SecureRandom.base64(48)
47 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_salt] = SecureRandom.base64(48)
48 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_salt] = SecureRandom.base64(48)
50 mysql_user "#{new_resource.database_user}@localhost" do
51 password new_resource.database_password
54 mysql_database new_resource.database_name do
55 permissions "#{new_resource.database_user}@localhost" => :all
58 declare_resource :directory, site_directory do
59 owner node[:wordpress][:user]
60 group node[:wordpress][:group]
64 subversion site_directory do
66 repository "https://core.svn.wordpress.org/tags/#{version}"
67 user node[:wordpress][:user]
68 group node[:wordpress][:group]
72 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
73 line.gsub!(/database_name_here/, new_resource.database_name)
74 line.gsub!(/username_here/, new_resource.database_user)
75 line.gsub!(/password_here/, new_resource.database_password)
76 line.gsub!(/wp_/, new_resource.database_prefix)
78 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_key]}'")
79 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_key]}'")
80 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_key]}'")
81 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_key]}'")
82 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_salt]}'")
83 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_salt]}'")
84 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_salt]}'")
85 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_salt]}'")
87 if line =~ /define\('WP_DEBUG'/
90 line += " * Don't allow file editing.\n"
92 line += "define('DISALLOW_FILE_EDIT', true);\n"
93 line += "define('FORCE_SSL_LOGIN', true);\n"
94 line += "define('FORCE_SSL_ADMIN', true);\n"
100 file "#{site_directory}/wp-config.php" do
101 owner node[:wordpress][:user]
102 group node[:wordpress][:group]
107 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
113 file "#{site_directory}/sitemap.xml" do
117 file "#{site_directory}/sitemap.xml.gz" do
121 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
123 owner node[:wordpress][:user]
124 group node[:wordpress][:group]
129 ssl_certificate new_resource.site do
130 domains [new_resource.site] + Array(new_resource.aliases)
133 apache_site new_resource.site do
135 template "apache.erb"
136 directory site_directory
137 variables :aliases => Array(new_resource.aliases),
138 :urls => new_resource.urls
142 http_request "https://#{new_resource.site}/wp-admin/upgrade.php" do
144 url "https://#{new_resource.site}/wp-admin/upgrade.php?step=1"
145 subscribes :get, "subversion[#{site_directory}]"
148 wordpress_plugin "wp-fail2ban" do
149 site new_resource.site
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.site
177 apache_site new_resource.site do
182 declare_resource :directory, site_directory do
187 mysql_database new_resource.database_name do
191 mysql_user "#{new_resource.database_user}@localhost" do
197 include Chef::Mixin::EditFile
200 new_resource.directory || "/srv/#{new_resource.site}"
205 notifies :reload, "service[apache2]" if reload_apache