Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save grosser/1a0f6a9ada817604e0f2 to your computer and use it in GitHub Desktop.
Save grosser/1a0f6a9ada817604e0f2 to your computer and use it in GitHub Desktop.
Capfile

create a default group with ssh + http allowed create a key-pair and download it adjust with your aws credentials

cap env:server:start # creates 1 micro ec2 instance
cap env:setup
cap deploy
cap env:server:stop
load 'deploy'
require 'bundler/capistrano'
def ec2_address(input='get')
file = 'deploy/server'
if input == 'get'
(File.read(file) rescue 'NO-SERVER').strip
else
File.open(file,'w'){|f|f.write(input)}
end
end
set :application, "foooo"
set :user, "ubuntu"
set :deploy_to, "/home/#{user}/#{application}"
set :public_dir, "#{deploy_to}/current/public"
set :use_sudo, true
set :runner, "#{user}"
set :scm, :git
set :repository, "git://github.com/user/foooo.git" # thats a public path, if you want private you have to setup github public key etc stuff
set :rails_env, "production"
set :key_pair, "mg-ec2" # ec2 key-pair name, store it in ~/.ssh/ec2/
# deploy to ec2
ssh_options[:keys] = "~/.ssh/ec2/#{key_pair}.pem"
server ec2_address, :app, :web, :db, :primary => true
namespace :deploy do
task(:start){}
task(:stop){}
task :restart do
run "touch #{current_release}/tmp/restart.txt"
end
desc "Copy config files to config/"
task :copy_config_files, :roles => [:app, :db] do
run "cp #{deploy_to}/shared/config/* #{current_release}/config/"
end
after 'deploy:update_code', 'deploy:copy_config_files'
end
after 'deploy', 'deploy:cleanup'
namespace :env do
namespace :server do
task :start do
server = fog.servers.create(
:image_id => 'ami-311f2b45', # ubuntu 10.04
:flavor_id => 't1.micro',
:key_name => key_pair
)
# wait for it to get online
server.wait_for { print "."; ready? }
puts "server started at #{server.dns_name}"
ec2_address(server.dns_name)
end
task :stop do
if server = fog.servers.detect{|s| s.dns_name == ec2_address }
server.destroy
ec2_address(nil)
else
puts "No server is running"
end
end
end
task :setup do
run "sudo apt-get update"
run "sudo apt-get install git-core sqlite3 libsqlite3-dev build-essential libcurl4-openssl-dev libssl-dev zlib1g-dev libreadline5-dev -y"
# env hacks
put "StrictHostKeyChecking no", "/home/#{user}/.ssh/config" # dont verify hosts
put "---\ngem: --no-ri --no-rdoc", "/home/#{user}/.gemrc"
install_ruby
install_bundler
install_nginx
run "sudo /etc/init.d/nginx restart"
# add project configuration
run "mkdir -f #{deploy_to}/shared #{deploy_to}/shared/config #{deploy_to}/shared/pids #{deploy_to}/shared/log || echo exist"
put File.read('config/config.yml'), "#{deploy_to}/shared/config/config.yml"
end
task :install_bundler do
run "sudo gem install bundler"
end
task :install_nginx do
version = '3.0.6'
run "sudo gem install passenger -v #{version}"
run "sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx"
config = File.read('deploy/nginx.conf') % {:passenger_version => version, :root => deploy_to}
sudo_put config, "/opt/nginx/conf/nginx.conf"
sudo_put File.read('deploy/nginx'), "/etc/init.d/nginx"
run "sudo chmod +x /etc/init.d/nginx"
end
# ree via .deb (faster then rvm)
task :install_ruby do
url = 'http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise_1.8.7-2011.03_i386_ubuntu10.04.deb'
run "which ruby || (cd /tmp && rm -rf ruby-enterprise-1.8.7* && wget -q #{url} && sudo dpkg -i /tmp/ruby-enterprise_*)"
end
end
# A hacky way to put files in Capistrano with sudoer permissions
def sudo_put(data, target)
tmp = "#{shared_path}/~tmp-#{rand(9999999)}"
put data, tmp
on_rollback { run "rm #{tmp}" }
sudo "cp -f #{tmp} #{target} && rm #{tmp}"
end
def fog
require 'fog'
@fog ||= Fog::Compute.new(
:provider => 'AWS',
:region=>'eu-west-1',
:aws_access_key_id=>'xxx',
:aws_secreat_key_id => 'yyy'
)
end
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if nginx -t $DAEMON_OPTS
then
return 0
else
return $?
fi
}
d_write_pid(){
ps -ef | grep nginx | grep master | grep -v grep | grep -v $0 | awk '{print $2}' > /var/run/$NAME.pid
}
case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
d_write_pid
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
d_write_pid
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
test_nginx_config
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
d_write_pid
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
configtest)
echo -n "Testing $DESC configuration: "
if test_nginx_config
then
echo "$NAME."
else
exit $?
fi
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
events {
worker_connections 1024;
}
http {
passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-%{passenger_version};
passenger_ruby /usr/local/bin/ruby;
server {
listen 80;
server_name fooo;
root %{root}/current/public;
passenger_enabled on;
}
}
@delebedev
Copy link

Noob question: where should i place deploy_nginx.conf and nginx files?
EDIT: they should be in deploy folder, obviously:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment