Bug 643063: add supporting files for slavealloc deployment; r=bear
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 22 Mar 2011 19:36:07 -0500
changeset 1298 b0eaa90448c505520f9fdc37a931d1277a5beb07
parent 1297 c194a7b7044bd48482883fc124d8c203c6e3c559
child 1299 25c5692b49de84ef8058f46b0bda0591b8b71ed0
push id909
push userdmitchell@mozilla.com
push dateWed, 23 Mar 2011 00:36:23 +0000
reviewersbear
bugs643063
Bug 643063: add supporting files for slavealloc deployment; r=bear
.hgignore
MANIFEST.in
lib/python/slavealloc/contrib/slavealloc-allocator.tac
lib/python/slavealloc/contrib/slavealloc-combined.tac
lib/python/slavealloc/contrib/slavealloc-nginx.conf
lib/python/slavealloc/contrib/slavealloc-staging-nginx.conf
lib/python/slavealloc/contrib/slavealloc-staging.initd
lib/python/slavealloc/contrib/slavealloc-ui.tac
lib/python/slavealloc/contrib/slavealloc.initd
setup.py
slavealloc-allocator.tac
slavealloc-combined.tac
slavealloc-ui.tac
--- a/.hgignore
+++ b/.hgignore
@@ -4,8 +4,9 @@ moz-n900-v
 flasher-
 RX-44
 RX-51
 build/
 slavealloc\.db
 \..*\.swp
 twistd\.pid
 .*\.egg-info
+dist/
new file mode 100644
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,3 @@
+recursive-include lib/python/slavealloc/contrib *
+recursive-include lib/python/slavealloc/www *
+
rename from slavealloc-allocator.tac
rename to lib/python/slavealloc/contrib/slavealloc-allocator.tac
--- a/slavealloc-allocator.tac
+++ b/lib/python/slavealloc/contrib/slavealloc-allocator.tac
@@ -1,7 +1,15 @@
 from twisted.application import service
 from slavealloc.daemon.application import Allocator
+from twisted.python.logfile import LogFile
+from twisted.python.log import ILogObserver, FileLogObserver
 
 application = service.Application("slavealloc")
+
+# set up logfile rotation
+logfile = LogFile.fromFullPath("slavealloc.log", rotateLength=1024**2,
+			 maxRotatedFiles=10)
+application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
+
 allocator = Allocator(http_port='tcp:8010', db_url='sqlite:///slavealloc.db',
     run_allocator=True)
 allocator.setServiceParent(application)
rename from slavealloc-combined.tac
rename to lib/python/slavealloc/contrib/slavealloc-combined.tac
--- a/slavealloc-combined.tac
+++ b/lib/python/slavealloc/contrib/slavealloc-combined.tac
@@ -1,8 +1,16 @@
 from twisted.application import service
 from slavealloc.daemon.application import Allocator
+from twisted.python.logfile import LogFile
+from twisted.python.log import ILogObserver, FileLogObserver
 
 application = service.Application("slavealloc")
+
+# set up logfile rotation
+logfile = LogFile.fromFullPath("slavealloc.log", rotateLength=1024**2,
+			 maxRotatedFiles=10)
+application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
+
 allocator = Allocator(http_port='tcp:8012', db_url='sqlite:///slavealloc.db',
     run_allocator=True,
     run_ui=True)
 allocator.setServiceParent(application)
new file mode 100644
--- /dev/null
+++ b/lib/python/slavealloc/contrib/slavealloc-nginx.conf
@@ -0,0 +1,22 @@
+server {
+	listen 80;
+	server_name slavealloc.build.mozilla.org slavealloc;
+
+	# proxy the API to the Twisted daemon
+	location ~ ^/(api|gettac) {
+		proxy_set_header X-Real-IP $remote_addr;
+		proxy_redirect false;
+		proxy_pass http://127.0.0.1:1080;
+	}
+
+	# serve the UI directly
+	location /ui {
+		# note that this path depends on slavealloc being installed with pip
+		alias /tools/slavealloc/src/tools/lib/python/slavealloc/www;
+	}
+
+	# and redirect / to /ui
+	location = / {
+		rewrite ^(.*) /ui permanent;
+	}
+}
new file mode 100644
--- /dev/null
+++ b/lib/python/slavealloc/contrib/slavealloc-staging-nginx.conf
@@ -0,0 +1,22 @@
+server {
+	listen 80;
+	server_name slavealloc-staging.build.mozilla.org slavealloc-staging;
+
+	# proxy the API to the Twisted daemon
+	location ~ ^/(api|gettac) {
+		proxy_set_header X-Real-IP $remote_addr;
+		proxy_redirect false;
+		proxy_pass http://127.0.0.1:1079;
+	}
+
+	# serve the UI directly
+	location /ui {
+		# note that this path depends on slavealloc being installed with pip
+		alias /tools/slavealloc/src/tools/lib/python/slavealloc/www;
+	}
+
+	# and redirect / to /ui
+	location = / {
+		rewrite ^(.*) /ui permanent;
+	}
+}
new file mode 100755
--- /dev/null
+++ b/lib/python/slavealloc/contrib/slavealloc-staging.initd
@@ -0,0 +1,115 @@
+#!/bin/sh
+#
+# slavealloc-staging
+#
+# chkconfig:   345 85 15
+# description: slave allocator daemon
+
+# Source function library.
+. /etc/rc.d/init.d/functions
+
+NAME=staging-1079
+BASEDIR=/builds/slavealloc/$NAME
+lockfile=/var/lock/subsys/slavealloc-$NAME
+pidfile=$BASEDIR/twistd.pid
+LOGFILE=$BASEDIR/slavealloc.log
+TACFILE=$BASEDIR/slavealloc.tac
+USER=slavealloc
+GROUP=slavealloc
+VIRTUALENV=/tools/slavealloc
+
+start() {
+    [ -x $VIRTUALENV/bin/twistd ] || exit 5
+    [ -f $TACFILE ] || exit 6
+    echo -n "starting slavealloc $NAME"
+    cd $BASEDIR # --rundir doesn't work with the customized .tac logging
+    daemon $VIRTUALENV/bin/twistd  \
+		    --pidfile=$pidfile \
+		    --rundir=$BASEDIR \
+		    --python=$TACFILE \
+		    --logfile=$LOGFILE \
+		    --uid=$USER \
+		    --gid=$GROUP || echo "FAILED"
+    retval=$?
+    echo
+    [ $retval -eq 0 ] && touch $lockfile
+    return $retval
+}
+
+stop() {
+    echo -n "stopping slavealloc $NAME"
+    kill `cat "${pidfile}"`
+    echo
+    rm -f $lockfile
+    return $retval
+}
+
+restart() {
+    stop && start
+}
+
+reload() {
+    restart
+}
+
+force_reload() {
+    restart
+}
+
+rh_status() {
+    # run checks to determine if the service is running or use generic status
+    if [ -f $pidfile ]; then
+	if kill -0 `cat $pidfile`; then
+	    echo "running"
+	    return 0
+        else
+	    echo "dead but pidfile remains"
+	    return 1
+    	fi
+    else
+	if [ -f $lockfile ]; then
+	    echo "dead but lockfile remains"
+	    return 2
+        else
+	    echo "not running"
+	    return 3
+    	fi
+    fi
+}
+
+rh_status_q() {
+    rh_status >/dev/null 2>&1
+}
+
+
+case "$1" in
+    start)
+        rh_status_q && exit 0
+        $1
+        ;;
+    stop)
+        rh_status_q || exit 0
+        $1
+        ;;
+    restart)
+        $1
+        ;;
+    reload)
+        rh_status_q || exit 7
+        $1
+        ;;
+    force-reload)
+        force_reload
+        ;;
+    status)
+        rh_status
+        ;;
+    condrestart|try-restart)
+        rh_status_q || exit 0
+        restart
+        ;;
+    *)
+        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
+        exit 2
+esac
+exit $?
rename from slavealloc-ui.tac
rename to lib/python/slavealloc/contrib/slavealloc-ui.tac
--- a/slavealloc-ui.tac
+++ b/lib/python/slavealloc/contrib/slavealloc-ui.tac
@@ -1,7 +1,15 @@
 from twisted.application import service
 from slavealloc.daemon.application import Allocator
+from twisted.python.logfile import LogFile
+from twisted.python.log import ILogObserver, FileLogObserver
 
 application = service.Application("slavealloc")
+
+# set up logfile rotation
+logfile = LogFile.fromFullPath("slavealloc.log", rotateLength=1024**2,
+			 maxRotatedFiles=10)
+application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
+
 allocator = Allocator(http_port='tcp:8010', db_url='sqlite:///slavealloc.db',
     run_ui=True)
 allocator.setServiceParent(application)
new file mode 100755
--- /dev/null
+++ b/lib/python/slavealloc/contrib/slavealloc.initd
@@ -0,0 +1,115 @@
+#!/bin/sh
+#
+# slavealloc
+#
+# chkconfig:   345 85 15
+# description: slave allocator daemon
+
+# Source function library.
+. /etc/rc.d/init.d/functions
+
+NAME=production-1080
+BASEDIR=/builds/slavealloc/$NAME
+lockfile=/var/lock/subsys/slavealloc-$NAME
+pidfile=$BASEDIR/twistd.pid
+LOGFILE=$BASEDIR/slavealloc.log
+TACFILE=$BASEDIR/slavealloc.tac
+USER=slavealloc
+GROUP=slavealloc
+VIRTUALENV=/tools/slavealloc
+
+start() {
+    [ -x $VIRTUALENV/bin/twistd ] || exit 5
+    [ -f $TACFILE ] || exit 6
+    echo -n "starting slavealloc $NAME"
+    cd $BASEDIR # --rundir doesn't work with the customized .tac logging
+    daemon $VIRTUALENV/bin/twistd  \
+		    --pidfile=$pidfile \
+		    --rundir=$BASEDIR \
+		    --python=$TACFILE \
+		    --logfile=$LOGFILE \
+		    --uid=$USER \
+		    --gid=$GROUP || echo "FAILED"
+    retval=$?
+    echo
+    [ $retval -eq 0 ] && touch $lockfile
+    return $retval
+}
+
+stop() {
+    echo -n "stopping slavealloc $NAME"
+    kill `cat "${pidfile}"`
+    echo
+    rm -f $lockfile
+    return $retval
+}
+
+restart() {
+    stop && start
+}
+
+reload() {
+    restart
+}
+
+force_reload() {
+    restart
+}
+
+rh_status() {
+    # run checks to determine if the service is running or use generic status
+    if [ -f $pidfile ]; then
+	if kill -0 `cat $pidfile`; then
+	    echo "running"
+	    return 0
+        else
+	    echo "dead but pidfile remains"
+	    return 1
+    	fi
+    else
+	if [ -f $lockfile ]; then
+	    echo "dead but lockfile remains"
+	    return 2
+        else
+	    echo "not running"
+	    return 3
+    	fi
+    fi
+}
+
+rh_status_q() {
+    rh_status >/dev/null 2>&1
+}
+
+
+case "$1" in
+    start)
+        rh_status_q && exit 0
+        $1
+        ;;
+    stop)
+        rh_status_q || exit 0
+        $1
+        ;;
+    restart)
+        $1
+        ;;
+    reload)
+        rh_status_q || exit 7
+        $1
+        ;;
+    force-reload)
+        force_reload
+        ;;
+    status)
+        rh_status
+        ;;
+    condrestart|try-restart)
+        rh_status_q || exit 0
+        restart
+        ;;
+    *)
+        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
+        exit 2
+esac
+exit $?
--- a/setup.py
+++ b/setup.py
@@ -23,10 +23,13 @@ setup(
         'twisted',
         'simplejson',
     ],
 
     entry_points = {
         'console_scripts': [
             'slavealloc = slavealloc.scripts.main:main'
         ],
-    }
+    },
+
+    # include files listed in MANIFEST.in
+    include_package_data = True,
 )