Skip to content

Instantly share code, notes, and snippets.

@unflores
Forked from nstielau/Capfile
Created February 24, 2011 21:51
Show Gist options
  • Save unflores/842962 to your computer and use it in GitHub Desktop.
Save unflores/842962 to your computer and use it in GitHub Desktop.
Generic cap deploy script
#Generic cap deploy script
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
##################################
# Edit these
set :application, "example_node_app"
set :node_file, ""
set :host, "public_domain_name"
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")] #your private ssh key
set :repository, "git@githubrepo_url"
set :branch, "master"
set :deploy_to, "/srv/#{application}"
####################################
set :scm, :git
set :deploy_via, :remote_cache
role :app, host
set :user, "ubuntu"
set :use_sudo, true
default_run_options[:pty] = true
namespace :deploy do
#few helper functions to help with deploy type stuff
def running?(program); capture("ps ax | grep -v grep | grep #{program} | xargs")[program] end #I pipe to xargs b/c capture needs some sort of output and xargs will always give a newline at least
def file_exists?(abs_loc); ! capture("ls #{abs_loc}")['No such file or directory'] end
def installed?(program); ! capture("whereis #{program}").sub( /#{program}:(\s)*/,"").empty? end
def to_screen msg; puts "\t===============#{msg}===============" end
task :start, :roles => :app, :except => { :no_release => true } do
run "#{sudo} rm /data/db/mongod.lock" if file_exists?("/data/db/mongod.lock") and not running?("mongod")
run "#{sudo} start mongo"
run "#{sudo} start #{application}"
end
task :start, :roles => :app, :except => { :no_release => true } do
run "#{sudo} start #{application}"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "#{sudo} stop #{application}"
run "#{sudo} stop mongo
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{sudo} restart #{application} || #{sudo} start #{application}"
end
task :create_deploy, :roles => :app do
to_screen "Checking dependencies..."
to_screen "Mongo not installed" unless installed? "mongo"
to_screen "NPM is not installed" unless installed? "npm"
to_screen "Nginx not set up" unless file_exists? "/etc/nginx/sites-enabled"
to_screen "Ok. I'm done checking your dependencies. Hopefully you don't need a bad operation."
run "#{sudo} mkdir -p #{deploy_to}"
run "#{sudo} chown #{user}:#{user} #{deploy_to}"
end
#Upstart scripts are the bomb. Pretty simple to get started if you don't already know how to use - http://upstart.ubuntu.com/getting-started.html
#Obviously the following upstarts are for node and mongo, but they equally apply for other services as well.
task :write_upstart_script, :roles => :app do
site_upstart_script = <<-SITE_UPSTART
description "#{application}"
start on startup
stop on shutdown
script
#TODO find out if this is necessary
export HOME="/home/#{admin_runner}"
cd #{current_path}
exec sudo -u root sh -c "node #{current_path}/#{node_file} >> #{shared_path}/log/#{application}.log 2>&1"
end script
respawn
SITE_UPSTART
mongo_upstart_script = <<-MONGO_UPSTART
description "mongo"
start on startup
stop on shutdown
script
#TODO find out if this is necessary
export HOME="/home/#{admin_runner}"
cd #{current_path}
exec sudo -u root sh -c "mongod"
end script
respawn
MONGO_UPSTART
put site_upstart_script, "/tmp/#{application}_upstart.conf"
put mongo_upstart_script, "/tmp/mongo_upstart.conf"
run "#{sudo} mv /tmp/mongo_upstart.conf /etc/init/mongo.conf"
run "#{sudo} mv /tmp/#{application}_upstart.conf /etc/init/#{application}.conf"
end
end
before 'deploy:setup', 'deploy:create_deploy'
after 'deploy:setup', 'deploy:write_upstart_script'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment