Skip to content

Instantly share code, notes, and snippets.

@yuanying
Last active March 23, 2017 01:13
Show Gist options
  • Save yuanying/b7bcf24b0c94bae8790de22c32dac103 to your computer and use it in GitHub Desktop.
Save yuanying/b7bcf24b0c94bae8790de22c32dac103 to your computer and use it in GitHub Desktop.
Delete Nova VM records from Nova DB (Mitaka Ver.)

Delete Nova instace without Nova CLI

Delete virt domain directly

$ virsh destroy ${TARGET_DOMAIN_ID}
$ virsh undefine ${TARGET_DOMAIN_ID}
$ # And another work what you need...

Delete record from Nova DB

$ bash delete-vm-from-db.sql.sh ${VM_UUID} ${OS_USER_ID} ${OS_PROJECT_ID} | mysql -uroot -p nova

Detach volume from Cinder DB

$ bash detach-volume-from-db.sql.sh ${VOLUME_UUID} | mysql -uroot -p cinder
#!/usr/bin/env bash
# Usage:
# bash delete-vm-from-db.sql.sh ${VM_UUID} ${OS_USER_ID} ${OS_PROJECT_ID} | mysql -uroot -p nova
set -eu
export LC_ALL=C
vm_uuid=${1}
user_id=${2}
project_id=${3}
flavor=$(openstack server show -f value -c flavor ${vm_uuid})
flavor=($flavor)
flavor=${flavor[0]}
eval $(openstack flavor show ${flavor[0]} -c vcpus -c ram -f shell)
cat <<EOF
update quota_usages
set
resource=resource-1,
updated_at=NOW()
where project_id='$project_id' and user_id='$user_id' and resource='instances';
update quota_usages
set
resource=resource-${ram},
updated_at=NOW()
where project_id='$project_id' and user_id='$user_id' and resource='ram';
update quota_usages
set
resource=resource-${vcpus},
updated_at=NOW()
where project_id='$project_id' and user_id='$user_id' and resource='cores';
update block_device_mapping
set
deleted='1',
deleted_at=NOW()
where instance_uuid='$vm_uuid';
insert into instance_actions
(created_at, action, instance_uuid, request_id, user_id, project_id, start_time)
values
(
NOW(),
'delete',
'$vm_uuid',
'req-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'$user_id',
'$project_id',
NOW()
);
insert into instance_actions_events
(created_at, updated_at, event, action_id, start_time, finish_time, result)
values
(
NOW(),
NOW(),
'compute_terminate_instance',
LAST_INSERT_ID(),
NOW(),
NOW(),
'Success'
);
update instance_extra
set
deleted='1',
deleted_at=NOW()
where instance_uuid='$vm_uuid';
update instance_info_caches
set
deleted='1',
deleted_at=NOW(),
network_info='[]'
where instance_uuid='$vm_uuid';
update instance_system_metadata
set
deleted='1',
deleted_at=NOW()
where instance_uuid='$vm_uuid';
update instances
set
deleted='1',
vm_state='deleted',
deleted_at=NOW()
where uuid='$vm_uuid';
EOF
#!/usr/bin/env bash
# Usage:
# bash detach-volume-from-db.sql.sh ${VOLUME_UUID} | mysql -uroot -p cinder
set -eu
export LC_ALL=C
volume_uuid=${1}
cat <<EOF
update volumes
set
attach_status='detached',
status='available'
where id ='$volume_uuid';
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment