forked from rwky/Linode-Bash-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinode_bash_api_macros
98 lines (84 loc) · 2.25 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
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
}