add escalator frame
[escalator.git] / api / escalator / cmd / api.py
1 #!/usr/bin/env python
2
3 # Copyright 2010 United States Government as represented by the
4 # Administrator of the National Aeronautics and Space Administration.
5 # Copyright 2011 OpenStack Foundation
6 # All Rights Reserved.
7 #
8 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
9 #    not use this file except in compliance with the License. You may obtain
10 #    a copy of the License at
11 #
12 #         http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #    Unless required by applicable law or agreed to in writing, software
15 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 #    License for the specific language governing permissions and limitations
18 #    under the License.
19
20 """
21 Escalator API Server
22 """
23
24 import os
25 import sys
26
27 import eventlet
28
29 from oslo_service import systemd
30 from oslo_config import cfg
31 from oslo_log import log as logging
32 import osprofiler.notifier
33 import osprofiler.web
34
35 from escalator.common import utils
36 from escalator.common import config
37 from escalator.common import exception
38 from escalator.common import wsgi
39 from escalator import notifier
40
41
42 # Monkey patch socket, time, select, threads
43 eventlet.patcher.monkey_patch(all=False, socket=True, time=True,
44                               select=True, thread=True, os=True)
45
46 # If ../escalator/__init__.py exists, add ../ to Python search path, so that
47 # it will override what happens to be installed in /usr/(local/)lib/python...
48 possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
49                                    os.pardir,
50                                    os.pardir))
51 if os.path.exists(os.path.join(possible_topdir, 'escalator', '__init__.py')):
52     sys.path.insert(0, possible_topdir)
53
54
55 CONF = cfg.CONF
56 CONF.import_group("profiler", "escalator.common.wsgi")
57 logging.register_options(CONF)
58
59
60 def fail(e):
61     sys.exit(100)
62
63
64 def main():
65     try:
66         config.parse_args()
67         wsgi.set_eventlet_hub()
68         logging.setup(CONF, 'escalator')
69
70         if cfg.CONF.profiler.enabled:
71             _notifier = osprofiler.notifier.create("Messaging",
72                                                    notifier.messaging, {},
73                                                    notifier.get_transport(),
74                                                    "escalator", "api",
75                                                    cfg.CONF.bind_host)
76             osprofiler.notifier.set(_notifier)
77         else:
78             osprofiler.web.disable()
79
80         server = wsgi.Server()
81         server.start(config.load_paste_app('escalator-api'), default_port=9393)
82         systemd.notify_once()
83         server.wait()
84     except Exception as e:
85         fail(e)
86
87
88 if __name__ == '__main__':
89     main()