Skip to content

Instantly share code, notes, and snippets.

@asmuth
Created February 17, 2012 09:47
Show Gist options
  • Save asmuth/1852251 to your computer and use it in GitHub Desktop.
Save asmuth/1852251 to your computer and use it in GitHub Desktop.
[ruby/rake] deploy with foreman/deamonize (w/o capistrano)
namespace :tlnt do
desc "deploy to staging environment"
task :deploy_staging => :environment do
deploy_to_environment(:staging)
end
desc "restart staging environment"
task :restart_staging => :environment do
restart_environment(:staging)
end
desc "tail staging log"
task :logtail_staging => :environment do
ssh(:staging, "tail -f staging/log/staging.log")
end
desc "deploy to production environment"
task :deploy_production => :environment do
deploy_to_environment(:production)
end
desc "restart production environment"
task :restart_production => :environment do
restart_environment(:production)
end
desc "tail production log"
task :logtail_production => :environment do
ssh(:production, "tail -f production/log/production.log")
end
private
def deploy_to_environment(environment)
log "trying to clone git repository"
ssh environment, "git clone #{AppConfig.git_url} #{environment}" rescue nil
log "updating git repository"
ssh environment, "cd #{environment} && git reset --hard"
ssh environment, "cd #{environment} && git checkout #{ENV["BRANCH"]||:master}"
ssh environment, "cd #{environment} && git pull origin #{ENV["BRANCH"]||:master}"
ssh environment, "cd #{environment} && git reset --hard"
log "installing bundle"
bundle_cmd = "cd #{environment} && #{AppConfig.bundle_binary}"
bundle_path = "#{basedir(environment)}.bundle/"
ssh environment, "#{bundle_cmd} --path #{ENV['BUNDLE_PATH']||bundle_path} install"
kill_worker(environment)
log "migrating database"
ssh environment, "#{bundle_cmd} exec rake db:migrate RAILS_ENV=#{environment}"
start_worker(environment)
end
def restart_environment(environment)
kill_worker(environment)
start_worker(environment)
end
def kill_worker(environment)
log "killing worker"
ssh environment, "cat #{basedir}#{environment}.pid | xargs kill -2" rescue nil
end
def start_worker(environment)
log "starting worker"
foreman_proc = AppConfig.send(:"proc_#{environment}")
ssh environment, app_cmd(environment, foreman_proc).join(" ")
end
def app_cmd(environment, foreman_procs="app=1")
[
"#{AppConfig.daemonize_binary} -v",
"-c #{basedir(environment)}#{environment}",
"-p #{basedir(environment)}#{environment}.pid",
"-o #{basedir(environment)}#{environment}.out",
"-e #{basedir(environment)}#{environment}.err",
"#{AppConfig.bundle_binary} exec",
"foreman start",
"-p 2300",
" --concurrency=\"#{foreman_procs}\"",
"-e env/#{environment}"
]
end
def ssh(environment, cmd)
sh "ssh #{AppConfig.send(:"ssh_#{environment}")} \"#{cmd}\""
end
def basedir(environment)
basedir = "/home/talentsuche.crypto/"
end
def log(string)
puts "deploy :: #{string}".green
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment