Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 98 lines (78 sloc) 3.037 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#!/usr/bin/env python
# coding=utf8

# Copyright (C) 2011 Saúl Ibarra Corretgé <saghul@gmail.com>
#

import os
import signal
import sys

from optparse import OptionParser

from application import log
from application.process import process, ProcessError

import sipwping


def stop_server(*args):
    from sipwping.app import SIPOptionsApplication
    app = SIPOptionsApplication()
    app.stop()

def main():
    name = 'sip-wping-server'
    fullname = 'SIPwPing'
    runtime_directory = '/var/run/sipwping'
    system_config_directory = '/etc/sipwping'
    default_pid = os.path.join(runtime_directory, 'server.pid')
    default_config = os.path.join(system_config_directory , sipwping.cfg_filename)

    parser = OptionParser(version='%%prog %s' % sipwping.__version__)
    parser.add_option('--no-fork', action='store_false', dest='fork', default=1,
                      help='run the process in the foreground (for debugging)')
    parser.add_option('--pid', dest='pid_file',
                      help='pid file ("%s")' % default_pid, metavar='File')
    parser.add_option('--config-file', dest='config_file', default=default_config,
                      help='path to configuration file to read ("%s")' % default_config,
                      metavar='File')
    (options, args) = parser.parse_args()

    path, configuration_filename = os.path.split(options.config_file)
    if path:
        system_config_directory = path

    process.system_config_directory = system_config_directory
    sipwping.cfg_filename = process.config_file(configuration_filename)

    # when run in foreground, do not require root access because of runtime directory
    if not options.fork:
        process._runtime_directory = None
    else:
        try:
            process.runtime_directory = runtime_directory
        except ProcessError, e:
            log.fatal("Cannot start %s: %s" % (fullname, e))
            sys.exit(1)

    pid_file = options.pid_file or default_pid

    if options.fork:
        try:
            process.daemonize(pid_file)
        except ProcessError, e:
            log.fatal("Cannot start %s: %s" % (fullname, e))
            sys.exit(1)
        log.start_syslog(name)

    if sipwping.cfg_filename:
        log.msg("Starting %s %s, config=%s" % (fullname, sipwping.__version__, sipwping.cfg_filename))
    else:
        log.fatal('Config file not found')
        sys.exit(1)

    try:
        from sipwping.app import SIPOptionsApplication
        app = SIPOptionsApplication()
    except Exception, e:
        log.fatal("failed to create %s: %s" % (fullname, e))
        log.err()
        sys.exit(1)

    process.signals.add_handler(signal.SIGTERM, stop_server)
    process.signals.add_handler(signal.SIGINT, stop_server)

    try:
        app.start()
        signal.pause()
        app.stop_event.wait()
        log.msg("%s stopped" % fullname)
    except Exception, e:
        log.fatal("failed to run %s: %s" % (fullname, e))
        log.err()
        sys.exit(1)


if __name__ == "__main__":
    main()
Something went wrong with that request. Please try again.