class Object
Public Instance Methods
are_you_sure?()
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 14 def are_you_sure? ask("Holy Moly! Are you sure you want to do that? (type 'yes' if so)") == 'yes' end
ask(question)
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 18 def ask question Capistrano::CLI.ui.ask question end
close_sessions()
click to toggle source
disconnect all sessions
# File lib/georgia_recipes/helper_methods.rb, line 83 def close_sessions sessions.values.each { |session| session.close } sessions.clear end
create_db()
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 83 def create_db run %Q{#{sudo} -u postgres psql -c "create database #{remote_db_name} owner #{remote_db_user} ENCODING = 'UTF-8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8' TEMPLATE template0;"} end
create_user()
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 79 def create_user run %Q{#{sudo} -u postgres psql -c "create user #{remote_db_user} with password '#{remote_db_password}';"} end
database_filename()
click to toggle source
# File lib/georgia_recipes/mysql.rb, line 56 def database_filename @database_filename ||= "#{application}_#{timestamp}.sql" end
database_table_filename()
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 71 def database_table_filename @database_table_filename ||= "#{application}_#{table_name}_#{timestamp}.sql" end
git_pull_or_clone(repo_name, repo_url)
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 58 def git_pull_or_clone repo_name, repo_url run "bash -c 'if cd #{repo_name}; then git pull origin master; else git clone #{repo_url} #{repo_name}; fi'" end
github_handle()
click to toggle source
# File lib/georgia_recipes/base.rb, line 45 def github_handle Capistrano::CLI.ui.ask "Which Github handle would you like to add?" end
handle_command_with_input(local_run_method, shell_command, input_query, response=nil)
click to toggle source
Does the actual capturing of the input and streaming of the output.
local_run_method: run or sudo shell_command: The command to run input_query: A regular expression matching a request for input: /^Please enter your password/
# File lib/georgia_recipes/helper_methods.rb, line 41 def handle_command_with_input(local_run_method, shell_command, input_query, response=nil) send(local_run_method, shell_command, {:pty => true}) do |channel, stream, data| if data =~ input_query if response logger.info "#{data} #{"*"*(rand(10)+5)}", channel[:host] channel.send_data "#{response}\n" else logger.info data, channel[:host] response = ::Capistrano::CLI.password_prompt "#{data}" channel.send_data "#{response}\n" end else logger.info data, channel[:host] end end end
local_db_config(key)
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 87 def local_db_config(key) begin config = File.read('config/database.yml') YAML.load(config)["development"][key.to_s] rescue request_from_prompt(key) end end
monit_config(name, destination = nil)
click to toggle source
# File lib/georgia_recipes/monit.rb, line 47 def monit_config(name, destination = nil) destination ||= "/etc/monit/conf.d/#{name}.conf" template "monit/#{name}.erb", "/tmp/monit_#{name}" run "#{sudo} mv /tmp/monit_#{name} #{destination}" run "#{sudo} chown root:root #{destination}" run "#{sudo} chmod 600 #{destination}" end
precompile_assets()
click to toggle source
# File lib/georgia_recipes/assets.rb, line 3 def precompile_assets run_locally "bundle exec rake assets:precompile" find_servers_for_task(current_task).each do |server| run_locally "rsync -vr --exclude='.DS_Store' -e 'ssh -p #{ssh_options[:port] || 22}' public/assets #{user}@#{server.host}:#{shared_path}/" end run_locally "rm -Rf public/assets" end
remote_db_config(key)
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 96 def remote_db_config(key) begin config = capture("cat #{shared_path}/config/database.yml") YAML.load(config)[rails_env][key.to_s] rescue request_from_prompt(key, env: rails_env) end end
remote_file_exists?(full_path)
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 8 def remote_file_exists?(full_path) 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip end
remote_process_exists?(pid_file)
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 12 def remote_process_exists?(pid_file) capture("ps -p $(cat #{pid_file}) ; true").strip.split("\n").size == 2 end
request_from_prompt(key, env='Development')
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 105 def request_from_prompt key, env='Development' case key when :database ask("#{env} database name: ") when :password Capistrano::CLI.password_prompt("#{env} database password: ") else ask("#{env} database #{key}: ") end end
revision()
click to toggle source
# File lib/georgia_recipes/errbit.rb, line 8 def revision @revision ||= run_locally('git rev-parse --short HEAD') @revision[0..-2] #strips the \n from stdout end
run_with_input(shell_command, input_query=/^Password/, response=nil)
click to toggle source
Run a command and ask for input when input_query is seen. Sends the response back to the server.
input_query
is a regular expression that defaults to /^Password/. Can be used where run
would otherwise be used. run_with_input
'ssh-keygen …', /^Are you sure you want to overwrite?/
# File lib/georgia_recipes/helper_methods.rb, line 31 def run_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:run, shell_command, input_query, response) end
set_default(name, *args, &block)
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 10 def set_default(name, *args, &block) set(name, *args, &block) unless exists?(name) end
set_user(user, pass)
click to toggle source
change the capistrano user
# File lib/georgia_recipes/helper_methods.rb, line 76 def set_user(user, pass) set :user, user set :password, pass close_sessions end
table_name()
click to toggle source
# File lib/georgia_recipes/postgresql.rb, line 75 def table_name @table_name ||= ask("Which remote database table would you like to pull?") end
template(from, to)
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 5 def template(from, to) erb = File.read(File.expand_path("../templates/#{from}", __FILE__)) put ERB.new(erb).result(binding), to end
timestamp()
click to toggle source
# File lib/georgia_recipes/helper_methods.rb, line 1 def timestamp @timestamp ||= Time.now.strftime('%Y%m%d%H%M%S') end
unicorn_get_oldbin_pid()
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 22 def unicorn_get_oldbin_pid oldbin_pid_file = "#{unicorn_pid}.oldbin" unicorn_get_pid(oldbin_pid_file) end
unicorn_get_pid(pid_file=unicorn_pid)
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 16 def unicorn_get_pid(pid_file=unicorn_pid) if remote_file_exists?(pid_file) && remote_process_exists?(pid_file) capture("cat #{pid_file}") end end
unicorn_send_signal(pid, signal)
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 27 def unicorn_send_signal(pid, signal) run "#{try_sudo} kill -s #{signal} #{pid}" end
unicorn_workers()
click to toggle source
# File lib/georgia_recipes/unicorn.rb, line 31 def unicorn_workers @unicorn_workers ||= Capistrano::CLI.ui.ask("How many unicorn workers?") end
username()
click to toggle source
# File lib/georgia_recipes/errbit.rb, line 3 def username @username ||= run_locally("git config user.email") @username[0..-2] #strips the \n from stdout end
with_user(new_user, new_pass) { || ... }
click to toggle source
run a command on the server with a different user
# File lib/georgia_recipes/helper_methods.rb, line 63 def with_user(new_user, new_pass, &block) old_user, old_pass = user, password set_user(new_user, new_pass) begin yield rescue Exception => e set_user(old_user, old_pass) raise e end set_user(old_user, old_pass) end