Skip to content

Commit

Permalink
Deployment WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
muness committed Sep 17, 2010
1 parent 73665fe commit 1047614
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bin/daemon-start.sh
@@ -0,0 +1,46 @@
#/bin/sh
# based on http://barelyenough.org/blog/2005/03/java-daemon/
# leaves out the clean shutdown hook for now

. bin/env.sh

launch_daemon()
{
/bin/sh <<EOC
java $JVM_ARGS $JAVA_ENV -cp $JARS:$SRC_DIR clojure.main -i $SRC_DIR/examples/daemon.clj -e "(use 'examples.daemon) (daemonize)" <&- &
pid=\$!
echo \${pid}
EOC
}

determine_if_daemon_is_running()
{
old_daemon_pid=`cat log/daemon.pid|tr -d '\n'`
ps -p "${old_daemon_pid}" >/dev/null 2>&1
if [ "$?" -eq "0" ]
then
echo "NOT STARTING NEW INSTANCE. PID file daemon.pid already exists and the process id referred to in it is running."
exit 1
else
# remove stale pid file
rm log/daemon.pid
fi
}

if [ -f log/daemon.pid ]
then
determine_if_daemon_is_running
fi

daemon_pid=`launch_daemon`

sleep 3 # give it a chance to boot up

ps -p "${daemon_pid}" >/dev/null 2>&1
if [ "$?" -eq "0" ]
then
echo ${daemon_pid} > log/daemon.pid
else
echo "Daemon did not start."
exit 1
fi
5 changes: 5 additions & 0 deletions bin/daemon-stop.sh
@@ -0,0 +1,5 @@
#/bin/sh

if [ -f log/daemon.pid ] ; then
kill `cat log/daemon.pid`
fi
10 changes: 10 additions & 0 deletions bin/env.sh
@@ -0,0 +1,10 @@
JARS=
for i in `ls ./lib/*.jar`
do
JARS=${JARS}:${i}
done

export JARS
export SRC_DIR=src
export TEST_DIRS=test
export JVM_ARGS="-Xmx1G"
99 changes: 99 additions & 0 deletions bin/euca-functions.sh
@@ -0,0 +1,99 @@
function install_euca_tools {
pushd ~/tmp
curl -L -o euca_deps.tgz http://open.eucalyptus.com/sites/all/modules/pubdlcnt/pubdlcnt.php?file=http://eucalyptussoftware.com/downloads/releases/euca2ools-1.2-src-deps.tar.gz &&
tar xzf euca_deps.tgz && cd euca2ools-1.2-src-deps/ &&
tar xzf M2Crypto-0.19.1.tar.gz && cd M2Crypto-0.19.1 && sudo python setup.py install && cd .. &&
tar xzf boto-1.8d.tar.gz && cd boto-1.8d && sudo python setup.py install && cd ..
cd .. &&
curl -L -o euca_tools.tgz http://open.eucalyptus.com/sites/all/modules/pubdlcnt/pubdlcnt.php?file=http://eucalyptussoftware.com/downloads/releases/euca2ools-1.2.tar.gz &&
tar xzf euca_tools.tgz &&
cd euca2ools-1.2 &&
sudo make # fails!
sudo cp bin/euca-* /usr/local/bin/ &&
cd ..
popd
}

function validate_environment_variables {
if [ -n "$EC2_URL" -a -n "$EC2_ACCESS_KEY" -a -n "$EC2_SECRET_KEY" ]; then
return 0
else
echo "You must set EC2_SECRET_KEY, EC2_ACCESS_KEY, and EC2_URL"
echo "If you're using EC2, export EC2_URL=https://ec2.amazonaws.com:443"
echo "Get the EC2_ACCESS_KEY and EC2_SECRET_KEY from https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key"
exit 1
fi
}

function create_security_group {
local group=$1
local ports=$2

euca-delete-group $group
euca-add-group -d "$group" $group

for port in ${ports[@]} ; do
euca-authorize -P tcp -p $port -s 0.0.0.0/0 $group
done
}

function start_instance {
local group=$1
export EUCA_IDENTIFIER=`euca-run-instances -k $group ami-19a34270 -g $group | grep INSTANCE | awk '{print $2}'`
export EUCA_IP="pending"
}

function extract_ip {
local group=$1
echo "Waiting for $EUCA_IDENTIFIER (provisioned for $1) to start"
EUCA_IP=`euca-describe-instances $EUCA_IDENTIFIER | grep INSTANCE | awk '{print $4}'`
while [ "$EUCA_IP" = "pending" ]
do
sleep 5
EUCA_IP=`euca-describe-instances $EUCA_IDENTIFIER | grep INSTANCE | awk '{print $4}'`
done
export EUCA_IP
}

function ensure_ssh {
local host=$1
local ssh_status=1
echo "Waiting for ssh connection on $host"
`ssh -q -q -o "BatchMode=yes" -o "ConnectTimeout 5" -o "StrictHostKeyChecking no" root@$host "echo 2>&1" && return 0 || return 1`
ssh_status=$?
while [ $ssh_status -ne 0 ]
do
sleep 5
`ssh -q -q -o "BatchMode=yes" -o "ConnectTimeout 5" -o "StrictHostKeyChecking no" root@$host "echo 2>&1" && return 0 || return 1`
ssh_status=$?
done
}

function servers_running_as_security_group {
local group=$1
export EUCA_SERVERS="`euca-describe-instances | grep -E \"$group(\s|$)\" -A 1 | grep INSTANCE | grep -v terminated | grep -v shutting-down | awk '{print $4}'`"
}

function instances_running_as_security_group {
local group=$1
export EUCA_INSTANCES="`euca-describe-instances | grep -E \"$group(\s|$)\" -A 1 | grep INSTANCE | grep -v terminated | grep -v shutting-down | awk '{print $2}'`"
}

function terminate_security_group_instances {
local group=$1
instances_running_as_security_group $group
euca-terminate-instances $EUCA_INSTANCES
}

function provision_server_with_group {
start_instance $1
extract_ip $1
ensure_ssh $EUCA_IP
}

function confirm_not_in_git_repo {
if [ -d .git/ ]; then
echo "Error - you are inside a git repo, instead of being in the parent of your Verizon projects -- please cd to the correct directory first!"
exit 1
fi
}
14 changes: 14 additions & 0 deletions bin/provision_and_deploy.sh
@@ -0,0 +1,14 @@
#/bin/sh

APP_NAME="zap"

source bin/euca-functions.sh
create_security_group $APP_NAME "22 8080 9090" &&
provision_server_with_group $APP_NAME &&
CAP_USER=root SERVER=$EUCA_IP cap deploy:setup &&
SERVER=$EUCA_IP cap deploy &&
echo "--------------------- DONE --------------------"
echo "Zap deployed to $EUCA_IP:"
echo " * curl http://$EUCA_IP:8080/ # to see it in action"
echo " * SERVER=$EUCA_IP cap deploy # to redeploy"
echo " * terminate_security_group_instances $APP_NAME # to terminate all running instances"
26 changes: 26 additions & 0 deletions config/bootstrap.rb
@@ -0,0 +1,26 @@
###########################################
# Bootstrap the Ubuntu server
###########################################

Capistrano::Configuration.instance.load do
namespace :bootstrap do

task :go do
sudo "apt-get update -qq"
sudo "apt-get install -y openjdk-6-jdk; sudo dpkg --configure -a"
create_app_user
end

task :create_app_user do
app_user = "richservices"
sudo "sh -c '(id #{app_user}) || /usr/sbin/useradd -s /bin/bash -m #{app_user}'"
home = "/home/#{app_user}"
sudo "mkdir -p #{home}/.ssh"
sudo "cp ~/.ssh/authorized_keys #{home}/.ssh/authorized_keys"
run "sudo -H -u #{app_user} sh -c 'cd && curl --silent -OL http://github.com/technomancy/leiningen/raw/stable/bin/lein && chmod +x lein && ./lein self-install'"
sudo "chown -R #{app_user}:#{app_user} #{home}"
sudo "chmod 700 #{home}/.ssh"
sudo "chmod 600 #{home}/.ssh/authorized_keys"
end
end
end
67 changes: 67 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,67 @@
$LOAD_PATH << "config"

set :application, "rich-services"
set :scm, :git
set(:current_branch) { `git branch`.match(/\* (\S+)\s/m)[1] || raise("Couldn't determine current branch") }
set :branch, defer { current_branch }
set :repository, "git@github.com:relevance/rich-services.git"
set :deploy_to, "/var/www/apps/#{application}"

set :deploy_via, :copy
set :copy_exclude, [".git/*"]
set :copy_compression, :bz2 # Also valid are :zip and :bz2
set :use_sudo, !!ENV["SUDO"]

set :domain, ENV['SERVER'] || abort("Error - You must specify a server to deploy to as an environment variable - for example: 'SERVER=web01.example.com cap deploy'")

set :non_privileged_user, "richservices"
set :user, ENV["CAP_USER"] || non_privileged_user
ssh_options[:auth_methods] = ["publickey"]
default_run_options[:pty] = true

role :app, domain
role :web, domain
role :db, domain, :primary => true

require 'bootstrap'

before "deploy:setup", "bootstrap:go"
after "deploy:setup", "deploy:fix_setup_permissions"
after "deploy:update_code", "deploy:create_shared_dirs"
after "deploy:update_code", "deploy:symlink_lib"
after "deploy", "deploy:cleanup"

namespace :deploy do

task :create_shared_dirs do
run "mkdir -p #{shared_path}/lib"
run "mkdir -p #{shared_path}/log"
end

task :symlink_lib do
run "ln -fs #{shared_path}/lib #{latest_release}/"
end

task :fix_setup_permissions do
sudo "chown -R #{non_privileged_user}:#{non_privileged_user} #{deploy_to}"
end

task :start do
run "cd #{current_path} && ~/lein deps"
run "cd #{current_path} && nohup bin/daemon-start.sh"
end

task :restart do
stop
start
end

task :stop do
run "cd #{current_path} && bin/daemon-stop.sh"
end

task :cold do
update
start
end
end
1 change: 1 addition & 0 deletions log/.gitignore
@@ -0,0 +1 @@
*

0 comments on commit 1047614

Please sign in to comment.