Files
distcloud/distributedcloud/dcmanager/orchestrator/states/base.py
Jessica Castelino eb97f4c8b6 Move dcmanager orchestration to a separate process
1) Remove DC manager orchestration from dcmanager-manager process
2) Create dcmanager-orchestrator process and associated files
3) Add new RPC calls for dcmanager-orchestrator process to notify
dcmanager
4) Create/update unit tests, to verify the implementation
changes

Story: 2007267
Task: 40734
Change-Id: Ibbbae77558a8a8fd95b636fa6c3aebb1dfefb514
Signed-off-by: Jessica Castelino <jessica.castelino@windriver.com>
2020-09-14 11:17:06 -04:00

137 lines
4.6 KiB
Python

#
# Copyright (c) 2020 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import abc
import six
from oslo_log import log as logging
from dccommon.drivers.openstack.barbican import BarbicanClient
from dccommon.drivers.openstack.sdk_platform import OpenStackDriver
from dccommon.drivers.openstack.sysinv_v1 import SysinvClient
from dccommon.drivers.openstack.vim import VimClient
from dcmanager.common import consts
from dcmanager.common import context
LOG = logging.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta)
class BaseState(object):
def __init__(self, next_state, region_name):
super(BaseState, self).__init__()
self.next_state = next_state
self.context = context.get_admin_context()
self._stop = None
self.region_name = region_name
def override_next_state(self, next_state):
self.next_state = next_state
def registerStopEvent(self, stop_event):
"""Store an orch_thread threading.Event to detect stop."""
self._stop = stop_event
def stopped(self):
"""Check if the threading.Event is set, otherwise return False."""
if self._stop is not None:
return self._stop.isSet()
else:
return False
def debug_log(self, strategy_step, details):
LOG.debug("Stage: %s, State: %s, Subcloud: %s, Details: %s"
% (strategy_step.stage,
strategy_step.state,
self.get_region_name(strategy_step),
details))
def info_log(self, strategy_step, details):
LOG.info("Stage: %s, State: %s, Subcloud: %s, Details: %s"
% (strategy_step.stage,
strategy_step.state,
self.get_region_name(strategy_step),
details))
def warn_log(self, strategy_step, details):
LOG.warn("Stage: %s, State: %s, Subcloud: %s, Details: %s"
% (strategy_step.stage,
strategy_step.state,
self.get_region_name(strategy_step),
details))
def error_log(self, strategy_step, details):
LOG.error("Stage: %s, State: %s, Subcloud: %s, Details: %s"
% (strategy_step.stage,
strategy_step.state,
self.get_region_name(strategy_step),
details))
@staticmethod
def get_region_name(strategy_step):
"""Get the region name for a strategy step"""
if strategy_step.subcloud_id is None:
# This is the SystemController.
return consts.DEFAULT_REGION_NAME
return strategy_step.subcloud.name
@staticmethod
def get_keystone_client(region_name=consts.DEFAULT_REGION_NAME):
"""Construct a (cached) keystone client (and token)"""
try:
os_client = OpenStackDriver(region_name=region_name,
region_clients=None)
return os_client.keystone_client
except Exception:
LOG.warning('Failure initializing KeystoneClient for region: %s'
% region_name)
raise
def get_sysinv_client(self, region_name):
"""construct a sysinv client
todo(abailey): determine if this client can be cached
"""
keystone_client = self.get_keystone_client(region_name)
return SysinvClient(region_name, keystone_client.session)
@property
def local_sysinv(self):
return self.get_sysinv_client(consts.DEFAULT_REGION_NAME)
@property
def subcloud_sysinv(self):
return self.get_sysinv_client(self.region_name)
def get_barbican_client(self, region_name):
"""construct a barbican client
"""
keystone_client = self.get_keystone_client(region_name)
return BarbicanClient(region_name, keystone_client.session)
@staticmethod
def get_vim_client(region_name):
"""construct a vim client for a region."""
# If keystone client fails to initialze, raise an exception
# a cached keystone client is used if valid
os_client = OpenStackDriver(region_name=region_name,
region_clients=None)
return VimClient(region_name,
os_client.keystone_client.session)
@abc.abstractmethod
def perform_state_action(self, strategy_step):
"""Perform the action for this state on the strategy_step
Returns the next state in the state machine on success.
Any exceptions raised by this method set the strategy to FAILED.
"""
pass