aboutsummaryrefslogtreecommitdiffstats
path: root/storm-server
blob: 95544269fdecdc87d168f5305eb293003af3fbd5 (plain)
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
#!/usr/bin/env python

import sys
import getopt
import os.path
import logging

from storm.daemon import Daemon

class StormDaemon(Daemon):
    def run(self):
        from storm.xmlrpc import run_server
        run_server(self.log_level, self.config_file)

def usage():
    print """
Usage: %s [options]
Options:
    -c, --config <file> - Use given config file instead of default
    -v - Debug/verbose logging
    -d, --start - Run in background (daemon mode)
    -r, --restart - Restart daemon
    -s, --stop - Stop daemon
    -h, --help - This help
""" % sys.argv[0]

if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('utf-8')
    sys.dont_write_bytecode = True

    log_level = logging.INFO
    config_file = ''

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hc:drsv', ['help', 'config=', 'start', 'stop', 'restart'])
    except getopt.GetoptError, e:
        print str(e)
        usage()
        sys.exit(2)

    action = 'run'
    
    for o, a in opts:
        if o in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif o in ('-v',):
            log_level = logging.DEBUG
        elif o in ('-c', '--config'):
            if os.path.isfile(a):
                config_file = a
        elif o in ('-d', '--start'):
            action = 'start'
        elif o in ('-r', '--restart'):
            action = 'restart'
        elif o in ('-s', '--stop'):
            action = 'stop'

    # Find default config file
    if not config_file:
        if os.path.isfile(os.path.join(sys.path[0], 'storm.conf')):
            # Try local config file
            config_file = os.path.join(sys.path[0], 'storm.conf')
        # Check for config file in /etc/storm/storm.conf
        elif os.path.isfile('/etc/storm/storm.conf'):
            config_file = '/etc/storm/storm.conf'

    if action == 'run':
        from storm.xmlrpc import run_server
        run_server(log_level, config_file)
    else:
        stormid = StormDaemon('/var/run/storm.pid',stdout='/var/log/storm.log',stderr='/var/log/storm.err.log')
        stormid.log_level = log_level
        stormid.config_file = config_file

        if action == 'start':
            stormid.start()
        elif action == 'stop':
            stormid.stop()
        elif action == 'restart':
            stormid.restart()
        else:
            usage()
            sys.exit(2)

    sys.exit(0)