Last active
December 18, 2015 16:49
-
-
Save j1n6/5814635 to your computer and use it in GitHub Desktop.
node upstart deploy example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :stages, %w(production demo staging) | |
set :default_stage, "staging" | |
require "capistrano/ext/multistage" | |
set :application, "example-app" | |
set :repository, "[email protected]" | |
set :branch, "master" | |
set :scm, :git | |
set :git_enable_submodules, 1 | |
set :admin_user, "ubuntu" | |
set :user, "app" | |
set :use_sudo, false | |
set :deploy_to, "/home/#{user}/#{application}" | |
set :deploy_via, :remote_cache | |
ssh_options[:forward_agent] = true | |
# pty return yes if host is first time added | |
default_run_options[:pty] = true | |
after "deploy:update_code", "deploy:npm_install" | |
after "deploy:update_code", "deploy:link_config" | |
after 'deploy:update_code', "deploy:write_upstart_script" | |
def with_user(new_user, &block) | |
old_user = user | |
set :user, new_user | |
close_sessions | |
yield | |
set :user, old_user | |
close_sessions | |
end | |
def upstart_script | |
<<-UPSTART_SCRIPT | |
description "#{application} upstart script" | |
start on startup | |
stop on shutdown | |
console output | |
respawn | |
respawn limit 5 60 | |
script | |
exec su #{user} -c `cd #{release_path} && NODE_ENV=#{stage} node #{release_path}/server.js >> #{release_path}/log/production.log 2>&1` | |
end script | |
post-start script | |
PID=`status #{application} | egrep -oi '([0-9]+)$' | head -n1` | |
su #{user} -c `echo $PID > #{release_path}/tmp/pids/#{application}.pid` | |
end script | |
post-stop script | |
su #{user} -c `rm -f #{release_path}/tmp/pids/#{application}.pid` | |
end script | |
UPSTART_SCRIPT | |
end | |
def close_sessions | |
sessions.values.each { |session| session.close } | |
sessions.clear | |
end | |
namespace :deploy do | |
task :start, :roles => :app do | |
with_user(admin_user) { run "sudo restart #{application} || sudo start #{application}" } | |
end | |
task :stop, :roles => :app do | |
with_user(admin_user) { run "sudo stop #{application}" } | |
end | |
task :restart, :roles => :app do | |
start | |
end | |
task :npm_install do | |
run "mkdir -p #{shared_path}/npm" | |
run "cp #{release_path}/package.json #{shared_path}/npm/package.json" | |
run "cp #{release_path}/npm-shrinkwrap.json #{shared_path}/npm/npm-shrinkwrap.json" | |
run "cd #{shared_path}/npm && npm install" | |
run "ln -s #{shared_path}/npm/node_modules #{release_path}/node_modules" | |
end | |
task :link_config do | |
run "ln -s #{shared_path}/config.json #{release_path}/config/config.json" | |
end | |
task :write_upstart_script, :roles => :app do | |
put upstart_script(), "#{release_path}/#{application}.conf" | |
with_user admin_user do | |
sudo "mv #{release_path}/#{application}.conf /etc/init" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment