summaryrefslogtreecommitdiffstats
path: root/bin/keystone-all
blob: 93ce34e44023d5a0aa9df2e220af7a60408d9f62 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4

import greenlet
import logging
import os
import signal
import sys

# If ../keystone/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(__file__),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(possible_topdir,
                               'keystone',
                               '__init__.py')):
    sys.path.insert(0, possible_topdir)


from paste import deploy
import pbr.version

from keystone import config
from keystone.common import wsgi_server
from keystone.common import utils
from keystone.openstack.common import importutils


CONF = config.CONF


def create_server(conf, name, host, port):
    app = deploy.loadapp('config:%s' % conf, name=name)
    server = wsgi_server.Server(app, host=host, port=port)
    if CONF.ssl.enable:
        server.set_ssl(CONF.ssl.certfile, CONF.ssl.keyfile,
                       CONF.ssl.ca_certs, CONF.ssl.cert_required)
    return server


def sigint_handler(signal, frame):
    """Exits at SIGINT signal."""
    logging.debug('SIGINT received, stopping servers.')
    sys.exit(0)


def serve(*servers):
    signal.signal(signal.SIGINT, sigint_handler)

    for server in servers:
        server.start()

    # notify calling process we are ready to serve
    if CONF.onready:
        try:
            notifier = importutils.import_module(CONF.onready)
            notifier.notify()
        except ImportError:
            try:
                utils.check_output(CONF.onready.split())
            except Exception:
                logging.exception('Failed to execute onready command')

    for server in servers:
        try:
            server.wait()
        except greenlet.GreenletExit:
            pass


if __name__ == '__main__':
    dev_conf = os.path.join(possible_topdir,
                            'etc',
                            'keystone.conf')
    config_files = None
    if os.path.exists(dev_conf):
        config_files = [dev_conf]

    CONF(project='keystone',
         version=pbr.version.VersionInfo('keystone').version_string(),
         default_config_files=config_files)

    config.setup_logging(CONF)

    # Log the options used when starting if we're in debug mode...
    if CONF.debug:
        CONF.log_opt_values(logging.getLogger(CONF.prog), logging.DEBUG)

    if CONF.config_file:
        paste_config = CONF.config_file[0]
    else:
        paste_config = CONF.find_file('keystone.conf')
        if not paste_config:
            print ("The keystone.conf file could not be found in the "
                   "configuration directories.")
            CONF.print_help()
            sys.exit(1)

    monkeypatch_thread = not CONF.standard_threads
    pydev_debug_url = utils.setup_remote_pydev_debug()
    if pydev_debug_url:
        # in order to work around errors caused by monkey patching we have to
        # set the thread to False.  An explanation is here:
        # http://lists.openstack.org/pipermail/openstack-dev/2012-August/
        # 000794.html
        monkeypatch_thread = False
    wsgi_server.monkey_patch_eventlet(monkeypatch_thread=monkeypatch_thread)

    options = deploy.appconfig('config:%s' % paste_config)

    servers = []
    servers.append(create_server(paste_config,
                                 'admin',
                                 CONF.bind_host,
                                 int(CONF.admin_port)))
    servers.append(create_server(paste_config,
                                 'main',
                                 CONF.bind_host,
                                 int(CONF.public_port)))
    serve(*servers)