Remove LegacyRoleResolver

LegacyRoleResolver is removed and replaced with RoleResolver.

RoleResolver is created once where before it was created several times
in a loop.

Also, typo in StandardConfigRolesHook class name is fixed.

Change-Id: Iccddec8948b47aa7441395af25782904f3ea7836
Closes-bug: 1546175
This commit is contained in:
Maciej Kwiek 2016-02-17 10:33:56 +01:00
parent 6371fe5028
commit 5bac79d092
7 changed files with 30 additions and 60 deletions

View File

@ -30,6 +30,7 @@ from nailgun import objects
from nailgun.orchestrator import priority_serializers as ps
from nailgun.orchestrator.tasks_serializer import TaskSerializers
from nailgun.policy.name_match import NameMatchingPolicy
from nailgun.utils.role_resolver import RoleResolver
class DeploymentGraph(nx.DiGraph):
@ -417,13 +418,15 @@ class AstuteGraph(object):
:param nodes: list of node db objects
"""
serialized = []
role_resolver = RoleResolver(nodes)
for task in tasks:
if self.graph.should_exclude_task(task['id']):
continue
serializer = self.serializers.get_stage_serializer(task)(
task, self.cluster, nodes)
task, self.cluster, nodes, role_resolver)
if not serializer.should_execute():
continue

View File

@ -21,10 +21,10 @@ import itertools
from nailgun import consts
from nailgun.errors import errors
from nailgun.logger import logger
from nailgun.orchestrator.tasks_serializer import LegacyRoleResolver
import nailgun.orchestrator.tasks_templates as templates
from nailgun.plugins.manager import PluginManager
from nailgun.settings import settings
from nailgun.utils.role_resolver import RoleResolver
class BasePluginDeploymentHooksSerializer(object):
@ -40,7 +40,7 @@ class BasePluginDeploymentHooksSerializer(object):
self.cluster = cluster
self.nodes = nodes
self.role_resolver = role_resolver or LegacyRoleResolver(nodes)
self.role_resolver = role_resolver or RoleResolver(nodes)
def deployment_tasks(self, plugins, stage):
plugin_tasks = []

View File

@ -35,15 +35,17 @@ def stage_serialize(serializer, graph_tasks):
return tasks
def pre_deployment_serialize(orchestrator_graph, cluster, nodes):
def pre_deployment_serialize(orchestrator_graph, cluster, nodes,
role_resolver=None):
graph_tasks = orchestrator_graph.pre_tasks_serialize(nodes)
return stage_serialize(
plugins_serializers.PluginsPreDeploymentHooksSerializer(
cluster, nodes), graph_tasks)
cluster, nodes, role_resolver=role_resolver), graph_tasks)
def post_deployment_serialize(orchestrator_graph, cluster, nodes):
def post_deployment_serialize(orchestrator_graph, cluster, nodes,
role_resolver=None):
graph_tasks = orchestrator_graph.post_tasks_serialize(nodes)
return stage_serialize(
plugins_serializers.PluginsPostDeploymentHooksSerializer(
cluster, nodes), graph_tasks)
cluster, nodes, role_resolver=role_resolver), graph_tasks)

View File

@ -26,7 +26,7 @@ from nailgun.errors import errors
from nailgun.logger import logger
from nailgun.orchestrator import plugins_serializers
from nailgun.orchestrator.tasks_serializer import CreateVMsOnCompute
from nailgun.orchestrator.tasks_serializer import StandartConfigRolesHook
from nailgun.orchestrator.tasks_serializer import StandardConfigRolesHook
from nailgun.orchestrator.tasks_serializer import TaskSerializers
from nailgun.orchestrator.tasks_templates import make_noop_task
from nailgun.utils.role_resolver import NameMatchingPolicy
@ -34,7 +34,7 @@ from nailgun.utils.role_resolver import NullResolver
from nailgun.utils.role_resolver import RoleResolver
class NoopSerializer(StandartConfigRolesHook):
class NoopSerializer(StandardConfigRolesHook):
"""Serializes tasks that should be skipped by astute."""
def should_execute(self):
return True
@ -51,7 +51,7 @@ class NoopSerializer(StandartConfigRolesHook):
yield make_noop_task(uids, self.task)
class PluginTaskSerializer(StandartConfigRolesHook):
class PluginTaskSerializer(StandardConfigRolesHook):
serializer_class = None
def should_execute(self):

View File

@ -23,49 +23,11 @@ import yaml
from nailgun import consts
from nailgun.errors import errors
from nailgun.expression import Expression
from nailgun.logger import logger
from nailgun import objects
from nailgun.orchestrator import deployment_serializers
from nailgun.orchestrator import tasks_templates as templates
from nailgun.settings import settings
from nailgun.utils.role_resolver import BaseRoleResolver
def get_uids_for_roles(nodes, roles):
"""Returns list of uids for nodes that matches roles
:param nodes: list of nodes
:param roles: list of roles or consts.TASK_ROLES.all
:returns: list of strings
"""
uids = set()
if roles == consts.TASK_ROLES.all:
uids.update([n.uid for n in nodes])
elif roles == consts.TASK_ROLES.master:
return [consts.MASTER_NODE_UID]
elif isinstance(roles, list):
for node in nodes:
if set(roles) & set(objects.Node.all_roles(node)):
uids.add(node.uid)
else:
logger.warn(
'Wrong roles format, `roles` should be a list or "*": %s',
roles)
return list(uids)
class LegacyRoleResolver(BaseRoleResolver):
"""The role resolver that implements legacy behaviour."""
# TODO(bgaifullin): remove this in 9.0
def __init__(self, nodes):
self.nodes = nodes
def resolve(self, roles, policy=None):
return get_uids_for_roles(self.nodes, roles)
from nailgun.utils.role_resolver import RoleResolver
@six.add_metaclass(abc.ABCMeta)
@ -122,13 +84,13 @@ class PuppetHook(GenericNodeHook):
yield templates.make_puppet_task([self.node['uid']], self.task)
class StandartConfigRolesHook(ExpressionBasedTask):
class StandardConfigRolesHook(ExpressionBasedTask):
"""Role hooks that serializes task based on config file only."""
def __init__(self, task, cluster, nodes, role_resolver=None):
super(StandartConfigRolesHook, self).__init__(task, cluster)
super(StandardConfigRolesHook, self).__init__(task, cluster)
self.nodes = nodes
self.role_resolver = role_resolver or LegacyRoleResolver(nodes)
self.role_resolver = role_resolver or RoleResolver(nodes)
def get_uids(self):
return self.role_resolver.resolve(
@ -141,7 +103,7 @@ class StandartConfigRolesHook(ExpressionBasedTask):
yield templates.make_generic_task(uids, self.task)
class GenericRolesHook(StandartConfigRolesHook):
class GenericRolesHook(StandardConfigRolesHook):
identity = abc.abstractproperty
@ -502,4 +464,4 @@ class TaskSerializers(object):
if task['id'] in self._stage_serializers_map:
return self._stage_serializers_map[task['id']]
else:
return StandartConfigRolesHook
return StandardConfigRolesHook

View File

@ -28,9 +28,6 @@ from sqlalchemy import not_
from sqlalchemy.orm import ColumnProperty
from sqlalchemy.orm import object_mapper
import nailgun.rpc as rpc
from nailgun import objects
from nailgun import consts
from nailgun.db import db
@ -42,6 +39,7 @@ from nailgun.errors import errors
from nailgun.logger import logger
from nailgun.network.checker import NetworkCheck
from nailgun.network.manager import NetworkManager
from nailgun import objects
from nailgun.orchestrator import deployment_graph
from nailgun.orchestrator import deployment_serializers
from nailgun.orchestrator import provisioning_serializers
@ -49,11 +47,13 @@ from nailgun.orchestrator import stages
from nailgun.orchestrator import task_based_deployment
from nailgun.orchestrator import tasks_serializer
from nailgun.orchestrator import tasks_templates
import nailgun.rpc as rpc
from nailgun.settings import settings
from nailgun.task.fake import FAKE_THREADS
from nailgun.task.helpers import TaskHelper
from nailgun.utils import logs as logs_utils
from nailgun.utils.restrictions import VmwareAttributesRestriction
from nailgun.utils.role_resolver import RoleResolver
from nailgun.utils.zabbix import ZabbixManager
@ -209,12 +209,15 @@ class DeploymentTask(object):
# NOTE(dshulyak) At this point parts of the orchestration can be empty,
# it should not cause any issues with deployment/progress and was
# done by design
role_resolver = RoleResolver(nodes)
serialized_cluster = deployment_serializers.serialize(
orchestrator_graph, task.cluster, nodes)
pre_deployment = stages.pre_deployment_serialize(
orchestrator_graph, task.cluster, nodes)
orchestrator_graph, task.cluster, nodes,
role_resolver=role_resolver)
post_deployment = stages.post_deployment_serialize(
orchestrator_graph, task.cluster, nodes)
orchestrator_graph, task.cluster, nodes,
role_resolver=role_resolver)
return {
'deployment_info': serialized_cluster,

View File

@ -450,7 +450,7 @@ class TestDeploymentTaskSerializer(BaseUnitTest):
factory.get_stage_serializer(
self.make_task('upload_repos')
),
task_based_deployment.StandartConfigRolesHook
task_based_deployment.StandardConfigRolesHook
)
)