flocklab 9.35 KB
Newer Older
Reto Da Forno's avatar
Reto Da Forno committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
#
# ------------------------------
# Command line tool for FlockLab
# v1.4
#
# Usage:
#
# -v <testconfig.xml>: validate
# -c <testconfig.xml>: create
# -a <testid>:         abort
# -d <testid>:         delete
# -g <testid>:         get via https
# -f <testid>:         fetch via webdav
# -o <platform>:       get a list of the currently available (online) observers
# -p:                  get a list of the available platforms
17
# -i <testid>:         get test info (title, desc, status, start time etc.)
Reto Da Forno's avatar
Reto Da Forno committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -h:                  display help
#
# For the commands -g and -f, the download directory can be specified as an additional argument.
#
# Your FlockLab login credentials need to be stored in .flocklabauth in your home directory as follows:
#   USER=[your_username]
#   PASSWORD=[your_password]
#
# If you want to be able to download your test results via webdav (-f), you need to install the client 'cadaver'.
# ------------------------------

DEST=${3:-/tmp/flocklab}  # destination directory (if not specified, the default is used: /tmp/flocklab)
EXTRACT=1                 # extract results?
TESTCONFIG=0              # also download XML test config?
32
SERVER_URL=https://flocklab.ethz.ch/user
Reto Da Forno's avatar
Reto Da Forno committed
33
34
35
CURL_PARAMS='-sS -4 --insecure'
FLAUTH_FILE=.flocklabauth
NETRC_FILE=$HOME/.netrc
36
DAV_URL=${SERVER_URL}/webdav
Reto Da Forno's avatar
Reto Da Forno committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
DAV_CLIENT=cadaver

if [ -f $FLAUTH_FILE ]
then
  source ./$FLAUTH_FILE
elif [ -f $HOME/$FLAUTH_FILE ]
then
  source $HOME/$FLAUTH_FILE
elif [ -f $NETRC_FILE ]
then
  #echo "using credentials from .netrc file"
  while read -r LINE; do
    if [[ "${LINE,,}" == *"login"* ]]
    then
      USER=$(echo $LINE | awk '{print $2}')
    elif [[ "${LINE,,}" == *"password"* ]]
    then
      PASSWORD=$(echo $LINE | awk '{print $2}')
    fi
  done < "$NETRC_FILE"
fi

59
60
61
62
63
64
if [ -z "$USER" ] || [ -z "$PASSWORD" ]
then
  echo "No username or password found. Add your credentials to ~/.flocklabauth."
  exit 1
fi

Reto Da Forno's avatar
Reto Da Forno committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
usage()
{
cat << EOF
command line tool for FlockLab (${SERVER_URL})

usage: $0 [OPTION]

OPTIONS:
   -v <testconfig.xml>: validate test config
   -c <testconfig.xml>: create / schedule new test
   -a <testid>:         abort test
   -d <testid>:         delete test
   -g <testid>:         get test results (via https)
   -f <testid>:         fetch test results (via webdav)
   -o <platform>:       get a list of currently available observers
   -p:                  get a list of available platforms
81
   -i <testid>:         get test info (title, desc, status, start time etc.)
Reto Da Forno's avatar
Reto Da Forno committed
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
   -h:                  display this help page
EOF
}

password()
{
cat << EOF
Wrong username / password
please check your settings in $FLAUTH_FILE
EOF
}

fetchtest()
{
  # if .netrc file does not exist, create it
  if [ ! -f $NETRC_FILE ]
  then
    echo "default
login $USER
password $PASSWORD" > $NETRC_FILE
    chmod 600 $NETRC_FILE
    echo ".netrc file created"
  fi

  TESTID=$OPTARG
  if [ ! -d $DEST ]
  then
    mkdir $DEST
  fi
  if [ -d $DEST/$TESTID ]
  then
    echo "Error: directory $DEST/$TESTID already exists"
    exit 1
  fi
  mkdir $DEST/$TESTID
  $DAV_CLIENT > $DEST/$TESTID/dav.log 2>&1 <<EOF
open $DAV_URL
get $TESTID/testconfiguration.xml $DEST/$TESTID/testconfiguration.xml
get $TESTID/results.tar.gz $DEST/$TESTID/$TESTID.tar.gz
exit
EOF
  if [ `sed '/^\(404\|500\)/!d' $DEST/$TESTID/dav.log | wc -l` -gt 0 ]
  then
    echo "Error occured while fetching data:"
    cat $DEST/$TESTID/dav.log
    rm -rf $DEST/$TESTID
  else
    echo "Downloaded data to $DEST/$TESTID"
    if [ $EXTRACT -gt 0 ]
    then
      tar -xvzf $DEST/$TESTID/$TESTID.tar.gz -C $DEST
      if [ $? -eq 0 ]
      then
        rm $DEST/$TESTID/$TESTID.tar.gz
      fi
    fi
    if [ $TESTCONFIG -eq 0 ]
    then
      rm $DEST/$TESTID/testconfiguration.xml
    fi
    rm $DEST/$TESTID/dav.log
  fi
}

if [ "$#" -lt 1 ]
then
  usage
  exit 1
fi

152
while getopts “v:c:a:d:f:g:o:pi:h” OPTION
Reto Da Forno's avatar
Reto Da Forno committed
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
do
  case $OPTION in
    v)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F first="no" -F xmlfile=@$OPTARG $SERVER_URL/xmlvalidate.php`
      if [ $? -eq 0 ]
      then
        if [ `echo $RESPONSE | wc -m` -lt 2 ]
        then
          password
          exit 1
        fi
        SUCCESS=`echo $RESPONSE | sed '/<p>The file validated correctly.<\/p>/!d' | wc -l`
        if [ $SUCCESS -eq 0 ]
        then
          echo $RESPONSE | sed 's/.*<!-- cmd -->\(.*\)<!-- cmd -->.*/\1/;s/<li>/\n/g;s/<[^>]*>//g'
          exit 1
        else
          echo 'The file validated correctly.'
        fi
      else
        echo 'There were connection problems contacting the FlockLab server '$SERVER_URL
        exit 1
      fi
      ;;
    c)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F first="no" -F xmlfile=@$OPTARG $SERVER_URL/newtest.php`
      if [ $? -eq 0 ]
      then
        if [ `echo $RESPONSE | wc -m` -lt 2 ]
        then
          password
          exit 1
        fi
        SUCCESS=`echo $RESPONSE | sed '/<!-- cmd --><p>Test (Id [0-9]*) successfully added.<\/p>/!d' | wc -l`
        if [ $SUCCESS -eq 0 ]
        then
          echo $RESPONSE | sed 's/.*<!-- cmd -->\(.*\)<!-- cmd -->.*/\1/;s/<li>/\n/g;s/<[^>]*>//g'
          exit 1
        else
          echo $RESPONSE | sed 's/.*<!-- flocklabscript\,\([0-9]*\),\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}+[0-9]\{4\}\),\([0-9]*\)-->.*/Test successfully added. Test ID: \1, Starttime: \2 (\3)/'
        fi
      else
        echo 'There were connection problems contacting the FlockLab server '$SERVER_URL
        exit 1
      fi
      ;;
    a)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F removeit="Remove test" -F testid=$OPTARG $SERVER_URL/test_abort.php`
      if [ $? -eq 0 ]
      then
        if [ `echo $RESPONSE | wc -m` -lt 2 ]
        then
          password
          exit 1
        fi
        SUCCESS=`echo $RESPONSE | sed '/<!-- cmd --><p>The test has been aborted.<\/p><!-- cmd -->/!d' | wc -l`
        if [ $SUCCESS -eq 0 ]
        then
          echo $RESPONSE | sed 's/.*<!-- cmd -->\(.*\)<!-- cmd -->.*/\1/;s/<li>/\n/g;s/<[^>]*>//g'
          exit 1
        else
          echo $RESPONSE | sed 's/.*<!-- cmd --><p>\(The test has been aborted\.\)<\/p><!-- cmd -->.*/\1/'
        fi
      else
        echo 'There were connection problems contacting the FlockLab server '$SERVER_URL
        exit 1
      fi
      ;;
    d)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F removeit="Remove test" -F testid=$OPTARG $SERVER_URL/test_delete.php`
      if [ $? -eq 0 ]
      then
        if [ `echo $RESPONSE | wc -m` -lt 2 ]
        then
          password
          exit 1
        fi
        SUCCESS=`echo $RESPONSE | sed '/<!-- cmd --><p>The test has been removed.<\/p><!-- cmd -->/!d' | wc -l`
        if [ $SUCCESS -eq 0 ]
        then
          echo $RESPONSE | sed 's/.*<!-- cmd -->\(.*\)<!-- cmd -->.*/\1/;s/<li>/\n/g;s/<[^>]*>//g'
          exit 1
        else
          echo $RESPONSE | sed 's/.*<!-- cmd --><p>\(The test has been removed\.\)<\/p><!-- cmd -->.*/\1/'
        fi
      else
        echo 'There were connection problems contacting the FlockLab server '$SERVER_URL
        exit 1
      fi
      ;;
    g)
      echo "Fetching results for test $OPTARG ..."
      if [ ! -d $DEST ]
      then
        mkdir $DEST
      fi
      OUTFILE=$DEST/$OPTARG.tar.gz
      RESPONSE=`curl $CURL_PARAMS -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "testid=${OPTARG}&query=get&username=${USER}&password=${PASSWORD}" -o $OUTFILE $SERVER_URL/result_download_archive.php`
      FILESIZE=`stat -c "%s" $OUTFILE`
      if [ $FILESIZE -lt 1000 ]
      then
        OUTFILECONT=`cat $OUTFILE`
        if [[ "$OUTFILECONT" == *"error"* ]]
        then
          ERRORMSG=`echo ${OUTFILECONT:1:-1} | awk -F':' '{print $3}'`
          echo "Failed: "$ERRORMSG
          rm $OUTFILE
          exit 1
        fi
      fi
      echo "Results saved as "$OUTFILE
      if [ $EXTRACT -gt 0 ]
      then
        tar -xvzf $OUTFILE -C $DEST
        if [ $? -eq 0 ]
        then
          rm $OUTFILE
        fi
      fi
      if [ $TESTCONFIG -gt 0 ]
      then
        if [ $EXTRACT -gt 0 ]
        then
          OUTFILE=$DEST/$OPTARG/testconfiguration.xml
        else
          OUTFILE=$DEST/${OPTARG}_config.xml
        fi
        RESPONSE=`curl $CURL_PARAMS -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "testid=${OPTARG}&query=get&username=${USER}&password=${PASSWORD}" -o $OUTFILE $SERVER_URL/testconfig_download.php`
      fi
      ;;
    f)
      which $DAV_CLIENT >> /dev/null
      if [[ $? != 0 ]]
      then
        echo 'WebDAV client '$DAV_CLIENT' not found!'
        exit 1
      fi
      echo "Fetching results for test $OPTARG ..."
      fetchtest
      ;;
    o)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F q="obs" -F platform=$OPTARG $SERVER_URL/api.php`
      if [ $? -eq 0 ]
      then
        echo $RESPONSE
      fi
      ;;
    p)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F q="platform" $SERVER_URL/api.php`
      if [ $? -eq 0 ]
      then
        echo $RESPONSE
      fi
      ;;
307
308
309
310
311
312
313
    i)
      RESPONSE=`curl $CURL_PARAMS -o - -F username="$USER" -F password="$PASSWORD" -F q="testinfo" -F id=$OPTARG $SERVER_URL/api.php`
      if [ $? -eq 0 ]
      then
        echo $RESPONSE
      fi
      ;;
Reto Da Forno's avatar
Reto Da Forno committed
314
315
316
317
318
319
320
321
322
323
324
    h)
      usage
      exit 0
      ;;
    ?)
      usage
      exit 0
      ;;
  esac
done