From c8967854806ce10f4dd38c63f6102490485c0cdc Mon Sep 17 00:00:00 2001 From: Josh Patterson Date: Mon, 24 Feb 2025 14:20:09 -0500 Subject: [PATCH] fix vm deletion --- salt/orch/dyanno_hypervisor.sls | 43 +++++++++---------- .../engines/master/virtual_node_manager.py | 10 +++-- salt/soc/dyanno/hypervisor/write_status.sls | 12 +++++- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/salt/orch/dyanno_hypervisor.sls b/salt/orch/dyanno_hypervisor.sls index b580339ad..c298a6101 100644 --- a/salt/orch/dyanno_hypervisor.sls +++ b/salt/orch/dyanno_hypervisor.sls @@ -11,6 +11,23 @@ {% if 'hvn' in salt['pillar.get']('features', []) %} +{# Macro to find hypervisor name from VM status file #} +{% macro find_hypervisor_from_status(vm_name) -%} + {%- set path = salt['file.find']('/opt/so/saltstack/local/salt/hypervisor/hosts/',type='f', name=vm_name ~ '.status') -%} + {%- if path | length == 1 -%} + {%- set parts = path[0].split('/') -%} + {%- set hypervisor = parts[-2] -%} + {%- do salt.log.debug('dyanno_hypervisor_orch: Found hypervisor from file.find: ' ~ hypervisor) -%} + {{- hypervisor -}} + {%- elif path | length == 0 -%} + {%- do salt.log.error('dyanno_hypervisor_orch: ' ~ vm_name ~ ' not found in any hypervisor directories') -%} + {{- '' -}} + {%- else -%} + {%- do salt.log.error('dyanno_hypervisor_orch: Found ' ~ vm_name ~ ' in multiple hypervisor directories: ' ~ path | string) -%} + {{- '' -}} + {%- endif -%} +{%- endmacro %} + {% do salt.log.info('dyanno_hypervisor_orch: Running') %} {% set data = pillar.get('data', {}) %} @@ -35,21 +52,11 @@ {% do salt.log.debug('dyanno_hypervisor_orch: Received data: ' ~ status_data|json|string) %} {% do salt.log.debug('dyanno_hypervisor_orch: Setting vm_name, hypervisor and status') %} {% set vm_name = data.get('id') %} -{% set grains = salt.saltutil.runner('cache.grains', tgt=vm_name).get(vm_name) %} -{% if grains %} -{% do salt.log.debug('dyanno_hypervisor_orch: Got cache.grains ' ~ grains|string) %} -{% if grains.get('salt-cloud').get('profile') %} -{% do salt.log.debug('dyanno_hypervisor_orch: Found salt-cloud:profile grain: ' ~ grains.get('salt-cloud').get('profile')|string) %} -{% set hypervisor = grains.get('salt-cloud').get('profile').split('-')[1] %} -{% do salt.log.debug('dyanno_hypervisor_orch: Got hypervisor: ' ~ hypervisor) %} -{% endif %} -{% else %} -{% do salt.log.debug('dyanno_hypervisor_orch: Did not get cache.grains.') %} -{% endif %} -{% set hypervisor = hypervisor %} +{% set hypervisor = find_hypervisor_from_status(vm_name) %} {% set status = 'Initialize Minion Pillars' %} {% endif %} + {# salt-cloud tag #} {% if tag.startswith('salt/cloud/') and (tag.endswith('/creating') or tag.endswith('/deploying') or tag.endswith('/created') or tag.endswith('/destroyed')) %} {% do salt.log.debug('dyanno_hypervisor_orch: Received data: ' ~ data|json|string) %} @@ -66,17 +73,7 @@ {% set hypervisor = data.profile.split('-')[1] %} {% do salt.log.debug('dyanno_hypervisor_orch: Got hypervisor from data: ' ~ hypervisor) %} {% else %} -{# If not in the event, find it by the .status file location #} -{% set path = salt['file.find']('/opt/so/saltstack/local/salt/hypervisor/hosts/',type='f', name=vm_name ~ '.status') %} -{% if path | length == 1 %} -{% set parts = path[0].split('/') %} -{% set hypervisor = parts[-2] %} -{% do salt.log.debug('dyanno_hypervisor_orch: Found hypervisor from file.find: ' ~ hypervisor) %} -{% elif path | length == 0 %} -{% do salt.log.error('dyanno_hypervisor_orch: ' ~ vm_name ~ ' not found in any hypervisor directories') %} -{% else %} -{% do salt.log.error('dyanno_hypervisor_orch: Found ' ~ vm_name ~ ' in multiple hypervisor directories: ' ~ path | string) %} -{% endif %} +{% set hypervisor = find_hypervisor_from_status(vm_name) %} {% endif %} {% set status = data.get('event').title() %} {% endif %} diff --git a/salt/salt/engines/master/virtual_node_manager.py b/salt/salt/engines/master/virtual_node_manager.py index 98a4eb572..6425a0063 100644 --- a/salt/salt/engines/master/virtual_node_manager.py +++ b/salt/salt/engines/master/virtual_node_manager.py @@ -736,9 +736,8 @@ def process_hypervisor(hypervisor_path: str) -> None: nodes_config = read_json_file(vms_file) if not nodes_config: log.debug("Empty VMs configuration in %s", vms_file) - return - # Get existing VMs - no lock needed + # Get existing VMs existing_vms = set() for file_path in glob.glob(os.path.join(hypervisor_path, '*_*')): basename = os.path.basename(file_path) @@ -761,7 +760,12 @@ def process_hypervisor(hypervisor_path: str) -> None: process_vm_creation(hypervisor_path, vm_config) # Process VM deletions - for vm_name in existing_vms - configured_vms: + vms_to_delete = existing_vms - configured_vms + log.debug(f"Existing VMs: {existing_vms}") + log.debug(f"Configured VMs: {configured_vms}") + log.debug(f"VMs to delete: {vms_to_delete}") + for vm_name in vms_to_delete: + log.info(f"Initiating deletion process for VM: {vm_name}") process_vm_deletion(hypervisor_path, vm_name) except Exception as e: diff --git a/salt/soc/dyanno/hypervisor/write_status.sls b/salt/soc/dyanno/hypervisor/write_status.sls index 7859069a1..be03b26b7 100644 --- a/salt/soc/dyanno/hypervisor/write_status.sls +++ b/salt/soc/dyanno/hypervisor/write_status.sls @@ -22,7 +22,17 @@ {% set status_file = status_dir ~ '/' ~ vm_name ~ '.status' %} # Define the list of process steps in order (case-sensitive) -{% set process_steps = ['Processing', 'IP Configuration', 'Starting Create', 'Executing Deploy Script', 'Initialize Minion Pillars', 'Created Instance', 'Hardware Configuration', 'Highstate Triggered', 'Destroyed Instance'] %} +{% set process_steps = [ + 'Processing', + 'IP Configuration', + 'Starting Create', + 'Executing Deploy Script', + 'Initialize Minion Pillars', + 'Created Instance', + 'Hardware Configuration', + 'Highstate Triggered', + 'Destroyed Instance' +] %} {% set new_index = process_steps.index(status_data.get('status')) %} {% do salt.log.debug('soc/dyanno/hypervisor/write_status: new_index: ' ~ new_index|string) %}