-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresttool
188 lines (163 loc) · 3.85 KB
/
resttool
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
#!/bin/bash
#
# resttool: tools for using RESTful API with Fujitsu iRMC S4/S5
#
# (c) Juergen Orth - Fujitsu Technology Solutions GmbH
#
# $Id: resttool 174 2023-03-28 14:08:48Z HMBJOrth $
# Display timestamp and params
showinfo() # [param ...]
{
date "+%Y-%m-%d %H:%M:%S -- $*"
}
# Output a message on stdout
msg() # [param ...]
{
showinfo $@
}
# Output an error message on stderr and exit with gven rc
error() # rc [param ...]
{
rc=$1
shift
showinfo $@ >&2
exit $rc
}
# Display debug info to stderr when debuglevel is appropriate
debugmsg()
{
[ $1 -le $DEBUGLEVEL ] && msg debug $@ >&2
}
# Do basic restcall via https. Result should be 2xx http code
RestCall() # get|post|delete [param ...]
{
debugmsg 2 RestCall $@
METHOD=${1^^}
shift
debugmsg 3 curl -s -k -K - -H "Accept: application/json" -X $METHOD -i https://${IRMC}/$@
echo 'user = "'$USER:$PW'"' | curl -s -k -K - -H "Accept: application/json" -X $METHOD -i https://${IRMC}/$@ | tee /tmp/RestCall-$$.log
RC=$(awk '/^HTTP/ { RC=$2; } END { print RC }' /tmp/RestCall-$$.log)
[ -z "$RC" ] && RC=999
[ $(expr match $RC '^2[0-9][0-9]$') -eq 0 ] && error 1 HTTP Error $RC
rm /tmp/RestCall-$$.log
}
# Perform RestCall with method get
RestGet() # [param ...]
{
RestCall GET $@
}
# Perform RestCall with method post
RestPost() # [param ...]
{
RestCall POST $@
}
# Perform RestCall with method put
RestPut() # [param ...]
{
RestCall PUT $@
}
# Perform RestCall with method delete
RestDelete() # [param ...]
{
RestCall DELETE $@
}
# Read value of given key in json string
ReadJsonValue() # key
{
awk -F: '/'$1'/ { print $2 }' | tr -d ',"'
}
# Get all running sessions
SessionList()
{
debugmsg 1 SessionList
RestGet sessionInformation
}
# Get session status
SessionStatus() # sessionid
{
debugmsg 1 SessionStatus $1
RestGet sessionInformation/$1/status
}
# Get session log
SessionLog() # sessionid
{
debugmsg 1 SessionLog $1
RestGet sessionInformation/$1/log
}
# Terminate a session
SessionTerminate() # sessionid
{
debugmsg 1 SessionTerminate $1
RestDelete sessionInformation/$1/terminate
}
# Delete a session
SessionDelete() # sessionid
{
debugmsg 1 SessionDelete $1
RestDelete sessionInformation/$1/remove
}
# Wait for session to turn to status "terminated regularly"
SessionWait() # sessionid
{
debugmsg 1 SessionWait $1
[ "$1" ] || error 1 No session ID
STATUS="?"
until [ "${STATUS%% *}" == "terminated" ]
do
STATUS="$(SessionStatus $1 | ReadJsonValue \"Status\")"
debugmsg 3 $STATUS
sleep 3
echo -n .
done
echo
showinfo Session $1 finished with status: $STATUS
}
# List all profiles in profile store
ProfileStoreList()
{
debugmsg 1 ProfileStoreList
RestGet rest/v1/Oem/eLCM/ProfileManagement
}
# Get profile from store
ProfileStoreGet() # profile
{
debugmsg 1 ProfileStoreGet ${1##*/}
RestGet rest/v1/Oem/eLCM/ProfileManagement/${1##*/}
}
# Copy running profile to store
ProfileStoreAdd() # profile
{
debugmsg 1 ProfileStoreAdd $1
RestPost rest/v1/Oem/eLCM/ProfileManagement/get?PARAM_PATH=$1 | ReadJsonValue '"Id":'
}
# Delete profile from store
ProfileStoreDelete() # profile
{
debugmsg 1 ProfileStoreDelete ${1##*/}
RestDelete rest/v1/Oem/eLCM/ProfileManagement/${1##*/}
}
# Assign profile
ProfileChange() # filename [option]
{
debugmsg 1 ProfileChange $1
[ -r "$1" ] || error 1 "File $1 not readable"
RestPost rest/v1/Oem/eLCM/ProfileManagement/set$2 --data @$1 | ReadJsonValue '"Id":'
}
# Upload Custom Image to eLCM
CustomImageUpload() # url_of_image
{
debugmsg 1 CustomImageUpload $1
RestPost "rest/v1/Oem/eLCM/CustomImage?file=$IMAGE&bootEnvironment=auto&tag=jotest" | ReadJsonValue '"Id":'
}
# Boot Custom Image
CustomImageBoot() # ImageName
{
debugmsg 1 CustomImageBoot $1
RestPut "rest/v1/Oem/eLCM/CustomImage/$1" | ReadJsonValue '"Id":'
}
# Delete Custom Image
CustomImageDelete() # ImageName
{
debugmsg 1 CustomImageDelete $1
RestDelete "rest/v1/Oem/eLCM/CustomImage/$1"
}