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.
 
 

89 lines
5.5 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}" ];
then
fatal "VMNAME is not defined. Usage : ./${SCRIPTNAME} vmname ram restart"
else
VMNAME="${1}"
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 "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
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 "Remove host from Proxmox"
curl --insecure -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: ${PROXMOX_CSRF}" -b "PVEAuthCookie=${PROXMOX_TOKEN}" -X DELETE "${PROXMOX_HOST}/api2/json/nodes/${nodename}/qemu/${VMID}" &> /dev/null
info "Get token for Zabbix"
ZABBIX_AUTH_TOKEN=$(curl -s -X POST -H "Content-Type: application/json-rpc" -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"'${ZABBIX_USER}'","password":"'${ZABBIX_PASS}'"},"auth": null,"id":0}' ${ZABBIX_API} | jq --raw-output '.result')
info "Remove host from Zabbix"
HOSTID=$(curl -s -X GET -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.get","params": {"filter": {"host": ["'${VMNAME}'"]}},"auth": "'${ZABBIX_AUTH_TOKEN}'","id": 1}' ${ZABBIX_API} | jq --raw-output '.result[0].hostid')
curl -s -X GET -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.delete","params": ["'${HOSTID}'"],"auth": "'${ZABBIX_AUTH_TOKEN}'","id": 1}' ${ZABBIX_API} &> /dev/null
info "Remove host from Ansible"
rm -f "${ANSIBLE_PATH}/host_vars/${VMNAME}"
sed -i "/${VMNAME}/d" "${ANSIBLE_PATH}/hosts"
sudo sed -i "/${VMNAME}/d" /etc/hosts
info "Remove host from Prometheus"
sudo rm -f "/etc/prometheus/nodes/*${VMNAME}*"
sudo systemctl restart prometheus
info "Remove from know_host"
ssh-keygen -R "${VMNAME}"
info "Remove host from Rudder"
RUDDER_ID=$(curl -s -X GET -H 'Content-Type: application/json' -H "X-API-Token: ${RUDDER_API_TOKEN}" -H "X-API-Version: 12" "${RUDDER_SRV}/rudder/api/nodes" | jq --raw-output '.data.nodes[] | select(.hostname=="'${VMNAME}'") | .id')
curl -s -X DELETE -H 'Content-Type: application/json' -H "X-API-Token: ${RUDDER_API_TOKEN}" -H "X-API-Version: 12" "${RUDDER_SRV}/rudder/api/nodes/${RUDDER_ID}" &> /dev/null
info "Remove host from 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")
interfaceid=$(curl -s -X GET -H "Authorization: Token ${NETBOX_TOKEN}" -H "Accept: application/json; indent=4" "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/interfaces/?name=eth0&virtual_machine=${VMNAME}" | jq --raw-output ".results[0].id")
ipid=$(curl -s -X GET -H "Authorization: Token ${NETBOX_TOKEN}" -H "Accept: application/json; indent=4" "${NETBOX_URL}${NETBOX_API_PATH}/ipam/ip-addresses/?interface_id=${interfaceid}" | jq --raw-output ".results[0].id")
curl -s -X DELETE -H "Content-Type: application/json" -H "Authorization: Token ${NETBOX_TOKEN}" "${NETBOX_URL}${NETBOX_API_PATH}/ipam/ip-addresses/${ipid}/" &> /dev/null
curl -s -X DELETE -H "Content-Type: application/json" -H "Authorization: Token ${NETBOX_TOKEN}" "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/interfaces/${interfaceid}/" &> /dev/null
curl -s -X DELETE -H "Content-Type: application/json" -H "Authorization: Token ${NETBOX_TOKEN}" "${NETBOX_URL}${NETBOX_API_PATH}/virtualization/virtual-machines/${deviceid}/" &> /dev/null
info "Ending"