Some script who permit create VM, drop VM, add this vm into netbox, rudder and zabbix. Increase VM ressource and drop it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

81 lines
3.8 KiB

#!/bin/bash
## author : Dryusdan
## date : 30/09/2019
## description : A vm deployment
## usage : ./upgradevm.sh vmname ram restart
## Import require config ##############################
source utils/config
source utils/color
source utils/logger
#######################################################
if [ $# -eq 0 ]
then
fatal "No arguments supplied. Usage : ./${SCRIPTNAME} vmname ram restart"
fi
if [ -z "${1+x}" ];
then
fatal "VMNAME is not defined. Usage : ./${SCRIPTNAME} vmname ram restart"
else
VMNAME="${1}"
fi
if [ -z "${2+x}" ];
then
fatal "VM Template ID is not defined. Usage : ./${SCRIPTNAME} vmname ram restart"
else
RAM="${2}"
fi
if [ -z "${3+x}" ];
then
RESTART="false"
else
RESTART="true"
fi
info "Get token for Proxmox"
PROXMOX_CREDENTIAL=$(curl -s --insecure -d "username=${PROXMOX_USER}@pam&password=${PROXMOX_PASSWORD}" "${PROXMOX_HOST}/api2/json/access/ticket")
PROXMOX_TOKEN=$(echo ${PROXMOX_CREDENTIAL} | jq --raw-output '.data.ticket')
PROXMOX_CSRF=$(echo ${PROXMOX_CREDENTIAL} | jq --raw-output '.data.CSRFPreventionToken')
info "Search vm with name ${VMNAME}"
for node in $(curl -s --insecure -b "PVEAuthCookie=${PROXMOX_TOKEN}" "${PROXMOX_HOST}/api2/json/nodes" | jq --raw-output ".data[] | @base64")
do
nodename=$(echo ${node} | base64 --decode | jq --raw-output ".node")
VMID=$(curl -s --insecure -b "PVEAuthCookie=${PROXMOX_TOKEN}" "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu" | jq --raw-output '.data[] | select(.name=="'${VMNAME}'") | .vmid')
if [ "${VMID}" != "" ]
then
info "Found vm ${VMNAME} with ID ${VMID} on ${nodename}"
break
fi
done
info "Upgrade RAM to ${RAM}MB"
curl -s --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X PUT -d "memory=${RAM}" "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}/config" &> /dev/null
if [ "${RESTART}" == "true" ]
then
info "Requested restart"
info "Stopping VM"
curl --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X POST "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}/status/shutdown" &> /dev/null
sleep 1
RUNNING=$(curl -s --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X GET "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}/status/current" | jq --raw-output '.data.status')
while [ ${RUNNING} == "running" ]
do
sleep 5
RUNNING=$(curl -s --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X GET "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}/status/current" | jq --raw-output '.data.status')
done
info "Starting VM"
curl --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X POST "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}/status/start" &> /dev/null
else
info "No restarting"
fi
info "Update Netbox"
clusterid=$(curl -s -X GET -H "Authorization: Token ${NETBOX_TOKEN}" -H "Accept: application/json; indent=4" "${NETBOX_URL}${NETBOX_API_PATH}/dcim/devices/?name=${nodename}" | jq --raw-output '.results[0].cluster.id')
deviceid=$(curl -s -X GET -H "Authorization: Token ${NETBOX_TOKEN}" -H "Accept: application/json; indent=4" "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/virtual-machines/?name=${VMNAME}" | jq --raw-output ".results[0].id")
curl -s -X PUT -H "Content-Type: application/json" -H "Authorization: Token ${NETBOX_TOKEN}" --data '{"name": "'${VMNAME}'", "cluster": '${clusterid}', "memory": '${RAM}'}' "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/virtual-machines/${deviceid}/" &> /dev/null
info "Update finish"
info "End"