Update vmcreate.sh

This commit is contained in:
2025-10-28 18:43:23 +00:00
parent a6ec7718ab
commit 136030d096

View File

@@ -9,6 +9,7 @@ storage="syno-tigra"
path="/mnt/pve/$storage/snippets/"
node=3
size=50
remove=false
harule=""
pubkey=""
@@ -31,6 +32,7 @@ show_help () {
echo "-n - specify target node NUMBER to migrate VM to after creating. Default is '3'"
echo "-t - add additional proxmox tag. Default is only pve node number."
echo "-f - get IP addresses and Hostnames from 'flilename'."
echo "-R - REMOVE specified VMs, snippets and HA rule (if specified). Be careful! It's a production cluster!"
echo
echo "If file not specified, script will use arguments as a list of IP addresses."
echo "In this case Hostname will be inherited from 2 last IP octets. Example for 10.10.35.20: 'vm035020'."
@@ -108,8 +110,21 @@ mksnippet () {
echo "Snippet $snippet created"
}
REMOVE () {
if [ $harule ]; then ha-manager rules remove $harule; fi
for line in (cat hosts.tmp)
do
ip=$(echo -n "$line" | cut -d ';')
vmid=$(get_vmid "$ip")
snippet="${path}${vmid}_user.yaml"
qm stop $vmid
if [ $harule ]; then ha-manager remove vm:${vmid}; fi
qm destroy $vmid --destroy-unreferenced-disks --purge
done
}
# Обрабатываем опции
while getopts "a:f:hk:u:p:d:n:t:" opt; do
while getopts "a:f:hk:u:p:d:n:t:R" opt; do
case $opt in
a) harule=${OPTARG};;
f) file=${OPTARG};;
@@ -120,6 +135,7 @@ while getopts "a:f:hk:u:p:d:n:t:" opt; do
d) size=${OPTARG};;
n) node=${OPTARG};;
t) tag=${OPTARG};;
R) remove=true;;
\?) echo "Invalid option."; show_help; exit 1;;
esac
done
@@ -154,7 +170,7 @@ if ! [[ $size -ge 10 && $size -le 500 ]]; then echo "Disk size increment shoud b
# Создаём hosts.tmp из аргументов/файла, попутно проверяя данные
# Если заданы И аргументы, И файл - сразу нахуй
if [[ $# -ne 0 && $use_file ]]; then
if [[ $# -ne 0 && $file ]]; then
echo "Please use EITHER file OR arguments. Use '-h' flag for help"
exit 3
else
@@ -210,6 +226,18 @@ else
fi
# Закончили с вводными данными
#Самое время УДАЛИТЬ, если задан флаг
if $remove; then
input=n
read -p "Script is ready to REMOVE $(cat hosts.tmp | wc -l) VMs. ARE YOU SURE???" input
read -p "REALLY??? Enter to continue or Ctrl+C to abort"
case $input in
y) REMOVE; echo "Specified VMs REMOVED."; exit 0;;
Y) REMOVE; echo "Specified VMs REMOVED."; exit 0;;
*) echo "Aborting..."; exit 1;;
esac
fi
# Проверяем ключ
if [ $pubkey ]; then
if [[ ! -f "$pubkey" ]]; then
@@ -237,7 +265,7 @@ for line in $(cat hosts.tmp)
do
ip=$(echo -n "$line" | cut -d ';' -f 1)
vmid=$(get_vmid "$ip")
snippet=
snippet="${path}${vmid}_user.yaml"
if qm status "$vmid" &>/dev/null; then
echo "VM $vmid exists. Aborting"
exit 7