Created
February 17, 2012 23:31
-
-
Save mcavage/1856186 to your computer and use it in GitHub Desktop.
stopmachine.sh
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
#!/bin/bash | |
# Globals | |
#export SDC_CLI_URL=https://us-west-1.api.joyentcloud.com | |
#export SDC_CLI_ACCOUNT=mark.cavage | |
#export SDC_CLI_KEY_ID=id_rsa | |
DELETE="sdc-deletemachine" | |
KILL=0 | |
LIST="sdc-listmachines" | |
NAME="" | |
STATE="" | |
STOP="sdc-stopmachine" | |
# Internals | |
function delete_machine() { | |
$LIST -n $NAME | json -a id | xargs $DELETE | |
return $? | |
} | |
function get_state() { | |
STATE=`$LIST -n $NAME | json -Ha state` | |
return $? | |
} | |
function stop_machine() { | |
$LIST -n $NAME | json -a id | xargs $STOP | |
return $? | |
} | |
function usage() { | |
cat << EOF | |
usage: $0 [-d] -n name | |
Stops, and optionally deletes a Joyent Machine | |
OPTIONS: | |
-h Show this message | |
-d Deletes the machine once stopped | |
-n name is the "friendly" name of your machine, not the uuid | |
-v Verbose | |
EOF | |
} | |
# Mainline | |
while getopts ":n:dhv" opt | |
do | |
case $opt in | |
h) | |
usage | |
exit 0 | |
;; | |
d) | |
KILL=1 | |
;; | |
n) | |
NAME=$OPTARG | |
;; | |
v) | |
LIST="sdc-listmachines -d" | |
DELETE="sdc-deletemachine -d" | |
STOP="sdc-stopmachine -d" | |
;; | |
?) | |
usage | |
exit 0 | |
;; | |
esac | |
done | |
if [[ -z "$NAME" ]]; then | |
usage | |
exit 1 | |
fi | |
get_state | |
if [[ "$STATE" != "stopped" ]]; then | |
echo "Shutting $NAME down" | |
stop_machine | |
if [[ $? != 0 ]]; then | |
exit $? | |
fi | |
echo "Waiting for $NAME to transition to 'stopped'" | |
get_state | |
while [[ "$STATE" != "stopped" ]]; do | |
sleep 1 | |
get_state | |
echo $STATE | |
done | |
fi | |
if [[ $KILL == 1 ]]; then | |
echo "Deleting $NAME" | |
delete_machine | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment