iotronic/iotronic/conductor/manager.py

102 lines
3.5 KiB
Python

# coding=utf-8
# 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.
from iotronic.common import exception
from iotronic.common.i18n import _LI
from iotronic.common.i18n import _LW
from iotronic.conductor import endpoints as endp
from iotronic.db import api as dbapi
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
import time
LOG = logging.getLogger(__name__)
MANAGER_TOPIC = 'iotronic.conductor_manager'
conductor_opts = [
cfg.StrOpt('api_url',
help='URL of Iotronic API service. If not set iotronic can '
'get the current value from the keystone service '
'catalog.'),
cfg.IntOpt('heartbeat_timeout',
default=60,
help='Maximum time (in seconds) since the last check-in '
'of a conductor. A conductor is considered inactive '
'when this time has been exceeded.'),
]
CONF = cfg.CONF
CONF.register_opts(conductor_opts, 'conductor')
class ConductorManager(object):
RPC_API_VERSION = '1.0'
def __init__(self, host):
logging.register_options(CONF)
CONF(project='iotronic')
logging.setup(CONF, "iotronic-conductor")
if not host:
host = CONF.host
self.host = host
self.topic = MANAGER_TOPIC
self.dbapi = dbapi.get_instance()
try:
cdr = self.dbapi.register_conductor(
{'hostname': self.host})
except exception.ConductorAlreadyRegistered:
LOG.warn(_LW("A conductor with hostname %(hostname)s "
"was previously registered. Updating registration"),
{'hostname': self.host})
cdr = self.dbapi.register_conductor({'hostname': self.host},
update_existing=True)
self.conductor = cdr
transport = oslo_messaging.get_transport(cfg.CONF)
target = oslo_messaging.Target(topic=self.topic, server=self.host,
version=self.RPC_API_VERSION)
endpoints = [
endp.ConductorEndpoint(),
]
server = oslo_messaging.get_rpc_server(transport, target, endpoints,
executor='threading')
try:
server.start()
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping server")
server.stop()
# server.wait()
def del_host(self, deregister=True):
if deregister:
try:
self.dbapi.unregister_conductor(self.host)
LOG.info(_LI('Successfully stopped conductor with hostname '
'%(hostname)s.'),
{'hostname': self.host})
except exception.ConductorNotFound:
pass
else:
LOG.info(_LI('Not deregistering conductor with hostname '
'%(hostname)s.'),
{'hostname': self.host})