Re: scgi server start

Neil Schemenauer <[email protected]> Fri, 23 Mar 2007 12:22:24 -0600
Newsgroups gmane.comp.web.quixote.user
Message-ID <[email protected]>
Michael Watkins <[email protected]> wrote:
> Perhaps someone might want to make a qp command line work-alike
> for Quixote?

Attached is a startup script (qrun.py).  It's not well tested (I
just threw it together) and it's not really like the qp tool but
maybe it's useful.  I also attached an init.d script that is
supposed to work on Linux systems (e.g. Red Hat, Debian, SuSE).
Again, it's not well tested.

  Neil

_______________________________________________
Quixote-users mailing list
[email protected]
http://mail.mems-exchange.org/mailman/listinfo/quixote-users
qrun.py (text/plain, 2.7 KB)
#!/usr/bin/env python

import sys
import os
import optparse
import pwd, grp
from quixote.util import import_object

def drop_root(user):
    # drop root if necessary
    if os.getuid() == 0:
        uid, gid = pwd.getpwnam(user)[2:4]
        os.setgid(gid)
        os.setuid(uid)

def fork_background():
    pid = os.fork()
    if pid != 0:
        sys.exit(0)
    os.chdir('/')
    os.setsid()
    os.umask(0)
    pid = os.fork()
    if pid != 0:
        sys.exit(0)

def redirect_output(filename):
    sys.stdout = sys.stderr = open(filename, 'a', 1)
    os.dup2(sys.stdout.fileno(), 1)
    os.dup2(sys.stderr.fileno(), 2)
    os.close(0)

def write_pid(filename):
    fp = open(filename, 'w')
    fp.write(str(os.getpid()))
    fp.close()

def main():
    parser = optparse.OptionParser()
    parser.add_option('--factory', default='quixote.demo.create_publisher',
                      help='Path to factory function to create the site '
                           'publisher.')
    parser.add_option('--pidfile', default=None,
                      help='If provided, the PID of the server process will '
                           'be written to this file.')
    parser.add_option('--background', action='store_true', default=False,
                      help='Fork server process into the background.')
    parser.add_option('--server-type', default='simple',
                      choices=['simple', 'scgi'],
                      help='Type of server to run ("simple" or "scgi")')
    parser.add_option('--host', dest="host", default='localhost',
                      help="Host interface to listen on.")
    parser.add_option('--port', dest="port", default=None, type="int",
                      help="Port to listen on.")
    parser.add_option('--user', default='web',
                      help='If started as root, use setuid to run as the '
                           'specified user.')
    parser.add_option('--logfile', default=None,
                      help='If specified, stderr and stdout will be redirected '
                           'to the file.')
    options, args = parser.parse_args()
    if options.background:
        fork_background()
    if options.pidfile:
        write_pid(options.pidfile)
    drop_root(options.user)
    if options.logfile:
        redirect_output(options.logfile)
    create_publisher = import_object(options.factory)
    if options.server_type == 'simple':
        from quixote.server.simple_server import run
        run(create_publisher, host=options.host, port=options.port or 8080)
    elif options.server_type == 'scgi':
        from quixote.server.scgi_server import run
        run(create_publisher, host=options.host, port=options.port or 3000)
    else:
        assert 0


if __name__ == '__main__':
    main()
qx (text/plain, 2 KB)
#!/bin/sh
# System startup script for Quixote web app.

SERVER_TYPE=scgi # "simple" or "scgi"
USER=web
PIDFILE=/var/run/quixote.pid

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
DAEMON=$USER/bin/qrun.py
ARGS="--server-type=$SERVER_TYPE --user=$USER \
	--pidfile $PIDFILE --background"
test -x $DAEMON || { echo "$DAEMON not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Source LSB init functions
.. /lib/lsb/init-functions

if test -f /etc/redhat-release; then
	# Even though it claims to conform to LSB 3.0, Red Hat (as of
	# Enterprise Linux ES release 4) does not accept the -p argument
	# to start_daemon, pidofproc, and killproc
	running () {
		##pidofproc -p $PIDFILE $DAEMON >/dev/null
		test -f $PIDFILE && ps -p `cat $PIDFILE` > /dev/null
	}

	do_start () {
		$DAEMON $ARGS
	}
	do_kill ()  {
		if running; then
			kill -TERM `cat $PIDFILE`
			status=$?
			rm -f $PIDFILE
			return $status
		fi
	}
else
	running () {
		pidofproc -p $PIDFILE $DAEMON >/dev/null
	}
	do_start () {
		start_daemon -p $PIDFILE $DAEMON $ARGS
	}
	do_kill () {
		killproc -p $PIDFILE $DAEMON
	}
fi

case "$1" in
    start)
	echo -n "Starting Quixote server... "
	if ! running 
	then
		if do_start; then
			log_success_msg "okay."
		else
			log_failure_msg "failed."
		fi
	else
		log_success_msg "okay."
	fi
	;;
    stop)
	echo -n "Shutting down Quixote server... "
	if do_kill; then
		log_success_msg "stopped."
	fi
	;;
    try-restart)
	## Do a restart only if the service was active before.
	if running 
	then
		$0 restart
	fi
	;;
    restart)
	$0 stop
	$0 start
	;;
    force-reload)
	echo -n "Reload Quixote server "
	$0 try-restart
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signaling, do nothing (!)
	exit 3
	;;
    status)
	echo -n "Checking for Quixote server... "
	if running
	then
		echo "running."
		exit 0
	else
		echo "stopped."
		exit 3
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac