management-script/upgradecpu.sh

120 lines
5.0 KiB
Bash
Raw Normal View History

2020-04-21 22:18:28 +02:00
#!/bin/bash
## author : Dryusdan
## date : 30/09/2019
## description : A vm deployment
## usage : ./upgradecpu.sh vmname cpu restart
## Bash strict mode ####################################
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
## Bash color ##########################################
# Set colors
RED='\033[0;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
NC='\033[0m' # No Color
## Logs ################################################
readonly SCRIPTNAME="$(basename "$0")"
info() { echo -e "${LBLUE}[INFO] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
warning() { echo -e "${YELLOW}[WARNING] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
error() { echo -e "${LRED}[ERROR] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
fatal() { echo -e "${RED}[FATAL] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; exit 1 ; }
########################################################
## Define variables ####################################
NETBOX_URL="https://netbox.example"
NETBOX_API_PATH="/api"
NETBOX_TOKEN=""
PROXMOX_HOST="https://proxmox.example"
PROXMOX_USER="user"
PROXMOX_PASSWORD=""
#######################################################
if [ $# -eq 0 ]
then
fatal "No arguments supplied. Usage : ./${SCRIPTNAME} vmname cpu restart"
fi
if [ -z "${1+x}" ];
then
fatal "VMNAME is not defined. Usage : ./${SCRIPTNAME} vmname cpu restart"
else
VMNAME="${1}"
fi
if [ -z "${2+x}" ];
then
fatal "CPU is not defined. Usage : ./${SCRIPTNAME} vmname cpu restart"
else
CPU="${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 vCPU to ${CPU}"
curl -s --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X PUT -d "cores=${CPU}" "${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")
info "update"
curl -s -X PUT -H "Content-Type: application/json" -H "Authorization: Token ${NETBOX_TOKEN}" --data '{"name": "'${VMNAME}'", "cluster": '${clusterid}', "vcpu": '${CPU}'}' "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/virtual-machines/${deviceid}/" &> /dev/null
info "Update finish"
info "End"