This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
linode_bash_api
executable file
·205 lines (179 loc) · 3.59 KB
/
linode_bash_api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
#Linode Bash API https://github.com/rwky/Linode-Bash-API
#Released under the Simplified BSD License see the LICENSE file at the above url
#README also found at the above url
set -e
APIURL="https://api.linode.com/"
APIKEY=""
COMMAND=""
DATA=""
VERBOSE=0
VERSION="20110714"
MACRO=""
HELP=0
QUIET=0
#check if curl is installed
which curl &>/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:m:q" opt; do
case $opt in
h)
#help
HELP=1
;;
k)
#api key
APIKEY=$OPTARG
;;
f)
#file for api key
APIKEY=`head -1 $OPTARG`
;;
v)
#verbose
VERBOSE=1
;;
c)
COMMAND=$OPTARG
;;
d)
#data for post command
DATA=$OPTARG
;;
i)
#read data from file, if - read from stdin
DATA=`cat $OPTARG`
;;
m)
#macro
MACRO="$OPTARG"
;;
F)
#response format
DATA="$DATA&api_responseFormat=$OPTARG"
;;
q)
QUIET=1
;;
\?)
#unknown option
exit 1
;;
esac
done
if [ $HELP -eq 1 ] || [ -z $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://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
~/.linode_api
/etc/linode/api
examples:
./linode_bash_api -F human -c test.echo -d "foo=bar"
EOT
if [ -z "$MACRO" ]
then
$MACRO
exit 0
fi
fi
if [ -z $APIKEY ]
then
if [ -f /etc/linode_api ]
then
APIKEY=`cat /etc/linode_api`
fi
if [ -f ~/.linode_api ]
then
APIKEY=`cat ~/.linode_api`
fi
fi
if [ -z $APIKEY ]
then
echo "No API key specified" >&2
exit 1
fi
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" >&2
fi
RESPONSE=$(curl -s -d "$DATA" "$APIURL")
if [[ $RESPONSE != *\"ERRORARRAY\":\[\]* ]]
then
echo "API ERROR!" >&2
echo "$RESPONSE" | sed 's/.*\(ERRORARRAY[^}]*\).*/\1}]/' >&2
exit 1
fi
if [[ $DATA != *api_responseFormat* ]]
then
RESPONSE=$(echo "$RESPONSE" | sed 's/,/ /g' | sed 's/\([{}]\)/ \1 /g')
INDENT=0
for R in $RESPONSE
do
if [[ $R == *{* ]]
then
INDENT=$INDENT+1
fi
i=0
while ((i<INDENT))
do
echo -n " "
i=$i+1
done
i=0
if [[ $R == *}* ]]
then
INDENT=$INDENT-1
fi
echo "$R"
done
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
if [ $QUIET -eq 1 ]
then
api_request > /dev/null
else
api_request
fi
else
if ! type $(echo "$MACRO" | sed 's/ .*//') >/dev/null 2>&1
then
echo "Invalid macro command"
exit 1
fi
if [ $QUIET -eq 1 ]
then
$MACRO > /dev/null
else
$MACRO
fi
fi