Skip to content

Instantly share code, notes, and snippets.

@nstielau
Created July 16, 2010 22:23
Show Gist options
  • Save nstielau/479007 to your computer and use it in GitHub Desktop.
Save nstielau/479007 to your computer and use it in GitHub Desktop.
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
##################################
# Edit these
set :application, "example_node_app"
set :node_file, "hello_world.js"
set :host, "ec2-174-129-114-66.compute-1.amazonaws.com"
ssh_options[:keys] = [File.join(ENV["HOME"], ".ec2", "default.pem")]
set :repository, "git://gist.github.com/479007.git"
set :branch, "master"
set :deploy_to, "/opt/#{application}"
####################################
set :scm, :git
set :deploy_via, :remote_cache
role :app, host
set :user, "ubuntu"
set :use_sudo, true
set :admin_runner, 'node_js'
default_run_options[:pty] = true
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo :as => 'root'} start #{application}"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo :as => 'root'} stop #{application}"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo :as => 'root'} restart #{application} || #{try_sudo :as => 'root'} start #{application}"
end
task :create_deploy_to_with_sudo, :roles => :app do
run "#{try_sudo :as => 'root'} mkdir -p #{deploy_to}"
run "#{try_sudo :as => 'root'} chown #{admin_runner}:#{admin_runner} #{deploy_to}"
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}"
cd #{current_path}
exec sudo -u #{admin_runner} sh -c "/usr/local/bin/node #{current_path}/#{node_file} >> #{shared_path}/log/#{application}.log 2>&1"
end script
respawn
UPSTART
put upstart_script, "/tmp/#{application}_upstart.conf"
run "#{try_sudo :as => 'root'} mv /tmp/#{application}_upstart.conf /etc/init/#{application}.conf"
end
end
before 'deploy:setup', 'deploy:create_deploy_to_with_sudo'
after 'deploy:setup', 'deploy:write_upstart_script'
#!/bin/bash
NODE_VERSION=0.1.100
NODE_FILE=node-v$NODE_VERSION
apt-get install -y build-essential git-core nginx
wget http://nodejs.org/dist/$NODE_FILE.tar.gz
tar -zxvf $NODE_FILE.tar.gz
pushd $NODE_FILE
./configure
make
make install
popd
rm -rf $NODE_FILE.tar.gz $NODE_FILE
sudo adduser \
--system \
--shell /bin/bash \
--gecos 'User for running node.js projects' \
--group \
--disabled-password \
--home /home/node \
node_js
# Add ubuntu user to the node_js group
/usr/sbin/usermod -a -G node_js ubuntu
# Setup basic nginx proxy.
unlink /etc/nginx/sites-available/default
cat > /etc/nginx/sites-available/node_proxy.conf <<EOF
server {
listen 80;
# proxy to node
location / {
proxy_pass http://127.0.0.1:8124/;
}
}
EOF
ln -s /etc/nginx/sites-available/node_proxy.conf /etc/nginx/sites-enabled/node_proxy.conf
/etc/init.d/nginx restart
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
# Ubuntu 10.04 Lucid AMI from http://alestic.com/
ec2-run-instances -t c1.medium -n 1 -g default -k default --user-data-file configure_server.sh ami-2d4aa444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment