Store VM metadata as JSON

This commit is contained in:
Thales Maciel 2026-01-31 23:17:12 -03:00
parent bbd57d8dd2
commit 7af04b7535
No known key found for this signature in database
GPG key ID: 33112E6833C34679
11 changed files with 188 additions and 178 deletions

28
nat.sh
View file

@ -13,23 +13,17 @@ Manage per-VM NAT rules for internet access.
EOF
}
get_prop() {
local info="$1"
local key="$2"
awk -F= -v k="$key" '$1==k {print $2}' "$info"
}
find_vm_info() {
find_vm_json() {
local query="$1"
local info match_count=0 match=""
local vm_json match_count=0 match=""
for info in state/vms/*/info; do
[[ -f "$info" ]] || continue
for vm_json in state/vms/*/vm.json; do
[[ -f "$vm_json" ]] || continue
local id name
id="$(get_prop "$info" "id")"
name="$(get_prop "$info" "name")"
id="$(jq -r '.meta.id // empty' "$vm_json")"
name="$(jq -r '.meta.name // empty' "$vm_json")"
if [[ "$id" == "$query"* || "$name" == "$query"* ]]; then
match="$info"
match="$vm_json"
match_count=$((match_count + 1))
fi
done
@ -64,12 +58,12 @@ if [[ -z "$ACTION" || -z "$QUERY" ]]; then
exit 1
fi
INFO_FILE="$(find_vm_info "$QUERY")"
GUEST_IP="$(get_prop "$INFO_FILE" "guest_ip")"
TAP="$(get_prop "$INFO_FILE" "tap")"
VM_JSON="$(find_vm_json "$QUERY")"
GUEST_IP="$(jq -r '.meta.guest_ip // empty' "$VM_JSON")"
TAP="$(jq -r '.meta.tap // empty' "$VM_JSON")"
if [[ -z "$GUEST_IP" || -z "$TAP" ]]; then
log "missing guest_ip or tap in $INFO_FILE"
log "missing guest_ip or tap in $VM_JSON"
exit 1
fi