Skip to content

Instantly share code, notes, and snippets.

@odyssey4me
Created February 5, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odyssey4me/8828644 to your computer and use it in GitHub Desktop.
Save odyssey4me/8828644 to your computer and use it in GitHub Desktop.
This is a pseudo-code description of the process to convert nova-network entries to quantum/neutron entries. The idea is based on retrieving the data directly from the nova database and then injecting the appropriate entries via the API into quantum/neutron. It's specific to the use-case of converting vlan-managed nova-network entries to a gre-b…
##### network conversion #####
PUBLIC_NETWORK_UUID=`quantum net-list | grep public | awk '{print $2}'`
foreach row of `select id,project_id,cidr,dns1,dns2 from networks where deleted=0 and project_id is not null;` do
TENANT_ID=project_id
TENANT_NAME=`keystone tenant-list | grep $(TENANT_ID) | awk '{print $4}'`
NETWORK_NAME=$(TENANT_NAME)+'_Network_'+id
SUBNET_NAME=$(TENANT_NAME)+'_Subnet_'+id
if dns1 = NULL then DNS_NAMESERVER1 = '8.8.8.8' else DNS_NAMESERVER1=dns1
if dns2 = NULL then DNS_NAMESERVER2 = '8.8.4.4' else DNS_NAMESERVER2=dns2
SUBNET_CIDR=cidr
## create customer network and subnet
#TODO: add check to see if network already exists
quantum net-create --tenant-id TENANT_ID NETWORK_NAME
# returns NETWORK_UUID
#TODO: add check to see if subnet already exists
quantum subnet-create --tenant-id TENANT_ID --name SUBNET_NAME --dns-nameserver DNS_NAMESERVER1 --dns-nameserver DNS_NAMESERVER2 NETWORK_UUID SUBNET_CIDR
# returns SUBNET_UUID
## create router for tenant, if one doesn't already exist
ROUTER_NAME=$(TENANT_NAME)+'_Router1'
if `quantum router-list --tenant_id=69ed9614675840ca923db1557a88862f | wc -l` > 1 then
ROUTER_UUID=`quantum router-list --tenant_id=35ec97f8a1194d0080668ce6e5063319 | tail -n 2 | head -n 1 | awk '{print $2}'`
else
quantum router-create --tenant-id TENANT_ID ROUTER_NAME
#returns ROUTER_UUID
## set the router gateway, but only if the tenant has floating ip associations
## this is to prevent the network being connected to the internet for specialised situations
select address from floating_ips where deleted=0 and project_id='TENANT_ID' and fixed_ip_id is not NULL;
if this returns an empty set, then
do nothing
else
#TODO: add check to see if gateway already exists
quantum router-gateway-set ROUTER_UUID PUBLIC_NETWORK_UUID
## attach internal interface to network
#TODO: add check to see if interface already exists
quantum router-interface-add ROUTER_UUID SUBNET_UUID
done
##### security group conversion #####
# We only select the list of projects which have networks associated with them. Any others are ignored.
foreach row of `select distinct project_id from networks where deleted=0 and project_id is not null;` do
TENANT_ID=project_id
# For each tenant we create all the groups
foreach row of `select name,description from security_groups where deleted=0 and project_id=TENANT_ID;` do
GROUP_NAME=name
GROUP_DESCRIPTION=description
# create the group if it doesn't already exist
if `quantum security-group-list --tenant_id=TENANT_ID | tail -n +4 | head -n -1 | awk '{print $4}' | grep "^$(GROUP_NAME)$" | wc -l` = 0 then
quantum security-group-create --tenant-id TENANT_ID --description GROUP_DESCRIPTION GROUP_NAME
done
# Now that the groups are created, we can safely create the rules
# We can't create all the rules in the previous loop as rules sometimes refer to each other
foreach row of `select name from security_groups where deleted=0 and project_id=TENANT_ID;` do
GROUP_NAME=name
SECURITY_GROUP_UUID=`quantum security-group-list --tenant_id=${TENANT_ID} --name=GROUP_NAME | grep GROUP_NAME | awk '{print $2}'`
# Now we convert the rules for that group
foreach row of `select protocol,from_port,to_port,cidr,group_id from security_group_rules where deleted=0 and parent_group_id=GROUP_ID;` do
PROTOCOL=protocol
PORT_RANGE_MIN=from_port
PORT_RANGE_MAX=to_port
REMOTE_IP_CIDR=cidr
if group_id=NULL then
SOURCE_GROUP=NULL
else
SOURCE_GROUP_NAME=`select name from security_groups where id=group_id`
SOURCE_GROUP_UUID=`quantum security-group-list --tenant_id=${TENANT_ID} --name=SOURCE_GROUP_NAME | grep SOURCE_GROUP_NAME | awk '{print $2}'`
quantum security-group-rule-create --tenant-id TENANT_ID --direction ingress --ethertype IPv4 --protocol PROTOCOL --port-range-min PORT_RANGE_MIN --port-range-max PORT_RANGE_MAX --remote-ip-prefix REMOTE_IP_CIDR --remote-group-id SOURCE_GROUP_UUID SECURITY_GROUP_UUID
done
done
done
##### port conversion #####
foreach row of `select id,project_id,cidr from networks where deleted=0 and project_id is not null;` do
TENANT_ID=project_id
SUBNET_CIDR=cidr
NETWORK_ID=id
SUBNET_UUID=`quantum subnet-list --tenant_id=$(TENANT_ID) --cidr="$(SUBNET_CIDR)" | tail -n +4 | head -n -1 | awk '{print $2}'`
NETWORK_UUID=`quantum net-list --tenant_id=$(TENANT_ID) | tail -n +4 | head -n -1 | grep "$(SUBNET_CIDR)" | awk '{print $2}'
foreach row of `select fixed_ips.address as ip_address,virtual_interfaces.address as mac_address,instances.uuid as instance_uuid from virtual_interfaces join instances on virtual_interfaces.instance_id=instances.id join fixed_ips on virtual_interfaces.id=fixed_ips.virtual_interface_id where virtual_interfaces.deleted=0 and instances.deleted=0 and fixed_ips.deleted=0 and virtual_interfaces.network_id=NETWORK_ID;` do
MAC_ADDRESS=mac_address
DEVICE_UUID=instance_uuid
IP_ADDRESS=ip_address
SECURITY_GROUPS_STRING=''
foreach row of `select security_groups.name from instances join security_group_instance_association on instances.id=security_group_instance_association.instance_id join security_groups on security_group_instance_association.security_group_id = security_groups.id where instances.deleted=0 and security_group_instance_association.deleted=0 and security_groups.deleted = 0 and instances.uuid="$(DEVICE_UUID)";
SECURITY_GROUPS_STRING="$(SECURITY_GROUPS_STRING) --security-group `quantum security-group-list --tenant_id=TENANT_ID | tail -n +4 | head -n -1 | awk '{print $2" "$4}' | grep $(name) | awk '{print $1}'`"
quantum port-create --tenant-id TENANT_ID --device-id DEVICE_UUID --fixed-ip subnet_id=SUBNET_UUID,ip_address=IP_ADDRESS --mac-address MAC_ADDRESS $(SECURITY_GROUPS_STRING) NETWORK_UUID
done
done
##### floating ip conversion #####
PUBLIC_NETWORK_UUID=`quantum net-list | grep public | awk '{print $2}'`
foreach row of `select address,project_id from floating_ips where deleted=0 and project_id is not null;`
quantum floatingip-create --tenant-id TENANT_ID FLOATING_NETWORK_UUID
done
foreach row of `select floating_ips.address as floating_ip,floating_ips.project_id,fixed_ips.address from floating_ips join fixed_ips on floating_ips.fixed_ip_id=fixed_ips.id where floating_ips.deleted=0 and fixed_ips.deleted=0 and floating_ips.project_id is not null;` do
FLOATINGIP=floating_ip
FLOATINGIP_UUID=`quantum floatingip-list --floating_ip_address=165.233.72.25 | tail -n +4 | head -n -1 | awk '{print $2}'`
TENANT_ID=project_id
FIXED_IP=address
PORT_UUID=`quantum port-list --tenant_id=TENANT_ID --fixed_ips=ip_address='FIXED_IP | tail -n +4 | head -n -1 | awk '{print $2}'`
quantum floatingip-associate FLOATINGIP_UUID PORT_UUID
done
## (optional) create provider network and subnet
quantum net-create --tenant-id TENANT_ID --provider:network_type vlan --provider:physical_network default --provider:segmentation_id VLAN_ID NETWORK_NAME
VLAN_ID needs to come from the networks team / should use the existing VLAN_ID from the nova database
NETWORK_NAME = TENANT_NAME + '_ProviderNetwork1'
quantum subnet-create --tenant-id TENANT_ID --name SUBNET_NAME NETWORK_UUID SUBNET_CIDR
SUBNET_NAME = TENANT_NAME + '_ProviderSubnet1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment