Skip to content

Instantly share code, notes, and snippets.

@mashihua
Created October 12, 2011 10:40
Show Gist options
  • Save mashihua/1280889 to your computer and use it in GitHub Desktop.
Save mashihua/1280889 to your computer and use it in GitHub Desktop.
Capistrano deploy script for node.js
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res){
//the log will out put to log/{node_env}.log
console.log("Method:" + req.method);
//send text to agent
res.send('Hello World. NODE_ENV=' + process.env.NODE_ENV);
});
//listening on application_port where set by capistrano
app.listen(process.argv[2] || 3000);
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
load 'config/node'
##
# Capistrano tasks for Ubantu.
#
# Author: Shihua Ma http://f2eskills.com/
set :stages, %w[staging production]
set :default_stage, 'staging'
require 'capistrano/ext/multistage'
#application name
set :application, "example"
#start server script
set :node_file, "app.js"
#deploy host
set :host, "hostname"
#user name,must be a sudoer without prompting for password
set :user, "username"
set :admin_runner, user
set :repository, "git@[email protected]:mashihua/Nodebot.git"
set :scm, :git
set :deploy_via, :remote_cache
role :app, host
set :use_sudo, true
namespace :deploy do
desc "Start node server"
task :start, :roles => :app, :except => { :no_release => true } do
run "sudo start #{application}_#{node_env}"
end
desc "Stop node server"
task :stop, :roles => :app, :except => { :no_release => true } do
run "sudo stop #{application}_#{node_env}"
end
desc "Restart node server"
task :restart, :roles => :app, :except => { :no_release => true } do
run "sudo restart #{application}_#{node_env} || sudo start #{application}_#{node_env}"
end
desc "Check required packages and install if packages are not installed"
task :check_packages, roles => :app do
run "cd #{release_path} && jake depends"
end
task :create_deploy_to_with_sudo, :roles => :app do
run "sudo mkdir -p #{deploy_to}"
run "sudo chown #{admin_runner}:#{admin_runner} #{deploy_to}"
end
desc "Update submodules"
task :update_submodules, :roles => :app do
run "cd #{release_path}; git submodule init && git submodule update"
end
task :write_upstart_script, :roles => :app do
upstart_script = <<-UPSTART
description "#{application}"
start on startup
stop on shutdown
script
# We found $HOME is needed. Without it, we ran into problems
export HOME="/home/#{admin_runner}"
export NODE_ENV="#{node_env}"
cd #{current_path}
exec sudo -u #{admin_runner} sh -c "NODE_PATH=#{node_path} /usr/local/bin/node #{current_path}/#{node_file} #{application_port} >> #{shared_path}/log/#{node_env}.log 2>&1"
end script
respawn
UPSTART
put upstart_script, "/tmp/#{application}_upstart.conf"
run "sudo mv /tmp/#{application}_upstart.conf /etc/init/#{application}_#{node_env}.conf"
end
end
before 'deploy:setup', 'deploy:create_deploy_to_with_sudo'
after 'deploy:setup', 'deploy:write_upstart_script'
after "deploy:finalize_update", "deploy:update_submodules", "deploy:check_packages"
var fs = require('fs');
desc('Check and install required packages');
task('depends', [], function (arg) {
var npm = require('npm');
npm.load({}, function (err) {
if (err) return commandFailed(err);
npm.on("log", function (message) { if(arg) console.log(message) })
var requirements = JSON.parse(fs.readFileSync('config/requirements.json'));
npm.commands.install(requirements, function (err, data) {
if (err) return commandFailed(err);
});
});
}, true);
##
# Custom installation tasks for Ubantu.
#
# Author: Shihua Ma http://f2eskills.com/
namespace :nodebot do
namespace :install do
desc "Install server software"
task :default do
setup
git
node
npm
jake
tree
httperf
set_time_to_utc
end
task :setup do
sudo "rm -rf src"
run "mkdir -p src"
end
desc "Install curl"
task :curl do
sudo "apt-get install curl"
end
desc "Install git"
task :git do
cmd = [
"cd src",
"wget http://ftp.ntu.edu.tw/pub2/software/scm/git/git-1.7.2.2.tar.gz",
"tar xfz git-1.5.3.5.tar.gz",
"cd git-1.5.3.5",
"make prefix=/usr/local all",
"sudo make prefix=/usr/local install"
].join(" && ")
run cmd
end
desc "install node.js"
task :node do
cmd = [
"cd src",
"wget http://nodejs.org/dist/node-v0.4.12.tar.gz",
"tar xfz node-v0.4.12.tar.gz",
"cd node-v0.4.12",
"./configure --prefix=/usr/local",
"make",
"sudo make install",
"echo 'export NODE_PATH=/usr/local:/usr/local/lib/node_modules' >> ~/.bash_profile"
].join(" && ")
run cmd
end
desc "install npm"
task :npm do
curl
sudo "curl http://npmjs.org/install.sh | sh"
end
desc "install jake"
task :jake do
sudo "npm install jake -g"
end
desc "Install command-line directory lister"
task :tree do
cmd = [
"cd src",
"wget ftp://mama.indstate.edu/linux/tree/tree-1.5.3.tgz",
"tar xfz tree-1.5.3.tgz",
"cd tree-1.5.3",
"make",
"sudo make install"
].join(' && ')
run cmd
end
desc "Install httperf"
task :httperf do
cmd = [
"cd src",
"wget ftp://ftp.hpl.hp.com/pub/httperf/httperf-0.9.0.tar.gz",
"tar xfz httperf-0.9.0.tar.gz",
"cd httperf-0.9.0",
"./configure --prefix=/usr/local",
"make",
"sudo make install"
].join(' && ')
run cmd
end
desc "Set time to UTC"
task :set_time_to_utc do
sudo "ln -nfs /usr/share/zoneinfo/UTC /etc/localtime"
end
end
end
set :node_env, "production"
#git repos branch
set :branch, "production"
#listing port
set :application_port, "1604"
#deploy path
set :deploy_to, "/srv/www/apps/#{application}/#{node_env}"
set :node_env, "staging"
#git repos branch
set :branch, "master"
#listing port
set :application_port, "1603"
#deploy path
set :deploy_to, "/srv/www/apps/#{application}/#{node_env}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment