trying to improve error handling inside get_all_by_host_and_network

This commit is contained in:
forest 2021-12-09 12:40:30 -06:00
parent 51d2c6e7a2
commit f2e03919d7
3 changed files with 34 additions and 14 deletions

View File

@ -3,9 +3,11 @@
printf '['
delimiter=""
virsh list --all | tail -n +3 | while read -r line; do
capsul_id="$(echo "$line" | awk '{ print $2 }')"
capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')"
printf '%s\n {"id":"%s", "state":"%s"}' "$delimiter" "$capsul_id" "$capsul_state"
delimiter=","
if [ "$line" != "" ]; then
capsul_id="$(echo "$line" | awk '{ print $2 }')"
capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')"
printf '%s\n {"id":"%s", "state":"%s"}' "$delimiter" "$capsul_id" "$capsul_state"
delimiter=","
fi
done
printf '\n]\n'

View File

@ -4,10 +4,12 @@
printf '['
delimiter=""
virsh net-list --all | tail -n +3 | awk '{ print $1 }' | while read -r network_name; do
virtual_bridge_name="$(virsh net-info "$network_name" | grep -E '^Bridge:' | awk '{ print $2 }')"
capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')"
printf '%s\n {"name":"%s", "virtual_bridge_name":"%s"}' "$delimiter" "$network_name" "$virtual_bridge_name"
delimiter=","
if [ "$line" != "" ]; then
virtual_bridge_name="$(virsh net-info "$network_name" | grep -E '^Bridge:' | awk '{ print $2 }')"
capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')"
printf '%s\n {"name":"%s", "virtual_bridge_name":"%s"}' "$delimiter" "$network_name" "$virtual_bridge_name"
delimiter=","
fi
done
printf '\n]\n'

View File

@ -139,7 +139,9 @@ class ShellScriptSpoke(VirtualizationInterface):
vm_list_process = run([join(current_app.root_path, 'shell_scripts/virsh-list.sh')], capture_output=True)
self.validate_completed_process(vm_list_process)
list_of_vms = json.loads(vm_list_process.stdout.decode("utf-8"))
vms_json_string = vm_list_process.stdout.decode("utf-8")
current_app.logger.info(f"vms_json_string: {vms_json_string}")
list_of_vms = json.loads(vms_json_string)
current_app.logger.info(f"list_of_vms: {json.dumps(list_of_vms)}")
@ -149,7 +151,9 @@ class ShellScriptSpoke(VirtualizationInterface):
net_list_process = run([join(current_app.root_path, 'shell_scripts/virsh-net-list.sh')], capture_output=True)
self.validate_completed_process(net_list_process)
list_of_networks = json.loads(net_list_process.stdout.decode("utf-8"))
net_list_json_string = net_list_process.stdout.decode("utf-8")
current_app.logger.info(f"net_list_json_string: {net_list_json_string}")
list_of_networks = json.loads(net_list_json_string)
current_app.logger.info(f"list_of_networks: {json.dumps(list_of_networks)}")
@ -158,8 +162,14 @@ class ShellScriptSpoke(VirtualizationInterface):
vm_id_by_mac = dict()
for network in list_of_networks:
with open(f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.macs", mode='r') as macs_json_file:
vms_with_macs = json.load(macs_json_file)
macs_json_filename = f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.macs"
with open(macs_json_filename, mode='r') as macs_json_file:
vms_with_macs = []
try:
vms_with_macs = json.load(macs_json_file)
except:
raise Exception(f"failed to parse the JSON file '{macs_json_filename}'")
for vm in vms_with_macs:
for mac in vm['macs']:
if mac not in vm_id_by_mac:
@ -178,8 +188,14 @@ class ShellScriptSpoke(VirtualizationInterface):
vms_by_id[vm['domain']]['macs'][mac] = True
with open(f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.status", mode='r') as status_json_file:
statuses = json.load(status_json_file)
status_json_filename = f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.status"
with open(status_json_filename, mode='r') as status_json_file:
statuses = []
try:
statuses = json.load(status_json_file)
except:
raise Exception(f"failed to parse the JSON file '{status_json_filename}'")
for status in statuses:
if status['mac-address'] in vm_id_by_mac:
vm_id = vm_id_by_mac[status['mac-address']]