-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocci-client.sh
executable file
·319 lines (281 loc) · 9.77 KB
/
occi-client.sh
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
OCCI_CLIENT_VERSION="0.1"
##############################################################################
# Copyright 2012
# Gesellschaft für wissenschaftliche Datenverarbeitung mbH Göttingen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
##############################################################################
# Description: BASH OCCI Client
# Author(s): Florian Feldhaus
##############################################################################
##############################################################################
# Initialize client configuration
DEBUG=false
GUI=true
OCCI_ENDPOINT="http://localhost/"
CONTENT_TYPE="text/plain"
ACCEPT="text/plain"
##############################################################################
# Handle options
function usage {
cat <<_EOF_
Usage: $0 [options] ...
Options:
-d, --DEBUG Echo all curl commands after they are executed
-e, --endpoint Specify OCCI endpoint
-g, --gui Use GUI mode (default)
-h, --help Display this message and exit
-t, --text Use text mode
-V, --version Display version and exit
_EOF_
}
function version {
echo "BASH OCCI Client, version "$OCCI_CLIENT_VERSION
}
if ! options=$(getopt -u -o dehV -l DEBUG,endpoint,help,version -- "$@")
then
# something went wrong, getopt will put out an error message for us
exit 1
fi
set -- $options
while [ $# -gt 0 ]
do
case $1 in
-d|--DEBUG) DEBUG=true ;;
-e|--endpoint) OCCI_ENDPOINT=$3 ;;
-g|--gui) GUI=true ;;
-h|--help) usage;exit 0 ;;
-t|--text) GUI=false ;;
-V|--version) version;exit 0 ;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
##############################################################################
# Check if required tools are available
# test if bash is at least version 4
if [[ `bash --version` =~ GNU\ bash,\ [vV]ersion\ [0-3] ]]; then
echo "I require at least Bash version 4"
exit 1
fi
# test if curl is installed
hash curl 2>&- || { echo >&2 "I require the curl binary but it's not installed. Aborting."; exit 1; }
# test if dialog is installed
if $GUI; then
hash dialog 2>&- || { echo >&2 "I require the dialog binary for GUI functionality, but it's not installed. Falling back to text mode.."; $GUI=false; }
fi
##############################################################################
# Define reqular expressions for OCCI rendering
# see OCCI HTTP Rendering at http://www.ogf.org/documents/GFD.185.pdf
# regular expression for parsing category strings
CATEGORY_REGEX='Category: *([^;]*); *scheme="([^"]*)"; *class="([^"]*)"; *(title="([^"]*)";)? *(rel="([^"]*)";)? *(location="([^"]*)";)? *(attributes="([^"]*)";)? *(actions="([^"]*)";)?'
X_OCCI_ATTRIBUTE_REGEX='X-OCCI-Attribute: *([^=]*)=(.*)'
X_OCCI_LOCATION_REGEX=''
LINK_REGEX='Link: *<([^>]*)>; *(rel="([^"]*);) *(self="([^"]*);)? *(category="([^"]*); *([^;]*))?'
##############################################################################
# Global variables
# global category registry
declare -A categories
# global list of currently selected entity URIs
declare -a entities
##############################################################################
# OCCI Client functionality
function config {
select_occi_endpoint
select_content_type
select_accept
}
function select_occi_endpoint {
# Ask for OCCI Endpoint to use
endpoints=($(dialog --backtitle "Bash OCCI Client" \
--form "Specify Endpoints:" 22 76 16 \
"OCCI Endpoint URI:" 1 1 $OCCI_ENDPOINT 1 19 255 0 \
2>&1 1>&3))
exit_code=$?
OCCI_ENDPOINT=${endpoints[0]}
return $exit_code
}
function select_content_type {
# Ask for Content-Type to use for requests
choice=($(dialog --backtitle "Bash OCCI Client" \
--menu "Select Content-Type for requests:" 22 76 16 \
1 "text/plain" \
2 "text/occi" \
3 "application/json" \
2>&1 1>&3))
exit_code=$?
case $choice in
1)
CONTENT_TYPE='text/plain'
;;
2)
CONTENT_TYPE='text/occi'
;;
3)
CONTENT_TYPE='application/json'
;;
esac
return $exit_code
}
function select_accept {
# Ask for Accept MIME-Type to use for requests
choice=($(dialog --backtitle "Bash OCCI Client" \
--menu "Select Accept MIME-Type for requests:" 22 76 16 \
1 "text/plain" \
2 "text/occi" \
3 "application/json" \
2>&1 1>&3))
exit_code=$?
case $choice in
1)
ACCEPT='text/plain'
;;
2)
ACCEPT='text/occi'
;;
3)
ACCEPT='application/json'
;;
esac
return $exit_code
}
function get_categories {
curl_categories=$(curl -v -X GET "$OCCI_ENDPOINT-/" 2>&1)
if $DEBUG; then
if $GUI;then
dialog --backtitle "Bash OCCI Client" \
--scrollbar \
--msgbox "$curl_categories" 22 76
else
echo $curl_categories
fi
fi
while read -r line; do
[[ $line =~ $CATEGORY_REGEX ]]
term=${BASH_REMATCH[1]}
scheme=${BASH_REMATCH[2]}
class=${BASH_REMATCH[3]}
title=${BASH_REMATCH[5]}
rel=${BASH_REMATCH[7]}
location=${BASH_REMATCH[9]}
attributes=${BASH_REMATCH[11]}
actions=${BASH_REMATCH[13]}
declare -A category
category=( [term]=$term [scheme]=$scheme [class]=$class [title]=$title [rel]=$rel [location]=$location )
if [ -n "$term" ]
then
categories[$term]=${category[@]}
fi
done <<< "$curl_categories"
}
function get_entities {
curl_entities=$(curl -v -X GET --header "Accept: text/uri-list" -w "\n" "$OCCI_ENDPOINT" 2>&1)
dialog_entities=""
if $DEBUG; then
dialog --backtitle "Bash OCCI Client" \
--scrollbar \
--msgbox "$curl_entities" 22 76
fi
declare -i i=1
while read -r line; do
if [[ $line == http* ]]; then
entities+=($line)
dialog_entities+="$i $line "
i+=1
fi
done <<< "$curl_entities"
choice=($(dialog --backtitle "Bash OCCI Client" \
--menu "Select entity to show details:" 22 76 16 \
$dialog_entities \
2>&1 1>&3))
if [[ -n $choice ]]; then
show_entity_details ${entities[$choice]}
fi
}
function show_entity_details {
curl_entity=$(curl -v -X GET --header "Accept: $ACCEPT" -w "\n" $1 2>&1)
if $DEBUG; then
dialog --backtitle "Bash OCCI Client" \
--scrollbar \
--msgbox "$curl_entity" 22 76
fi
declare -a entity_categories
declare -A entity_attributes
declare -a entity_links
while read -r line; do
# parse if category string
[[ $line =~ $CATEGORY_REGEX ]]
if [[ -n ${BASH_REMATCH[0]} ]]; then
entity_categories+=(${categories[${BASH_REMATCH[1]}]})
fi
# parse if link
[[ $line =~ $LINK_REGEX ]]
if [[ -n ${BASH_REMATCH[0]} ]]; then echo ${BASH_REMATCH[0]};fi
# parse if X-OCCI-Attribute
[[ $line =~ $X_OCCI_ATTRIBUTE_REGEX ]]
if [[ -n ${BASH_REMATCH[0]} ]]; then
entity_attributes[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]}
[[ $entity_rendering_length -lt ${#BASH_REMATCH[2]} ]] && entity_rendering_length=${#BASH_REMATCH[2]}
fi
# parse if X_OCCI_Location
[[ $line =~ $X_OCCI_LOCATION_REGEX ]]
done <<< "$curl_entity"
for key in "${!entity_attributes[@]}"; do
entity_rendering+=$(printf "%${entity_rendering_length}s: %s" "$key" "${entity_attributes[$key]}")"\n"
done
dialog --backtitle "Bash OCCI Client" \
--scrollbar \
--msgbox "$entity_rendering" 22 76
}
function main_menu {
# initialize list of categories
get_categories
while true; do
choice=($(dialog --backtitle "Bash OCCI Client" \
--cancel-label "Exit" \
--menu "Select command to run:" 22 76 16 \
A "Configure OCCI Client" \
B "GET - categories" \
C "GET - entities" \
D "GET - entity" \
E "POST - create mixin" \
F "POST - create/update entity" \
G "POST - trigger action" \
H "POST - add mixin to resource" \
I "PUT - full update of a Mixin Collection" \
J "PUT - full update of a resource instance" \
K "DELETE - entity" \
L "DELETE - user defined mixin" \
M "DELETE - " \
2>&1 1>&3))
exit_code=$?
if [[ $exit_code != 0 ]]; then return $exit_code; fi
case $choice in
A)
config ;;
B)
get_categories ;;
C)
get_entities ;;
esac
done
}
##############################################################################
# OCCI Client User Interface
exec 3>&1
main_menu
exec 3>&-