此脚本(initnetwork.py)只适用于删除单个用户下的路由接口及路由,同时删除provider(外部)网络和selfservice(内部)网络及其所对应的子网(字网名与网络名相同),删除后根据数据库信息重建OpenStack网络和路由(网络名及路由名均已写死)。
#-*- coding:utf-8 -*-
#!/usr/bin/env python
import traceback
import MySQLdb
from keystoneauth1 import loading
from keystoneauth1 import session
from novaclient import client
from glanceclient import client as gg
from neutronclient.v2_0 import client as nn
class programe(object):
def __init__(self):
self.loader = loading.get_plugin_loader('password')
self.auth = self.loader.load_from_options(auth_url='http://controller:5000/', username='admin', password='admin', project_id='xxxxxxxxxxxxxxxxxxxxxxxxxx')
self.sess = session.Session(auth = self.auth)
self.nova = client.Client(2, session = self.sess)
self.glance = gg.Client(2, session = self.sess)
self.neutron = nn.Client(session = self.sess)
# 连接数据库
def connection_db(self):
try:
self.db = MySQLdb.connect(host='0.0.0.0', user='root', passwd='root', db='database')
self.db_cur = self.db.cursor()
except:
traceback.print_exc()
# 关闭数据库
def close_db(self):
try:
self.db.close()
self.db_cur.close()
except:
raceback.print_exc()
# 删除旧路由接口及路由器
def delete_interface(self):
try:
subnet = self.neutron.list_subnets(name = 'selfservice')['subnets'][0]['id']
router_interface = {'subnet_id': subnet}
router_id = self.neutron.list_routers(name = 'router')['routers'][0]['id']
self.neutron.remove_interface_router(router_id, router_interface)
except:
pass
try:
self.neutron.remove_gateway_router(router_id)
except:
pass
try:
self.neutron.delete_router(router_id)
except:
pass
# 删除旧网络及子网
def delete_network(self):
try:
selfsub = self.neutron.list_subnets(name = 'selfservice')['subnets'][0]['id']
self.neutron.delete_subnet(selfsub)
selfservice = self.neutron.list_networks(name = 'selfservice')['networks'][0]['id']
self.neutron.delete_network(selfservice)
except:
pass
try:
prosub = self.neutron.list_subnets(name = 'provider')['subnets'][0]['id']
self.neutron.delete_subnet(prosub)
provider = self.neutron.list_networks(name = 'provider')['networks'][0]['id']
self.neutron.delete_network(provider)
except:
pass
# 将子网掩码转换为子网掩码长度
def exchange_mask(self, mask):
count_bit = lambda bin_str: len([i for i in bin_str if i=='1'])
mask_splited = mask.split('.')
mask_count = [count_bit(bin(int(i))) for i in mask_splited]
return sum(mask_count)
# 创建外部网络
def create_provider(self):
dbGetProvider = """select startip, endip, netmask, gateway, subnet, dns from system_ip where type = 'provider'"""
self.db_cur.execute(dbGetProvider)
startip, endip, netmask, gateway, subnet, dns = self.db_cur.fetchone()
masklen = self.exchange_mask(netmask)
cidr = str(subnet) + '/' + str(masklen)
network = {'name': 'provider', 'admin_state_up': True, 'provider:network_type': 'flat', 'provider:physical_network': 'provider', 'router:external': True, 'shared': True}
provider_id = self.neutron.create_network({'network': network})['network']['id']
subnet = {'enable_dhcp': True, 'network_id': provider_id, 'dns_nameservers': [dns], 'allocation_pools': [{'start': startip, 'end': endip}], 'gateway_ip': gateway, 'ip_version': 4, 'cidr': cidr, 'name': 'provider'}
self.neutron.create_subnet({'subnet': subnet})
return provider_id
# 创建内部网络
def create_selfservice(self):
dbGetSelfservice = """select netmask, gateway, subnet, dns from system_ip where type = 'selfservice'"""
self.db_cur.execute(dbGetSelfservice)
netmask, gateway, subnet, dns = self.db_cur.fetchone()
masklen = self.exchange_mask(netmask)
cidr = str(subnet) + '/' + str(masklen)
network = {'name': 'selfservice', 'admin_state_up': True}
selfservice_id = self.neutron.create_network({'network': network})['network']['id']
subnet = {'enable_dhcp': True, 'network_id': selfservice_id, 'dns_nameservers': [dns], 'gateway_ip': gateway, 'ip_version': 4, 'cidr': cidr, 'name': 'selfservice'}
subnet_id = self.neutron.create_subnet({'subnet': subnet})['subnet']['id']
return subnet_id
# 新建路由及添加接口
def add_interface(self, network_id, subnet_id):
router_id = self.neutron.create_router({'router':{'name': 'router'}})['router']['id']
router_interface = {'subnet_id': subnet_id}
self.neutron.add_interface_router(router_id, router_interface)
router_interface = {'network_id': network_id}
self.neutron.add_gateway_router(router_id, router_interface)
# 入口
def main(self):
self.connection_db()
self.delete_interface()
self.delete_network()
network_id = self.create_provider()
subnet_id = self.create_selfservice()
self.add_interface(network_id, subnet_id)
self.close_db()
initnetwork = programe()
initnetwork.main()
文章评论
:surprised:
@Lsir 2333333