Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active February 24, 2018 04:17
Show Gist options
  • Save ThinGuy/b33901446391b64f161a0a86ad2db178 to your computer and use it in GitHub Desktop.
Save ThinGuy/b33901446391b64f161a0a86ad2db178 to your computer and use it in GitHub Desktop.
Remove Hypervisors from Openstack (mysql) Database (deployed via juju)
export DB_APP=$(juju 2>/dev/null status --format=json|jq 2>/dev/null -r '.applications | to_entries[] |"\(select((.value."charm-name"|startswith("perc")) or .value."charm-name" == "mysql").key)"')
export MYSQL_PW=$(juju 2>/dev/null run --unit ${DB_APP}/0 leader-get |awk 2>/dev/null '/mysql.passwd/{print $2}')
#Create array of Hypervisors with hostnames, virt type and IP
declare -ag OS_COMPUTE_NODES=($(juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -s -N -D nova -e 'SELECT hypervisor_hostname,hypervisor_type,host_ip FROM compute_nodes;'" 2>/dev/null|sed 's/\t/,/g'|sort -uV))
# Print list of hypervisors from above array
for N in ${!OS_COMPUTE_NODES[@]};do printf "\e[2G$(($N+1))\e[0m) ${OS_COMPUTE_NODES[$N]//,/\\t}\n"; done|column -next
#Declare Hypervisor hostname that should be removed
export DELNODE=
# List compute nodes in nova db
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D nova -e 'select hypervisor_hostname,hypervisor_type,host_ip FROM compute_nodes;'"
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D nova -e 'SELECT hypervisor_hostname,hypervisor_type,host_ip FROM compute_nodes WHERE hypervisor_hostname LIKE \"${DELNODE}%\";'"
# Delete node in question from nova db
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D nova -e 'DELETE FROM compute_nodes WHERE hypervisor_hostname="${DELNODE}";'" 2>/dev/null
# Show compute nodes nova db again
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D nova -e 'select hypervisor_hostname,hypervisor_type,host_ip FROM compute_nodes;'"
# Show OVS agents in neutron db
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D neutron -e 'SELECT id,agent_type,host FROM agents;'"
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D neutron -e 'SELECT id,agent_type,host FROM agents WHERE agent_type LIKE \"Open%\";'"
# Delete OVS agents on nodes in question from neutron db
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D neutron -e 'DELETE FROM agents WHERE host="${DELNODE}";'"
#Show OVS agents again in neutron db
juju ssh mysql/0 "mysql 2>/dev/null -u root -p${MYSQL_PW} -D neutron -e 'SELECT id,agent_type,host FROM agents WHERE agent_type LIKE \"Open%\";'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment