介绍
在以openstack为底层开发的一些项目中,常常会遇到项目中数据与openstack数据不同步的问题,为了简化清空无效数据的繁琐,提供以下脚本便于运维操作。
环境变量
[root@cloud ~]# cat admin.sh
export OS_USERNAME=admin
export OS_PASSWORD=openstack
export OS_PROJECT_NAME=admin
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default
export OS_AUTH_URL=http://cloud:35357/v3
export OS_IDENTITY_API_VERSION=3
脚本示例
[root@cloud ~]# cat clean.sh
source /root/admin.sh#删除无项目虚拟机
project_id=`openstack project list |awk '{print $2}'`
vms=`nova list --all-tenants |awk -F'|' '{print $2,$3,$4,$5}'|grep -v "ID"`
ifs_old=$IFS
IFS=$'\n'
echo "开始清除无效虚拟机"
for vm in $vms
do
{
vm_id=`echo $vm |awk '{print $1}'`
p_id=`echo $vm |awk '{print $3}'`
name=`echo $vm |awk '{print $2}'`
if [[ $project_id =~ $p_id ]]
then
sleep 0
else
nova delete $vm_id 2&>/etc/null
echo "清除虚拟机($name)"
fi
}&
done
wait
####################################################################################
echo "无效虚拟机已被清除,下一步清除无效路由器"
#删除无项目路由器
routers=`openstack router list |awk -F'|' '{print $2,$3,$8}' |grep -v "Project"`
for router in $routers
do
{
router_id=`echo $router|awk '{print $1}'`
router_name=`echo $router|awk '{print $2}'`
router_p_id=`echo $router|awk '{print $3}'`
if [[ $project_id =~ $router_p_id ]]
then
sleep 0
else
mysql -uroot -popenstack << EOF
use neutron;
delete from routerroutes where router_id="$router_id"
EOF
subnet_ids=`neutron router-port-list $router_id |awk -F '|' '{print $6}'|grep "subnet_id"|awk -F'"' '{print $4}'`
for i in $subnet_ids
do
neutron router-interface-delete $router_id $i 2&>/dev/null
done
neutron router-delete $router_id 2&>/dev/null
echo "清除路由器($router_name)"
fi
}&
done
wait
###############################################################################################
echo "无效路由器已被清除,下一步清除无效子网"
nets==`neutron net-list |awk -F'|' '{print $2,$3,$4,$5}'|grep -v "tenant_id"`
for net in $nets
do
{
net_id=`echo $net |awk '{print $1}'`
net_name=`echo $net |awk '{print $2}'`
net_p_id=`echo $net |awk '{print $3}'`
sub_id=`echo $net |awk '{print $4}'`
if [[ $project_id =~ $net_p_id ]]
then
sleep 0
else
mysql -uroot -popenstack << EOF
use neutron;
delete from ports where network_id="$net_id";
EOF
if [ -z $sub_id ]
then
sleep 0
else
openstack subnet delete $sub_id
fi
neutron net-delete $net_id 2&>/dev/null
echo "清除子网($net_name)"
fi
}&
done
wait
echo "清除脚本执行完成,请继续使用平台"
分享、在看与点赞
只要你点,我们就是胖友
来自: 脚本清空openstack无效数据https://mp.weixin.qq.com/s?__biz=Mzk0NTQ3OTk3MQ==&mid=2247486386&idx=1&sn=a2734c5a301815bfae1beaff54458401&chksm=c31583fef4620ae848727e26d190a1a1576f0b4f053adfb9d43290652d8daf96b64a534ea966&token=355315523&lang=zh_CN#rd