Skip to content

Instantly share code, notes, and snippets.

@sridhargaddam
Created November 21, 2018 17:31
Show Gist options
  • Save sridhargaddam/2888893bfca9d976b6259f5ddbb1f3ba to your computer and use it in GitHub Desktop.
Save sridhargaddam/2888893bfca9d976b6259f5ddbb1f3ba to your computer and use it in GitHub Desktop.
Script to automate spawning of VNF instance
#!/bin/bash
# This is a sample script that automates the creation of necessary neutron resources
# and spawning of a VNF as shown in proposal-1.
# Usage:
# To simply create the necessary Neutron resources run the following command
# ./spawnvnf.sh
# To create resources and also to Spawn the VNF, run the command
# ./spawnvnf.sh vnf
# For cleanup, run the command
# ./spawnvnf.sh d
SITE=2
if [ $SITE == 1 ]
then
TENANT_SUBNET_CIDR=30.0.0.0/24
REMOTE_SUBNET_CIDR=40.0.0.0/24
VNF_INT_PORT_IPADDRESS=30.0.0.100
else
TENANT_SUBNET_CIDR=40.0.0.0/24
REMOTE_SUBNET_CIDR=30.0.0.0/24
VNF_INT_PORT_IPADDRESS=40.0.0.100
fi
if [ $# -gt 0 ] && [ $1 == 'd' ]; then
echo "Deleting the resources, please wait..."
VNF_VM=$(openstack server list | grep vnf-vpn | awk '{print $2}')
if [ ! -z $VNF_VM ]
then
echo "Deleting the VNF"
nova delete vnf-vpn
fi
openstack router set --no-route router1
openstack router unset --external-gateway router1
openstack router remove subnet router1 tenant_subnet
VNF_EXT_PORT_UUID=$(openstack port list | grep vnf-ext-port | awk '{print $2}')
VNF_INT_PORT_UUID=$(openstack port list | grep vnf-int-port | awk '{print $2}')
openstack port delete $VNF_EXT_PORT_UUID
openstack port delete $VNF_INT_PORT_UUID
openstack router delete router1
openstack subnet delete tenant_subnet
openstack network delete tenant_net
exit 0
fi
EXT_NET=$(openstack network list | grep ext-net | awk '{print $2}')
if [ -z $EXT_NET ]
then
echo "External network is missing, please create the external network"
exit 1
fi
IMGLIST=$(openstack flavor list | grep m1.extra_tiny | awk '{print $2}')
if [ -z $IMGLIST ]
then
echo "Creating a tiny flavor"
nova flavor-create --is-public true m1.extra_tiny auto 100 0 1 --rxtx-factor 1
else
echo "Skipping flavor creation as its already present"
fi
TENANT_NETWORK=$(openstack network list | grep tenant_net | awk '{print $2}')
if [ -z $TENANT_NETWORK ]
then
echo "Creating the necessary resources for VPN Orchestration"
openstack network create tenant_net
neutron subnet-create --name tenant_subnet --ip-version 4 tenant_net $TENANT_SUBNET_CIDR
TENANT_SUBNET_UUID=$(openstack subnet list | grep tenant_subnet | awk '{print $2}')
openstack router create router1
openstack router add subnet router1 tenant_subnet
echo "Associating the router to the ext-net"
openstack router set --external-gateway ext-net router1
neutron port-create --name vnf-ext-port ext-net
neutron port-create --name vnf-int-port --fixed-ip subnet_id=$TENANT_SUBNET_UUID,ip_address=$VNF_INT_PORT_IPADDRESS tenant_net
VNF_EXT_PORT_UUID=$(openstack port list | grep vnf-ext-port | awk '{print $2}')
VNF_INT_PORT_UUID=$(openstack port list | grep vnf-int-port | awk '{print $2}')
neutron subnet-update tenant_subnet --host-routes type=dict list=true destination=$REMOTE_SUBNET_CIDR,nexthop=$VNF_INT_PORT_IPADDRESS
openstack router set --route destination=$REMOTE_SUBNET_CIDR,gateway=$VNF_INT_PORT_IPADDRESS router1
else
echo "Skipping creation of resources as they are already present"
fi
if [ $# -gt 0 ] && [ $1 == 'vnf' ]; then
VNF_VM=$(openstack server list | grep vnf-vpn | awk '{print $2}')
if [ -z $VNF_VM ]
then
echo "Creating a VNF in the tenant network..."
VNF_IMAGE=$(openstack image list | grep cirros | awk '{print $2}')
VNF_EXT_PORT_UUID=$(openstack port list | grep vnf-ext-port | awk '{print $2}')
VNF_INT_PORT_UUID=$(openstack port list | grep vnf-int-port | awk '{print $2}')
nova boot --image $VNF_IMAGE --flavor m1.extra_tiny --nic port-id=$VNF_INT_PORT_UUID --nic port-id=$VNF_EXT_PORT_UUID vnf-vpn
else
echo "Skipping VNF creation as the VM is already running."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment