basic protection service: Implement a runnable service

As a basic service, it takes a manager and enables
rpc by listening to queues based on topic.

It also periodically runs tasks on the manager and
reports it state to the database services table.

start smaug protection service
python /usr/local/bin/smaug-protection --config-file
/etc/smaug/smaug.conf

Change-Id: I304a394cac58a4199354f3f54384db703bcc0001
Closes-Bug: #1529207
This commit is contained in:
chenying 2015-12-26 10:12:28 +08:00
parent 033064e93c
commit c88cd3950c
8 changed files with 182 additions and 1 deletions

View File

@ -34,6 +34,7 @@ console_scripts =
smaug-api = smaug.cmd.api:main
smaug-manage = smaug.cmd.manage:main
smaug-scheduler = smaug.cmd.scheduler:main
smaug-protection = smaug.cmd.protection:main
smaug.database.migration_backend =
sqlalchemy = oslo_db.sqlalchemy.migration

42
smaug/cmd/protection.py Normal file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env python
# 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.
"""Starter script for smaug protection service."""
import eventlet
eventlet.monkey_patch()
import sys
from oslo_config import cfg
from oslo_log import log as logging
from smaug import i18n
i18n.enable_lazy()
# Need to register global_opts
from smaug.common import config # noqa
from smaug import service
from smaug import version
CONF = cfg.CONF
def main():
CONF(sys.argv[1:], project='smaug',
version=version.version_string())
logging.setup(CONF, "smaug")
server = service.Service.create(binary='smaug-protection')
service.serve(server)
service.wait()

View File

@ -55,6 +55,12 @@ global_opts = [
cfg.StrOpt('scheduler_manager',
default='smaug.scheduler.manager.SchedulerManager',
help='Full class name for the Manager for scheduler'),
cfg.StrOpt('protection_topic',
default='smaug-protection',
help='The topic that protection nodes listen on'),
cfg.StrOpt('protection_manager',
default='smaug.protection.manager.ProtectionManager',
help='Full class name for the Manager for Protection'),
cfg.StrOpt('host',
default=socket.gethostname(),
help='Name of this node. This can be an opaque identifier. '

View File

37
smaug/protection/api.py Normal file
View File

@ -0,0 +1,37 @@
# 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.
"""Handles all requests relating to protection service."""
from oslo_config import cfg
from oslo_log import log as logging
from smaug.db import base
from smaug.protection import rpcapi as protection_rpcapi
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
class API(base.Base):
"""API for interacting with the protection manager."""
def __init__(self, db_driver=None):
self.protection_rpcapi = protection_rpcapi.ProtectionAPI()
super(API, self).__init__(db_driver)
def create_test_protection(self, context, request_spec):
self.protection_rpcapi.create_test_protection(context, request_spec)

View File

@ -0,0 +1,42 @@
# 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.
"""
Protection Service
"""
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging as messaging
from smaug import manager
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
class ProtectionManager(manager.Manager):
"""Smaug Protection Manager."""
RPC_API_VERSION = '1.0'
target = messaging.Target(version=RPC_API_VERSION)
def __init__(self, service_name=None,
*args, **kwargs):
super(ProtectionManager, self).__init__(*args, **kwargs)
def create_test_protection(self, context, request_spec=None):
LOG.debug("Received a rpc call from a scheduler service."
"request_spec:%s", request_spec)

View File

@ -0,0 +1,51 @@
# Copyright 2012, Red Hat, Inc.
#
# 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.
"""
Client side of the protection manager RPC API.
"""
from oslo_config import cfg
import oslo_messaging as messaging
from oslo_serialization import jsonutils
from smaug import rpc
CONF = cfg.CONF
class ProtectionAPI(object):
"""Client side of the protection rpc API.
API version history:
1.0 - Initial version.
"""
RPC_API_VERSION = '1.0'
def __init__(self):
super(ProtectionAPI, self).__init__()
target = messaging.Target(topic=CONF.protection_topic,
version=self.RPC_API_VERSION)
self.client = rpc.get_client(target, version_cap=None)
def create_test_protection(self, ctxt, request_spec=None):
request_spec_p = jsonutils.to_primitive(request_spec)
cctxt = self.client.prepare(version='1.0')
return cctxt.cast(
ctxt,
'create_test_protection',
request_spec=request_spec_p)

View File

@ -19,7 +19,7 @@ from oslo_log import log as logging
import oslo_messaging as messaging
from smaug import manager
from smaug.protection import api as protection_api
CONF = cfg.CONF
@ -35,8 +35,10 @@ class SchedulerManager(manager.Manager):
def __init__(self, service_name=None,
*args, **kwargs):
self.protection_api = protection_api.API()
super(SchedulerManager, self).__init__(*args, **kwargs)
def create_test_scheduler(self, context, request_spec=None):
LOG.debug("Received a rpc call from a api service."
"request_spec:%s", request_spec)
self.protection_api.create_test_protection(context, request_spec)