diff --git a/tdp/core/collections.py b/tdp/core/collections.py index cde442d9..3f1370c4 100644 --- a/tdp/core/collections.py +++ b/tdp/core/collections.py @@ -19,6 +19,7 @@ from tdp.core.collection import Collection from tdp.core.entities.hostable_entity_name import ServiceComponentName from tdp.core.entities.operation import Operations +from tdp.core.inventory_reader import InventoryReader from tdp.core.operation import Operation from tdp.core.variables.schema.service_schema import ServiceSchema @@ -48,6 +49,17 @@ def __iter__(self): def __len__(self): return self._collections.__len__() + def get_collections_hosts(self): + host_operation_list = [ + self.operations.__getitem__(operation).host_names + for operation in self.operations + ] + hosts_set = set() + for operation_hosts in host_operation_list: + hosts_set.update(operation_hosts) + + return {host for host in hosts_set if host != "localhost"} + @staticmethod def from_collection_list(collections: Sequence[Collection]) -> Collections: """Factory method to build Collections from a sequence of Collection. @@ -231,3 +243,13 @@ def get_components_from_service( if operation.service_name == service_name and not operation.is_service_operation() } + + def check_collections_hosts(self): + """Checks if hosts has not been removed from the inventory.ini file""" + inventory = InventoryReader().inventory + inventory.refresh_inventory() + + inventory_list = [str(name) for name in inventory.get_hosts()] + for host in self.get_collections_hosts(): + if host not in inventory_list: + return host diff --git a/tdp/core/deployment/deployment_iterator.py b/tdp/core/deployment/deployment_iterator.py index 241bd018..bf2ad852 100644 --- a/tdp/core/deployment/deployment_iterator.py +++ b/tdp/core/deployment/deployment_iterator.py @@ -125,6 +125,18 @@ def __next__( operation_rec.state = OperationStateEnum.RUNNING + if host := self._collections.check_collections_hosts(): + message = f"{host} has been removed from the inventory file." + logger.error(message) + operation_rec.logs = message.encode("utf-8") + operation_rec.start_time = operation_rec.end_time = ( + datetime.utcnow() + ) + operation_rec.state = OperationStateEnum.FAILURE + self.deployment.end_time = datetime.utcnow() + self.deployment.state = DeploymentStateEnum.FAILURE + return operation_rec, None + return operation_rec, partial(self._process_operation_fn, operation_rec) # StopIteration is a "normal" exception raised when the iteration has stopped except StopIteration as e: