Skip to content

Instantly share code, notes, and snippets.

@ranganathanm
Created December 7, 2018 19:39
Show Gist options
  • Save ranganathanm/6fcd94ad0d568d00156cff08b055c4b0 to your computer and use it in GitHub Desktop.
Save ranganathanm/6fcd94ad0d568d00156cff08b055c4b0 to your computer and use it in GitHub Desktop.
Controller script to set up routing into an octavia router network
from keystoneauth1 import identity
from keystoneauth1 import session
from neutronclient.v2_0 import client
from argparse import ArgumentParser
import os
import sys
import pdb
from ovs_vsctl import VSCtl
import subprocess
import ovs_vsctl.parser as ovs_vsctl_parser
"""
emit the commands you need to run to link a router namespace to a network.
To run this script create a port for the entwork you want to access.
Usage : python access-lbnet-from-qrouter.py port router
Leave out the router argument for the root namespace.
"""
vsctl = VSCtl('tcp', '127.0.0.1', 6640)
def find_internal_vlan_tag_for_network( network_id):
interal_vlan_tag = -1
ports = vsctl.run(command='list port',parser=ovs_vsctl_parser.list_cmd_parser)
for port in ports:
other_config = port.other_config
if 'net_uuid' in other_config and str(other_config['net_uuid']) == network_id:
internal_vlan_tag = other_config['tag']
break
return internal_vlan_tag
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("port", help = "network port", default = None)
parser.add_argument("router", help = "Router name that you want to link to", default = None)
args = parser.parse_args()
router_name = args.router username= os.environ.get("OS_USERNAME")
password= os.environ.get("OS_PASSWORD")
project_name= os.environ.get("OS_PROJECT_NAME")
project_domain_id='default'
if os.environ.get("OS_PROJECT_DOMAIN_NAME") is not None:
project_domain_id = os.environ.get("OS_PROJECT_DOMAIN_NAME")
user_domain_id='default'
if os.environ.get("OS_USER_DOMAIN_NAME") is not None:
user_domain_id= os.environ.get("OS_USER_DOMAIN_NAME")
auth_url= os.environ.get("OS_AUTH_URL")
if auth_url is None:
print "Missing auth url ... set OS_AUTH_URL environment variable"
sys.exit()
auth = identity.Password(auth_url=auth_url,
username=username,
password=password,
project_name=project_name,
project_domain_id=project_domain_id,
user_domain_id=user_domain_id)
sess = session.Session(auth=auth)
neutron = client.Client(session=sess)
ports = neutron.list_ports()['ports']
lb_ovs_port_name = None
port_mac_address = None
port_ip_address = None
network_id = None
for port in ports:
if port_name == port['id'] or port_name == port['name']:
if lb_ovs_port_name is None:
lb_ovs_port_name = "lb-" + port['id'][0:11]
port_mac_address = port['mac_address']
port_ip_address = port['fixed_ips'][0]['ip_address']
network_id = port['network_id']
else:
print ("ERROR: More than one port with name " + port_name)
break
if port_ip_address is None or network_id is None or port_mac_address is None:
print("Cant find port assignments")
sys.exit()
ovs_command = "ovs-vsctl add-port br-int {} -- set interface {} type=internal -- set port {} tag={} -- set port {} other-config:hwaddr={}"\
.format(lb_ovs_port_name,lb_ovs_port_name,lb_ovs_port_name,vlan_tag,lb_ovs_port_name,port_mac_address)
print ovs_command
ifconfig_command = "ifconfig {} {}".format(lb_ovs_port_name,port_ip_address)
if router_name is None:
print ifconfig_command
else:
routers = neutron.list_routers()['routers']
router_id = None
for router in routers:
if str(router['name']) == router_name:
router_id = router['id']
elif str(router['id']) == router_name:
router_id = router_name
if router_id is None:
print ("Cannot find router " + router_name)
sys.exit()
router_namespace = "qrouter-" + router_id
print "ip link set {} netns {}".format(lb_ovs_port_name,router_namespace)
print "ip netns exec " + router_namespace + " " + ifconfig_command
vlan_tag = find_internal_vlan_tag_for_network(network_id)
port_name = args.port
if port_name is None:
print "Specify lbnet"
sys.exit()
username= os.environ.get("OS_USERNAME")
password= os.environ.get("OS_PASSWORD")
project_name= os.environ.get("OS_PROJECT_NAME")
project_domain_id='default'
if os.environ.get("OS_PROJECT_DOMAIN_NAME") is not None:
project_domain_id = os.environ.get("OS_PROJECT_DOMAIN_NAME")
user_domain_id='default'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment