class Object
Public Instance Methods
NinjaDeploy( &block )
click to toggle source
# File lib/ninja_deploy.rb, line 19 def NinjaDeploy( &block ) Cape( &block ) end
database_config( db )
click to toggle source
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
# File lib/ninja_deploy/recipes/database.rb, line 130 def database_config( db ) database = local_database_config_hash return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] end
dump_database( password )
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 96 def dump_database( password ) puts "*** Dumping #{environment_database} database..." run dump_database_cmd( password ) end
dump_database_cmd( password )
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 101 def dump_database_cmd( password ) "mysqldump --add-drop-table -u #{environment_db_user} -h #{environment_db_host.gsub('-master', '-replica')} -p#{password} #{environment_database} #{tables_to_dump} | bzip2 -c > #{dump_file_bz2_full_path}" end
dump_database_to_local()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 45 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
dump_database_to_staging()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 62 def dump_database_to_staging prepare_for_database_dump puts "*** Reading database credentials... " user, password, database, host = remote_database_config( rails_env ) dump_database( password ) staging_file_full_path = "/tmp/#{File.basename dump_file_bz2_full_path}" puts "*** Fetching the dump file from '#{environment_host}:#{dump_file_bz2_full_path}' and putting it at '#{staging_host}:#{staging_file_full_path}'..." scp_cmd = "scp #{environment_host}:#{dump_file_bz2_full_path} #{staging_file_full_path}" puts "*** Executing: #{scp_cmd}" Net::SSH.start( staging_host, 'deployer' ) do |ssh| ssh.exec! scp_cmd end remove_dump_file staging_file_full_path end
host_database_config( host, user, db )
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 145 def host_database_config( host, user, db ) remote_config = nil Net::SSH.start( host, user ) do |ssh| remote_config = ssh.exec!( "cat #{shared_path}/config/database.yml" ) end database = YAML::load( remote_config ) return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] end
local_database_config_hash()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 122 def local_database_config_hash YAML::load_file( 'config/database.yml' ) end
prepare_for_database_dump()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 86 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
rake(*tasks)
click to toggle source
# File lib/ninja_deploy/recipes/thinking_sphinx.rb, line 24 def rake(*tasks) rails_env = fetch(:rails_env, "production") rake = fetch(:rake, "rake") tasks.each do |t| run "if [ -d #{release_path} ]; then cd #{release_path}; else cd #{current_path}; fi; #{rake} RAILS_ENV=#{rails_env} #{t}" end end
remote_database_config( db )
click to toggle source
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
# File lib/ninja_deploy/recipes/database.rb, line 139 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
remove_dump_file()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 117 def remove_dump_file puts "*** Removing the dump file from the server..." run "rm #{dump_file_bz2_full_path}" end
tables_to_dump()
click to toggle source
# File lib/ninja_deploy/recipes/database.rb, line 105 def tables_to_dump require 'rubygems' require 'active_record' except = ENV['EXCEPT'] except = except.nil? ? nil : Array( except.split( ',' ) ) ActiveRecord::Base.establish_connection( local_database_config_hash['development'] ) except.nil? ? nil : (ActiveRecord::Base.connection.tables - except).join( ' ' ) end