lib/capcake.rb in capcake-0.0.3 vs lib/capcake.rb in capcake-0.0.4
- old
+ new
@@ -30,11 +30,11 @@
set :scm, :git
set :git_enable_submodules, 1
set :revision, source.head
set :deploy_via, :checkout
- set :shared_children, %w(system tmp)
+ set :shared_children, %w(config system tmp)
set :git_flag_quiet, ""
_cset(:cake_branch) { "" }
_cset(:cake_repo) { "http://github.com/cakephp/cakephp1x.git" }
@@ -43,17 +43,30 @@
_cset :logs_files, %w(debug error)
def capcake()
set :deploy_to, "/var/www/#{application}" if (deploy_to.empty?)
set(:current_path) { File.join(deploy_to, current_dir) }
+ set(:database_path) { File.join(File.join(shared_path, "config"), "database.php") }
set(:shared_path) { File.join(deploy_to, shared_dir) }
_cset(:cake_path) { shared_path }
_cset(:tmp_path) { File.join(shared_path, "tmp") }
_cset(:cache_path) { File.join(tmp_path, "cache") }
_cset(:logs_path) { File.join(tmp_path, "logs") }
+
+ after("deploy:setup", "cake:database:config") if (!remote_file_exists?(database_path))
+ after("deploy:symlink", "cake:database:symlink") if (remote_file_exists?(database_path))
end
+ def defaults(val, default)
+ val = default if (val.empty?)
+ val
+ end
+
+ def remote_file_exists?(full_path)
+ 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
+ end
+
# =========================================================================
# These are the tasks that are available to help with deploying web apps,
# and specifically, Rails applications. You can have cap give you a summary
# of them with `cap -T'.
# =========================================================================
@@ -145,11 +158,11 @@
desc <<-DESC
Updates the symlinks to the most recently deployed version. Capistrano works \
by putting each new release of your application in its own directory. When \
you deploy a new version, this task's job is to update the `current', \
`current/tmp', `current/webroot/system' symlinks to point at the new version. \
-
+
You will rarely need to call this task directly; instead, use the `deploy' \
task (which performs a complete deploy, including `restart') or the 'update' \
task (which does everything except `restart').
DESC
task :symlink, :except => { :no_release => true } do
@@ -444,9 +457,75 @@
You will rarely need to call this task directly; instead, use the `deploy' \
task (which performs a complete deploy, including `cake:cache:clear')
DESC
task :clear, :roles => :web, :except => { :no_release => true } do
run "#{try_sudo} find -P #{cache_path} -ignore_readdir_race -type f -name '*' -exec rm -f {} \\;"
+ end
+ end
+
+ namespace :database do
+ desc <<-DESC
+ Generates CakePHP database configuration file in #{shared_path}/config \
+ and symlinks #{current_path}/config/database.php to it
+ DESC
+ task :config, :roles => :web, :except => { :no_release => true } do
+ require 'erb'
+ on_rollback { run "rm #{database_path}" }
+ puts "Database configuration"
+ _cset :db_driver, defaults(Capistrano::CLI.ui.ask("driver [mysql]:"), 'mysql')
+ _cset :db_host, defaults(Capistrano::CLI.ui.ask("hostname [localhost]:"), 'localhost')
+ _cset :db_login, defaults(Capistrano::CLI.ui.ask("username [#{user}]:"), user)
+ _cset :db_password, Capistrano::CLI.password_prompt("password:")
+ _cset :db_name, defaults(Capistrano::CLI.ui.ask("db name [#{application}]:"), application)
+ _cset :db_prefix, Capistrano::CLI.ui.ask("prefix:")
+ _cset :db_persistent, defaults(Capistrano::CLI.ui.ask("persistent [false]:"), 'false')
+ _cset :db_encoding, defaults(Capistrano::CLI.ui.ask("encoding [utf8]:"), 'utf8')
+
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "database.rphp"))
+ result = ERB.new(template).result(binding)
+
+ put(result, "#{database_path}", :mode => 0644, :via => :scp)
+ after("deploy:symlink", "cake:database:symlink")
+ end
+ desc <<-DESC
+ Creates MySQL database, database user and grants permissions on DB servers
+ DESC
+ task :create, :roles => :db, :except => { :no_releases => true } do
+ require 'erb'
+
+ _cset :mysql_admin_user, defaults(Capistrano::CLI.ui.ask("username [root]:"), 'root')
+ _cset :mysql_admin_password, Capistrano::CLI.password_prompt("password:")
+
+ _cset :mysql_grant_priv_type, defaults(Capistrano::CLI.ui.ask("Grant privilege types:"), 'ALL')
+ _cset :mysql_grant_locations, defaults(Capistrano::CLI.ui.ask("Grant locations:"), ["localhost"])
+
+ _cset :db_login, defaults(Capistrano::CLI.ui.ask("username [#{user}]:"), user)
+ _cset :db_password, Capistrano::CLI.password_prompt("password:")
+ _cset :db_name, defaults(Capistrano::CLI.ui.ask("db name [#{application}]:"), application)
+ _cset :db_encoding, defaults(Capistrano::CLI.ui.ask("encoding [utf8]:"), 'utf8')
+
+ set :tmp_filename, File.join(shared_path, "config/create_db_#{db_name}.sql")
+
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "create_database.rsql"))
+ result = ERB.new(template).result(binding)
+
+ put(result, "#{tmp_filename}", :mode => 0644, :via => :scp)
+
+ run "mysql -u #{mysql_admin_user} -p#{mysql_admin_password} < #{tmp_filename}"
+ run "#{try_sudo} rm #{tmp_filename}"
+ end
+ desc <<-DESC
+ Creates database tables on primary DB servers
+ DESC
+ task :schema, :roles => :db, :primary => true, :except => { :no_releases => true } do
+ # ...
+ end
+ desc <<-DESC
+ Creates required CakePHP's APP/config/database.php as a symlink to \
+ #{deploy_to}/shared/config/database.php
+ DESC
+ task :symlink, :roles => :web, :except => { :no_release => true } do
+ run "#{try_sudo} ln -s #{database_path} #{current_path}/config/database.php"
end
end
namespace :logs do
desc <<-DESC
\ No newline at end of file