Skip to content

Commit

Permalink
Add Capfile and Debian init script.
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Nov 9, 2011
1 parent 6e7b023 commit 5aff5a6
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Capfile
@@ -0,0 +1,78 @@
# Steps to doing an initial deployment:
#
# Create system user "pgxn"
# cap deploy:setup
# cap deploy:cold -s branch=$tag
# cap deploy -s branch=$tag

load 'deploy'

default_run_options[:pty] = true # Must be set for the password prompt from git to work

set :application, "site"
set :domain, "pgxn.org"
set :repository, "https://github.com/pgxn/pgxn-site.git"
set :scm, :git
set :deploy_via, :remote_cache
set :use_sudo, false
set :branch, "master"
set :deploy_to, "~/pgxn-site"
set :run_from, "/var/www/#{domain}"

# Prevent creation of Rails-style shared directories.
set :shared_children, %()

role :app, 'xanthan.postgresql.org'

namespace :deploy do
desc 'Confirm attempts to deploy master'
task :check_branch do
if self[:branch] == 'master'
unless Capistrano::CLI.ui.agree("\n Are you sure you want to deploy master? ")
puts "\n", 'Specify a branch via "-s branch=vX.X.X"', "\n"
exit
end
end
end

task :finalize_update, :except => { :no_release => true } do
# Build it!
run <<-CMD
cd #{ latest_release };
perl Build.PL || exit $?;
./Build installdeps || exit $?;
./Build || exit $?;
ln -s #{shared_path}/www #{latest_release}/www;
CMD
end

task :start_script do
top.upload 'eg/debian_init', '/tmp/pgxn-site', :mode => 0755
run 'sudo mv /tmp/pgxn-site /etc/init.d/ && sudo /usr/sbin/update-rc.d pgxn-site defaults'
end

task :symlink_production do
run "sudo ln -fs #{ latest_release } #{ run_from }"
end

task :migrate do
# Do nothing.
end

task :start do
run 'sudo /etc/init.d/pgxn-site start'
end

task :restart do
run 'sudo /etc/init.d/pgxn-site restart'
end

task :stop do
run 'sudo /etc/init.d/pgxn-site stop'
end

end

before 'deploy:update', 'deploy:check_branch'
after 'deploy:update', 'deploy:start_script'
after 'deploy:symlink', 'deploy:symlink_production'
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -16,6 +16,9 @@ Revision history for Perl extension PGXN::Site
- Added Identity page a Creative Commons Attribution-ShareAlike 3.0
Unported License, description of fonts, and links to original art
work.
- Added `Capfile` for capistrano deployment.
- Added `eg/debian_init`, an example init script for Debian that runs
PGXN::Site on Starman.

0.7.4 2011-05-18T17:54:32
- The router tests are now skipped if PGXN::API::Searcher is not
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.SKIP
Expand Up @@ -27,6 +27,7 @@

# Avoid build files.
^PGXN-Site
^Capfile
^www

# Avoid spelling test.
Expand Down
177 changes: 177 additions & 0 deletions eg/debian_init
@@ -0,0 +1,177 @@
#!/bin/sh

# Start PGXN Site on Starman on Debian.
# Based on code blatently stolen from the Catalyst init script:

# http://github.com/arodland/cat-fcgi-init
# Copyright (c) 2009-2010, Andrew Rodland
# Distributed under the 3-clause BSD license:
# http://github.com/arodland/cat-fcgi-init/blob/master/LICENSE
#
### BEGIN INIT INFO
# Provides: pgxn_site
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $network $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The PGXN Site server
### END INIT INFO

. /lib/lsb/init-functions

APPNAME="PGXN::Site"
APPDIR=/var/www/pgxn.org
UNIXNAME=$(echo $APPNAME | perl -pe 's/::/_/;$_=lc')
PLACKUP="$APPDIR/blib/script/${UNIXNAME}_server"
PSGI=""
PSGI_OPTS="--errors-to pgxn-admins@googlegroups.com --errors-from pgxn@pgexperts.com --feedback-to pgxn@pgexperts.com --api-url http://api.pgxn.org/ --private-api-url file:/var/www/api.pgxn.org/www/ --reverse-proxy"
PLACK_SERVER=Starman
PLACK_SERVER_OPTS="-E prod --workers 5 --preload-app --max-requests 100"
SOCKET=127.0.0.1:7498
# Leave these unset and we won't try to setuid/setgid.
USER=pgxn
GROUP=
# Set this if you have more than one instance of the app and you don't want
# them to step on each other's pidfile.
PIDSUFFIX=

# local::lib path, if you want to use it.
LOCALLIB=

if [ -f "/etc/default/"$UNIXNAME ]; then
. "/etc/default/"$UNIXNAME
fi

if [ $(id -u) -eq 0 ] ; then
PIDDIR=/var/run/$UNIXNAME
mkdir $PIDDIR >/dev/null 2>&1
if [ -n "$USER" ] ; then
chown $USER${GROUP:+":"}$GROUP $PIDDIR
fi
chmod 775 $PIDDIR
else
PIDDIR=/tmp
fi

PIDFILE=$PIDDIR/$UNIXNAME${PIDSUFFIX:+"-$PIDSUFFIX"}.pid

if [ -n "$LOCALLIB" ] ; then
eval `perl -I"$LOCALLIB/lib/perl5" -Mlocal::lib="$LOCALLIB"`
fi

PERL5LIB="$APPDIR/lib"${PERL5LIB:+":"}$PERL5LIB
export PERL5LIB

getpid() {
awk '{ print $1 }' "$PIDFILE"
}

check_running() {
[ -s $PIDFILE ] && kill -0 $(getpid) >/dev/null 2>&1
}

check_compile() {
if [ -n "$USER" ] ; then
if su $USER -c "cd $APPDIR ; perl -Ilib -M$APPNAME -c $PLACKUP" ; then
return 0
fi
return 1
else
if ( cd $APPDIR ; perl -Ilib -M$APPNAME -c $PSGI ) ; then
return 0
fi
return 1
fi
}

_start() {
start-stop-daemon --start --quiet --pidfile $PIDFILE --chdir $APPDIR \
${USER:+"--chuid"} $USER ${GROUP:+"--group"} $GROUP --background \
--startas "$PLACKUP" -- \
-MFile::Pid::Quick="$PIDFILE" \
--listen "$SOCKET" ${PLACK_SERVER:+"-s"} $PLACK_SERVER $PLACK_SERVER_OPTS \
--app "$PSGI" $PSGI_OPTS
# start-stop-daemon --start --quiet --pidfile $PIDFILE --chdir $APPDIR \
# ${USER:+"--chuid"} $USER ${GROUP:+"--group"} $GROUP --background \
# --startas "$PLACKUP" -- --port "$PORT" ${PLACK_SERVER:+"-s"} \
# $PLACK_SERVER $PLACK_SERVER_OPTS --pid $PIDFILE --daemonize --app "$PSGI"

for i in 1 2 3 4 5 6 7 8 9 10; do
sleep 1
if check_running ; then
return 0
fi
done
return 1
}

start() {
log_daemon_msg "Starting $APPNAME" $UNIXNAME
if check_running; then
log_progress_msg "already running"
log_end_msg 0
exit 0
fi

rm -f $PIDFILE 2>/dev/null

_start
log_end_msg $?
return $?
}

_stop() {
start-stop-daemon --stop --user $USER --quiet --oknodo --pidfile $PIDFILE \
--retry TERM/5/TERM/30/KILL/30 \
|| log_failure_message "It won't die!"
}

stop() {
log_daemon_msg "Stopping $APPNAME" $UNIXNAME

_stop
log_end_msg $?
return $?
}

restart() {
log_daemon_msg "Restarting $APPNAME" $UNIXNAME

check_compile && _stop && _start
log_end_msg $?
return $?
}

reload() {
log_daemon_msg "Reloading $APPNAME" $UNIXNAME

[ -s $PIDFILE ] && kill -HUP $(getpid) >/dev/null 2>&1
log_end_msg $?
return $?
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
check_running && reload || restart
;;
check|check-compile)
check_compile
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|force-reload|check}"
exit 1
esac
exit $?

0 comments on commit 5aff5a6

Please sign in to comment.