Subversion Repositories SmartDukaan

Rev

Rev 3537 | Rev 4651 | Go to most recent revision | 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
31
        try:
32
            self.environment = os.environ["ENV_TYPE"]
33
        except KeyError:
34
            self.environment = "DEV"
35
 
36
        self.cfg_file_name = os.path.dirname(os.path.realpath(__file__)) + '/resources/shop2020.cfg'
94 ashish 37
 
3915 chandransh 38
        if options.port:
39
            self.port = options.port
40
 
41
        if options.cfg_file_name:
42
            self.cfg_file_name = options.cfg_file_name
43
 
44
        if options.environment:
45
            self.enironment = options.environment
46
 
47
    def run(self):
48
        handler = ConfigServerHandler(self.cfg_file_name, self.environment)
49
        processor = Configuration.Processor(handler)
50
        transport = TSocket.TServerSocket(self.host, self.port)
51
        tfactory = TTransport.TFramedTransportFactory()
52
        pfactory = TBinaryProtocolFactory()
53
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
54
        print "Starting config server with config params, port "+ str(self.port)+" file "+ self.cfg_file_name
55
        sys.stdout.flush()
56
        server.serve()
57
        print "Server functioning" 
58
 
59
def main():
60
    parser = optparse.OptionParser()
61
    parser.add_option("-f", "--file", dest="cfg_file_name",
62
                   type="string",
63
                   help="Read the configuration from FILE",
64
                   metavar="FILE")
65
    parser.add_option("-p", "--port", dest="port",
66
                      type="int",
67
                      help="Start config server on PORT",
68
                      metavar="PORT")
69
    parser.add_option("-e", "--env", dest="environment",
70
                      type="string",
71
                      help="Load properties for ENV environment",
72
                      metavar="ENV")
73
    parser.add_option("-l", "--logfile", dest="logfile",
74
                      type="string",
75
                      help="Log all output to LOG_FILE",
76
                      )
77
    parser.add_option("-i", "--pidfile", dest="pidfile",
78
                      type="string",
79
                      help="Write the PID to pidfile")
80
    (options, args) = parser.parse_args()
81
    config_server = ConfigServer(options)
82
    if len(args) == 0:
83
        config_server.run()
84
    elif len(args) == 1:
85
        if 'start' == args[0]:
86
            config_server.start()
87
        elif 'stop' == args[0]:
88
            config_server.stop()
89
        elif 'restart' == args[0]:
90
            config_server.restart()
91
        else:
92
            print "Unknown command"
93
            sys.exit(2)
94
        sys.exit(0)
95
    else:
96
        print "usage: %s start|stop|restart %s" % sys.argv[0], parser.get_usage()
94 ashish 97
        sys.exit(2)
1292 vikas 98
 
94 ashish 99
if __name__ == '__main__':
3915 chandransh 100
    main()