Skip to content

Instantly share code, notes, and snippets.

@xer0x
Created January 15, 2013 19:23
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 xer0x/4541232 to your computer and use it in GitHub Desktop.
Save xer0x/4541232 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
#
# Copyright (c) 2011, 2012, Joyent Inc. All rights reserved.
#
# TODO http response from deleting a zone is not finding a "transition uri"
#set -o errexit
set -o pipefail
unset LD_LIBRARY_PATH
PATH=/usr/bin:/usr/sbin
export PATH
usage()
{
printf "usage: jpc-destroy-fraudster [-v] uuid {show|destroy}\n"
printf "\n"
printf "For example, to list the machines that would be destroyed: \n"
printf "\n"
printf " jpc-destroy-fraudster 1234-5678-90\n"
printf "\n"
printf "To destroy the machines: \n"
printf "\n"
printf " jpc-destroy-fraudster 1234-5678-90 destroy\n"
printf "\n"
printf "\n"
printf " -v enables verbose output\n"
printf " -V enables very verbose output via xtrace\n"
printf "\n"
exit 1
}
warn()
{
printf "Warning: %s\n" "$1"
}
cmd=""
name=""
hostname=""
mutil=""
tag="external"
nic=""
reserved=""
creds=admin:$CONFIG_mapi_http_admin_pw
while getopts "vV" opt
do
case "$opt" in
V) set -o xtrace
export PS4='${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
verbose="-v"
shift;;
v) verbose="-v"
shift;;
esac
done
uuid=$1
cmd=$2
[ -z "$uuid" ] && usage
[ -z "$cmd" ] && cmd="show"
. /smartdc/lib/sdc-common
init_mapi_config
# Check if MAPI is happy
curl -s -u $creds http://${mapi}/servers >/tmp/sdc$$.out 2>&1
check_mapi_err
rm -f /tmp/sdc$$.out
[ -n "$emsg" ] && fatal "MAPI API is not responding"
# Get customer information
# sdc-capi /customers/13XXX282-f4eb-4bf7-b512-34f484ae9a2d
#################################################################
capi=$CONFIG_capi_client_url
headers="Accept:application/json"
uri="/customers/$uuid"
curl -sS -u $creds ${capi}${uri} -H $headers --insecure | json -H | nawk '
BEGIN {
printf("%-10s %-20s %-10s %-10s %-10s\n",
"Login", "Email", "First", "Last", "Company")
printf("%-10s %-20s %-10s %-10s %-10s\n",
"-----", "-----", "-----", "----", "-------")
}
{
if ($1 == "\"login\":") {
# strip quotes and comma
login = substr($2, 2, length($2) - 3)
}
if ($1 == "\"first_name\":") {
# strip quotes and comma
first = substr($2, 2, length($2) - 3)
}
if ($1 == "\"last_name\":") {
# strip quotes and comma
last = substr($2, 2, length($2) - 3)
}
if ($1 == "\"company_name\":") {
# strip quotes and comma
#company = substr($2, 2, length($2) - 3)
$2 = substr($2, 2, length($2))
for (i=2;i<NF;i++) {
company = company$i" "
}
}
# print once we hit last field:
if ($1 == "\"email_address\":") {
# strip quotes and comma
email = substr($2, 2, length($2) - 3)
}
if ($1 ~/\}/) {
printf("%-10s %-20s %-10s %-10s %-10s\n",
login, email, first, last, company)
}
}
'
echo
# Get customer zones
# sdc-mapi /zones?owner_uuid=13XXX282-f4eb-4bf7-b512-34f484ae9a2d
###################################################################
uri="/zones?owner_uuid=${uuid}"
zones_json=$(curl -sS -i -u $creds http://${mapi}${uri})
echo "${zones_json}" | json -H | nawk '
BEGIN {
type="ZONE"
name="NOTFOUNDYET"
printf("%-4s %-40s %-20s %-12s %-10s\n",
"Type", "Name", "Dataset", "Alias", "Status")
printf("%-4s %-40s %-20s %-12s %-10s\n",
"----", "----", "-------", "-----", "------")
}
{
if ($1 == "\"name\":") {
if (name == "NOTFOUNDYET") {
# strip quotes and comma
name = substr($2, 2, length($2) - 3)
}
}
if ($1 == "\"alias\":") {
# strip quotes and comma
alias = substr($2, 2, length($2) - 3)
}
if ($1 == "\"dataset_name\":") {
# strip quotes and comma
dataset = substr($2, 2, length($2) - 3)
}
if ($1 == "\"running_status\":") {
# strip quotes and comma
status = substr($2, 2, length($2) - 3)
}
# print once we hit last field:
if ($1 == "\"updated\":") {
printf("%-4s %-40s %-20s %-12s %-10s\n",
type, name, dataset, alias, status)
# reset name
name="NOTFOUNDYET"
}
}
'
# Get customer VMs
# sdc-mapi /vms?owner_uuid=65bd6d46-d2dd-4c3f-a297-f51c2e1cbefa
###################################################################
uri="/vms?owner_uuid=${uuid}"
vms_json=$(curl -sS -i -u $creds http://${mapi}${uri})
echo "${vms_json}" | json -H | nawk '
BEGIN {
type="VM"
name="NOTFOUNDYET"
}
{
if ($1 == "\"name\":") {
if (name == "NOTFOUNDYET") {
# strip quotes and comma
name = substr($2, 2, length($2) - 3)
}
}
if ($1 == "\"alias\":") {
# strip quotes and comma
alias = substr($2, 2, length($2) - 3)
}
if ($1 == "\"dataset_name\":") {
# strip quotes and comma
dataset = substr($2, 2, length($2) - 3)
}
if ($1 == "\"running_status\":") {
# strip quotes and comma
status = substr($2, 2, length($2) - 3)
}
# print once we hit last field:
if ($1 == "\"updated\":") {
printf("%-4s %-40s %-20s %-12s %-10s\n",
type, name, dataset, alias, status)
# reset name
name="NOTFOUNDYET"
}
}
'
# Issue Shutdown before Destroy (For SDC 7? and NOT for SDC 6.5)
# Issue Destroy IF $cmd == "destroy"
if [ "$cmd" == "destroy" ]; then
# ripped from the 'sdc-mapi-destroy-container' script
function mapi_shutdown_machine {
uuid=$1
if [ -z $uuid ]; then
return 1
fi
type=$2
if [[ ("$type" != "zone" && "$type" != "vm") ]]; then
echo "Need 'zone' or 'vm' as machine type"
return 1
fi
uri="/${type}s/${uuid}/shutdown"
## Shutdown()
response=$(curl -sS -i -u $creds http://${mapi}${uri} -X POST -H 'Content-Length:0')
status=$(echo "$response" | grep "^Status" | awk '{print $2}')
if [[ ("$status" == "409") || ("$status" == "204") ]]; then
# Probably already shutdown. That's fine.
#echo "$uuid -> already shutdown."
elif [[ "$status" == "410" ]]; then
echo "$uuid -> destroyed."
return 1
elif [[ "$status" == "404" ]]; then
echo "$uuid -> not found."
return 1
elif [[ ("$status" == "202") ]]; then
transition_uri=$(echo "$response" \
| tr '\r' '\n' \
| grep X-Joyent-Transition-URI \
| awk '{print $2}')
if [ "$transition_uri" == "" ]; then
echo "No transition uri found!" >&2
echo "$response" >&2
return 1
fi
echo "Wait for shutdown to complete ($transition_uri)."
while true; do
sleep 1
response=$(curl -sS -i -u $creds http://${mapi}${transition_uri})
status=$(echo "$response" | grep "^Status" | awk '{print $2}')
if [ "$status" != "200" ]; then
echo "error: unexpected status from '$transition_uri': $status"
echo "-- 'GET $transition_uri' response:"
echo $response
echo "--"
return 1
fi
progress=$(echo "$response" | json -H progress)
echo -n "."
if [ "$progress" == "100" ]; then
echo ""
success=$(echo "$response" | json -H success)
if [ "$success" == "false" ]; then
echo "error: shutdown failed"
echo "-- 'sdc-mapi $transition_uri' response:"
echo $response
echo "--"
exit 1
fi
break
fi
done
else
echo "$uuid -> unexpected error code: $status"
return 1
fi
}
function mapi_destroy_machine {
ZONENAME=$1
if [ -z $ZONENAME ]; then
return 1
fi
type=$2
if [[ ("$type" != "zone" && "$type" != "vm") ]]; then
echo "Need 'zone' or 'vm' as machine type"
return 1
fi
response=$(curl -sS -i -u $creds ${mapi}/${type}s/$ZONENAME -X DELETE)
status=$(echo "$response" \
| tr '\r' '\n' \
| grep Status \
| awk '{print $2}')
if [ "$status" == "410" ]; then
echo "$ZONENAME -> already destroyed."
return 1
fi
if [[ "$status" == "202" && "$type" == "vm" ]]; then
echo "$ZONENAME -> destroyed."
return 0
fi
transition_uri=$(echo "$response" \
| tr '\r' '\n' \
| grep X-Joyent-Transition-URI \
| awk '{print $2}')
if [ "$transition_uri" == "" ]; then
echo "No transition uri found!" >&2
echo "$response" >&2
return 1
fi
echo "Wait for delete to complete ($transition_uri)."
while true; do
sleep 1
response=$(curl -sS -i -u $creds ${mapi}$transition_uri)
status=$(echo "$response" | grep "^Status" | awk '{print $2}')
if [ "$status" != "200" ]; then
echo "error: unexpected status from '$transition_uri': $status"
echo "-- 'GET $transition_uri' response:"
echo $response
echo "--"
return 1
fi
echo "From destroy:"
echo $response
progress=$(echo "$response" | json -H progress)
echo -n "."
if [ "$progress" == "100" ]; then
echo ""
success=$(echo "$response" | json -H success)
if [ "$success" == "false" ]; then
echo "error: delete failed"
echo "-- 'sdc-mapi $transition_uri' response:"
echo $response
echo "--"
return 1
fi
break
fi
done
}
################# ZONES ###############################
names=$(echo "$zones_json" | json -H | grep '^ "name":' | cut -d": " -f2 | tr -d ' \015",')
for name in $names; do
mapi_shutdown_machine $name zone && \
mapi_destroy_machine $name zone
done
################# VMS ###############################
names=$(echo "$vms_json" | json -H | grep '^ "name":' | cut -d": " -f2 | tr -d ' \015",')
for name in $names; do
mapi_shutdown_machine $name vm && \
mapi_destroy_machine $name vm
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment