you're reading...
Bash Scripts, Code, OpenStack

OpenStack Database Instance Removal Script

NOTE: This is old, I have been working on new scripts for Rundeck, one of the scripts is meant to do this same operation but for Icehouse.
I recommend you take a look at it. This script might work still, but I have not tested it against any of the newer releases of OpenStack, I believe it was written against Essex.

This is a script I use to remove instances from the OpenStack database when things go bad.

The script is fairly straight forward, you pass it an instance name, it looks it up in the DB to get the ID and UUID, then removes it from the tables.

If you go this route, you will need to use virsh to shutdown and undefine the instance and manually remove the instance data from the file system.
If using Quantum/OVS then you will need to also find the network ports (quantum port-list) and remove them also (port-delete).

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    GNU General Public License for more details.
#    <http://www.gnu.org/licenses/>.


if [ -z "$1" ]; then echo "VM Name not given"; exit 1; fi
Q=`cat <<EOF
SELECT id FROM nova.instances WHERE instances.display_name = '$VMNAME';
SELECT uuid FROM nova.instances WHERE instances.display_name = '$VMNAME';
RQ=`mysql --batch --skip-column-names -e "$Q"`

ID=`echo $RQ | cut -d' ' -f1`
UUID=`echo $RQ | cut -d' ' -f2`

if [ -z "$ID" ]; then echo "ID for $VNAME not found"; exit 1; fi
if [ -z "$UUID" ]; then echo "UUID for $VNAME not found"; exit 1; fi

echo "ID: $ID"
echo "UUID: $UUID"

echo "Delete $VMNAME? (y/n)"
read -e YN
if [ "$YN" != 'y' ]; then echo "Exiting...";exit 1;fi

Q=`cat <<EOF
DELETE FROM nova.instance_faults WHERE instance_faults.instance_uuid = '$UUID';
DELETE FROM nova.instance_id_mappings WHERE instance_id_mappings.uuid = '$UUID';
DELETE FROM nova.instance_info_caches WHERE instance_info_caches.instance_uuid = '$UUID';
DELETE FROM nova.instance_system_metadata WHERE instance_system_metadata.instance_uuid = '$UUID';
DELETE FROM nova.security_group_instance_association WHERE security_group_instance_association.instance_uuid = '$UUID';
DELETE FROM nova.instances WHERE instances.uuid = '$UUID';
RQ=`mysql --batch --skip-column-names -e "$Q"`
echo "$RQ"


2 thoughts on “OpenStack Database Instance Removal Script

  1. With my particular error scenario, I also had to delete the record from the metadata table as well:
    DELETE FROM nova.instance_metadata WHERE instance_uuid = ‘690941e1-06cc-4ada-8df3-ed589f96a8b4’;

    Thanks for the post though! It really worked well.

    Posted by Danny Beutler | January 7, 2014, 9:48 am
  2. Here’s an openstack Kilo set of queries:

    for Q in \
    “DELETE from nova.instance_actions_events where action_id in ( select id from nova.instance_actions where instance_uuid = ‘$UUID’ );” \
    “DELETE from nova.instance_actions where instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.instance_faults WHERE instance_faults.instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.instance_id_mappings WHERE instance_id_mappings.uuid = ‘$UUID’;” \
    “DELETE FROM nova.instance_info_caches WHERE instance_info_caches.instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.instance_system_metadata WHERE instance_system_metadata.instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.security_group_instance_association WHERE security_group_instance_association.instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.migrations where instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.instance_extra where instance_uuid = ‘$UUID’;” \
    “DELETE FROM nova.instances WHERE instances.uuid = ‘$UUID’;”
    echo $Q
    RQ=`mysql –batch –skip-column-names -e “$Q”`
    echo $RQ

    Posted by kindjal | January 19, 2016, 9:28 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s