Files
vmcreate/vmcreate1.sh
2025-10-25 08:24:40 +00:00

158 lines
5.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
### МЫСЛИ ПО ФИЧАМ
# Сделать проверку, какие правила НА существуют, и вывести выбор
# Сделать добавление кастомного полльзователя с паролем
# Сделать замену тега влан
show_help()
echo 'Usage:'
echo 'vmcreate [-h][-a rule_name][-k pub_keyfile][-f filename]'
echo 'Arguments:'
echo '-h - show this help;'
echo '-a - add hosts to HA affinity rules;'
echo "-f - get IP addresses and Hostnames from 'flilename';"
echo "-k - embed custom public key or create new if 'pub_keyfile' not specified"
echo "\nIf 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'."
echo "IP address should be 10.10.*.*"
# Обрабатываем опции
while getopts "a:f:h" opt; do
case $opt in
a) harule="$OPTARG";;
f) file="$OPTARG" ;;
h) show_help ; exit 0;;
k) pubkey="$OPTARG";;
# *) noopts=true
esac
done
# Если файл не задан, но флаг есть
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
input=y
echo "File $file does not exist. Use default 'hosts' file? Y/n: "
read input
case $input in
y) file=hosts;;
Y) file=hosts;;
n) read -p "Enter file name: " file;;
N) read -p "Enter file name: " file;;
*) echo "Use '-f' flag for help"; exit 0;;
esac
if [ ! -f hosts ]; then echo "File 'hosts' does not exist. Exiting."; exit 0; fi
fi
# Если заданы аргументы И файл
if [[ $# -ne 0 && -v file ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 0; fi
# Если заданы аргументы, то создаем временный файл
if [ $# -ne 0 ] then
touch hosts.tmp
for arg in "$@"; do
ipcheck "$arg"
if [ $? -eq 0 ]
then
echo -n "$arg " >> ./hosts.tmp
echo -n "vm" >> ./hosts.tmp
printf "%05d\n" "$(echo -n $arg | cut -d '.' -f 3)" >> ./hosts.tmp
printf "%05d\n" "$(echo -n $arg | cut -d '.' -f 4)" >> ./hosts.tmp
echo >> ./hosts.tmp
else
echo "Argument $arg is not valid IP address (10.10.*.*). Using next argument."
fi
done
fi
ip_check() {
# Функция для проверки IP адреса по шаблону 10.10.*.*, написана гуглом
local ip=$1
local stat=1
oldIFS=$IFS
# Check if the IP matches the general IPv4 pattern
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
# Split the IP into octets
IFS='.' read -r -a octets <<< "$ip"
# Check if each octet is within the valid range (0-255)
if [[ ${octets[0]} -eq 10 && ${octets[1]} -eq 10 && ${octets[2]} -le 252 && ${octets[3]} -le 252 && ${octets[3]} -ne 0 ]]; then
stat=0
fi
fi
return $stat
IFS=$oldIFS
}
#Проверяем ключ
if [ -v pubkey ]; then
if [[ ! -f pubkey ]]; then
input=y
echo "Private key does not exist. Generate new pair? y/n"
read input
case $input in
y) keypair_generate;;
Y) keypair_generate;;
n) read -p "Enter name for your private key: " privkey && keypair_generate ("$privkey");;
N) read -p "Enter name for your private key: " privkey && keypair_generate ("$privkey");;
*) echo "Use '-f' flag for help"; exit 0;;
esac
else keypair_generate
fi
else
privkey="key.pub"
if [[ ! -e key.pub ]]; then echo "'key.pub' does not exist. Please use '-k' flag to specify."; exit 0; fi
fi
keypair_generate (){
ssh-keygen -t rsa -N "" -f $privkey
#указать в конце функции переменную для вывода названия приватного ключа
pubkey="$privkey".pub
}
embed_key(){
#
}
for line in $(<$file)
do
ip=$(echo $line | cut -d ' ' -f 1)
vlan=$(echo $ip | cut -d '.' -f 3)
if [[ vlan -eq 0 ]]; then mask=23; gw="10.10.0.1"; else mask=24; gw="10.10.$vlan.1"
hostname=$(echo $line | cut -d ' ' -f 2)
vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//')
if [[ $vlan -eq 0 ]]; then $vlan=100
qm clone 5000 $vmnum --name $hostname --full
if [ $? -eq 0 ]
then echo "clone OK"
else "clone ERROR"; exit 1
fi
qm set $vmnum --tags 3,gfx
## preparing custom CloudInit snippets
cp ./user.yaml /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
sed -i "s/HOSTNAME/$hostname/g" /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
qm resize $vmnum scsi0 +50G
qm set $vmnum --cicustom "user=syno-tigra:snippets/${vmnum}_user.yaml"
qm set $vmnum --ipconfig0 ip=$ip/24,gw=10.10.35.1
qm cloudinit update $vmnum
qm start $vmnum
echo "VMs from $1 successfully created"
if [ haadd ]
then
ha-manager add vm:$vmnum --state started --max_relocate 2
ha-manager rules add node-affinity $ha_rule --resources vm:$vmnum --nodes pve1,pve2,pve3 --strict 1
# ОБЯЗАТЕЛЬНО ПРОВЕРИТЬ КОМАНДУ!!!
fi
echo "HA rules added"
done
# Финальная часть
if [ -e hosts.tmp]
then
rm hosts.tmp
fi
if [ -v $privkey ]
then
echo "Generated private key $privkey"
echo "SAVE IT IMMEDIATELY!!!"
fi