Subversion Repositories SmartDukaan

Rev

Rev 4655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3915 chandransh 1
#!/usr/bin/env python
2
 
94 ashish 3
'''
4
Created on 23-Feb-2010
5
 
6
@author: ashish
3130 rajveer 7
@author: rajveer
3915 chandransh 8
@author: Chandranshu
94 ashish 9
'''
3130 rajveer 10
import os
94 ashish 11
import sys
3915 chandransh 12
import optparse
13
 
14
if __name__ == '__main__' and __package__ is None:
15
    sys.path.insert(0, os.getcwd())
16
 
94 ashish 17
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
18
from thrift.transport import TSocket, TTransport
19
from thrift.server import TServer
3915 chandransh 20
 
21
from shop2020.utils.daemon import Daemon
22
from shop2020.config.impl.ConfigServerHandler import ConfigServerHandler
23
from shop2020.thriftpy.config import Configuration
24
 
25
class ConfigServer(Daemon):
26
 
27
    def __init__(self, options):
28
        Daemon.__init__(self, options.pidfile, stdout=options.logfile, stderr=options.logfile)
29
        self.host = "localhost"
30
        self.port = 9999
4651 rajveer 31
 
32
        if options.environment:
4655 phani.kuma 33
            self.environment = options.environment
4651 rajveer 34
        else:
6511 kshitij.so 35
            self.environment = 'DEV'
3915 chandransh 36
 
37
        self.cfg_file_name = os.path.dirname(os.path.realpath(__file__)) + '/resources/shop2020.cfg'
94 ashish 38
 
3915 chandransh 39
        if options.port:
40
            self.port = options.port
41
 
42
        if options.cfg_file_name:
43
            self.cfg_file_name = options.cfg_file_name
44
 
45
 
46
    def run(self):
47
        handler = ConfigServerHandler(self.cfg_file_name, self.environment)
48
        processor = Configuration.Processor(handler)
49
        transport = TSocket.TServerSocket(self.host, self.port)
50
        tfactory = TTransport.TFramedTransportFactory()
51
        pfactory = TBinaryProtocolFactory()
52
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
53
        print "Starting config server with config params, port "+ str(self.port)+" file "+ self.cfg_file_name
54
        sys.stdout.flush()
55
        server.serve()
56
        print "Server functioning" 
57
 
58
def main():
59
    parser = optparse.OptionParser()
60
    parser.add_option("-f", "--file", dest="cfg_file_name",
61
                   type="string",
62
                   help="Read the configuration from FILE",
63
                   metavar="FILE")
64
    parser.add_option("-p", "--port", dest="port",
65
                      type="int",
66
                      help="Start config server on PORT",
67
                      metavar="PORT")
68
    parser.add_option("-e", "--env", dest="environment",
69
                      type="string",
70
                      help="Load properties for ENV environment",
71
                      metavar="ENV")
72
    parser.add_option("-l", "--logfile", dest="logfile",
73
                      type="string",
74
                      help="Log all output to LOG_FILE",
75
                      )
76
    parser.add_option("-i", "--pidfile", dest="pidfile",
77
                      type="string",
78
                      help="Write the PID to pidfile")
79
    (options, args) = parser.parse_args()
80
    config_server = ConfigServer(options)
81
    if len(args) == 0:
82
        config_server.run()
83
    elif len(args) == 1:
84
        if 'start' == args[0]:
85
            config_server.start()
86
        elif 'stop' == args[0]:
87
            config_server.stop()
88
        elif 'restart' == args[0]:
89
            config_server.restart()
90
        else:
91
            print "Unknown command"
92
            sys.exit(2)
93
        sys.exit(0)
94
    else:
95
        print "usage: %s start|stop|restart %s" % sys.argv[0], parser.get_usage()
94 ashish 96
        sys.exit(2)
1292 vikas 97
 
94 ashish 98
if __name__ == '__main__':
3915 chandransh 99
    main()