Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active August 29, 2015 14:08
Show Gist options
  • Save cloudnull/f128662d49677ec85e22 to your computer and use it in GitHub Desktop.
Save cloudnull/f128662d49677ec85e22 to your computer and use it in GitHub Desktop.
Exersize Neutron networks, routers, dhcp, metadata, and dns.
#!/usr/bin/env bash
set -e
EXTNET="${EXTNET:-EXNET}"
FLAVOR="512"
IMAGE="cirros"
OPENRC="${OPENRC:-/root/openrc}"
STOP_ON_FAILURE="${STOP_ON_FAILURE:-}"
cat >user-data.tmp <<EOF
#!/bin/sh
set -e
ping -c 3 8.8.8.8
ping -c 3 opendns.com
echo "it works!"
EOF
trap my_trap_handler ERR
function my_trap_handler() {
destroy
}
function warn() {
echo -e "\e[0;35m${@}\e[0m"
}
function info() {
echo -e "\e[0;33m${@}\e[0m"
}
function failure() {
echo -e "\e[0;31m${@}\e[0m"
}
function success() {
echo -e "\e[0;32m${@}\e[0m"
}
function destroy() {
nova_delete_wait
if [[ $STOP_ON_FAILURE ]]; then
exit 1
else
warn "$(neutron router-gateway-clear ${ROUTER_ID})"
warn "$(neutron router-interface-delete ${ROUTER_ID} ${SUBNET_ID})"
warn "$(neutron subnet-delete ${SUBNET_ID})"
warn "$(neutron router-delete ${ROUTER_ID})"
warn "$(neutron net-delete ${NET_ID})"
fi
}
function nova_delete_wait() {
warn "$(nova delete ${NOVA_ID})"
if ! timeout 60 sh -c "while nova list | grep ${NOVA_ID} | grep ACTIVE; do sleep 1; done"; then
return 1
else
return 0
fi
}
function nova_boot_wait() {
info "Creating Instance"
FLAVOR_ID="$(nova flavor-list | grep -w ${FLAVOR} | head -n 1 | awk '{print $2}')"
IMAGE_ID="$(nova image-list | grep -w ${IMAGE} | head -n 1 | awk '{print $2}')"
NOVA_ID="$(nova boot --flavor "${FLAVOR_ID}" --image "${IMAGE_ID}" --user-data user-data.tmp --nic net-id=$NET_ID $PREFIX-instance | grep -w id | awk '{print $4}')"
if ! timeout 60 sh -c "while ! nova list | grep ${NOVA_ID} | grep ACTIVE; do sleep 1; done"; then
failure "Instance ${NOVA_ID} failed to go active after 60 seconds"
return 1
else
HOST=$(nova show ${NOVA_ID} | grep ATTR:host | grep -Po [0-9]+-compute[0-9]+)
info "Instance Created. Compute Node: $HOST, Network Agent: $NET_HOST"
return 0
fi
}
function nova_console() {
if ! timeout 30 sh -c "while ! nova console-log ${NOVA_ID} | grep 'it works'; do sleep 1; done"; then
failure "Instance failed to ping the outside world."
info "Router Port Information"
failure "$(neutron router-port-list ${ROUTER_ID})"
info "Router and L3 agent Information"
failure "$(neutron l3-agent-list-hosting-router ${ROUTER_ID})"
info "Port information"
for i in $(neutron port-list | grep -w "${SUBNET_ID}" | awk '{print $2}'); do
failure "$(neutron port-show $i)"
done
return 1
else
success "Test succeeded on node $HOST:$NET_HOST."
destroy
return 0
fi
}
if [ ! -f "${OPENRC}" ];then
failure "No OpenRC File found"
exit 1
else
source "${OPENRC}"
fi
for count in {1..10}; do
PREFIX="NeutronExercise"
NET_ID="$(neutron net-create ${PREFIX}-net | grep -w id | awk '{print $4}')"
info "Network created. ID: ${NET_ID}"
SUBNET_ID=$(neutron subnet-create --name ${PREFIX}-subnet --dns-nameserver 8.8.8.8 ${NET_ID} 10.10.10.0/24 | grep -w id | awk '{print $4}')
info "Subnet created. ID: ${SUBNET_ID}"
ROUTER_ID="$(neutron router-create ${PREFIX}-router | grep -w id | awk '{print $4}')"
info "Router created. ID: ${ROUTER_ID}"
info "$(neutron router-gateway-set ${ROUTER_ID} ${EXTNET})"
info "$(neutron router-interface-add ${ROUTER_ID} ${SUBNET_ID})"
NET_HOST=$(neutron l3-agent-list-hosting-router ${ROUTER_ID} | head -n 4 | tail -n 1 | grep -Po "[0-9]{6}-[^\ ]+")
nova_boot_wait
nova_console
info "Next test in 5 Seconds."
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment