From e88df481f2e39c253c856c1e87ea024dc8843134 Mon Sep 17 00:00:00 2001 From: Lyanis SOUIDI Date: Tue, 10 Sep 2024 10:55:38 +0200 Subject: [PATCH] Update vm/create_from_template.sh --- vm/create_from_template.sh | 41 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/vm/create_from_template.sh b/vm/create_from_template.sh index 8b267d3..4056ee1 100644 --- a/vm/create_from_template.sh +++ b/vm/create_from_template.sh @@ -1,29 +1,38 @@ #!/bin/bash +TEMPLATE_VM_ID=202404001 INPUT_FILE=$1 +POOL_NAME=$2 +NEXT_VM_ID=$3 -if [ -z "$INPUT_FILE" ]; then - echo "Usage: $0 " +if ! command -v jq &> /dev/null; then + echo "jq is not installed. Please install it using 'apt install jq' to continue." + exit 1 +fi + +if [ -z "$INPUT_FILE" ] | [ -z "$POOL_NAME" ] | [ -z "$NEXT_VM_ID" ]; then + echo "Usage: $0 " echo "Input file format:" - echo -e "\tusername pool_name" - echo -e "\tusername pool_name" + echo -e "\tusername1" + echo -e "\tusername2" echo -e "\t..." exit 1 fi -NEXT_VM_ID=202404101 +POOL_EXISTS=$(pvesh get /pools --output-format=json | jq -r ".[] | select(.poolid == \"$POOL_NAME\") | .poolid") -TEMPLATE_VM_ID=202404001 - -while read -r line; do - USERNAME=$(echo "$line" | awk '{print $1}') - POOL_NAME=$(echo "$line" | awk '{print $2}') - - echo "Creating VM for $USERNAME ($POOL_NAME)" - - qm clone $TEMPLATE_VM_ID $NEXT_VM_ID --name "$USERNAME" --full false --pool $POOL_NAME - - pvesh set /access/acl --path /vms/$NEXT_VM_ID --roles PVEVMUser --users "$USERNAME@arda.lan" +if [ "$POOL_EXISTS" == "$POOL_NAME" ]; then + echo "Resource pool '$POOL_NAME' exists." +else + echo "Resource pool '$POOL_NAME' does not exist, creating..." + pvesh create /pools --poolid $POOL_NAME +fi +while read -r username; do + USERNAME=$(echo "$line") + echo "Creating VM for $username in $POOL_NAME" + qm clone $TEMPLATE_VM_ID $NEXT_VM_ID --name "$username" --full false --pool $POOL_NAME + qm set $NEXT_VM_ID --args "-fw_cfg name=\"opt/fr.iut-fbleau/hostname\",string=\"vm-$username\" -fw_cfg name=\"opt/fr.iut-fbleau/domain\",string=\"vm.iut-fbleau.fr\"" + pvesh set /access/acl --path /vms/$NEXT_VM_ID --roles PVEVMUser --users "$username@arda.lan" ((NEXT_VM_ID++)) done < "$INPUT_FILE"