-
Notifications
You must be signed in to change notification settings - Fork 217
/
capifony.rb
executable file
·343 lines (284 loc) · 10.3 KB
/
capifony.rb
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
require 'yaml'
# Dirs that need to remain the same between deploys (shared dirs)
set :shared_children, %w(log web/uploads)
def prompt_with_default(var, default)
set(var) do
Capistrano::CLI.ui.ask "#{var} [#{default}] : "
end
set var, default if eval("#{var.to_s}.empty?")
end
def load_database_config(data, env = 'prod')
databases = YAML::load(data)
if databases[env]
db_param = databases[env]['doctrine']['param']
else
db_param = databases['all']['doctrine']['param']
end
{
'type' => /(\w+)\:/.match(db_param['dsn'])[1],
'user' => db_param['username'],
'pass' => db_param['password'],
'db' => /dbname=([^;$]+)/.match(db_param['dsn'])[1]
}
end
namespace :deploy do
desc "Overwrite the start task to set the permissions on the project."
task :start do
symfony.configure.database
symfony.project.permissions
doctrine.build_all_and_load
end
desc "Overwrite the restart task because symfony doesn't need it."
task :restart do ; end
desc "Overwrite the stop task because symfony doesn't need it."
task :stop do ; end
desc "Customize migrate task because symfony doesn't need it."
task :migrate do
doctrine.migrate
end
desc "Symlink static directories that need to remain between deployments."
task :create_dirs do
if shared_children
shared_children.each do |link|
run "mkdir -p #{shared_path}/#{link}"
run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
end
end
run "mkdir -p #{shared_path}/config"
run "touch #{shared_path}/config/databases.yml"
end
desc "Customize the finalize_update task to work with symfony."
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
run "mkdir -p #{latest_release}/cache"
create_dirs
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = %w(css images js).map { |p| "#{latest_release}/web/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
end
end
desc "Need to overwrite the deploy:cold task so it doesn't try to run the migrations."
task :cold do
update
start
end
desc "Deploy the application and run the test suite."
task :testall do
update_code
symlink
doctrine.build_all_and_load_test
symfony.tests.all
end
end
namespace :symlink do
desc "Symlink the database"
task :db do
run "ln -nfs #{shared_path}/config/databases.yml #{latest_release}/config/databases.yml"
end
end
namespace :symfony do
desc "Downloads & runs check_configuration.php on remote"
task :check_configuration do
prompt_with_default(:version, "1.4")
run "wget http://sf-to.org/#{version}/check.php -O /tmp/check_configuration.php"
run "php /tmp/check_configuration.php"
run "rm /tmp/check_configuration.php"
end
desc "Clears the cache"
task :cc do
run "php #{latest_release}/symfony cache:clear"
end
desc "Runs custom symfony task"
task :run_task do
prompt_with_default(:task_arguments, "cache:clear")
run "php #{latest_release}/symfony #{task_arguments}"
end
namespace :configure do
desc "Configure database DSN"
task :database do
prompt_with_default(:dsn, "mysql:host=localhost;dbname=example_dev")
prompt_with_default(:user, "root")
prompt_with_default(:pass, "")
dbclass = "sfDoctrineDatabase"
run "php #{latest_release}/symfony configure:database --class=#{dbclass} '#{dsn}' '#{user}' '#{pass}'"
end
end
namespace :project do
desc "Fixes symfony directory permissions"
task :permissions do
run "php #{latest_release}/symfony project:permissions"
end
desc "Optimizes a project for better performance"
task :optimize do
prompt_with_default(:application, "frontend")
run "php #{latest_release}/symfony project:optimize #{application}"
end
desc "Clears all non production environment controllers"
task :clear_controllers do
run "php #{latest_release}/symfony project:clear-controllers"
end
end
namespace :plugin do
desc "Publishes web assets for all plugins"
task :publish_assets do
run "php #{latest_release}/symfony plugin:publish-assets"
end
end
namespace :log do
desc "Clears log files"
task :clear do
run "php #{latest_release}/symfony log:clear"
end
desc "Rotates an application's log files"
task :rotate do
prompt_with_default(:application, "frontend")
prompt_with_default(:env, "prod")
run "php #{latest_release}/symfony log:rotate #{application} #{env}"
end
end
namespace :tests do
desc "Task to run all the tests for the application."
task :all do
run "php #{latest_release}/symfony test:all"
end
end
end
namespace :doctrine do
desc "Migrates database to current version"
task :migrate do
run "php #{latest_release}/symfony doctrine:migrate --env=prod"
end
desc "Generate code & database based on your schema"
task :build_all do
run "php #{latest_release}/symfony doctrine:build --all --no-confirmation --env=prod"
end
desc "Generate code & database based on your schema & load fixtures"
task :build_all_and_load do
run "php #{latest_release}/symfony doctrine:build --all --and-load --no-confirmation --env=prod"
end
desc "Generate code & database based on your schema & load fixtures for test environment"
task :build_all_and_load_test do
run "php #{latest_release}/symfony doctrine:build --all --and-load --no-confirmation --env=test"
end
end
namespace :database do
namespace :dump do
desc "Dump remote database"
task :remote do
filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.bz2"
file = "/tmp/#{filename}"
sqlfile = "#{application}_dump.sql"
config = ""
run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
config = load_database_config data, 'prod'
end
case config['type']
when 'mysql'
run "mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}" do |ch, stream, data|
puts data
end
when 'pgsql'
run "pg_dump -U #{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}" do |ch, stream, data|
puts data
end
end
`mkdir -p backups`
get file, "backups/#{filename}"
`cd backups && ln -s #{filename} #{application}.remote_dump.latest.sql.bz2`
run "rm #{file}"
end
desc "Dump local database"
task :local do
filename = "#{application}.local_dump.#{Time.now.to_i}.sql.bz2"
file = "backups/#{filename}"
config = load_database_config IO.read('config/databases.yml'), 'dev'
sqlfile = "#{application}_dump.sql"
`mkdir -p backups`
case config['type']
when 'mysql'
`mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}`
when 'pgsql'
`pg_dump -U #{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}`
end
`cd backups && ln -s #{filename} #{application}.local_dump.latest.sql.bz2`
end
end
namespace :move do
desc "Dump remote database, download it to local & populate here"
task :to_local do
filename = "#{application}.remote_dump.latest.sql.bz2"
config = load_database_config IO.read('config/databases.yml'), 'dev'
database.dump.remote
`bunzip2 -kc backups/#{filename} > backups/#{sqlfile}`
case config['type']
when 'mysql'
`mysql -u#{config['user']} --password='#{config['pass']}' #{config['db']} < backups/#{sqlfile}`
when 'pgsql'
`psql -U #{config['user']} --password='#{config['pass']}' #{config['db']} < backups/#{sqlfile}`
end
`rm backups/#{sqlfile}`
end
desc "Dump local database, load it to remote & populate there"
task :to_remote do
filename = "#{application}.local_dump.latest.sql.bz2"
file = "backups/#{filename}"
database.dump.local
upload(file, "/tmp/#{filename}", :via => :scp)
run "bunzip2 -kc /tmp/#{filename} > /tmp/#{sqlfile}"
run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
config = load_database_config data, 'prod'
end
case config['type']
when 'mysql'
run "mysql -u#{config['user']} --password='#{config['pass']}' #{config['db']} < /tmp/#{sqlfile}" do |ch, stream, data|
puts data
end
when 'pgsql'
run "psql -U #{config['user']} --password='#{config['pass']}' #{config['db']} < /tmp/#{sqlfile}" do |ch, stream, data|
puts data
end
end
run "rm /tmp/#{filename}"
run "rm /tmp/#{sqlfile}"
end
end
end
namespace :shared do
namespace :databases do
desc "Download config/databases.yml from remote server"
task :to_local do
download("#{shared_path}/config/databases.yml", "config/databases.yml", :via => :scp)
end
desc "Upload config/databases.yml to remote server"
task :to_remote do
upload("config/databases.yml", "#{shared_path}/config/databases.yml", :via => :scp)
end
end
namespace :log do
desc "Download all logs from remote folder to local one"
task :to_local do
download("#{shared_path}/log", "./", :via => :scp, :recursive => true)
end
desc "Upload all logs from local folder to remote one"
task :to_remote do
upload("log", "#{shared_path}/", :via => :scp, :recursive => true)
end
end
namespace :uploads do
desc "Download all files from remote web/uploads folder to local one"
task :to_local do
download("#{shared_path}/web/uploads", "web", :via => :scp, :recursive => true)
end
desc "Upload all files from local web/uploads folder to remote one"
task :to_remote do
upload("web/uploads", "#{shared_path}/web", :via => :scp, :recursive => true)
end
end
end
after "deploy:finalize_update", # After finalizing update:
"symlink:db", # 1. Symlink database
"symfony:cc", # 2. Clear cache
"symfony:plugin:publish_assets", # 3. Publish plugin assets
"symfony:project:permissions" # 4. Fix project permissions
"symfony:project:clear_controllers" # 5. Clear controllers