Last active
February 18, 2017 20:16
-
-
Save rm-you/30da92a54d5d4123f006e6108cc23ba0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export MYPROJECT=$(openstack token issue | awk '/ project_id / { print $4 }') | |
export OCTAVIA_IP=localhost | |
export OCTAVIA_PORT=9876 | |
export TOKEN_EXPIRATION_TIME=600 # 10 minutes | |
export MEMBER1_IP="10.0.0.10" | |
export MEMBER2_IP="10.0.0.11" | |
export DEFAULT_SUBNET=$(openstack subnet list | awk '/ private-subnet / {print $2}') | |
export PUBLIC_NETWORK=$(openstack network list | awk '/ public / {print $2}') | |
# Get a token and set expiry | |
function get_token_with_expiry { | |
if [ -z "$MYTOKEN_EXPIRY" ] || [ "$MYTOKEN_EXPIRY" -lt "$(date +%s)" ]; then | |
export MYTOKEN=$(openstack token issue | awk '/ id / { print $4 }') | |
export MYTOKEN_EXPIRY=$(($(date +%s) + $TOKEN_EXPIRATION_TIME)) | |
fi | |
echo $MYTOKEN | |
} | |
# Get a loadbalancer until it is no longer pending create/update | |
function wait_for_status { | |
TOKEN=$(get_token_with_expiry) | |
LB_STATUS=$(curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1} | jq -r .provisioning_status) | |
while [ "$LB_STATUS" != "ACTIVE" ]; do | |
if [ "$LB_STATUS" != "PENDING_UPDATE" -a "$LB_STATUS" != "PENDING_CREATE" ]; then | |
echo "LB is ${LB_STATUS}, aborting!" >&2 | |
return 1 | |
fi | |
echo "LB is ${LB_STATUS}, sleeping..." >&2 | |
sleep 2 | |
TOKEN=$(get_token_with_expiry) | |
LB_STATUS=$(curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1} | jq -r .provisioning_status) | |
done | |
} | |
function create_lb { | |
TOKEN=$(get_token_with_expiry) | |
SUBNET="$1" | |
if [ -z "$SUBNET" ]; then | |
SUBNET="$DEFAULT_SUBNET" | |
fi | |
LB_ID=$(curl -s -X POST -H Content-type:application/json -H "X-Auth-Token:$TOKEN" -d '{"name": "test_lb", "vip": {"subnet_id": "'$SUBNET'"}}' http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers | jq -r .id) | |
if [ -z "$LB_ID" ]; then | |
echo "Failed to create LB." >&2 | |
return 1 | |
else | |
wait_for_status "${LB_ID}" && echo $LB_ID && echo "Created LB: $LB_ID" >&2 && return 0 | |
return 1 | |
fi | |
} | |
function create_listener { | |
TOKEN=$(get_token_with_expiry) | |
LISTENER_ID=$(curl -s -X POST -H Content-type:application/json -H "X-Auth-Token:$TOKEN" -d '{"name": "test_listener", "protocol": "HTTP", "protocol_port": 80}' http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners | jq -r .id) | |
if [ -z "$LISTENER_ID" ]; then | |
echo "Failed to create Listener." >&2 | |
return 1 | |
else | |
wait_for_status "${1}" && echo $LISTENER_ID && echo "Created Listener: $LISTENER_ID" >&2 && return 0 | |
return 1 | |
fi | |
} | |
function create_pool { | |
TOKEN=$(get_token_with_expiry) | |
POOL_ID=$(curl -s -X POST -H Content-type:application/json -H "X-Auth-Token:$TOKEN" -d '{"name":"test_pool1", "protocol": "HTTP", "lb_algorithm": "ROUND_ROBIN"}' http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners/${2}/pools | jq -r .id) | |
if [ -z "$POOL_ID" ]; then | |
echo "Failed to create Pool." >&2 | |
return 1 | |
else | |
wait_for_status "${1}" && echo $POOL_ID && echo "Created Pool: $POOL_ID" >&2 && return 0 | |
return 1 | |
fi | |
} | |
function create_member { | |
TOKEN=$(get_token_with_expiry) | |
MEMBER_IP="$4" | |
MEMBER_ID=$(curl -s -X POST -H Content-type:application/json -H "X-Auth-Token:$TOKEN" -d '{"ip_address": "'${4}'", "protocol_port": 80}' http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners/${2}/pools/${3}/members | jq -r .id) | |
if [ -z "$MEMBER_ID" ]; then | |
echo "Failed to create Member." >&2 | |
return 1 | |
else | |
wait_for_status "${1}" && echo $MEMBER_ID && echo "Created Member: ${MEMBER_ID} (${MEMBER_IP}:80)" >&2 && return 0 | |
return 1 | |
fi | |
} | |
function get_lb { | |
TOKEN=$(get_token_with_expiry) | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1} | jq | |
} | |
function get_lbs { | |
TOKEN=$(get_token_with_expiry) | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers | jq | |
} | |
function get_listener { | |
TOKEN=$(get_token_with_expiry) | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners/${2} | jq | |
} | |
function get_listeners { | |
TOKEN=$(get_token_with_expiry) | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners | jq | |
} | |
function get_pool { | |
TOKEN=$(get_token_with_expiry) | |
LB_ID="$1" | |
if [ -n "$3" ]; then | |
POOL_ID="$3" | |
else | |
POOL_ID="$2" | |
fi | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${LB_ID}/pools/${POOL_ID} | jq | |
} | |
function get_pools { | |
TOKEN=$(get_token_with_expiry) | |
LB_ID="$1" | |
if [ -n "$2" ]; then | |
POOL_PATH="listeners/${2}/pools" | |
else | |
POOL_PATH="pools" | |
fi | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${LB_ID}/${POOL_PATH} | jq | |
} | |
function get_member { | |
TOKEN=$(get_token_with_expiry) | |
LB_ID="$1" | |
if [ -n "$4" ]; then | |
POOL_ID="$3" | |
MEMBER_ID="$4" | |
else | |
POOL_ID="$2" | |
MEMBER_ID="$3" | |
fi | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${LB_ID}/pools/${POOL_ID}/members/${MEMBER_ID} | jq | |
} | |
function get_members { | |
TOKEN=$(get_token_with_expiry) | |
LB_ID="$1" | |
if [ -n "$3" ]; then | |
POOL_ID="$3" | |
else | |
POOL_ID="$2" | |
fi | |
curl -s -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${LB_ID}/pools/${POOL_ID}/members | jq | |
} | |
function delete_lb { | |
TOKEN=$(get_token_with_expiry) | |
curl -X DELETE -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1} | |
} | |
function delete_listener { | |
TOKEN=$(get_token_with_expiry) | |
curl -X DELETE -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${1}/listeners/${2} | |
} | |
function delete_pool { | |
TOKEN=$(get_token_with_expiry) | |
LB_ID="$1" | |
if [ -n "$3" ]; then | |
POOL_PATH="listeners/${2}/pools/${3}" | |
else | |
POOL_PATH="pools/${2}" | |
fi | |
curl -X DELETE -H "X-Auth-Token:$TOKEN" http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers/${LB_ID}/${POOL_PATH} | |
} | |
function create_graph { | |
TOKEN=$(get_token_with_expiry) | |
SUBNET="$1" | |
if [ -z "$SUBNET" ]; then | |
SUBNET="$DEFAULT_SUBNET" | |
fi | |
curl -s -X POST -H Content-type:application/json -H "X-Auth-Token:$TOKEN" -d '{"name": "test_singlecreate_lb", "vip": {"subnet_id": "'$SUBNET'"}, "listeners": [{"name": "test_listener", "protocol": "HTTP", "protocol_port": 80, "default_pool": {"name":"test_pool", "protocol": "HTTP", "lb_algorithm": "ROUND_ROBIN", "members": [{"ip_address": "'$MEMBER1_IP'", "protocol_port": 80}, {"ip_address": "'$MEMBER2_IP'", "protocol_port": 80}]}}]}' http://${OCTAVIA_IP}:${OCTAVIA_PORT}/v1/loadbalancers | jq | |
} | |
function create_graph_steps { | |
echo "Creating full LB graph..." | |
LB_ID=$(create_lb) && \ | |
LISTENER_ID=$(create_listener $LB_ID) && \ | |
POOL_ID=$(create_pool $LB_ID $LISTENER_ID) && \ | |
MEMBER1_ID=$(create_member $LB_ID $LISTENER_ID $POOL_ID $MEMBER1_IP) && \ | |
MEMBER2_ID=$(create_member $LB_ID $LISTENER_ID $POOL_ID $MEMBER2_IP) && \ | |
export LB_ID LISTENER_ID POOL_ID MEMBER1_ID MEMBER2_ID && \ | |
echo "Done." && return 0 | |
echo "Failed!" && return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment