fix vm deletion

This commit is contained in:
Josh Patterson
2025-02-24 14:20:09 -05:00
parent 0006948c29
commit c896785480
3 changed files with 38 additions and 27 deletions

View File

@@ -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 %}

View File

@@ -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:

View File

@@ -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) %}