forked from kronda/wp_capistrano
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Capfile
250 lines (214 loc) · 10 KB
/
Capfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# TODO: Cap db:push should support rollback or backup in some way
# TODO: Better handling of database creation - perhaps make the deploy user a database admin?
# TODO: Better DB handling -> set variable of domain name and replace all youdomain.com instances
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy.rb'
require 'capistrano/ext/multistage'
namespace :deploy do
desc "Prepares one or more servers for deployment."
task :setup, :roles => :web, :except => { :no_release => true } do
dirs = [deploy_to, releases_path, shared_path]
domains.each do |domain|
dirs += [shared_path + "/#{domain}/files"]
dirs += [shared_path + "/#{domain}/files/avatars"]
dirs += [shared_path + "/#{domain}/files/uploads"]
dirs += [shared_path + "/#{domain}/files/w3tc"]
dirs += [shared_path + "/#{domain}/files/cache"]
end
dirs += %w(system).map { |d| File.join(shared_path, d) }
run "mkdir -m 0775 -p #{dirs.join(' ')}"
# add setgid bit, so that files/ contents are always in the httpd group
run "chmod 2775 #{shared_path}/*/files"
run "chmod 2775 #{shared_path}/*/files/avatars"
run "chmod 2775 #{shared_path}/*/files/uploads"
run "chmod 2775 #{shared_path}/*/files/w3tc"
run "chmod 2775 #{shared_path}/*/files/cache"
run "chgrp #{httpd_group} #{shared_path}/*/files"
run "chgrp #{httpd_group} #{shared_path}/*/files/*"
end
desc "Create local local_config.php in shared/config"
task :create_settings_php, :roles => :web do
domains.each do |domain|
configuration = <<-EOF
<?php
define('DB_NAME', '#{short_name(domain)}');
/** MySQL database username */
define('DB_USER', '#{tiny_name(domain)}');
/** MySQL database password */
define('DB_PASSWORD', '#{db_pass}');
/** MySQL hostname */
define('DB_HOST', 'localhost');
EOF
put configuration, "#{deploy_to}/#{shared_dir}/#{domain}/local-config.php"
end
end
desc "link file dirs and the local_settings.php to the shared copy"
task :symlink_files, :roles => :web do
domains.each do |domain|
# link settings file
run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/local-config.php #{release_path}/#{app_root}/local-config.php"
# Link Various files directories (uploads, w3tc, avatars, cache)
run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/files/uploads #{release_path}/#{app_root}/wp-content/uploads"
run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/files/w3tc #{release_path}/#{app_root}/wp-content/w3tc"
run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/files/avatars #{release_path}/#{app_root}/wp-content/avatars"
run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/files/cache #{release_path}/#{app_root}/wp-content/cache"
# IF YOU WANT .HTACCESS FILES PUSHED --> ADD HERE. ---- THIS WILL LINK .htpasswd
# run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/.htpasswd #{release_path}/#{app_root}/.htpasswd"
# run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/.htaccess #{release_path}/#{app_root}/.htaccess"
# run "ln -nfs #{deploy_to}/#{shared_dir}/#{domain}/lo-check.php #{release_path}/#{app_root}/lo-check.php"
end
end
# desc '[internal] Touches up the released code.'
desc "Changing permissions on WP files"
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g-w #{deploy_to}"
run "chmod 644 #{release_path}/#{app_root}/wp-config.php"
end
# Each of the following tasks are Rails specific. They're removed.
task :migrate do
end
task :migrations do
end
task :cold do
end
task :start do
end
task :stop do
end
task :restart, :roles => :web do
end
after "deploy:setup",
"deploy:create_settings_php",
"db:create"
after "deploy:update_code",
"deploy:symlink_files"
after "deploy",
"deploy:cleanup"
end
namespace :db do
desc "Download a backup of the database(s) from the given stage."
task :down, :roles => :db, :only => { :primary => true } do
domains.each do |domain|
filename = "#{domain}_#{stage}.sql"
temp = "/tmp/#{release_name}_#{application}_#{filename}"
run "touch #{temp} && chmod 600 #{temp}"
run_locally "mkdir -p db"
run "cd #{deploy_to}/current/webroot && #{wp} db export #{temp} && cd -"
if "#{stage}" == "prod"
search = "#{default_url}"
# search = "yourdomain.com"
else
search = "#{stage}.#{default_url}"
# search = "#{stage}.yourdomain.com"
end
replace = local_domain
puts "searching (#{search}) and replacing (#{replace}) domain information"
run_locally "sed -e 's/#{search}/#{replace}/g' -i .bak db/#{filename}"
run "rm #{temp}"
end
end
desc "Download and apply a backup of the database(s) from the given stage LORELLE Gonna handle this."
task :pull, :roles => :db, :only => { :primary => true } do
domains.each do |domain|
filename = "#{domain}_#{stage}.sql"
temp = "/tmp/#{release_name}_#{application}_#{filename}"
run "touch #{temp} && chmod 600 #{temp}"
run_locally "mkdir -p db"
run "source ~/.bash_profile && cd #{deploy_to}/current/webroot && #{wp} db export #{temp} && cd -"
download("#{temp}", "db/#{filename}", :via=> :scp)
if "#{stage}" == "prod"
search = "thegiftedprogramnyc.com"
else
search = "#{stage}.thegiftedprogramnyc.com"
end
replace = local_domain
puts "searching (#{search}) and replacing (#{replace}) domain information"
run_locally "sed -e 's/#{search}/#{replace}/g' -i .bak db/#{filename}"
run "rm #{temp}"
local_filename = "#{domain}_#{stage}.sql"
system "cd db && cp #{local_filename} ../#{app_root}; cd ../#{app_root} && wp db import #{local_filename}; rm #{local_filename}"
end
end
desc "Download and apply a backup of the database(s) from the given stage."
task :pullx, :roles => :db, :only => { :primary => true } do
domains.each do |domain|
filename = "#{domain}_#{stage}.sql"
system "cd #{app_root} ; #{wp} db import #{filename}"
end
end
desc "Upload database(s) to the given stage."
task :push, :roles => :db, :only => { :primary => true } do
domains.each do |domain|
filename = "#{domain}_#{stage}.sql"
temp = "/tmp/#{release_name}_#{application}_#{filename}"
run "touch #{temp} && chmod 600 #{temp}"
if "#{stage}" == "prod"
replace = "#{default_url}"
else
replace = "#{stage}.#{default_url}"
end
search = local_domain
puts "searching (#{search}) and replacing (#{replace}) domain information"
run_locally "sed -e 's/#{search}/#{replace}/g' -i .bak db/#{filename}"
upload("db/#{filename}", "#{temp}", :via=> :scp)
run "cd #{deploy_to}/current/webroot/ && #{wp} db import #{temp}"
run "rm #{temp}"
end
end
desc "Create database"
task :create, :roles => :db, :only => { :primary => true } do
# Create and gront privs to the new db user
domains.each do |domain|
create_sql = "CREATE DATABASE IF NOT EXISTS \\\`#{short_name(domain)}\\\` ;
GRANT ALL ON \\\`#{short_name(domain)}\\\`.* TO '#{tiny_name(domain)}'@'localhost' IDENTIFIED BY '#{db_pass}';
FLUSH PRIVILEGES;"
run "mysql -u root -p#{db_root_pass} -e \"#{create_sql}\""
puts "Using pass: #{db_pass}"
end
end
before "db:pull", "db:down"
end
namespace :files do
desc "Download a backup of the wp-content (minus themes + plugins) directory from the given stage."
task :pull, :roles => :web do
domains.each do |domain|
if exists?(:gateway)
run_locally("rsync --recursive --times --omit-dir-times --chmod=ugo=rwX --rsh='ssh #{ssh_options[:user]}@#{gateway} ssh #{ssh_options[:user]}@#{find_servers(:roles => :web).first.host}' --compress --human-readable --progress --exclude 'webroot/plugins' --exclude 'webroot/themes' :#{deploy_to}/#{shared_dir}/#{domain}/files/ webroot/wp-content/")
else
run_locally("rsync --recursive --times --omit-dir-times --chmod=ugo=rwX --rsh=ssh --compress --human-readable --progress --exclude 'webroot/plugins' --exclude 'webroot/themes' #{ssh_options[:user]}@#{find_servers(:roles => :web).first.host}:#{deploy_to}/#{shared_dir}/#{domain}/files/ webroot/wp-content/")
end
end
end
desc "Push a backup of the wp-content (minus themes + plugins) directory from the given stage."
task :push, :roles => :web do
domains.each do |domain|
if exists?(:gateway)
run_locally("rsync --recursive --times --omit-dir-times --chmod=ugo=rwX --rsh='ssh #{ssh_options[:user]}@#{gateway} ssh #{ssh_options[:user]}@#{find_servers(:roles => :web).first.host}' --compress --human-readable --progress --exclude 'webroot/plugins' --exclude 'webroot/themes' webroot/wp-content/ :#{deploy_to}/#{shared_dir}/#{domain}/files/")
else
run_locally("rsync --recursive --times --omit-dir-times --chmod=ugo=rwX --rsh=ssh --compress --human-readable --progress --exclude 'webroot/plugins' --exclude 'webroot/themes' webroot/wp-content/ #{ssh_options[:user]}@#{find_servers(:roles => :web).first.host}:#{deploy_to}/#{shared_dir}/#{domain}/files/")
end
end
end
end
namespace :z_lotest do
desc "Lorelle's Testing Purposes."
task :printVariables, :roles => :web do
puts "Default URL is: " + default_url
puts "Dev URL is: " + dev_url
puts "Web Config: #{find_servers(:roles => :web)}"
puts "DB Config : #{find_servers(:roles => :db)}"
end
end
def short_name(domain=nil)
return "#{application}_#{stage}_#{domain}".gsub('.', '_')[0..63] if domain && domain != 'default'
return "#{application}_#{stage}".gsub('.', '_')[0..63]
end
def tiny_name(domain=nil)
return "#{application[0..5]}_#{stage.to_s[0..2]}_#{domain[0..4]}".gsub('.', '_') if domain && domain != 'default'
return "#{application[0..11]}_#{stage.to_s[0..2]}".gsub('.', '_')
end
def random_password(size = 16)
chars = (('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..size).collect{|a| chars[rand(chars.size)] }.join
end