We now have improved mailchimp plugin packaging, this hack is no longer needed.
[arvados-dev.git] / jenkins / puppet_update.sh
1 #!/bin/bash 
2
3 DEBUG=1
4 SSH_PORT=22
5 ECODE=0
6
7 PUPPET_AGENT='
8 now() { date +%s; }
9 let endtime="$(now) + 600"
10 while [ "$endtime" -gt "$(now)" ]; do
11     puppet agent --test --detailed-exitcodes
12     agent_exitcode=$?
13     if [ 0 = "$agent_exitcode" ] || [ 2 = "$agent_exitcode" ]; then
14         break
15     else
16         sleep 10s
17     fi
18 done
19 exit ${agent_exitcode:-99}
20 '
21
22 title () {
23   date=`date +'%Y-%m-%d %H:%M:%S'`
24   printf "$date $1\n"
25 }
26
27
28
29 function run_puppet() {
30   node=$1
31   return_var=$2
32
33   title "Running puppet on $node"
34   TMP_FILE=`mktemp`
35   if [[ "$DEBUG" != "0" ]]; then
36     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" | tee $TMP_FILE
37   else
38     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" > $TMP_FILE 2>&1
39   fi
40
41   ECODE=${PIPESTATUS[0]}
42   RESULT=$(cat $TMP_FILE)
43
44   if [[ "$ECODE" != "255" && ! ("$RESULT" =~ 'already in progress') && "$ECODE" != "2" && "$ECODE" != "0"  ]]; then
45     # Ssh exits 255 if the connection timed out. Just ignore that.
46     # Puppet exits 2 if there are changes. For real!
47     # Puppet prints 'Notice: Run of Puppet configuration client already in progress' if another puppet process
48     #   was already running
49     echo "ERROR running puppet on $node: exit code $ECODE"
50     if [[ "$DEBUG" == "0" ]]; then
51       title "Command output follows:"
52       echo $RESULT
53     fi
54   fi
55   if [[ "$ECODE" == "255" ]]; then
56     title "Connection timed out"
57     ECODE=0
58   fi
59   if [[ "$ECODE" == "2" ]]; then
60     ECODE=0
61   fi
62   rm -f $TMP_FILE
63   eval "$return_var=$ECODE"
64 }
65
66
67 run_puppet $1 ECODE
68 title "$1 exited code: $ECODE"