66import logging
77from collections .abc import Iterable
88from datetime import datetime
9- from typing import TYPE_CHECKING , NamedTuple , Optional
9+ from typing import TYPE_CHECKING , Literal , NamedTuple , Optional
1010
1111from sqlalchemy import JSON
1212from sqlalchemy .orm import Mapped , mapped_column , relationship
1313from tabulate import tabulate
1414
1515from tdp .core .constants import OPERATION_SLEEP_NAME , OPERATION_SLEEP_VARIABLE
1616from tdp .core .dag import Dag
17- from tdp .core .entities .operation import PlaybookOperation
17+ from tdp .core .entities .operation import OperationName , PlaybookOperation
1818from tdp .core .filters import FilterFactory
1919from tdp .core .models .base_model import BaseModel
2020from tdp .core .models .enums import (
@@ -358,22 +358,34 @@ def from_stale_hosted_entities(
358358 """
359359
360360 class OperationHostTuple (NamedTuple ):
361- operation_name : str
361+ operation : Operation
362362 host_name : Optional [str ]
363363
364+ def _get_operation (
365+ status : HostedEntityStatus , action : Literal ["config" , "restart" ]
366+ ) -> Optional [Operation ]:
367+ operation_name = OperationName (status .entity .name , action )
368+ try :
369+ operation = collections .operations [operation_name ]
370+ except KeyError as e :
371+ logger .warning (str (e ), "skipping" )
372+ return
373+ return operation
374+
364375 operation_hosts : set [OperationHostTuple ] = set ()
365376 for status in stale_hosted_entity_statuses :
366- if status .to_config :
377+ if status .to_config and (
378+ config_operation := _get_operation (status , "config" )
379+ ):
367380 operation_hosts .add (
368- OperationHostTuple (
369- f"{ status .entity .name } _config" ,
370- status .entity .host ,
371- )
381+ OperationHostTuple (config_operation , status .entity .host )
372382 )
373- if status .to_restart :
383+ if status .to_restart and (
384+ restart_operation := _get_operation (status , "restart" )
385+ ):
374386 operation_hosts .add (
375387 OperationHostTuple (
376- f" { status . entity . name } _start" ,
388+ restart_operation ,
377389 status .entity .host ,
378390 )
379391 )
@@ -383,22 +395,15 @@ class OperationHostTuple(NamedTuple):
383395 # Sort by hosts to improve readability
384396 sorted_operation_hosts = sorted (
385397 operation_hosts ,
386- key = lambda x : f"{ x .operation_name } _{ x .host_name } " ,
398+ key = lambda x : f"{ x .operation . name } _{ x .host_name } " ,
387399 )
388400
389- # Sort operations using DAG topological sort. Convert operation name to
390- # Operation instance and replace "start" action by "restart".
401+ # Sort operations using DAG topological sort
391402 dag = Dag (collections )
392- reconfigure_operations_sorted = list (
393- map (
394- lambda x : (
395- dag .node_to_operation (x .operation_name , restart = True ),
396- x .host_name ,
397- ),
398- dag .topological_sort_key (
399- sorted_operation_hosts , key = lambda x : x .operation_name
400- ),
401- )
403+ reconfigure_operations_sorted = dag .topological_sort_key (
404+ sorted_operation_hosts ,
405+ # topological sort only applies to start operations
406+ key = lambda x : str (x .operation .name ).replace ("_restart" , "_start" ),
402407 )
403408
404409 # Generate deployment
0 commit comments