Skip to content

Instantly share code, notes, and snippets.

@hurulu
Created February 18, 2016 02:59
Show Gist options
  • Select an option

  • Save hurulu/bd3b14f67114718ea78c to your computer and use it in GitHub Desktop.

Select an option

Save hurulu/bd3b14f67114718ea78c to your computer and use it in GitHub Desktop.
Launch a test instance in neutron net environment
#!/bin/bash
prefix="lei-test"
public_net="public-net-id"
subnet_cidr="192.168.9.0/24"
gateway="192.168.9.1"
instance_flavor="Micro-Small"
instance_image="CentOS-7"
export OS_TENANT_NAME="my_tenant_name"
#Create a net
net_id=`neutron net-create $prefix-net|awk '{if($2 == "id")print $4}'`
echo "A net has been created"
neutron net-show ${net_id}
#Create a subnet
subnet_id=`neutron subnet-create ${net_id} ${subnet_cidr} --name $prefix-subnet|awk '{if($2 == "id")print $4}'`
echo "A subnet has been created"
neutron subnet-show ${subnet_id}
#Create a router
router_id=`neutron router-create $prefix-router|awk '{if($2 == "id")print $4}'`
echo "A Router has been created"
neutron router-show ${router_id}
#Attach subnet to the router
echo "Attaching subnet to router"
neutron router-interface-add ${router_id} ${subnet_id}
#Set gateway for the router
echo "Set external gateway for the router"
neutron router-gateway-set ${router_id} ${public_net}
#Launch an instance
echo "An instance has been created"
instance_id=`nova boot --image ${instance_image} --flavor ${instance_flavor} --nic net-id=${net_id} $prefix-instance --poll|awk '{if($2 == "id")print $4}'`
nova show ${instance_id}
instance_ip=`nova show ${instance_id}|awk '{if($2 == NET)print $5}' NET="$prefix-net"`
echo ${instance_ip}
export OS_TENANT_NAME=admin
net_node_hosting_router=`neutron l3-agent-list-hosting-router ${router_id}|awk '{if($4 ~ /net-00/)print $4}'`
net_node_hosting_net=`neutron dhcp-agent-list-hosting-net ${net_id}|awk '{if($4 ~ /net-00/)print $4}'`
sleep 30
export OS_TENANT_NAME="my_tenant_name"
echo "Pinging gateway from net node"
echo "ssh ${net_node_hosting_router} \"ip netns exec qrouter-${router_id} ping -c5 -w3 $gateway\""
ssh ${net_node_hosting_router} "ip netns exec qrouter-${router_id} ping -c5 -w3 $gateway"
echo "Pinging instance IP from net node"
echo "ssh ${net_node_hosting_router} \"ip netns exec qrouter-${router_id} ping -c5 -w3 ${instance_ip}\""
ssh ${net_node_hosting_router} "ip netns exec qrouter-${router_id} ping -c5 -w3 ${instance_ip}"
echo "Pinging 8.8.8.8 from the net node"
echo "ssh ${net_node_hosting_router} \"ip netns exec qrouter-${router_id} ping -c5 -w3 8.8.8.8\""
ssh ${net_node_hosting_router} "ip netns exec qrouter-${router_id} ping -c5 -w3 8.8.8.8"
for i in ${net_node_hosting_net}
do
echo "ssh $i \"ip netns exec qdhcp-${net_id} ping -c5 -w3 $gateway\""
ssh $i "ip netns exec qdhcp-${net_id} ping -c5 -w3 $gateway"
echo "ssh $i \"ip netns exec qdhcp-${net_id} ping -c5 -w3 ${instance_ip}\""
ssh $i "ip netns exec qdhcp-${net_id} ping -c5 -w3 ${instance_ip}"
echo "ssh $i \"ip netns exec qdhcp-${net_id} ping -c5 -w3 8.8.8.8\""
ssh $i "ip netns exec qdhcp-${net_id} ping -c5 -w3 8.8.8.8"
done
#Clean up
echo "Cleaning up resources..."
#echo "To clean up, run the following commands:
echo "nova delete ${instance_id}"
nova delete ${instance_id}
sleep 5
echo "neutron router-interface-delete ${router_id} ${subnet_id}"
neutron router-interface-delete ${router_id} ${subnet_id}
sleep 5
echo "neutron router-delete ${router_id}"
neutron router-delete ${router_id}
sleep 5
echo "neutron subnet-delete ${subnet_id}"
neutron subnet-delete ${subnet_id}
sleep 5
echo "neutron net-delete ${net_id}"
neutron net-delete ${net_id}
echo "All done."
#"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment