Bug 619956: have ssh-shell script retry calls multiple times before failing (r=brbaker)
[tamarin-stm.git] / build / buildbot / slaves / all / ssh-shell.sh
blob9e6128ed0afc4ad9d7e99c2773f85d8c1f510124
1 #!/bin/bash
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
13 # License.
15 # The Original Code is [Open Source Virtual Machine.].
17 # The Initial Developer of the Original Code is
18 # Adobe System Incorporated.
19 # Portions created by the Initial Developer are Copyright (C) 2010
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
23 # Adobe AS3 Team
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
37 # ***** END LICENSE BLOCK ***** */
38 # acts as a proxy to remotely run abc files via a ssh call to the remote machine
39 # usage: ./ssh_shell.sh <vmargs> file.abc
40 # assumes the shell is deployed to $SSH_SHELL_REMOTE_DIR/avmshell
43 if [ "$SSH_SHELL_REMOTE_USER" = "" ] ||
44 [ "$SSH_SHELL_REMOTE_HOST" = "" ] ||
45 [ "$SSH_SHELL_REMOTE_DIR" = "" ];
46 then
47 echo "missing environment variable: "
48 echo "SSH_SHELL_REMOTE_USER" = "$SSH_SHELL_REMOTE_USER"
49 echo "SSH_SHELL_REMOTE_HOST" = "$SSH_SHELL_REMOTE_HOST"
50 echo "SSH_SHELL_REMOTE_DIR" = "$SSH_SHELL_REMOTE_DIR"
51 exit 1
56 MAX_RETRIES=5
57 filelist=""
58 flatfilelist=""
59 expectedExitCode=0
61 function try_command () {
62 count=1
63 while [ $count -le $MAX_RETRIES ]
65 # run the passed in command - not that "$@" (quoted) is used since that
66 # is the only form that will work with quoted arguments containing spaces
67 # see http://www.tldp.org/LDP/abs/html/internalvariables.html#APPREF2
68 # for details
69 "$@"
70 ec=$?
71 if [ "$ec" -eq "$expectedExitCode" ]; then
72 # command ran with no errors
73 return 0
74 else
75 echo "Command Failed: $*"
76 echo "Exit Code: $ec"
77 echo "Try $count of $MAX_RETRIES"
78 sleep 3
80 ((count++))
81 done
82 # command failed SSH_RETRIES times, report failure and exit
83 echo "Reached max tries, exiting with exit code $ec ..."
84 exit $ec
88 if [ "$1" = "" ]
89 then
90 # running the shell with no args prints the help and exits with exitcode 1
91 expectedExitCode=1
92 try_command ssh $SSH_SHELL_REMOTE_USER@$SSH_SHELL_REMOTE_HOST "cd $SSH_SHELL_REMOTE_DIR;./avmshell"
93 else
94 # Note that testfiles are copied to the SSH_SHELL_REMOTE_DIR directly and
95 # run one at a time. No dir structure is preserved when copying the files.
96 args=""
97 for a in $*
99 # look for an .abc file
100 echo "$a" | grep ".*\.abc" > /dev/null
101 res=$?
102 if [ "$res" = "0" ]
103 then
104 file=$a
105 flatfile=`basename $a`
106 # check to see if flatfile is already in filelist
107 echo "$filelist" | grep "$flatfile" > /dev/null
108 res=$?
109 if [ "$res" = "1" ]; then
110 # flatfile is not in filelist; add it
111 filelist="$filelist $flatfile"
112 # copy file to device
113 try_command scp $file $SSH_SHELL_REMOTE_USER@$SSH_SHELL_REMOTE_HOST:$SSH_SHELL_REMOTE_DIR/$flatfile > /dev/null
115 # even if flatfile is already in filelist, add to args
116 args="$args $flatfile"
117 else
118 args="$args $a"
120 done
121 # workaround for not returning exit code, run a shell script and print exit code to stdout
122 try_command ssh $SSH_SHELL_REMOTE_USER@$SSH_SHELL_REMOTE_HOST "cd $SSH_SHELL_REMOTE_DIR;./ssh-shell-runner.sh $args" > ./stdout
123 ret=`cat ./stdout | grep "EXITCODE=" | awk -F= '{printf("%d",$2)}'`
124 # clean up copied over files
125 for a in $filelist
127 try_command ssh $SSH_SHELL_REMOTE_USER@$SSH_SHELL_REMOTE_HOST "cd $SSH_SHELL_REMOTE_DIR;rm $a"
128 done
129 # remove the EXITCODE from the stdout before returning it so that exact output matching will be fine
130 cat ./stdout | sed 's/^EXITCODE=[0-9][0-9]*$//g' > ./stdout_clean
131 cat ./stdout_clean
132 rm -f ./stdout ./stdout_clean
133 exit $ret