-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,5 +86,6 @@ | |
- golang | ||
- glibc-langpack-es | ||
- mosh | ||
- fpaste | ||
ignore_errors: true | ||
tags: [ 01-packages ] |