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