Merge "Add parameter Ec2ApiExternalNetwork for VPCs"
[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 command_arguments=${command_arguments:-}
42
43 # yum check-update exits 100 if updates are available
44 set +e
45 check_update=$(yum check-update 2>&1)
46 check_update_exit=$?
47 set -e
48
49 if [[ "$check_update_exit" == "1" ]]; then
50     echo "Failed to check for package updates"
51     echo "$check_update"
52     exit 1
53 elif [[ "$check_update_exit" != "100" ]]; then
54     echo "No packages require updating"
55     exit 0
56 fi
57
58 pacemaker_status=""
59 if hiera -c /etc/puppet/hiera.yaml service_names | grep -q pacemaker; then
60     pacemaker_status=$(systemctl is-active pacemaker)
61 fi
62
63 # special case https://bugs.launchpad.net/tripleo/+bug/1635205 +bug/1669714
64 special_case_ovs_upgrade_if_needed
65
66 if [[ "$pacemaker_status" == "active" ]] ; then
67     echo "Pacemaker running, stopping cluster node and doing full package update"
68     node_count=$(pcs status xml | grep -o "<nodes_configured.*/>" | grep -o 'number="[0-9]*"' | grep -o "[0-9]*")
69     if [[ "$node_count" == "1" ]] ; then
70         echo "Active node count is 1, stopping node with --force"
71         pcs cluster stop --force
72     else
73         pcs cluster stop
74     fi
75 else
76     echo "Upgrading openstack-puppet-modules and its dependencies"
77     yum -q -y update openstack-puppet-modules
78     yum deplist openstack-puppet-modules | awk '/dependency/{print $2}' | xargs yum -q -y update
79     echo "Upgrading other packages is handled by config management tooling"
80     echo -n "true" > $heat_outputs_path.update_managed_packages
81     exit 0
82 fi
83
84 command=${command:-update}
85 full_command="yum -q -y $command $command_arguments"
86 echo "Running: $full_command"
87
88 result=$($full_command)
89 return_code=$?
90 echo "$result"
91 echo "yum return code: $return_code"
92
93 if [[ "$pacemaker_status" == "active" ]] ; then
94     echo "Starting cluster node"
95     pcs cluster start
96
97     hostname=$(hostname -s)
98     tstart=$(date +%s)
99     while [[ "$(pcs status | grep "^Online" | grep -F -o $hostname)" == "" ]]; do
100         sleep 5
101         tnow=$(date +%s)
102         if (( tnow-tstart > cluster_start_timeout )) ; then
103             echo "ERROR $hostname failed to join cluster in $cluster_start_timeout seconds"
104             pcs status
105             exit 1
106         fi
107     done
108
109     RETVAL=$( pcs resource show galera-master | grep wsrep_cluster_address | grep -q `crm_node -n` ; echo $? )
110
111     if [[ $RETVAL -eq 0 && -e /etc/sysconfig/clustercheck ]]; then
112         tstart=$(date +%s)
113         while ! clustercheck; do
114             sleep 5
115             tnow=$(date +%s)
116             if (( tnow-tstart > galera_sync_timeout )) ; then
117                 echo "ERROR galera sync timed out"
118                 exit 1
119             fi
120         done
121     fi
122
123     echo "Waiting for pacemaker cluster to settle"
124     if ! timeout -k 10 $cluster_settle_timeout crm_resource --wait; then
125         echo "ERROR timed out while waiting for the cluster to settle"
126         exit 1
127     fi
128
129     pcs status
130 fi
131
132 echo "Finished yum_update.sh on server $deploy_server_id at `date`"
133
134 exit $return_code