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
/
Copy pathlinode_bash_api_macros
228 lines (194 loc) · 4.68 KB
/
linode_bash_api_macros
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
}
function list_labels_and_ids {
COMMAND="linode.list"
RESPONSE=`api_request`
for l in $RESPONSE
do
if [[ $l == *LABEL* ]]
then
echo -n "Label: "
echo -n "$l" | sed 's/"LABEL":"\(.*\)"/\1/' | tr -d "\n"
fi
if [[ $l == *LINODEID* ]]
then
echo -n " ID: "
echo $l | sed 's/"LINODEID"://'
fi
done
}
function get_domain_id {
#$1 domain
if [ $HELP -eq 1 ]
then
cat <<EOT
Help for macro get_domain_id
Requires one argument
The argument is the domain you wish to get the ID for
Example: linode_bash_api -m 'get_domain_id toast.com' returns the ID for the doamin toast.com
EOT
return
fi
if [ -z $1 ]
then
echo "You must specify the domain"
exit 1
fi
COMMAND="domain.list"
RESPONSE=`api_request`
for l in $RESPONSE
do
if [[ $l == *DOMAINID* ]]
then
ID=$(echo -n "$l" | sed 's/"DOMAINID":\(.*\)/\1/' | tr -d "\n")
fi
if [[ $l == *DOMAIN* ]] && [[ $l == *$1* ]]
then
echo $ID
fi
done
}
function get_domain_resource_id
{
#$1 domain id
#$2 the resource name to get
if [ $HELP -eq 1 ]
then
cat <<EOT
Help for macro get_domain_resource_id
Requires three arguments
The argument is the domain you wish to get the ID for
Example: linode_bash_api -m 'get_domain_resource_id toast.com www' returns the resource ID for the doamin www.toast.com
EOT
return
fi
if [ -z $1 ]
then
echo "You must specify the domain id"
exit 1
fi
if [ -z $2 ]
then
echo "You must specify the resource name"
exit 1
fi
COMMAND="domain.resource.list"
DATA="domainid=$1"
RESPONSE=`api_request`
for l in $RESPONSE
do
if [[ $l == *RESOURCEID* ]]
then
ID=$(echo -n "$l" | sed 's/"RESOURCEID":\(.*\)/\1/' | tr -d "\n")
fi
if [[ $l == *NAME* ]] && [[ $l == *$2* ]]
then
echo $ID
fi
done
}
function update_domain_resource_target
{
#$1 domain name
#$2 the resource name to get
#$3 the new target
if [ $HELP -eq 1 ]
then
cat <<EOT
Help for macro update_domain_resource_target
Requires three arguments
The argument is the domain you wish to get the ID for
Example: linode_bash_api -m 'update_domain_resource_target toast.com www 8.8.8.8' sets the target of www.toast.com to 8.8.8.8
EOT
return
fi
if [ -z $1 ]
then
echo "You must specify the domain name"
exit 1
fi
if [ -z $2 ]
then
echo "You must specify the resource name"
exit 1
fi
if [ -z $3 ]
then
echo "You must specify the new target"
exit 1
fi
DOMAINID=$(get_domain_id $1)
RESOURCEID=$(get_domain_resource_id $DOMAINID $2)
if [ $VERBOSE -eq 1 ]
then
echo "Updating $DOMAINID $RESOURCEID to $3"
fi
COMMAND="domain.resource.update"
DATA="domainid=$DOMAINID&resourceid=$RESOURCEID&target=$3"
api_request
}