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 # https://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 :urls, :kind_of => Hash, :default => {}
33 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
36 version = new_resource.version || Chef::Wordpress.current_version
38 node.normal_unless[:wordpress][:sites][new_resource.site] = {}
40 node.normal[:wordpress][:sites][new_resource.site][:directory] = site_directory
42 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_key] = SecureRandom.base64(48)
43 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_key] = SecureRandom.base64(48)
44 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_key] = SecureRandom.base64(48)
45 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_key] = SecureRandom.base64(48)
46 node.normal_unless[:wordpress][:sites][new_resource.site][:auth_salt] = SecureRandom.base64(48)
47 node.normal_unless[:wordpress][:sites][new_resource.site][:secure_auth_salt] = SecureRandom.base64(48)
48 node.normal_unless[:wordpress][:sites][new_resource.site][:logged_in_salt] = SecureRandom.base64(48)
49 node.normal_unless[:wordpress][:sites][new_resource.site][:nonce_salt] = SecureRandom.base64(48)
51 mysql_user "#{new_resource.database_user}@localhost" do
52 password new_resource.database_password
55 mysql_database new_resource.database_name do
56 permissions "#{new_resource.database_user}@localhost" => :all
59 declare_resource :directory, site_directory do
60 owner node[:wordpress][:user]
61 group node[:wordpress][:group]
65 subversion site_directory do
67 repository "https://core.svn.wordpress.org/tags/#{version}"
68 user node[:wordpress][:user]
69 group node[:wordpress][:group]
73 wp_config = edit_file "#{site_directory}/wp-config-sample.php" do |line|
74 line.gsub!(/database_name_here/, new_resource.database_name)
75 line.gsub!(/username_here/, new_resource.database_user)
76 line.gsub!(/password_here/, new_resource.database_password)
77 line.gsub!(/wp_/, new_resource.database_prefix)
79 line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_key]}'")
80 line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_key]}'")
81 line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_key]}'")
82 line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_key]}'")
83 line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:auth_salt]}'")
84 line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:secure_auth_salt]}'")
85 line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:logged_in_salt]}'")
86 line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][new_resource.site][:nonce_salt]}'")
88 if line =~ /define\('WP_DEBUG'/
91 line += " * Don't allow file editing.\n"
93 line += "define('DISALLOW_FILE_EDIT', true);\n"
94 line += "define('FORCE_SSL_LOGIN', true);\n"
95 line += "define('FORCE_SSL_ADMIN', true);\n"
101 file "#{site_directory}/wp-config.php" do
102 owner node[:wordpress][:user]
103 group node[:wordpress][:group]
108 declare_resource :directory, "#{site_directory}/wp-content/uploads" do
114 file "#{site_directory}/sitemap.xml" do
118 file "#{site_directory}/sitemap.xml.gz" do
122 cookbook_file "#{site_directory}/googlefac54c35e800caab.html" do
124 owner node[:wordpress][:user]
125 group node[:wordpress][:group]
130 ssl_certificate new_resource.site do
131 domains [new_resource.site] + Array(new_resource.aliases)
134 apache_site new_resource.site do
136 template "apache.erb"
137 directory site_directory
138 variables :aliases => Array(new_resource.aliases),
139 :urls => new_resource.urls
143 http_request "https://#{new_resource.site}/wp-admin/upgrade.php" do
145 url "https://#{new_resource.site}/wp-admin/upgrade.php?step=1"
146 subscribes :get, "subversion[#{site_directory}]"
149 wordpress_plugin "wp-fail2ban" do
150 site new_resource.site
154 script "#{site_directory}/wp-content/plugins/wp-fail2ban" do
161 @include "wp-config.php";
162 @include_once "wp-includes/functions.php";
163 @include_once "wp-admin/includes/plugin.php";
164 activate_plugin("wp-fail2ban/wp-fail2ban.php", '', false, false);
167 subscribes :run, "wordpress_plugin[wp-fail2ban]"
172 wordpress_plugin "wp-fail2ban" do
174 site new_resource.site
178 apache_site new_resource.site do
183 declare_resource :directory, site_directory do
188 mysql_database new_resource.database_name do
192 mysql_user "#{new_resource.database_user}@localhost" do
198 include Chef::Mixin::EditFile
201 new_resource.directory || "/srv/#{new_resource.site}"
206 notifies :reload, "service[apache2]" if reload_apache