OpenStack(4)
脚本主要包括三个方面:
1.删除指定的僵尸实例;
2.清空某个project的数据;
3.清空所有projects的数据。
#!/bin/bash
case &$1& in
read -p &Please input specified instance UUID: &
echo &Begin to remove specified instance $REPLY.....&
./clean_instance.sh $1 $REPLY
echo &Script completes!!!!!&
read -p &Please input specified project tenand_id: &
echo &Begin to clear up specified project instances $REPLY.....&
./clean_instance.sh $1 $REPLY
echo &Script completes!!!!!&
read -p &Warnning: The operate would be clear up all project instances,do you want to excute it still [y/n]?&
echo &begin to clear up all project instances.....&
./clean_instance.sh $1 $REPLY
echo &Script completes!!!!!&
echo &Usage:
--- will remove specified instance.
--- will clear up specified project instances.
--- will clear up all project instances.&
clean_all_instances.sh
#!/bin/bash
###########################################
# @file clean_all_instances.sh
# @brief Remove project database in mysql #
# @author Evan Yang
# @version 1.1
###########################################
#####################################
Remove nova datebase
#####################################
##############Set variables##########
USER=&root&
PASSWORD=&password&
if [ &$1& -eq 2 ];then
OPTIONAL_TID=&WHERE tenant_id='$2'&
OPTIONAL_TID2=&AND tenant_id='$2'&
OPTIONAL_PID=&WHERE project_id='$2'&
OPTIONAL_TID=&&
OPTIONAL_TID2=&&
OPTIONAL_PID=&&
echo $OPTIONAL_TID
FLOATING_IPS=`mysql -u$USER -p$PASSWORD -se &USE SELECT floating_ip_address FROM floatingips $OPTIONAL_TID;&`
INSTANCES=`mysql -u$USER -p$PASSWORD -se &USE SELECT distinct display_name FROM instances $OPTIONAL_PID;&`
UUIDS=`mysql -u$USER -p$PASSWORD -se &USE SELECT uuid FROM instances $OPTIONAL_PID;&`
#########Remove all floating-ip############
for fi in $FLOATING_IPS
for in in $INSTANCES
echo $in &:Begin to remove floating ip...&
nova remove-floating-ip $in $fi
echo &The floating ip has been removed.&
########Reset values of quota_usages########
mysql -u$USER -p$PASSWORD -e &USE UPDATE quota_usages SET in_use=0 $OPTIONAL_PID;&
########Remove instance in nova database#####
for ui in $UUIDS
echo &begin to remove instance $ui...&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM security_group_instance_association WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_info_caches WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM block_device_mapping WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_extra WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_actions_events WHERE action_id IN (SELECT id FROM instance_actions WHERE instance_uuid='$ui') ;&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_actions WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_faults WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_system_metadata WHERE instance_uuid='$ui';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instances WHERE uuid='$ui';&
rm -rf /var/lib/nova/instances/$ui
echo &The instance has been removed.&
#####################################
Remove neutron datebase
#####################################
echo &Begin to remove neutron...&
#######Remove routers from neutron database#######
echo &Begin to remove routers...&
RESERVE_ROUTER_ID=`mysql -u$USER -p$PASSWORD -se &USE SELECT id FROM routers WHERE name ='extrouter';&`
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM routers WHERE id != '$RESERVE_ROUTER_ID' $OPTIONAL_TID2;&
echo &All routers have been removed.&
########Remove ports from neutron database########
echo &Begin to remove ports...&
RESERVE_PORT_ID=`mysql -u$USER -p$PASSWORD -se &USE SELECT port_id FROM routerports WHERE router_id = '$RESERVE_ROUTER_ID';&`
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM ports WHERE id !='$RESERVE_PORT_ID' $OPTIONAL_TID2;&
echo &All ports have been removed.&
#########Remove subnets from neutron database######
echo &Begin to remove subnets...&
RESERVE_NETWORK_ID=`mysql -u$USER -p$PASSWORD -se &USE SELECT network_id FROM&`
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM subnets WHERE network_id != '$RESERVE_NETWORK_ID' $OPTIONAL_TID2;&
echo &All subnets have been removed.&
#########Remove networks from neutron database######
echo &Begin to remove networks...&
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM networks WHERE id != '$RESERVE_NETWORK_ID' $OPTIONAL_TID2;&
echo &All networks have been removed.&
echo &The neutron has been removed.&
#####################################
Remove cinder datebase
#####################################
echo &Begin to remove cinder...&
VOLUME_ID=`mysql -u$USER -p$PASSWORD -se &USE SELECT id FROM volumes $OPTIONAL_PID;&`
for vid in $VOLUME_ID
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM reservations $OPTIONAL_PID;&
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM quota_usages $OPTIONAL_PID;&
mysql -u$USER -p$PASSWORD -se &USE UPDATE iscsi_targets SET volume_id='NULL' where volume_id='$vid';&
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM volume_admin_metadata where volume_id='$vid';&
mysql -u$USER -p$PASSWORD -se &USE DELETE FROM volumes $OPTIONAL_PID;&
echo &delete volume $vid from hardware... &
rm -rf /dev/cinder-volumes/volume-$vid
echo &volume $vid has been removed.&
echo &All subnets have been removed.&
clean_instance.sh
#!/bin/bash
###########################################
# @file clean_instance.sh
# @brief Remove database in mysql
# @author Evan Yang
# @version 1.0
###########################################
##############Set variables##########
USER=&root&
PASSWORD=&password&
#####################################
if [ -n &$2& ];then
if [ &$1& -eq 1 ];then
UUID=`mysql -u$USER -p$PASSWORD -se &USE SELECT distinct uuid FROM instances WHERE uuid='$2';&`
if [ -n &$UUID& ];then
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM security_group_instance_association WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_info_caches WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM block_device_mapping WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_extra WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_actions_events WHERE action_id IN (SELECT id FROM instance_actions WHERE instance_uuid='$2') ;&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_actions WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_faults WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instance_system_metadata WHERE instance_uuid='$2';&
mysql -u$USER -p$PASSWORD -e &USE DELETE FROM instances WHERE uuid='$2';&
rm -rf /var/lib/nova/instances/$2
echo &The instance $2 has been removed.&
echo &The specified instance is not exsit,please check it.&
elif [ &$1& -eq 2 ];then
INSTANCE=`mysql -u$USER -p$PASSWORD -se &USE SELECT distinct id FROM project WHERE id='$2';&`
if [ -n &$INSTANCE& ];then
./clean_all_instances.sh $1 $2
echo &The specified project is not exsit,please check it.&
elif [ &$1& -eq 3 ];then
if [ &$2&x = &y&x ]||[ &$2&x = &yes&x ]||[ &$2&x = &Y&x ];then
./clean_all_instances.sh $1
echo &You have cancelled the operation.&
echo &Wrong input parameter,please check.&
echo &Lack of some parameters,please check and try it again.&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1390次
排名:千里之外dreaminthewind 的BLOG
用户名:dreaminthewind
文章数:40
访问量:21368
注册日期:
阅读量:5863
阅读量:12276
阅读量:382866
阅读量:1074489
51CTO推荐博文
前天强制重启一台 OpenStack Nova 控制结点以后发现虚拟机消失,但是 nova-list 命令显示 instances 仍然是 running 的状态,使用 nova-delete 终止命令仍然无效,暂时把这样的 instance 称作“僵尸实例(zombie instance)”:# virsh list
----------------------------------
# euca-describe-instances
RESERVATION r-bkl83j20 bangcloud default
INSTANCE i-0000001d ami-.16.39.121 172.16.39.121 running vpsee (vpseecloud, node00) 0
T12:45:12Z nova aki- ami-
RESERVATION r-j335q6ny bangcloud default
INSTANCE i-0000001e ami-.16.39.122 172.16.39.122 running vpsee (vpseecloud, node00) 0
T12:54:27Z nova aki- ami-
# euca-terminate-instances i-0000001d
# euca-terminate-instances i-0000001e
这篇文章提到的解决办法一样,直接操作数据库来删除这2条僵尸实例的记录。登录 mysql,使用 nova 数据库,找出要删除 instance 的 id,然后删除:# mysql -u root -p
Enter password:
mysql& select *
mysql& delete from instances where id = '29';
ERROR ): Cannot delete or update a parent row: a foreign key constraint fails (`nova`.`virtual_interfaces`, CONSTRAINT `virtual_interfaces_ibfk_1` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`id`))
MySQL 删除 id 为 29 的 instance 时触发外键限制错误,简单的办法是暂时关闭外键检查,等删除后再打开:mysql& SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)
mysql& delete from instances where id = '29';
Query OK, 1 row affected (0.04 sec)
mysql& delete from instances where id = '30';
Query OK, 1 row affected (0.04 sec)
mysql& SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.00 sec)
删除 instance 29 和 30后再用 euca-describe-instances 命令验证一下:# euca-describe-instances
了这篇文章
类别:未分类┆阅读(0)┆评论(0)