Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Added macros and redirected verbose output to stderr
Browse files Browse the repository at this point in the history
Macros work by specifying functions that use the api_request function with various options to perform common operations, the macros are stored in ~/.linode_bash_api_macros and/or /etc/linode_bash_api_macros two macros are included by default, the first is a test macro that outputs its arguments, the second toggles the read only attribute of a disk specified by linode label and disk label.
  • Loading branch information
rwky committed Jul 14, 2011
1 parent edaeddd commit 73943a9
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 23 deletions.
86 changes: 63 additions & 23 deletions linode_bash_api
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,18 @@ APIKEY=""
COMMAND=""
DATA=""
VERBOSE=0
VERSION="20110621"
VERSION="20110714"
MACRO=""
HELP=0

#check if curl is installed
hash curl 2>/dev/null || { echo "Curl is required but is not installed or not in PATH. Aborting." >&2; exit 1; }

while getopts "hk:f:c:d:vi:F:" opt; do
while getopts "hk:f:c:d:vi:F:m:" opt; do
case $opt in
h)
#help
cat <<EOT
Linode Bash API $VERSION
options:
-h Help
-v Verbose
-F arg Response format, default bash (easily readable in a shell, formatted from json), options, json wddx or human (html)
-k arg API Key to use
-f arg File to get API key from, key must be the only string on the first line
-c arg The command to run, for a list of commands see http:https://www.linode.com/api
-d arg A quote string containing the data to send in "foo=bar&foo2=bar2" format
-i arg Get data from file if - is used get data from stdin
If -k or -f are ommited or don't provide an API key then the following loctions are checked
$HOME/.linode_api
/etc/linode/api
Detiails of commands you can run can be found at http:https://www.linode.com/api/
EOT
exit 0
HELP=1
;;
k)
#api key
Expand All @@ -62,6 +46,10 @@ i)
#read data from file, if - read from stdin
DATA=`cat $OPTARG`
;;
m)
#macro
MACRO="$OPTARG"
;;
F)
#response format
DATA="$DATA&api_responseFormat=$OPTARG"
Expand All @@ -73,6 +61,34 @@ exit 1
esac
done

if [ $HELP -eq 1 ]
then
cat <<EOT
Linode Bash API $VERSION
options:
-h Help
-v Verbose
-F arg Response format, default bash (easily readable in a shell, formatted from json), options, json wddx or human (html)
-k arg API Key to use
-f arg File to get API key from, key must be the only string on the first line
-c arg The command to run, for a list of commands see http:https://www.linode.com/api
-d arg A quote string containing the data to send in "foo=bar&foo2=bar2" format
-i arg Get data from file if - is used get data from stdin
-m "arg" Run a macro, a quote encased string with at least one space separated argument which is the macro name, subsequent arguments will be passed to the macro as arguments. Macros are bash functions kept in /etc/linode_bash_api_macros or ~/.linode_bash_api_macros
If -k or -f are ommited or don't provide an API key then the following loctions are checked
$HOME/.linode_api
/etc/linode/api
Details of commands you can run can be found at http:https://www.linode.com/api/
EOT
if [ -z "$MACRO" ]
then
$MACRO
exit 0
fi
fi

if [ -z $APIKEY ]
then
if [ -f /etc/linode_api ]
Expand All @@ -91,17 +107,20 @@ echo "No API key specified" >&2
exit 1
fi

if [ -z $COMMAND ]
if [ -z $COMMAND ] && [ -z "$MACRO" ]
then
echo "No command specified" >&2
exit 1
fi

function api_request
{

DATA="$DATA&api_key=$APIKEY&api_action=$COMMAND"

if [ $VERBOSE -eq 1 ]
then
echo "Running curl -d $DATA $APIURL"
echo "Running curl -d $DATA $APIURL" >&2
fi

RESPONSE=$(curl -s -d "$DATA" "$APIURL")
Expand Down Expand Up @@ -136,5 +155,26 @@ then
else
echo "$RESPONSE"
fi
}

if [ -f /etc/linode_bash_api_macros ]
then
. /etc/linode_bash_api_macros
fi

if [ -f ~/.linode_bash_api_macros ]
then
. ~/.linode_bash_api_macros
fi

if [ -z "$MACRO" ]
then
api_request
else
if ! type $(echo "$MACRO" | sed 's/ .*//') >/dev/null 2>&1
then
echo "Invalid macro command"
exit 1
fi
$MACRO
fi
79 changes: 79 additions & 0 deletions linode_bash_api_macros
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function test_macro
{
echo "This is the test macro the arguments passed are $@"
}

function toggle_disk_read_only {
#$1 linode label
#$2 linode disk label

if [ $HELP -eq 1 ]
then
cat <<EOT

Help for macro toggle_disk_read_only
Requires two arguments
The first is the Linode label, it is case sensitive but may only be partial, i.e. if your Linode is named "mytoast123" then you can simply use "toast"
The second argument is the disk label, again it's case sensitive and can be parital
If the Linode and disk are found then the disk will be marked as read only if it's writable and vice versa
Example: linode_bash_api -m 'toggle_disk_read_only toast toaster' would toggle the ISREADONLY attribute of the disk toaster on linode toast
EOT
return
fi

if [ -z $1 ]
then
echo "You must specify the linode label"
exit 1
fi

if [ -z $2 ]
then
echo "You must specify the disk label"
exit 1
fi

if [ $VERBOSE -eq 1 ]
then
echo "Toggling readonly attribute of disk $2 on linode $1 read only"
echo "Retrieving linode ID"
fi

COMMAND="linode.list"
DATA="api_responseFormat=json"
RESPONSE=`api_request`
LINODEID=$( echo "$RESPONSE" | sed "s/.*$1[^}]*LINODEID\":\([0-9]*\).*.*/\1/" )
if [ "$LINODEID" == "$RESPONSE" ]
then
echo "Linode with label containing $1 not found"
exit 1
fi

if [ $VERBOSE -eq 1 ]
then
echo "Retrieving disk ID"
fi

COMMAND="linode.disk.list"
DATA="api_responseFormat=json&linodeid=$LINODEID"
RESPONSE=`api_request`
declare -a SETTINGS=($( echo "$RESPONSE" | sed "s/.*ISREADONLY\":\([0-1]\).*$2[^}].*DISKID\":\([0-9]*\).*/\1 \2/" ))

if [ "$RESPONSE" == "${SETTINGS[*]}" ]
then
echo "Disk with label $2 not found"
exit 1
fi

COMMAND="linode.disk.update"
if [ "${SETTINGS[0]}" == "0" ]
then
DATA="linodeid=$LINODEID&diskid=${SETTINGS[1]}&isreadonly=1"
echo "Disk is writable marking read only"
else
DATA="linodeid=$LINODEID&diskid=${SETTINGS[1]}&isreadonly=0"
echo "Disk is readonly marking writable"
fi
api_request

}

0 comments on commit 73943a9

Please sign in to comment.