/
service.py
169 lines (132 loc) · 5.33 KB
/
service.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2011 Justin Santa Barbara
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import inspect
from oslo import messaging
from oslo.config import cfg
from designate.openstack.common import service
from designate.openstack.common import log as logging
from designate.openstack.deprecated import wsgi
from designate.i18n import _
from designate import rpc
from designate import policy
from designate import version
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
class Service(service.Service):
"""
Service class to be shared among the diverse service inside of Designate.
"""
def __init__(self, threads=1000):
super(Service, self).__init__(threads)
policy.init()
# NOTE(kiall): All services need RPC initialized, as this is used
# for clients AND servers. Hence, this is common to
# all Designate services.
if not rpc.initialized():
rpc.init(CONF)
class RPCService(Service):
"""
Service class to be shared by all Designate RPC Services
"""
def __init__(self, host, binary, topic, service_name=None, endpoints=None):
super(RPCService, self).__init__()
self.host = host
self.binary = binary
self.topic = topic
self.service_name = service_name
# TODO(ekarlso): change this to be loadable via mod import or
# stevedore?
self.endpoints = endpoints or [self]
def start(self):
super(RPCService, self).start()
version_string = version.version_info.version_string()
LOG.info(_('Starting %(topic)s node (version %(version_string)s)') %
{'topic': self.topic, 'version_string': version_string})
LOG.debug(_("Creating RPC server on topic '%s'") % self.topic)
target = messaging.Target(topic=self.topic, server=self.host)
self.rpcserver = rpc.get_server(target, self.endpoints)
self.rpcserver.start()
self.notifier = rpc.get_notifier(self.service_name)
for e in self.endpoints:
if e != self and hasattr(e, 'start'):
e.start()
@classmethod
def create(cls, host=None, binary=None, topic=None, service_name=None,
endpoints=None):
"""Instantiates class and passes back application object.
:param host: defaults to CONF.host
:param binary: defaults to basename of executable
:param topic: defaults to bin_name - 'cinder-' part
"""
if not host:
host = CONF.host
if not binary:
binary = os.path.basename(inspect.stack()[-1][1])
if not topic:
name = "_".join(binary.split('-')[1:]) + '_topic'
topic = CONF.get(name)
service_obj = cls(host, binary, topic, service_name=service_name,
endpoints=endpoints)
return service_obj
def stop(self):
for e in self.endpoints:
if e != self and hasattr(e, 'stop'):
e.stop()
# Try to shut the connection down, but if we get any sort of
# errors, go ahead and ignore them.. as we're shutting down anyway
try:
self.rpcserver.stop()
except Exception:
pass
super(RPCService, self).stop()
def wait(self):
for e in self.endpoints:
if e != self and hasattr(e, 'wait'):
e.wait()
super(RPCService, self).wait()
class WSGIService(wsgi.Service, Service):
"""
Service class to be shared by all Designate WSGI Services
"""
def __init__(self, application, port, host='0.0.0.0', backlog=4096,
threads=1000):
# NOTE(kiall): We avoid calling super(cls, self) here, as our parent
# classes have different argspecs. Additionally, if we
# manually call both parent's __init__, the openstack
# common Service class's __init__ method will be called
# twice. As a result, we only call the designate base
# Service's __init__ method, and duplicate the
# wsgi.Service's constructor functionality here.
#
Service.__init__(self, threads)
self.application = application
self._port = port
self._host = host
self._backlog = backlog if backlog else CONF.backlog
_launcher = None
def serve(server, workers=None):
global _launcher
if _launcher:
raise RuntimeError(_('serve() can only be called once'))
_launcher = service.launch(server, workers=workers)
def wait():
try:
_launcher.wait()
except KeyboardInterrupt:
_launcher.stop()
rpc.cleanup()