management-script/deletehost.sh

128 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
## author : Dryusdan
## date : 30/09/2019
## description : A vm deployment
## usage : ./upgradevm.sh vmname ram 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.exemple"
NETBOX_API_PATH="/api"
NETBOX_TOKEN=""
PROXMOX_HOST="https://proxmox.exemple"
PROXMOX_USER="user"
PROXMOX_PASSWORD=""
ZABBIX_USER="user"
ZABBIX_PASS=""
ZABBIX_SERVER="zabbix.tld"
ZABBIX_API="https://${ZABBIX_SERVER}/api_jsonrpc.php"
# First hostgroup of vm
ZABBIX_HOSTGROUPID1=2
# second hostgroup of vm
ZABBIX_HOSTGROUPID2=15
# First template of vm
ZABBIX_TEMPLATEID=10001
# Ansible path to run postinstall script
ASIBLE_PATH="/home/ansible/Ansible"
RUDDER_API_TOKEN=""
RUDDER_SRV="https://rudder.exemple"
#######################################################
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
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 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")
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 "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 "Ending"