capcake 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/capcake.rb CHANGED
@@ -32,7 +32,7 @@ Capistrano::Configuration.instance(:must_exist).load do
32
32
  set :git_enable_submodules, 1
33
33
  set :revision, source.head
34
34
  set :deploy_via, :checkout
35
- set :shared_children, %w(system tmp)
35
+ set :shared_children, %w(config system tmp)
36
36
 
37
37
  set :git_flag_quiet, ""
38
38
 
@@ -45,11 +45,24 @@ Capistrano::Configuration.instance(:must_exist).load do
45
45
  def capcake()
46
46
  set :deploy_to, "/var/www/#{application}" if (deploy_to.empty?)
47
47
  set(:current_path) { File.join(deploy_to, current_dir) }
48
+ set(:database_path) { File.join(File.join(shared_path, "config"), "database.php") }
48
49
  set(:shared_path) { File.join(deploy_to, shared_dir) }
49
50
  _cset(:cake_path) { shared_path }
50
51
  _cset(:tmp_path) { File.join(shared_path, "tmp") }
51
52
  _cset(:cache_path) { File.join(tmp_path, "cache") }
52
53
  _cset(:logs_path) { File.join(tmp_path, "logs") }
54
+
55
+ after("deploy:setup", "cake:database:config") if (!remote_file_exists?(database_path))
56
+ after("deploy:symlink", "cake:database:symlink") if (remote_file_exists?(database_path))
57
+ end
58
+
59
+ def defaults(val, default)
60
+ val = default if (val.empty?)
61
+ val
62
+ end
63
+
64
+ def remote_file_exists?(full_path)
65
+ 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
53
66
  end
54
67
 
55
68
  # =========================================================================
@@ -147,7 +160,7 @@ Capistrano::Configuration.instance(:must_exist).load do
147
160
  by putting each new release of your application in its own directory. When \
148
161
  you deploy a new version, this task's job is to update the `current', \
149
162
  `current/tmp', `current/webroot/system' symlinks to point at the new version. \
150
-
163
+
151
164
  You will rarely need to call this task directly; instead, use the `deploy' \
152
165
  task (which performs a complete deploy, including `restart') or the 'update' \
153
166
  task (which does everything except `restart').
@@ -449,6 +462,72 @@ Capistrano::Configuration.instance(:must_exist).load do
449
462
  end
450
463
  end
451
464
 
465
+ namespace :database do
466
+ desc <<-DESC
467
+ Generates CakePHP database configuration file in #{shared_path}/config \
468
+ and symlinks #{current_path}/config/database.php to it
469
+ DESC
470
+ task :config, :roles => :web, :except => { :no_release => true } do
471
+ require 'erb'
472
+ on_rollback { run "rm #{database_path}" }
473
+ puts "Database configuration"
474
+ _cset :db_driver, defaults(Capistrano::CLI.ui.ask("driver [mysql]:"), 'mysql')
475
+ _cset :db_host, defaults(Capistrano::CLI.ui.ask("hostname [localhost]:"), 'localhost')
476
+ _cset :db_login, defaults(Capistrano::CLI.ui.ask("username [#{user}]:"), user)
477
+ _cset :db_password, Capistrano::CLI.password_prompt("password:")
478
+ _cset :db_name, defaults(Capistrano::CLI.ui.ask("db name [#{application}]:"), application)
479
+ _cset :db_prefix, Capistrano::CLI.ui.ask("prefix:")
480
+ _cset :db_persistent, defaults(Capistrano::CLI.ui.ask("persistent [false]:"), 'false')
481
+ _cset :db_encoding, defaults(Capistrano::CLI.ui.ask("encoding [utf8]:"), 'utf8')
482
+
483
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "database.rphp"))
484
+ result = ERB.new(template).result(binding)
485
+
486
+ put(result, "#{database_path}", :mode => 0644, :via => :scp)
487
+ after("deploy:symlink", "cake:database:symlink")
488
+ end
489
+ desc <<-DESC
490
+ Creates MySQL database, database user and grants permissions on DB servers
491
+ DESC
492
+ task :create, :roles => :db, :except => { :no_releases => true } do
493
+ require 'erb'
494
+
495
+ _cset :mysql_admin_user, defaults(Capistrano::CLI.ui.ask("username [root]:"), 'root')
496
+ _cset :mysql_admin_password, Capistrano::CLI.password_prompt("password:")
497
+
498
+ _cset :mysql_grant_priv_type, defaults(Capistrano::CLI.ui.ask("Grant privilege types:"), 'ALL')
499
+ _cset :mysql_grant_locations, defaults(Capistrano::CLI.ui.ask("Grant locations:"), ["localhost"])
500
+
501
+ _cset :db_login, defaults(Capistrano::CLI.ui.ask("username [#{user}]:"), user)
502
+ _cset :db_password, Capistrano::CLI.password_prompt("password:")
503
+ _cset :db_name, defaults(Capistrano::CLI.ui.ask("db name [#{application}]:"), application)
504
+ _cset :db_encoding, defaults(Capistrano::CLI.ui.ask("encoding [utf8]:"), 'utf8')
505
+
506
+ set :tmp_filename, File.join(shared_path, "config/create_db_#{db_name}.sql")
507
+
508
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "create_database.rsql"))
509
+ result = ERB.new(template).result(binding)
510
+
511
+ put(result, "#{tmp_filename}", :mode => 0644, :via => :scp)
512
+
513
+ run "mysql -u #{mysql_admin_user} -p#{mysql_admin_password} < #{tmp_filename}"
514
+ run "#{try_sudo} rm #{tmp_filename}"
515
+ end
516
+ desc <<-DESC
517
+ Creates database tables on primary DB servers
518
+ DESC
519
+ task :schema, :roles => :db, :primary => true, :except => { :no_releases => true } do
520
+ # ...
521
+ end
522
+ desc <<-DESC
523
+ Creates required CakePHP's APP/config/database.php as a symlink to \
524
+ #{deploy_to}/shared/config/database.php
525
+ DESC
526
+ task :symlink, :roles => :web, :except => { :no_release => true } do
527
+ run "#{try_sudo} ln -s #{database_path} #{current_path}/config/database.php"
528
+ end
529
+ end
530
+
452
531
  namespace :logs do
453
532
  desc <<-DESC
454
533
  Clears CakePHP's APP/tmp/logs and its sub-directories
@@ -0,0 +1,6 @@
1
+ <% mysql_grant_locations.each do |location| %>GRANT <%= mysql_grant_priv_type %> ON <%= db_name %>.* TO '<%= db_login %>'@'<%= location %>' IDENTIFIED BY '<%= db_password %>';
2
+ <% end %>
3
+
4
+ CREATE DATABASE IF NOT EXISTS <%= db_name %> CHARACTER SET = <%= db_encoding %>;
5
+
6
+ FLUSH PRIVILEGES;
@@ -0,0 +1,14 @@
1
+ <?php
2
+ class DATABASE_CONFIG {
3
+ public $default = array(
4
+ 'driver' => '<%= db_driver %>',
5
+ 'persistent' => '<%= db_persistent %>',
6
+ 'host' => '<%= db_host %>',
7
+ 'login' => '<%= db_login %>',
8
+ 'password' => '<%= db_password %>',
9
+ 'database' => '<%= db_name %>',
10
+ 'prefix' => '<%= db_prefix %>',
11
+ 'encoding' => '<%= db_encoding %>',
12
+ );
13
+ }
14
+ ?>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capcake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jad Bitar
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-13 00:00:00 -05:00
12
+ date: 2010-01-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,8 @@ extra_rdoc_files:
34
34
  files:
35
35
  - lib/TODO
36
36
  - lib/capcake.rb
37
+ - lib/templates/create_database.rsql
38
+ - lib/templates/database.rphp
37
39
  - lib/templates/maintenance.rhtml
38
40
  - LICENSE
39
41
  - README.markdown