Merge "Force stop a single node pacemaker on yum update"
[apex-tripleo-heat-templates.git] / extraconfig / tasks / yum_update.sh
1 #!/bin/bash
2
3 # A heat-config-script which runs yum update during a stack-update.
4 # Inputs:
5 #   deploy_action - yum will only be run if this is UPDATE
6 #   update_identifier - yum will only run for previously unused values of update_identifier
7 #   command - yum sub-command to run, defaults to "update"
8 #   command_arguments - yum command arguments, defaults to ""
9
10 echo "Started yum_update.sh on server $deploy_server_id at `date`"
11 echo -n "false" > $heat_outputs_path.update_managed_packages
12
13 if [[ -z "$update_identifier" ]]; then
14     echo "Not running due to unset update_identifier"
15     exit 0
16 fi
17
18 timestamp_dir=/var/lib/overcloud-yum-update
19 mkdir -p $timestamp_dir
20
21 # sanitise to remove unusual characters
22 update_identifier=${update_identifier//[^a-zA-Z0-9-_]/}
23
24 # seconds to wait for this node to rejoin the cluster after update
25 cluster_start_timeout=360
26
27 timestamp_file="$timestamp_dir/$update_identifier"
28 if [[ -a "$timestamp_file" ]]; then
29     echo "Not running for already-run timestamp \"$update_identifier\""
30     exit 0
31 fi
32 touch "$timestamp_file"
33
34 command_arguments=${command_arguments:-}
35
36 list_updates=$(yum list updates)
37
38 if [[ "$list_updates" == "" ]]; then
39     echo "No packages require updating"
40     exit 0
41 fi
42
43 pacemaker_status=$(systemctl is-active pacemaker)
44
45 if [[ "$pacemaker_status" == "active" ]] ; then
46     echo "Pacemaker running, stopping cluster node and doing full package update"
47     node_count=$(pcs status xml | grep -o "<nodes_configured.*/>" | grep -o 'number="[0-9]*"' | grep -o "[0-9]*")
48     if [[ "$node_count" == "1" ]] ; then
49         echo "Active node count is 1, stopping node with --force"
50         pcs cluster stop --force
51     else
52         pcs cluster stop
53     fi
54 else
55     echo "Excluding upgrading packages that are handled by config management tooling"
56     command_arguments="$command_arguments --skip-broken"
57     for exclude in $(cat /var/lib/tripleo/installed-packages/* | sort -u); do
58         command_arguments="$command_arguments --exclude $exclude"
59     done
60 fi
61
62 command=${command:-update}
63 full_command="yum -y $command $command_arguments"
64 echo "Running: $full_command"
65
66 result=$($full_command)
67 return_code=$?
68 echo "$result"
69 echo "yum return code: $return_code"
70
71 if [[ "$pacemaker_status" == "active" ]] ; then
72     echo "Starting cluster node"
73     pcs cluster start
74
75     hostname=$(hostname -s)
76     tstart=$(date +%s)
77     while [[ "$(pcs status | grep "^Online" | grep -F -o $hostname)" == "" ]]; do
78         sleep 5
79         tnow=$(date +%s)
80         if (( tnow-tstart > cluster_start_timeout )) ; then
81             echo "ERROR $hostname failed to join cluster in $cluster_start_timeout seconds"
82             pcs status
83             exit 1
84         fi
85     done
86     pcs status
87
88 else
89     echo -n "true" > $heat_outputs_path.update_managed_packages
90 fi
91
92 echo "Finished yum_update.sh on server $deploy_server_id at `date`"
93
94 exit $return_code