capifony 2.1.7 → 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,21 @@
1
+ == 2.1.9 / ???
2
+
3
+ n/a
4
+
5
+ == 2.1.8 / July 2, 2012
6
+
7
+ * bugfixes
8
+ * fix database tasks to use role :db
9
+ * BC break: capifony now requires Capistrano >= 2.11.0
10
+ * added red color for error messages.
11
+ * added commented lines in the generated deploy.rb file to set verbosity as
12
+ before.
13
+ * BC break: verbose mode is disabled by default (log level = IMPORTANT), so
14
+ that it's more readable.
15
+ * added dependency to 'colored'.
16
+ * added human readable messages in deploy related tasks.
17
+ * removed unexistent Doctrine commands
18
+
1
19
  == 2.1.7 / June 21, 2012
2
20
 
3
21
  * added maintenance page feature for Symfony2, use symfony 1.4 tasks
@@ -66,7 +66,7 @@ if symfony_version == 2
66
66
 
67
67
  set :repository, "\#{domain}:/var/repos/\#{application}.git"
68
68
  set :scm, :git
69
- # Or: `accurev`, `bzr`, `cvs`, `darcs`, `subversion`, `mercurial`, `perforce`, `subversion` or `none`
69
+ # Or: `accurev`, `bzr`, `cvs`, `darcs`, `subversion`, `mercurial`, `perforce`, or `none`
70
70
 
71
71
  set :model_manager, "doctrine"
72
72
  # Or: `propel`
@@ -76,6 +76,9 @@ if symfony_version == 2
76
76
  role :db, domain, :primary => true # This is where Rails migrations will run
77
77
 
78
78
  set :keep_releases, 3
79
+
80
+ # Be more verbose by uncommenting the following line
81
+ # logger.level = Logger::MAX_LEVEL
79
82
  FILE
80
83
  }
81
84
  else
@@ -93,7 +96,7 @@ set :deploy_to, "/var/www/#{domain}"
93
96
 
94
97
  set :repository, "#{domain}:/var/repos/#{application}.git"
95
98
  set :scm, :git
96
- # Or: `accurev`, `bzr`, `cvs`, `darcs`, `subversion`, `mercurial`, `perforce`, `subversion` or `none`
99
+ # Or: `accurev`, `bzr`, `cvs`, `darcs`, `subversion`, `mercurial`, `perforce`, or `none`
97
100
 
98
101
  role :web, domain # Your HTTP server, Apache/etc
99
102
  role :app, domain # This may be the same as your `Web` server
@@ -15,15 +15,25 @@ def prompt_with_default(var, default, &block)
15
15
  end
16
16
 
17
17
  namespace :deploy do
18
- desc "Overwrite the start task because symfony doesn't need it."
19
- task :start do ; end
18
+ desc <<-DESC
19
+ Blank task exists as a hook into which to install your own environment \
20
+ specific behaviour.
21
+ DESC
22
+ task :start, :roles => :app do
23
+ # Empty Task to overload with your platform specifics
24
+ end
25
+
26
+ desc <<-DESC
27
+ Blank task exists as a hook into which to install your own environment \
28
+ specific behaviour.
29
+ DESC
30
+ task :stop, :roles => :app do
31
+ # Empty Task to overload with your platform specifics
32
+ end
20
33
 
21
34
  desc "Overwrite the restart task because symfony doesn't need it."
22
35
  task :restart do ; end
23
36
 
24
- desc "Overwrite the stop task because symfony doesn't need it."
25
- task :stop do ; end
26
-
27
37
  desc <<-DESC
28
38
  Prepares one or more servers for deployment. Before you can use any \
29
39
  of the Capistrano deployment tasks with your project, you will need to \
@@ -1,5 +1,15 @@
1
- require 'yaml'
2
1
  load Gem.find_files('capifony.rb').last.to_s
2
+ load_paths.push File.expand_path('../', __FILE__)
3
+
4
+ load 'symfony1/database'
5
+ load 'symfony1/deploy'
6
+ load 'symfony2/doctrine'
7
+ load 'symfony2/propel'
8
+ load 'symfony1/shared'
9
+ load 'symfony1/symfony'
10
+ load 'symfony1/web'
11
+
12
+ require 'yaml'
3
13
 
4
14
  # Dirs that need to remain the same between deploys (shared dirs)
5
15
  set :shared_children, %w(log web/uploads)
@@ -14,14 +24,17 @@ set :asset_children, %w(web/css web/images web/js)
14
24
  set :use_orm, true
15
25
 
16
26
  # Symfony default ORM
17
- set(:symfony_orm) { guess_symfony_orm }
27
+ set(:symfony_orm) { guess_symfony_orm }
28
+
29
+ # Default database connection name
30
+ set(:connection) { symfony_orm }
18
31
 
19
32
  # Symfony lib path
20
- set(:symfony_lib) { guess_symfony_lib }
33
+ set(:symfony_lib) { guess_symfony_lib }
21
34
 
22
35
  # Shared symfony lib
23
36
  set :use_shared_symfony, false
24
- set :symfony_version, "1.4.18"
37
+ set :symfony_version, "1.4.18"
25
38
 
26
39
  def guess_symfony_orm
27
40
  databases = YAML::load(IO.read('config/databases.yml'))
@@ -39,587 +52,59 @@ def guess_symfony_lib
39
52
  /\((.*)\)/.match(symfony_version)[1]
40
53
  end
41
54
 
55
+ def deep_merge(hash1, hash2)
56
+ hash1.merge(hash2){|key, subhash1, subhash2|
57
+ if (subhash1.is_a?(Hash) && subhash2.is_a?(Hash))
58
+ next deep_merge(subhash1, subhash2)
59
+ end
60
+ subhash2
61
+ }
62
+ end
63
+
42
64
  def load_database_config(data, env)
43
- databases = YAML::load(data)
65
+ db_config = YAML::load(data)
44
66
 
45
- if databases[env]
46
- db_param = databases[env][symfony_orm]['param']
47
- else
48
- db_param = databases['all'][symfony_orm]['param']
49
- end
67
+ connections = deep_merge(db_config['all'], db_config[env.to_s])
68
+
69
+ db_param = connections[connection]['param']
70
+
71
+ dsn = db_param['dsn']
72
+ host = dsn.match(/host=([^;$]+)/)[1] if dsn.match("host")
73
+ port = dsn.match(/port=([0-9]+)/)[1] if dsn.match("port")
50
74
 
51
75
  {
52
76
  'type' => /(\w+)\:/.match(db_param['dsn'])[1],
53
77
  'user' => db_param['username'],
54
78
  'pass' => db_param['password'],
55
- 'db' => /dbname=([^;$]+)/.match(db_param['dsn'])[1]
79
+ 'db' => dsn.match(/dbname=([^;$]+)/)[1],
80
+ 'host' => host,
81
+ 'port' => port
56
82
  }
57
83
  end
58
84
 
59
- namespace :deploy do
60
- desc "Customize migrate task because symfony doesn't need it."
61
- task :migrate do
62
- symfony.orm.migrate
63
- end
64
-
65
- desc "Symlink static directories and static files that need to remain between deployments."
66
- task :share_childs do
67
- if shared_children
68
- shared_children.each do |link|
69
- run "mkdir -p #{shared_path}/#{link}"
70
- run "if [ -d #{release_path}/#{link} ] ; then rm -rf #{release_path}/#{link}; fi"
71
- run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
72
- end
73
- end
74
- if shared_files
75
- shared_files.each do |link|
76
- link_dir = File.dirname("#{shared_path}/#{link}")
77
- run "mkdir -p #{link_dir}"
78
- run "touch #{shared_path}/#{link}"
79
- run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
80
- end
81
- end
82
- end
83
-
84
- desc "Customize the finalize_update task to work with symfony."
85
- task :finalize_update, :except => { :no_release => true } do
86
- run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
87
- run "mkdir -p #{latest_release}/cache"
88
- run "chmod -R g+w #{latest_release}/cache"
89
-
90
- # Share common files & folders
91
- share_childs
92
-
93
- if fetch(:normalize_asset_timestamps, true)
94
- stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
95
- asset_paths = asset_children.map { |p| "#{latest_release}/#{p}" }.join(" ")
96
- run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
97
- end
98
- end
99
-
100
- desc "Need to overwrite the deploy:cold task so it doesn't try to run the migrations."
101
- task :cold do
102
- update
103
- symfony.orm.build_db_and_load
104
- start
105
- end
106
-
107
- desc "Deploy the application and run the test suite."
108
- task :testall do
109
- update_code
110
- symlink
111
- symfony.orm.build_db_and_load
112
- symfony.tests.all
113
- end
114
-
115
- namespace :web do
116
- desc "Use symfony 1.4 task to disable the application"
117
- task :disable, :roles => :web, :except => { :no_release => true } do
118
- symfony.project.disable
119
- end
120
-
121
- desc "Use symfony 1.4 task to enable the application"
122
- task :enable, :roles => :web, :except => { :no_release => true } do
123
- symfony.project.enable
124
- end
125
- end
126
- end
127
-
128
- namespace :symfony do
129
- desc "Runs custom symfony task"
130
- task :default do
131
- prompt_with_default(:task_arguments, "cache:clear")
132
-
133
- stream "cd #{latest_release} && #{php_bin} ./symfony #{task_arguments}"
134
- end
135
-
136
- desc "Downloads & runs check_configuration.php on remote"
137
- task :check_configuration do
138
- prompt_with_default(:version, "1.4")
139
-
140
- run "wget http://sf-to.org/#{version}/check.php -O /tmp/check_configuration.php"
141
- stream "#{php_bin} /tmp/check_configuration.php"
142
- run "rm /tmp/check_configuration.php"
143
- end
144
-
145
- desc "Clears the cache"
146
- task :cc do
147
- run "cd #{latest_release} && #{try_sudo} #{php_bin} ./symfony cache:clear"
148
- run "chmod -R g+w #{latest_release}/cache"
149
- end
150
-
151
- desc "Creates symbolic link to symfony lib in shared"
152
- task :create_lib_symlink do
153
- prompt_with_default(:version, symfony_version)
154
- symlink_path = "#{latest_release}/lib/vendor/symfony"
155
-
156
- run "if [ ! -d #{shared_path}/symfony-#{version} ]; then exit 1; fi;"
157
- run "ln -nfs #{shared_path}/symfony-#{version} #{symlink_path};"
158
- end
159
-
160
- namespace :configure do
161
- desc "Configure database DSN"
162
- task :database do
163
- prompt_with_default(:dsn, "mysql:host=localhost;dbname=#{application}")
164
- prompt_with_default(:db_username, "root")
165
- db_password = Capistrano::CLI.password_prompt("db_password : ")
166
-
167
- # surpress debug log output to hide the password
168
- current_logger_level = self.logger.level
169
- if current_logger_level >= Capistrano::Logger::DEBUG
170
- logger.debug %(executing "cd #{latest_release} && #{php_bin} ./symfony configure:database '#{dsn}' '#{db_username}' ***")
171
- self.logger.level = Capistrano::Logger::INFO
172
- end
173
-
174
- stream "cd #{latest_release} && #{php_bin} ./symfony configure:database '#{dsn}' '#{db_username}' '#{db_password}'"
175
-
176
- # restore logger level
177
- self.logger.level = current_logger_level
178
- end
179
- end
180
-
181
- namespace :project do
182
- desc "Disables an application in a given environment"
183
- task :disable do
184
- run "cd #{latest_release} && #{php_bin} ./symfony project:disable #{symfony_env_prod}"
185
- end
186
-
187
- desc "Enables an application in a given environment"
188
- task :enable do
189
- run "cd #{latest_release} && #{php_bin} ./symfony project:enable #{symfony_env_prod}"
190
- end
191
-
192
- desc "Fixes symfony directory permissions"
193
- task :permissions do
194
- run "cd #{latest_release} && #{php_bin} ./symfony project:permissions"
195
- end
196
-
197
- desc "Optimizes a project for better performance"
198
- task :optimize do
199
- prompt_with_default(:application, "frontend")
200
-
201
- run "cd #{latest_release} && #{php_bin} ./symfony project:optimize #{application}"
202
- end
203
-
204
- desc "Clears all non production environment controllers"
205
- task :clear_controllers do
206
- run "cd #{latest_release} && #{php_bin} ./symfony project:clear-controllers"
207
- end
208
-
209
- desc "Sends emails stored in a queue"
210
- task :send_emails do
211
- prompt_with_default(:message_limit, 10)
212
- prompt_with_default(:time_limit, 10)
213
-
214
- stream "cd #{latest_release} && #{php_bin} ./symfony project:send-emails --message-limit=#{message_limit} --time-limit=#{time_limit} --env=#{symfony_env_prod}"
215
- end
216
-
217
- desc 'Task to set all front controllers to a specific environment'
218
- task :set_environment do
219
- if (env = fetch(:symfony_env_prod, nil)) && env != 'prod'
220
- cmd = []
221
- apps = fetch(:symfony_apps, ['frontend'])
222
-
223
- # First application listed becomes index.php
224
- if app = apps.shift
225
- cmd << "cp #{release_path}/web/#{app}_#{env}.php #{release_path}/web/index.php"
226
- end
227
-
228
- # All other apps are copied to their default controllers
229
- for app in apps
230
- cmd << "cp #{release_path}/web/#{app}_#{env}.php #{release_path}/web/#{app}.php"
231
- end
232
-
233
- run cmd.join(';') if cmd.join(';')
234
- end
235
- end
236
- end
237
-
238
- namespace :plugin do
239
- desc "Publishes web assets for all plugins"
240
- task :publish_assets do
241
- run "cd #{latest_release} && #{php_bin} ./symfony plugin:publish-assets"
242
- end
243
- end
244
-
245
- namespace :log do
246
- desc "Clears log files"
247
- task :clear do
248
- run "cd #{latest_release} && #{php_bin} ./symfony log:clear"
249
- end
250
-
251
- desc "Rotates an application's log files"
252
- task :rotate do
253
- prompt_with_default(:application, "frontend")
254
-
255
- run "cd #{latest_release} && #{php_bin} ./symfony log:rotate #{application} #{symfony_env_prod}"
256
- end
257
- end
258
-
259
- namespace :tests do
260
- desc "Launches all tests"
261
- task :all do
262
- run "cd #{latest_release} && #{php_bin} ./symfony test:all"
263
- end
264
-
265
- desc "Launches functional tests"
266
- task :functional do
267
- prompt_with_default(:application, "frontend")
268
-
269
- run "cd #{latest_release} && #{php_bin} ./symfony test:functional #{application}"
270
- end
271
-
272
- desc "Launches unit tests"
273
- task :unit do
274
- run "cd #{latest_release} && #{php_bin} ./symfony test:unit"
275
- end
276
- end
277
-
278
- namespace :orm do
279
- desc "Ensure symfony ORM is properly configured"
280
- task :setup do
281
- find_and_execute_task("symfony:#{symfony_orm}:setup")
282
- end
283
-
284
- desc "Migrates database to current version"
285
- task :migrate do
286
- find_and_execute_task("symfony:#{symfony_orm}:migrate")
287
- end
288
-
289
- desc "Generate model lib form and filters classes based on your schema"
290
- task :build_classes do
291
- find_and_execute_task("symfony:#{symfony_orm}:build_classes")
292
- end
293
-
294
- desc "Generate code & database based on your schema"
295
- task :build_all do
296
- find_and_execute_task("symfony:#{symfony_orm}:build_all")
297
- end
298
-
299
- desc "Generate code & database based on your schema & load fixtures"
300
- task :build_all_and_load do
301
- find_and_execute_task("symfony:#{symfony_orm}:build_all_and_load")
302
- end
303
-
304
- desc "Generate sql & database based on your schema"
305
- task :build_db do
306
- find_and_execute_task("symfony:#{symfony_orm}:build_db")
307
- end
308
-
309
- desc "Generate sql & database based on your schema & load fixtures"
310
- task :build_db_and_load do
311
- find_and_execute_task("symfony:#{symfony_orm}:build_db_and_load")
312
- end
313
- end
314
-
315
- namespace :doctrine do
316
- desc "Compile doctrine"
317
- task :compile do
318
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:compile"
319
- end
320
-
321
- desc "Ensure Doctrine is correctly configured"
322
- task :setup do
323
- conf_files_exists = capture("if test -s #{shared_path}/config/databases.yml ; then echo 'exists' ; fi").strip
324
- if (!conf_files_exists.eql?("exists"))
325
- symfony.configure.database
326
- end
327
- end
328
-
329
- desc "Execute a DQL query and view the results"
330
- task :dql do
331
- prompt_with_default(:query, "")
332
-
333
- stream "cd #{latest_release} && #{php_bin} ./symfony doctrine:dql #{query} --env=#{symfony_env_prod}"
334
- end
335
-
336
- desc "Dumps data to the fixtures directory"
337
- task :data_dump do
338
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:data-dump --env=#{symfony_env_prod}"
339
- end
340
-
341
- desc "Loads YAML fixture data"
342
- task :data_load do
343
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:data-load --env=#{symfony_env_prod}"
344
- end
345
-
346
- desc "Loads YAML fixture data without remove"
347
- task :data_load_append do
348
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:data-load --append --env=#{symfony_env_prod}"
349
- end
350
-
351
- desc "Migrates database to current version"
352
- task :migrate do
353
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:migrate --env=#{symfony_env_prod}"
354
- end
355
-
356
- desc "Generate model lib form and filters classes based on your schema"
357
- task :build_classes do
358
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:build --all-classes --env=#{symfony_env_prod}"
359
- end
360
-
361
- desc "Generate code & database based on your schema"
362
- task :build_all do
363
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database? (y/N)")
364
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:build --all --no-confirmation --env=#{symfony_env_prod}"
365
- end
366
- end
367
-
368
- desc "Generate code & database based on your schema & load fixtures"
369
- task :build_all_and_load do
370
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database and load #{symfony_env_prod}'s fixtures? (y/N)")
371
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:build --all --and-load --no-confirmation --env=#{symfony_env_prod}"
372
- end
373
- end
374
-
375
- desc "Generate sql & database based on your schema"
376
- task :build_db do
377
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database? (y/N)")
378
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:build --sql --db --no-confirmation --env=#{symfony_env_prod}"
379
- end
380
- end
381
-
382
- desc "Generate sql & database based on your schema & load fixtures"
383
- task :build_db_and_load do
384
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database and load #{symfony_env_prod}'s fixtures? (y/N)")
385
- run "cd #{latest_release} && #{php_bin} ./symfony doctrine:build --sql --db --and-load --no-confirmation --env=#{symfony_env_prod}"
386
- end
387
- end
388
- end
389
-
390
- namespace :propel do
391
- desc "Ensure Propel is correctly configured"
392
- task :setup do
393
- conf_files_exists = capture("if test -s #{shared_path}/config/propel.ini -a -s #{shared_path}/config/databases.yml ; then echo 'exists' ; fi").strip
394
-
395
- # share childs again (for propel.ini file)
396
- shared_files << "config/propel.ini"
397
- deploy.share_childs
398
-
399
- if (!conf_files_exists.eql?("exists"))
400
- run "cp #{symfony_lib}/plugins/sfPropelPlugin/config/skeleton/config/propel.ini #{shared_path}/config/propel.ini"
401
- symfony.configure.database
402
- end
403
- end
404
-
405
- desc "Migrates database to current version"
406
- task :migrate do
407
- puts "propel doesn't have built-in migration for now"
408
- end
409
-
410
- desc "Generate model lib form and filters classes based on your schema"
411
- task :build_classes do
412
- run "php #{latest_release}/symfony propel:build --model --env=#{symfony_env_prod}"
413
- run "php #{latest_release}/symfony propel:build --forms --env=#{symfony_env_prod}"
414
- run "php #{latest_release}/symfony propel:build --filters --env=#{symfony_env_prod}"
415
- end
416
-
417
- desc "Generate code & database based on your schema"
418
- task :build_all do
419
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database? (y/N)")
420
- run "cd #{latest_release} && #{php_bin} ./symfony propel:build --sql --db --no-confirmation --env=#{symfony_env_prod}"
421
- end
422
- end
423
-
424
- desc "Generate code & database based on your schema & load fixtures"
425
- task :build_all_and_load do
426
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database and load #{symfony_env_prod}'s fixtures? (y/N)")
427
- run "cd #{latest_release} && #{php_bin} ./symfony propel:build --sql --db --and-load --no-confirmation --env=#{symfony_env_prod}"
428
- end
429
- end
430
-
431
- desc "Generate sql & database based on your schema"
432
- task :build_db do
433
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database? (y/N)")
434
- run "cd #{latest_release} && #{php_bin} ./symfony propel:build --sql --db --no-confirmation --env=#{symfony_env_prod}"
435
- end
436
- end
437
-
438
- desc "Generate sql & database based on your schema & load fixtures"
439
- task :build_db_and_load do
440
- if Capistrano::CLI.ui.agree("Do you really want to rebuild #{symfony_env_prod}'s database and load #{symfony_env_prod}'s fixtures? (y/N)")
441
- run "cd #{latest_release} && #{php_bin} ./symfony propel:build --sql --db --and-load --no-confirmation --env=#{symfony_env_prod}"
442
- end
443
- end
444
- end
445
- end
446
-
447
- namespace :database do
448
- namespace :dump do
449
- desc "Dump remote database"
450
- task :remote do
451
- filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.gz"
452
- file = "/tmp/#{filename}"
453
- sqlfile = "#{application}_dump.sql"
454
- config = ""
455
-
456
- run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
457
- config = load_database_config data, symfony_env_prod
458
- end
459
-
460
- case config['type']
461
- when 'mysql'
462
- run "mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | gzip -c > #{file}" do |ch, stream, data|
463
- puts data
464
- end
465
- when 'pgsql'
466
- run "pg_dump -U #{config['user']} #{config['db']} | gzip -c > #{file}" do |ch, stream, data|
467
- puts data
468
- end
469
- end
470
-
471
- require "fileutils"
472
- FileUtils.mkdir_p("backups")
473
- get file, "backups/#{filename}"
474
- begin
475
- FileUtils.ln_sf(filename, "backups/#{application}.remote_dump.latest.sql.gz")
476
- rescue NotImplementedError # hack for windows which doesnt support symlinks
477
- FileUtils.cp_r("backups/#{filename}", "backups/#{application}.remote_dump.latest.sql.gz")
478
- end
479
- run "rm #{file}"
480
- end
481
-
482
- desc "Dump local database"
483
- task :local do
484
- filename = "#{application}.local_dump.#{Time.now.to_i}.sql.gz"
485
- tmpfile = "backups/#{application}_dump_tmp.sql"
486
- file = "backups/#{filename}"
487
- config = load_database_config IO.read('config/databases.yml'), symfony_env_local
488
- sqlfile = "#{application}_dump.sql"
489
-
490
- require "fileutils"
491
- FileUtils::mkdir_p("backups")
492
- case config['type']
493
- when 'mysql'
494
- `mysqldump -u#{config['user']} --password=\"#{config['pass']}\" #{config['db']} > #{tmpfile}`
495
- when 'pgsql'
496
- `pg_dump -U #{config['user']} #{config['db']} > #{tmpfile}`
497
- end
498
- File.open(tmpfile, "r+") do |f|
499
- gz = Zlib::GzipWriter.open(file)
500
- while (line = f.gets)
501
- gz << line
502
- end
503
- gz.flush
504
- gz.close
505
- end
506
-
507
- begin
508
- FileUtils.ln_sf(filename, "backups/#{application}.local_dump.latest.sql.gz")
509
- rescue NotImplementedError # hack for windows which doesnt support symlinks
510
- FileUtils.cp_r("backups/#{filename}", "backups/#{application}.local_dump.latest.sql.gz")
511
- end
512
- FileUtils.rm(tmpfile)
513
- end
514
- end
515
-
516
- namespace :move do
517
- desc "Dump remote database, download it to local & populate here"
518
- task :to_local do
519
- filename = "#{application}.remote_dump.latest.sql.gz"
520
- config = load_database_config IO.read('config/databases.yml'), symfony_env_local
521
- sqlfile = "#{application}_dump.sql"
522
-
523
- database.dump.remote
524
-
525
- require "fileutils"
526
- f = File.new("backups/#{sqlfile}", "a+")
527
- require "zlib"
528
- gz = Zlib::GzipReader.new(File.open("backups/#{filename}", "r"))
529
- f << gz.read
530
- f.close
531
-
532
- case config['type']
533
- when 'mysql'
534
- `mysql -u#{config['user']} --password=\"#{config['pass']}\" #{config['db']} < backups/#{sqlfile}`
535
- when 'pgsql'
536
- `psql -U #{config['user']} --password=\"#{config['pass']}\" #{config['db']} < backups/#{sqlfile}`
537
- end
538
- FileUtils.rm("backups/#{sqlfile}")
539
- end
540
-
541
- desc "Dump local database, load it to remote & populate there"
542
- task :to_remote do
543
- filename = "#{application}.local_dump.latest.sql.gz"
544
- file = "backups/#{filename}"
545
- sqlfile = "#{application}_dump.sql"
546
- config = ""
547
-
548
- database.dump.local
549
-
550
- upload(file, "/tmp/#{filename}", :via => :scp)
551
- run "gunzip -c /tmp/#{filename} > /tmp/#{sqlfile}"
552
-
553
- run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
554
- config = load_database_config data, symfony_env_prod
555
- end
556
-
557
- case config['type']
558
- when 'mysql'
559
- run "mysql -u#{config['user']} --password='#{config['pass']}' #{config['db']} < /tmp/#{sqlfile}" do |ch, stream, data|
560
- puts data
561
- end
562
- when 'pgsql'
563
- run "psql -U #{config['user']} --password='#{config['pass']}' #{config['db']} < /tmp/#{sqlfile}" do |ch, stream, data|
564
- puts data
565
- end
566
- end
567
-
568
- run "rm /tmp/#{filename}"
569
- run "rm /tmp/#{sqlfile}"
570
- end
571
- end
572
- end
573
-
574
- namespace :shared do
575
- namespace :databases do
576
- desc "Download config/databases.yml from remote server"
577
- task :to_local do
578
- download("#{shared_path}/config/databases.yml", "config/databases.yml", :via => :scp)
579
- end
580
-
581
- desc "Upload config/databases.yml to remote server"
582
- task :to_remote do
583
- upload("config/databases.yml", "#{shared_path}/config/databases.yml", :via => :scp)
584
- end
585
- end
586
-
587
- namespace :log do
588
- desc "Download all logs from remote folder to local one"
589
- task :to_local do
590
- download("#{shared_path}/log", "./", :via => :scp, :recursive => true)
591
- end
592
-
593
- desc "Upload all logs from local folder to remote one"
594
- task :to_remote do
595
- upload("log", "#{shared_path}/", :via => :scp, :recursive => true)
596
- end
597
- end
598
-
599
- namespace :uploads do
600
- desc "Download all files from remote web/uploads folder to local one"
601
- task :to_local do
602
- download("#{shared_path}/web/uploads", "web", :via => :scp, :recursive => true)
603
- end
604
-
605
- desc "Upload all files from local web/uploads folder to remote one"
606
- task :to_remote do
607
- upload("web/uploads", "#{shared_path}/web", :via => :scp, :recursive => true)
608
- end
609
- end
610
-
611
- namespace :symfony do
612
- desc "Downloads symfony framework to shared directory"
613
- task :download do
614
- prompt_with_default(:version, symfony_version)
615
-
616
- run <<-CMD
617
- if [ ! -d #{shared_path}/symfony-#{version} ]; then
618
- wget -q http://www.symfony-project.org/get/symfony-#{version}.tgz -O- | tar -zxf - -C #{shared_path};
619
- fi
620
- CMD
621
- end
622
- end
85
+ def generate_sql_command(cmd_type, config)
86
+ db_type = config['type']
87
+ cmd_conf = {
88
+ 'mysql' => {
89
+ 'create' => "mysqladmin -u #{config['user']} --password='#{config['pass']}' create",
90
+ 'dump' => "mysqldump -u #{config['user']} --password='#{config['pass']}'",
91
+ 'drop' => "mysqladmin -f -u #{config['user']} --password='#{config['pass']}' drop",
92
+ 'import' => "mysql -u #{config['user']} --password='#{config['pass']}'"
93
+ },
94
+ 'pgsql' => {
95
+ 'create' => "createdb -U #{config['user']}",
96
+ 'dump' => "pg_dump -U #{config['user']}",
97
+ 'drop' => "dropdb -U #{config['user']}",
98
+ 'import' => "psql -U #{config['user']} --password='#{config['pass']}'"
99
+ }
100
+ }
101
+
102
+ cmd = cmd_conf[db_type][cmd_type]
103
+ cmd+= " --host=#{config['host']}" if config['host']
104
+ cmd+= " --port=#{config['port']}" if config['port']
105
+ cmd+= " #{config['db']}"
106
+
107
+ cmd
623
108
  end
624
109
 
625
110
  # After setup