Skip to content

Instantly share code, notes, and snippets.

@bmuller
Last active December 17, 2015 10:28
Show Gist options
  • Select an option

  • Save bmuller/5594833 to your computer and use it in GitHub Desktop.

Select an option

Save bmuller/5594833 to your computer and use it in GitHub Desktop.
Capfile / monit file for a node project
require 'cgi'
role :server, "1.2.3.4", "5.6.7.8"
set :repo, "git@github.com:whatever/project.git"
set :destpath, "/home/myuser/project"
set :user, "myuser"
set :ssh_options, { :forward_agent => true }
after "deploy", "restart"
after "deploy", "airbrake_deploy_msg"
desc "Deploy project to production"
task :deploy, :roles => :server do
run "mkdir -p #{destpath}/shared/node_modules"
run "mkdir -p #{destpath}/shared/logs"
run "mkdir -p #{destpath}/releases"
newpath = "#{destpath}/releases/#{Time.now.strftime('%Y%m%d%H%M%S')}"
run "git clone --depth 1 #{repo} #{newpath}"
run "rm -f #{destpath}/current && ln -s #{newpath} #{destpath}/current"
run "ln -s #{destpath}/shared/node_modules #{destpath}/current/node_modules"
run "chmod 0700 #{destpath}/current/monitrc"
# we can delete old crap now, though next line doesn't seem to work any more
# run "find #{destpath}/releases -mtime +14 -delete"
end
desc "Tell airbrake we just deployed"
task :airbrake_deploy_msg do
msg = CGI::escape((`git log -n 1 --oneline`).strip)
url = "http://errors.somepath.com/deploys.txt?api_key=secret"
url+= "&deploy[rails_env]=production&deploy[scm_repository]=git@github.com:something.git"
url+= ("&deploy[local_username]=" + `whoami`).strip
url+= "&deploy[message]=" + CGI::escape((`git log -n 1 --oneline`).strip)
`wget "#{url}" -O - -q`
end
desc "Restart node"
task :restart, :roles => :server do
stop
start
end
desc "Stop node"
task :stop, :roles => :server do
pidfile = "#{destpath}/shared/monit.pid"
run "test ! -f #{pidfile} || (kill -QUIT $(cat #{pidfile}) && rm #{pidfile})"
pidfile = "#{destpath}/shared/node.pid"
run "test ! -f #{pidfile} || (kill -KILL $(cat #{pidfile}) && rm #{pidfile})"
end
desc "Start node"
task :start, :roles => :server do
run "cd #{destpath}/current && npm install"
pidfile = "#{destpath}/shared/node.pid"
run "cd #{destpath}/current && /usr/bin/node lib/main.js production #{pidfile} > /dev/null 2>&1 &"
pidfile = "#{destpath}/shared/monit.pid"
run "monit -d 5 -c #{destpath}/current/monitrc"
end
set logfile /home/project/shared/shared/logs/monit.log
set pidfile /home/project/shared/shared/monit.pid
set alert anemailaddess@gmail.com
set mailserver smtp.gmail.com port 587 username "noreply@mydomain.com" password "secret" using tlsv1 with timeout 30 seconds
check process exguard with pidfile "/home/user/project/shared/node.pid"
start program = "/usr/bin/node /home/user/project/current/lib/main.js production /home/user/project/shared/node.pid > /dev/null 2>&1"
stop program = "/bin/bash -c 'kill -KILL $(cat /home/user/project/shared/node.pid)' && rm -f /home/user/project/shared/node.pid"
if failed port 8080 protocol HTTP request / with timeout 5 seconds then restart
if failed port 8080 protocol HTTP request / with timeout 5 seconds then alert
if memory is greater than 60% for 3 cycles then restart
if memory is greater than 60% for 3 cycles then alert
if cpu is greater than 60% for 3 cycles then restart
if cpu is greater than 60% for 3 cycles then alert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment