-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxmox-cleanup-templates
More file actions
executable file
·102 lines (96 loc) · 3.52 KB
/
Copy pathproxmox-cleanup-templates
File metadata and controls
executable file
·102 lines (96 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
# ==========================================================================
# __ ____ __ ___ ___ _ _ _
# \ \ / / \/ | |_ _|_ __ __ _ __ _ ___ | _ )_ _(_) |__| |___ _ _
# \ V /| |\/| | | || ' \/ _` / _` / -_) | _ \ || | | / _` / -_) '_|
# \_/ |_| |_| |___|_|_|_\__,_\__, \___| |___/\_,_|_|_\__,_\___|_|
# |___/
#
# --- Virtual Machine Image Builder and System Installation Scripts ---
# https://www.nntb.no/~dreibh/vmimage-builder-scripts/
# ==========================================================================
#
# VM Image Builder Scripts
# Copyright (C) 2017-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com
# Bash options:
set -euo pipefail
# ====== Check arguments ====================================================
dryRun=1
if [ $# -gt 0 ] ; then
if [ "$1" == "--really-remove" ] ; then
dryRun=0
else
echo >&2 "Usage: $0 [--really-remove]"
exit 1
fi
fi
# ====== Identify templates and VMs for removal =============================
echo -e "\x1b[34mLooking for VMs/templates to clean up ...\x1b[0m"
qm list | sort -k2,2 -k1,1rn | awk '
BEGIN {
lastName="";
lastID=-1;
}
/^[ ]*2[0-9][0-1][0-9][0-3][0-9][0-9][0-9][0-9] / {
id=$1;
name=$2;
status=$3;
if(id ~ /^2[0-9][0-1][0-9][0-3][0-9][0-9][0-9][0-9]$/ ) {
if(status == "running") {
print "=> " name " is still running, but should be a not-running template!" | "cat 1>&2"
print id " " name " " status;
}
else if(status == "stopped") {
if(name == lastName) {
# This is an older version of the same template!
if(id < lastID) {
print "=> " name " (ID " id ") is older than " lastName " (ID " lastID ")" | "cat 1>&2"
print "" id " " name " " status;
}
else {
print "Something is wrong! id:" id " >= lastID:" lastID | "cat 1>&2"
exit 1;
}
}
else {
lastName = name;
lastID = id;
}
}
}
}
' | (
echo -e "\x1b[34mCleaning up obsolete VMs/templates ...\x1b[0m"
while read -r id name status ; do
if [[ ! "${id}" =~ ^2[0-9][0-1][0-9][0-3][0-9][0-9][0-9][0-9]$ ]] ; then
echo >&2 "ERROR: ID is not in the expected format! Something is wrong!"
exit 1
fi
if [ "${dryRun}" -eq 0 ] ; then
echo -e "\x1b[35mRemoving ${id} (${name}) with status \"${status}\" ...\x1b[0m"
echo -e "\x1b[36mqm stop ${id}\x1b[0m"
qm stop "${id}"
echo -e "\x1b[36mqm destroy ${id}\x1b[0m"
qm destroy "${id}"
else
echo -e "\x1b[35mDRY RUN ONLY: Would remove ${id} (${name}) with status \"${status}\" ...\x1b[0m"
echo -e "\x1b[36mqm stop ${id}\x1b[0m"
echo -e "\x1b[36mqm destroy ${id}\x1b[0m"
fi
done
)