capsul.org webapp

trying to improve error handling inside get_all_by_host_and_network

forest f2e03919 51d2c6e7

Changed files
+34 -14
capsulflask
+6 -4
capsulflask/shell_scripts/virsh-list.sh
··· 3 3 printf '[' 4 4 delimiter="" 5 5 virsh list --all | tail -n +3 | while read -r line; do 6 - capsul_id="$(echo "$line" | awk '{ print $2 }')" 7 - capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')" 8 - printf '%s\n {"id":"%s", "state":"%s"}' "$delimiter" "$capsul_id" "$capsul_state" 9 - delimiter="," 6 + if [ "$line" != "" ]; then 7 + capsul_id="$(echo "$line" | awk '{ print $2 }')" 8 + capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')" 9 + printf '%s\n {"id":"%s", "state":"%s"}' "$delimiter" "$capsul_id" "$capsul_state" 10 + delimiter="," 11 + fi 10 12 done 11 13 printf '\n]\n'
+6 -4
capsulflask/shell_scripts/virsh-net-list.sh
··· 4 4 printf '[' 5 5 delimiter="" 6 6 virsh net-list --all | tail -n +3 | awk '{ print $1 }' | while read -r network_name; do 7 - virtual_bridge_name="$(virsh net-info "$network_name" | grep -E '^Bridge:' | awk '{ print $2 }')" 8 - capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')" 9 - printf '%s\n {"name":"%s", "virtual_bridge_name":"%s"}' "$delimiter" "$network_name" "$virtual_bridge_name" 10 - delimiter="," 7 + if [ "$line" != "" ]; then 8 + virtual_bridge_name="$(virsh net-info "$network_name" | grep -E '^Bridge:' | awk '{ print $2 }')" 9 + capsul_state="$(echo "$line" | sed -E 's/^ *[0-9-]+ +[^ ]+ +//')" 10 + printf '%s\n {"name":"%s", "virtual_bridge_name":"%s"}' "$delimiter" "$network_name" "$virtual_bridge_name" 11 + delimiter="," 12 + fi 11 13 done 12 14 printf '\n]\n' 13 15
+22 -6
capsulflask/spoke_model.py
··· 139 139 140 140 vm_list_process = run([join(current_app.root_path, 'shell_scripts/virsh-list.sh')], capture_output=True) 141 141 self.validate_completed_process(vm_list_process) 142 - list_of_vms = json.loads(vm_list_process.stdout.decode("utf-8")) 142 + vms_json_string = vm_list_process.stdout.decode("utf-8") 143 + current_app.logger.info(f"vms_json_string: {vms_json_string}") 144 + list_of_vms = json.loads(vms_json_string) 143 145 144 146 current_app.logger.info(f"list_of_vms: {json.dumps(list_of_vms)}") 145 147 ··· 149 151 150 152 net_list_process = run([join(current_app.root_path, 'shell_scripts/virsh-net-list.sh')], capture_output=True) 151 153 self.validate_completed_process(net_list_process) 152 - list_of_networks = json.loads(net_list_process.stdout.decode("utf-8")) 154 + net_list_json_string = net_list_process.stdout.decode("utf-8") 155 + current_app.logger.info(f"net_list_json_string: {net_list_json_string}") 156 + list_of_networks = json.loads(net_list_json_string) 153 157 154 158 current_app.logger.info(f"list_of_networks: {json.dumps(list_of_networks)}") 155 159 ··· 158 162 vm_id_by_mac = dict() 159 163 for network in list_of_networks: 160 164 161 - with open(f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.macs", mode='r') as macs_json_file: 162 - vms_with_macs = json.load(macs_json_file) 165 + macs_json_filename = f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.macs" 166 + with open(macs_json_filename, mode='r') as macs_json_file: 167 + vms_with_macs = [] 168 + try: 169 + vms_with_macs = json.load(macs_json_file) 170 + except: 171 + raise Exception(f"failed to parse the JSON file '{macs_json_filename}'") 172 + 163 173 for vm in vms_with_macs: 164 174 for mac in vm['macs']: 165 175 if mac not in vm_id_by_mac: ··· 178 188 179 189 vms_by_id[vm['domain']]['macs'][mac] = True 180 190 181 - with open(f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.status", mode='r') as status_json_file: 182 - statuses = json.load(status_json_file) 191 + status_json_filename = f"{current_app.config['LIBVIRT_DNSMASQ_PATH']}/{network['virtual_bridge_name']}.status" 192 + with open(status_json_filename, mode='r') as status_json_file: 193 + statuses = [] 194 + try: 195 + statuses = json.load(status_json_file) 196 + except: 197 + raise Exception(f"failed to parse the JSON file '{status_json_filename}'") 198 + 183 199 for status in statuses: 184 200 if status['mac-address'] in vm_id_by_mac: 185 201 vm_id = vm_id_by_mac[status['mac-address']]