Skip to content

Commit

Permalink
added confirmations for database tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Thomas committed Apr 17, 2015
1 parent 33b2df5 commit ac0abac
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
4 changes: 2 additions & 2 deletions config/database.example.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
staging:
production:
host: localhost
database: db_name
username: db_user
password: 'db_pass'
production:
staging:
host: localhost
database: db_name
username: db_user
Expand Down
19 changes: 10 additions & 9 deletions config/templates/wp-config.php.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
define('DB_NAME', '<%= database['database'] %>');
define('DB_USER', '<%= database['username'] %>');
define('DB_PASSWORD', '<%= database['password'] %>');
define('DB_HOST', '<%= database['host'] %>');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('WPLANG', '');
define('DB_NAME', '<%= database['database'] %>');
define('DB_USER', '<%= database['username'] %>');
define('DB_PASSWORD', '<%= database['password'] %>');
define('DB_HOST', '<%= database['host'] %>');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('WPLANG', '');

$table_prefix = 'wp_';

Expand All @@ -25,7 +25,8 @@
// define('WP_CONTENT_DIR', realpath(dirname(__FILE__) . '/content'));
define('WP_CONTENT_DIR', realpath($_SERVER['DOCUMENT_ROOT'] . '/content'));

if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
if ( !defined('ABSPATH') ){
define('ABSPATH', dirname(__FILE__) . '/');
}

require_once(ABSPATH . 'wp-settings.php');
75 changes: 65 additions & 10 deletions lib/capistrano/tasks/db.cap
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,71 @@ namespace :db do
end
end

desc "Confirms the database action before proceeeding"
task :confirm do
on roles(:web) do

database = YAML::load_file('config/database.yml')[fetch(:stage).to_s]

set :confirmed, proc {
puts <<-WARN
\033[31m
========================================================================

WARNING: You're about to overwrite the production database!
To continue, please enter the name of the database for this site.

Datebase name:\033[0m \033[1m \033[34m #{database['database']} \033[0m \033[22m \033[31m

========================================================================
\033[0m
WARN
ask :answer, database['database']
if fetch(:answer) == database['database'] then
true
else
loopCount = 1
loop do
loopCount = loopCount + 1
puts "\033[31mYou typed the database name incorrectly. Please enter \033[0m\033[1m\033[34m#{database['database']}\033[0m\033[22m\033[0m\033[0m"
ask :answer, database['database']
break if loopCount == 3
break if fetch(:answer) == database['database']
end
end

if fetch(:answer) == database['database'] then
true
end
}.call

unless fetch(:confirmed)
puts <<-WARN
\033[31m
========================================================================
Sorry, you have entered the database name incorrectly too many times
========================================================================
\033[0m
WARN
exit
end

end
end

desc "Takes a database dump from remote server"
task :backup do
invoke 'db:backup_name'
on roles(:db) do
within release_path do
on roles(:db) do

within release_path do
execute :wp, "db export #{fetch(:backup_file)} --add-drop-table"
end

system('mkdir -p db_backups')
download! "#{fetch(:backup_file)}", "db_backups/#{fetch(:backup_filename)}.sql"

within release_path do
within release_path do
execute :rm, "#{fetch(:backup_file)}"
end

Expand All @@ -35,9 +87,9 @@ namespace :db do
desc "Imports the remote database into your local environment"
task :pull do
invoke 'db:backup'
on roles(:db) do

on roles(:db) do

run_locally do
execute :wp, "db import db_backups/#{fetch(:backup_filename)}.sql"
execute :wp, "search-replace #{fetch(:stage_url)} #{fetch(:wp_localurl)}"
Expand All @@ -54,10 +106,13 @@ namespace :db do

desc "Imports the local database into your remote environment"
task :push do

invoke 'db:confirm'

invoke 'db:backup_name'
on roles(:db) do
run_locally do
on roles(:db) do

run_locally do
execute :mkdir, "-p db_backups"
execute :wp, "db export db_backups/#{fetch(:backup_filename)}.sql --add-drop-table"
end
Expand Down
17 changes: 9 additions & 8 deletions lib/capistrano/tasks/wp.cap
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ namespace :wp do

desc "Setup WP on remote environment"
task :remote do
invoke 'db:confirm'
invoke 'deploy'
invoke 'wp:setup:generate_remote_files'
on roles(:web) do

within release_path do

if !fetch(:setup_all)
# Generate a random password
o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
Expand All @@ -62,7 +63,7 @@ namespace :wp do
\e[32m
=========================================================================
WordPress has successfully been installed. Here are your login details:

Username: #{user}
Password: #{password}
Email address: #{email}
Expand All @@ -76,10 +77,10 @@ namespace :wp do

end
end

desc "Setup WP on local environment"
task :local do

run_locally do

if !fetch(:setup_all)
Expand All @@ -89,7 +90,7 @@ namespace :wp do
else
password = fetch(:wp_pass)
end

# Get WP details from config in /config
title = fetch(:wp_sitename)
email = fetch(:wp_email)
Expand All @@ -109,14 +110,14 @@ namespace :wp do
\e[32m
=========================================================================
WordPress has successfully been installed. Here are your login details:

Username: #{user}
Password: #{password}
Email address: #{email}
=========================================================================
\e[0m
MSG

end
end

Expand Down

0 comments on commit ac0abac

Please sign in to comment.