Skip to content

Commit

Permalink
Add some database recipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Harrelson committed Nov 10, 2010
1 parent 6edcc6a commit 9159d5f
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .rvmrc
@@ -0,0 +1,2 @@
rvm use ruby-1.8.7-p302@gemdev
rvm info ruby
4 changes: 2 additions & 2 deletions README.rdoc
@@ -1,9 +1,9 @@
= ninja-deploy

Description goes here.
Common shared deployment recipes.

== Note on Patches/Pull Requests

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -6,9 +6,9 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "ninja-deploy"
gem.summary = %Q{Common shared deployment recipes.}
gem.description = %Q{TODO: longer description of your gem}
gem.description = %Q{Common shared deployment recipes for your pleasure.}
gem.email = "ninja.loss@gmail.com"
gem.homepage = "http://github.com/midas/ninja-deploy"
gem.homepage = "http://github.com/ninja-loss/ninja-deploy"
gem.authors = ["Ninja Loss"]
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
Expand Down
Empty file removed lib/ninja-deploy.rb
Empty file.
3 changes: 3 additions & 0 deletions lib/ninja_deploy.rb
@@ -0,0 +1,3 @@
module NinjaDeploy
VERSION = File.read( "#{File.dirname __FILE__}/../VERSION" ).chomp
end
86 changes: 86 additions & 0 deletions lib/ninja_deploy/recipes/database.rb
@@ -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
60 changes: 60 additions & 0 deletions ninja-deploy.gemspec
@@ -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

0 comments on commit 9159d5f

Please sign in to comment.