Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 5800a08

Browse files
committed
Simplify cleanup of VMs + Boxes
1 parent 7853999 commit 5800a08

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

vagrant_init_destroy_boxes.sh

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ check_vagrant_vm() {
5555
esac
5656
}
5757

58-
vagrant_remove_boxes_images() {
58+
vagrant_cleanup() {
59+
vagrant destroy -f
5960
vagrant box remove -f "$VAGRANT_BOX_NAME"
6061

6162
if [[ "$VAGRANT_BOX_NAME" =~ "libvirt" ]]; then
6263
virsh --connect=qemu:///system vol-delete --pool default --vol "${VAGRANT_BOX_NAME}_vagrant_box_image_0.img"
6364
fi
65+
66+
rm -rf "${VAGRANT_CWD}"/{Vagrantfile,.vagrant}
67+
rmdir "${VAGRANT_CWD}"
6468
}
6569

66-
vagrant_destroy() {
67-
vagrant destroy -f
70+
# trap ctrl-c and call ctrl_c()
71+
trap ctrl_c INT
72+
73+
ctrl_c() {
74+
echo "** Trapped CTRL-C"
75+
vagrant_cleanup
6876
}
6977

7078

@@ -101,11 +109,8 @@ main() {
101109

102110
check_vagrant_vm 2>&1 | tee -a "${LOG_FILE}"
103111

104-
vagrant_destroy
105-
vagrant_remove_boxes_images
112+
vagrant_cleanup
106113

107-
rm -rf "${VAGRANT_CWD}"/{Vagrantfile,.vagrant}
108-
rmdir "${VAGRANT_CWD}"
109114
echo "*** Completed"
110115
done
111116

0 commit comments

Comments
 (0)