2020-04-21 22:18:28 +02:00
|
|
|
#!/bin/bash
|
|
|
|
## author : Dryusdan
|
|
|
|
## date : 30/09/2019
|
|
|
|
## description : A vm deployment
|
|
|
|
## usage : ./upgradevm.sh vmname ram restart
|
|
|
|
|
2020-04-21 23:54:23 +02:00
|
|
|
## Import require config ##############################
|
|
|
|
source utils/config
|
|
|
|
source utils/color
|
|
|
|
source utils/logger
|
2020-04-21 22:18:28 +02:00
|
|
|
#######################################################
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-21 23:54:23 +02:00
|
|
|
|
2020-04-21 22:18:28 +02:00
|
|
|
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
|
|
|
|
|
2020-04-21 23:54:23 +02:00
|
|
|
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
|
|
|
|
|
2020-04-21 22:18:28 +02:00
|
|
|
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
|
|
|
|
|
2020-04-23 15:17:29 +02:00
|
|
|
info "Remove from know_host"
|
|
|
|
ssh-keygen -R "${VMNAME}"
|
|
|
|
|
2020-04-21 22:18:28 +02:00
|
|
|
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")
|
2020-04-21 23:54:23 +02:00
|
|
|
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
|
2020-04-21 22:18:28 +02:00
|
|
|
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"
|