Merge "Exposing the ability to enable/disable the repository"
[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 [ -f /.dockerenv ]; then
14     echo "Not running due to running inside a container"
15     exit 0
16 fi
17
18 if [[ -z "$update_identifier" ]]; then
19     echo "Not running due to unset update_identifier"
20     exit 0
21 fi
22
23 timestamp_dir=/var/lib/overcloud-yum-update
24 mkdir -p $timestamp_dir
25
26 # sanitise to remove unusual characters
27 update_identifier=${update_identifier//[^a-zA-Z0-9-_]/}
28
29 # seconds to wait for this node to rejoin the cluster after update
30 cluster_start_timeout=600
31 galera_sync_timeout=1800
32 cluster_settle_timeout=1800
33
34 timestamp_file="$timestamp_dir/$update_identifier"
35 if [[ -a "$timestamp_file" ]]; then
36     echo "Not running for already-run timestamp \"$update_identifier\""
37     exit 0
38 fi
39 touch "$timestamp_file"
40
41 pacemaker_status=""
42 # We include word boundaries in order to not match pacemaker_remote
43 if hiera -c /etc/puppet/hiera.yaml service_names | grep -q '\bpacemaker\b'; then
44     pacemaker_status=$(systemctl is-active pacemaker)
45 fi
46
47 # (NB: when backporting this s/pacemaker_short_bootstrap_node_name/bootstrap_nodeid)
48 # This runs before the yum_update so we are guaranteed to run it even in the absence
49 # of packages to update (the check for -z "$update_identifier" guarantees that this
50 # is run only on overcloud stack update -i)
51 if [[ "$pacemaker_status" == "active" && \
52         "$(hiera -c /etc/puppet/hiera.yaml pacemaker_short_bootstrap_node_name | tr '[:upper:]' '[:lower:]')" == "$(facter hostname | tr '[:upper:]' '[:lower:]')" ]] ; then \
53     # OCF scripts don't cope with -eu
54     echo "Verifying if we need to fix up any IPv6 VIPs"
55     set +eu
56     fixup_wrong_ipv6_vip
57     ret=$?
58     set -eu
59     if [ $ret -ne 0 ]; then
60         echo "Fixing up IPv6 VIPs failed. Stopping here. (See https://bugs.launchpad.net/tripleo/+bug/1686357 for more info)"
61         exit 1
62     fi
63 fi
64
65 command_arguments=${command_arguments:-}
66
67 # Always ensure yum has full cache
68 yum makecache || echo "Yum makecache failed. This can cause failure later on."
69
70 # yum check-update exits 100 if updates are available
71 set +e
72 check_update=$(yum check-update 2>&1)
73 check_update_exit=$?
74 set -e
75
76 if [[ "$check_update_exit" == "1" ]]; then
77     echo "Failed to check for package updates"
78     echo "$check_update"
79     exit 1
80 elif [[ "$check_update_exit" != "100" ]]; then
81     echo "No packages require updating"
82     exit 0
83 fi
84
85 # special case https://bugs.launchpad.net/tripleo/+bug/1635205 +bug/1669714
86 special_case_ovs_upgrade_if_needed
87
88 if [[ "$pacemaker_status" == "active" ]] ; then
89     echo "Pacemaker running, stopping cluster node and doing full package update"
90     node_count=$(pcs status xml | grep -o "<nodes_configured.*/>" | grep -o 'number="[0-9]*"' | grep -o "[0-9]*")
91     if [[ "$node_count" == "1" ]] ; then
92         echo "Active node count is 1, stopping node with --force"
93         pcs cluster stop --force
94     else
95         pcs cluster stop
96     fi
97 else
98     echo "Upgrading openstack-puppet-modules and its dependencies"
99     check_for_yum_lock
100     yum -q -y update openstack-puppet-modules
101     yum deplist openstack-puppet-modules | awk '/dependency/{print $2}' | xargs yum -q -y update
102     echo "Upgrading other packages is handled by config management tooling"
103     echo -n "true" > $heat_outputs_path.update_managed_packages
104     exit 0
105 fi
106
107 command=${command:-update}
108 full_command="yum -q -y $command $command_arguments"
109
110 echo "Running: $full_command"
111 check_for_yum_lock
112 result=$($full_command)
113 return_code=$?
114 echo "$result"
115 echo "yum return code: $return_code"
116
117 if [[ "$pacemaker_status" == "active" ]] ; then
118     echo "Starting cluster node"
119     pcs cluster start
120
121     hostname=$(hostname -s)
122     tstart=$(date +%s)
123     while [[ "$(pcs status | grep "^Online" | grep -F -o $hostname)" == "" ]]; do
124         sleep 5
125         tnow=$(date +%s)
126         if (( tnow-tstart > cluster_start_timeout )) ; then
127             echo "ERROR $hostname failed to join cluster in $cluster_start_timeout seconds"
128             pcs status
129             exit 1
130         fi
131     done
132
133     RETVAL=$( pcs resource show galera-master | grep wsrep_cluster_address | grep -q `crm_node -n` ; echo $? )
134
135     if [[ $RETVAL -eq 0 && -e /etc/sysconfig/clustercheck ]]; then
136         tstart=$(date +%s)
137         while ! clustercheck; do
138             sleep 5
139             tnow=$(date +%s)
140             if (( tnow-tstart > galera_sync_timeout )) ; then
141                 echo "ERROR galera sync timed out"
142                 exit 1
143             fi
144         done
145     fi
146
147     echo "Waiting for pacemaker cluster to settle"
148     if ! timeout -k 10 $cluster_settle_timeout crm_resource --wait; then
149         echo "ERROR timed out while waiting for the cluster to settle"
150         exit 1
151     fi
152
153     pcs status
154 fi
155
156
157 echo "Finished yum_update.sh on server $deploy_server_id at `date` with return code: $return_code"
158
159 exit $return_code