Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason Harrelson
committed
Nov 10, 2010
1 parent
6edcc6a
commit 9159d5f
Showing
7 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rvm use ruby-1.8.7-p302@gemdev | ||
rvm info ruby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module NinjaDeploy | ||
VERSION = File.read( "#{File.dirname __FILE__}/../VERSION" ).chomp | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
Capistrano::Configuration.instance( :must_exist ).load do | ||
namespace :db do | ||
namespace :dump do | ||
desc 'Dump a remote database, fetch the dump file to local and then load it to the development database' | ||
task :to_local_db, :roles => :db, :only => { :primary => true } do | ||
local_file_full_path = dump_database_to_local | ||
|
||
puts "*** Reading local database config..." | ||
username, password, database, host = database_config( 'development' ) | ||
|
||
puts "*** Loading data to local #{database} database" | ||
system "bzip2 -d -c #{local_file_full_path} | mysql -u #{username} --password='#{password}' #{database}" | ||
|
||
puts "*** Removing local dump file..." | ||
system "rm #{local_file_full_path}" | ||
end | ||
|
||
desc 'Dump a remote database and fetch the dump file to local /tmp directory' | ||
task :to_local_file, :roles => :db, :only => { :primary => true } do | ||
local_file_full_path = dump_database_to_local | ||
puts "*** You can find the dump file at: #{local_file_full_path}" | ||
end | ||
|
||
# desc "Dump a remote database, fetch the dump file to the staging server and then load it to the staging database" | ||
# task :to_staging, :roles => :db, :only => { :primary => true } do | ||
# | ||
# end | ||
end | ||
|
||
def dump_database_to_local | ||
prepare_for_database_dump | ||
|
||
puts "*** Reading database credentials... " | ||
user, password, database, host = remote_database_config( rails_env ) | ||
|
||
dump_database( password ) | ||
|
||
local_file_full_path = "/tmp/#{File.basename dump_file_bz2_full_path}" | ||
puts "*** Fetching the dump file from '#{dump_file_bz2_full_path}' and putting it at '#{local_file_full_path}'..." | ||
get dump_file_bz2_full_path, local_file_full_path | ||
|
||
remove_dump_file | ||
|
||
local_file_full_path | ||
end | ||
|
||
def prepare_for_database_dump | ||
now = Time.now | ||
run "mkdir -p #{shared_path}/db_backups" | ||
dump_time = [now.year, now.month, now.day, now.hour, now.min, now.sec].join( '' ) | ||
set :dump_file, "#{environment_database}-dump-#{dump_time}.dmp" | ||
set :dump_file_path, "#{shared_path}/db_backups" | ||
set :dump_file_full_path, "#{dump_file_path}/#{dump_file}" | ||
set :dump_file_bz2_full_path, "#{dump_file_full_path}.bz2" | ||
end | ||
|
||
def dump_database( password ) | ||
puts "*** Dumping #{environment_database} database..." | ||
run "mysqldump --add-drop-table -u #{environment_db_user} -h #{environment_db_host.gsub('-master', '-replica')} -p#{password} #{environment_database} | bzip2 -c > #{dump_file_bz2_full_path}" | ||
end | ||
|
||
def remove_dump_file | ||
puts "*** Removing the dump file from the server..." | ||
run "rm #{dump_file_bz2_full_path}" | ||
end | ||
|
||
# Reads the database credentials from the local config/database.yml file | ||
# +db+ the name of the environment to get the credentials for | ||
# Returns username, password, database | ||
# | ||
def database_config( db ) | ||
database = YAML::load_file( 'config/database.yml' ) | ||
return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] | ||
end | ||
|
||
# Reads the database credentials from the remote config/database.yml file | ||
# +db+ the name of the environment to get the credentials for | ||
# Returns username, password, database | ||
# | ||
def remote_database_config( db ) | ||
remote_config = capture("cat #{shared_path}/config/database.yml") | ||
database = YAML::load( remote_config ) | ||
return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Generated by jeweler | ||
# DO NOT EDIT THIS FILE DIRECTLY | ||
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |s| | ||
s.name = %q{ninja-deploy} | ||
s.version = "0.1.0" | ||
|
||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | ||
s.authors = ["Ninja Loss"] | ||
s.date = %q{2010-11-09} | ||
s.description = %q{Common shared deployment recipes for your pleasure.} | ||
s.email = %q{ninja.loss@gmail.com} | ||
s.extra_rdoc_files = [ | ||
"LICENSE", | ||
"README.rdoc" | ||
] | ||
s.files = [ | ||
".document", | ||
".gitignore", | ||
".rvmrc", | ||
"LICENSE", | ||
"README.rdoc", | ||
"Rakefile", | ||
"VERSION", | ||
"lib/ninja_deploy.rb", | ||
"lib/ninja_deploy/recipes/database.rb", | ||
"ninja-deploy.gemspec", | ||
"spec/ninja-deploy_spec.rb", | ||
"spec/spec.opts", | ||
"spec/spec_helper.rb" | ||
] | ||
s.homepage = %q{http://github.com/ninja-loss/ninja-deploy} | ||
s.rdoc_options = ["--charset=UTF-8"] | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = %q{1.3.7} | ||
s.summary = %q{Common shared deployment recipes.} | ||
s.test_files = [ | ||
"spec/ninja-deploy_spec.rb", | ||
"spec/spec_helper.rb" | ||
] | ||
|
||
if s.respond_to? :specification_version then | ||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION | ||
s.specification_version = 3 | ||
|
||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then | ||
s.add_development_dependency(%q<rspec>, [">= 1.2.9"]) | ||
s.add_development_dependency(%q<yard>, [">= 0"]) | ||
else | ||
s.add_dependency(%q<rspec>, [">= 1.2.9"]) | ||
s.add_dependency(%q<yard>, [">= 0"]) | ||
end | ||
else | ||
s.add_dependency(%q<rspec>, [">= 1.2.9"]) | ||
s.add_dependency(%q<yard>, [">= 0"]) | ||
end | ||
end | ||
|