Subversion Repositories SmartDukaan

Rev

Rev 3537 | Rev 4651 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3537 Rev 3915
Line -... Line 1...
-
 
1
#!/usr/bin/env python
-
 
2
 
1
'''
3
'''
2
Created on 23-Feb-2010
4
Created on 23-Feb-2010
3
 
5
 
4
@author: ashish
6
@author: ashish
5
@author: rajveer
7
@author: rajveer
-
 
8
@author: Chandranshu
6
'''
9
'''
7
import os
10
import os
8
import sys
11
import sys
9
from getopt import getopt, GetoptError
12
import optparse
-
 
13
 
10
from shop2020.config.impl.ConfigServerHandler import ConfigServerHandler
14
if __name__ == '__main__' and __package__ is None:
-
 
15
    sys.path.insert(0, os.getcwd())
-
 
16
 
11
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
17
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
12
from shop2020.thriftpy.config import Configuration
-
 
13
from thrift.transport import TSocket, TTransport
18
from thrift.transport import TSocket, TTransport
14
from thrift.server import TServer
19
from thrift.server import TServer
15
def main(argv):
-
 
16
        
-
 
17
    try:
-
 
18
        opts, args = getopt(argv,"p:f:e:", ["port=", "file=", "env="])
-
 
19
    except GetoptError:
-
 
20
        usage()
-
 
21
        sys.exit(2)
-
 
22
 
20
 
23
    try:
-
 
24
        environment = os.environ["ENV_TYPE"]
21
from shop2020.utils.daemon import Daemon
25
    except KeyError:
-
 
26
        environment = "DEV"
-
 
27
    
-
 
28
    cfg_file_name = os.path.dirname(os.path.realpath(__file__)) + '/resources/shop2020.cfg'
22
from shop2020.config.impl.ConfigServerHandler import ConfigServerHandler
29
    
-
 
30
    host = "localhost"
-
 
31
    port = 9999
-
 
32
    for opt,arg in opts:
-
 
33
        if opt in ('-p', "--port"):
-
 
34
            port = arg;
-
 
35
        elif opt in ('-f', "--file"):
-
 
36
            cfg_file_name = arg
-
 
37
        elif opt in ('-e', "--env"):
-
 
38
            environment = arg
-
 
39
    
-
 
40
    start_server(cfg_file_name, host, port, environment)
23
from shop2020.thriftpy.config import Configuration
41
    
24
 
42
def start_server(filename, host, port, environment):
-
 
43
    handler = ConfigServerHandler(filename, environment)
-
 
44
    processor = Configuration.Processor(handler)
-
 
45
    transport = TSocket.TServerSocket(host, port)
-
 
46
    tfactory = TTransport.TFramedTransportFactory()
-
 
47
    pfactory = TBinaryProtocolFactory()
-
 
48
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
-
 
49
    print "Starting config server with config params, port "+ str(port)+" file "+ filename
-
 
50
    server.serve()
-
 
51
    print "Server functioning"
25
class ConfigServer(Daemon):
52
    
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'
-
 
37
        
-
 
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
 
53
def usage():
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:
54
    print 'Usage: ConfigServer.py -p <port_to_run> -f <config_file_path> -e <environment> or ConfigServer.py --port=<port_to_run> --file=<config_file_path> --env=<environment>' 
96
        print "usage: %s start|stop|restart %s" % sys.argv[0], parser.get_usage()
-
 
97
        sys.exit(2)
55
 
98
 
56
if __name__ == '__main__':
99
if __name__ == '__main__':
57
    main(sys.argv[1:])
-
 
58
    
100
    main()