Skip to content

Commit

Permalink
Merge pull request #388 from rh-ecosystem-edge/cleaning-script-contro…
Browse files Browse the repository at this point in the history
…l-arguments
  • Loading branch information
iranzo committed Jun 8, 2022
2 parents 4710937 + e2cc0cb commit 123f189
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions hack/clean/clean-libvirt-pool.sh
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
#!/usr/bin/env bash

set -o pipefail
set -o nounset
set -m

resourcesUsed=""
if [ -n "${1}" ]; then

# get all disk and isos in use
getUsedResources() {
for vm in $(kcli list vm -o json | jq -r .[].name); do
# preserve ISO/iso used by existing vm's
resourcesUsed=$(printf "%s|${resourcesUsed}" "${vm}.ISO")
resourcesUsed=""

# some vm's have a boot-ID iso - depending on how they were created
resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep iso: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')")
# get all disk and isos in use
getUsedResources() {
for vm in $(kcli list vm -o json | jq -r .[].name); do
# preserve ISO/iso used by existing vm's
resourcesUsed=$(printf "%s|${resourcesUsed}" "${vm}.ISO")

# preserve images used by existing vm's
resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep image: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')")
# some vm's have a boot-ID iso - depending on how they were created
resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep iso: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')")

# preserve all disks used by existing vm's
for disk in $(kcli show vm $vm | grep -E 'diskname:' | awk '{print $10}' | sed 's#\/var\/lib\/libvirt\/images\/##'); do
resourcesUsed=$(printf "%s|${resourcesUsed}" "${disk}")
# preserve images used by existing vm's
resourcesUsed=$(printf "%s|${resourcesUsed}" "$(kcli show vm ${vm} | grep image: | awk '{print $2}' | sed 's#\/var\/lib\/libvirt\/images\/##g')")

# preserve all disks used by existing vm's
for disk in $(kcli show vm $vm | grep -E 'diskname:' | awk '{print $10}' | sed 's#\/var\/lib\/libvirt\/images\/##'); do
resourcesUsed=$(printf "%s|${resourcesUsed}" "${disk}")
done
done
done
}
}

# get the resources
getUsedResources
# get the resources
getUsedResources

# build a cmd that greps out the *used* resources
# cmd has to guarantee resources in toDelete are *unused*
# sanitize output by removing any trailing character
# and also double || introduced by getUsedResources\
# failing to do this will make cmd unusable and unreliable
resourcesGrep=$(echo $resourcesUsed | sed 's/.$//' | sed 's/||/|/'g)
cmd=$(printf "ls /var/lib/libvirt/images | grep -Ev '%s'" ${resourcesGrep})
toDelete=$(eval $cmd)
# build a cmd that greps out the *used* resources
# cmd has to guarantee resources in toDelete are *unused*
# sanitize output by removing any trailing character
# and also double || introduced by getUsedResources\
# failing to do this will make cmd unusable and unreliable
resourcesGrep=$(echo $resourcesUsed | sed 's/.$//' | sed 's/||/|/'g)
cmd=$(printf "ls /var/lib/libvirt/images | grep -Ev '%s'" ${resourcesGrep})
toDelete=$(eval $cmd)

# dry-run will prompt the images to be deleted
# now will wipe the pool
case "${1}" in
'dry-run')
echo "############### ${0} dry-run"
echo "############### resources in use - won't be deleted"
echo $cmd
echo ""
# dry-run will prompt the images to be deleted
# now will wipe the pool
case $1 in
'dry-run')
echo "############### ${0} dry-run"
echo "############### resources in use - won't be deleted"
echo $cmd
echo ""

echo "############### resources unused - will be delete"
for resource in $(echo $toDelete); do
echo $resource
done
;;
echo "############### resources unused - will be delete"
for resource in $(echo $toDelete); do
echo $resource
done
;;

'now')
for resource in $(echo $toDelete); do
kcli delete disk --novm --yes ${resource}
done
;;
'now')
for resource in $(echo $toDelete); do
kcli delete disk --novm --yes ${resource}
done
;;

*)
echo "Usage: $0 [dry-run|now]"
;;
esac
*)
echo "Usage: [dry-run|now]"
;;
esac
else
echo "Usage: [dry-run|now]"
fi

0 comments on commit 123f189

Please sign in to comment.