Replace hard-coded regionOne with parameter references
[apex-tripleo-heat-templates.git] / extraconfig / tasks / major_upgrade_controller_pacemaker_1.sh
1 #!/bin/bash
2
3 set -eu
4
5 cluster_sync_timeout=1800
6
7 if pcs status 2>&1 | grep -E '(cluster is not currently running)|(OFFLINE:)'; then
8     echo_error "ERROR: upgrade cannot start with some cluster nodes being offline"
9     exit 1
10 fi
11
12
13 # We want to disable fencing during the cluster --stop as it might fence
14 # nodes where a service fails to stop, which could be fatal during an upgrade
15 # procedure. So we remember the stonith state. If it was enabled we reenable it
16 # at the end of this script
17 STONITH_STATE=$(pcs property show stonith-enabled | grep "stonith-enabled" | awk '{ print $2 }')
18 pcs property set stonith-enabled=false
19
20 # If for some reason rpm-python are missing we want to error out early enough
21 if [ ! rpm -q rpm-python &> /dev/null ]; then
22     echo_error "ERROR: upgrade cannot start without rpm-python installed"
23     exit 1
24 fi
25
26 # In case the mysql package is updated, the database on disk must be
27 # upgraded as well. This typically needs to happen during major
28 # version upgrades (e.g. 5.5 -> 5.6, 5.5 -> 10.1...)
29 #
30 # Because in-place upgrades are not supported across 2+ major versions
31 # (e.g. 5.5 -> 10.1), we rely on logical upgrades via dump/restore cycle
32 # https://bugzilla.redhat.com/show_bug.cgi?id=1341968
33 #
34 # The default is to determine automatically if upgrade is needed based
35 # on mysql package versionning, but this can be overriden manually
36 # to support specific upgrade scenario
37
38 # Where to backup current database if mysql need to be upgraded
39 MYSQL_BACKUP_DIR=/var/tmp/mysql_upgrade_osp
40 MYSQL_TEMP_UPGRADE_BACKUP_DIR=/var/lib/mysql-temp-upgrade-backup
41 # Spare disk ratio for extra safety
42 MYSQL_BACKUP_SIZE_RATIO=1.2
43
44 # Shall we upgrade mysql data directory during the stack upgrade?
45 if [ "$mariadb_do_major_upgrade" = "auto" ]; then
46     ret=$(is_mysql_upgrade_needed)
47     if [ $ret = "1" ]; then
48         DO_MYSQL_UPGRADE=1
49     else
50         DO_MYSQL_UPGRADE=0
51     fi
52     echo "mysql upgrade required: $DO_MYSQL_UPGRADE"
53 elif [ "$mariadb_do_major_upgrade" = 0 ]; then
54     DO_MYSQL_UPGRADE=0
55 else
56     DO_MYSQL_UPGRADE=1
57 fi
58
59 if [ "$(hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid)" = "$(facter hostname)" ]; then
60     if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
61         if [ -d "$MYSQL_BACKUP_DIR" ]; then
62             echo_error "Error: $MYSQL_BACKUP_DIR exists already. Likely an upgrade failed previously"
63             exit 1
64         fi
65         mkdir "$MYSQL_BACKUP_DIR"
66         if [ $? -ne 0 ]; then
67                 echo_error "Error: could not create temporary backup directory $MYSQL_BACKUP_DIR"
68                 exit 1
69         fi
70
71         # the /root/.my.cnf is needed because we set the mysql root
72         # password from liberty onwards
73         backup_flags="--defaults-extra-file=/root/.my.cnf -u root --flush-privileges --all-databases --single-transaction"
74         # While not ideal, this step allows us to calculate exactly how much space the dump
75         # will need. Our main goal here is avoiding any chance of corruption due to disk space
76         # exhaustion
77         backup_size=$(mysqldump $backup_flags 2>/dev/null | wc -c)
78         database_size=$(du -cb /var/lib/mysql | tail -1 | awk '{ print $1 }')
79         free_space=$(df -B1 --output=avail "$MYSQL_BACKUP_DIR" | tail -1)
80
81         # we need at least space for a new mysql database + dump of the existing one,
82         # times a small factor for additional safety room
83         # note: bash doesn't do floating point math or floats in if statements,
84         # so use python to apply the ratio and cast it back to integer
85         required_space=$(python -c "from __future__ import print_function; print(\"%d\" % int((($database_size + $backup_size) * $MYSQL_BACKUP_SIZE_RATIO)))")
86         if [ $required_space -ge $free_space ]; then
87                 echo_error "Error: not enough free space in $MYSQL_BACKUP_DIR ($required_space bytes required)"
88                 exit 1
89         fi
90
91         mysqldump $backup_flags > "$MYSQL_BACKUP_DIR/openstack_database.sql"
92         cp -rdp /etc/my.cnf* "$MYSQL_BACKUP_DIR"
93     fi
94
95     pcs resource disable httpd
96     check_resource httpd stopped 1800
97     pcs resource disable openstack-core
98     check_resource openstack-core stopped 1800
99     pcs resource disable redis
100     check_resource redis stopped 600
101     pcs resource disable mongod
102     check_resource mongod stopped 600
103     pcs resource disable rabbitmq
104     check_resource rabbitmq stopped 600
105     pcs resource disable memcached
106     check_resource memcached stopped 600
107     pcs resource disable galera
108     check_resource galera stopped 600
109     # Disable all VIPs before stopping the cluster, so that pcs doesn't use one as a source address:
110     #   https://bugzilla.redhat.com/show_bug.cgi?id=1330688
111     for vip in $(pcs resource show | grep ocf::heartbeat:IPaddr2 | grep Started | awk '{ print $1 }'); do
112       pcs resource disable $vip
113       check_resource $vip stopped 60
114     done
115     pcs cluster stop --all
116 fi
117
118 # Swift isn't controled by pacemaker
119 systemctl_swift stop
120
121 tstart=$(date +%s)
122 while systemctl is-active pacemaker; do
123     sleep 5
124     tnow=$(date +%s)
125     if (( tnow-tstart > cluster_sync_timeout )) ; then
126         echo_error "ERROR: cluster shutdown timed out"
127         exit 1
128     fi
129 done
130
131 # The reason we do an sql dump *and* we move the old dir out of
132 # the way is because it gives us an extra level of safety in case
133 # something goes wrong during the upgrade. Once the restore is
134 # successful we go ahead and remove it. If the directory exists
135 # we bail out as it means the upgrade process had issues in the last
136 # run.
137 if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
138     if [ -d $MYSQL_TEMP_UPGRADE_BACKUP_DIR ]; then
139         echo_error "ERROR: mysql backup dir already exist"
140         exit 1
141     fi
142     mv /var/lib/mysql $MYSQL_TEMP_UPGRADE_BACKUP_DIR
143 fi
144
145 yum -y install python-zaqarclient  # needed for os-collect-config
146 yum -y -q update
147
148 # We need to ensure at least those two configuration settings, otherwise
149 # mariadb 10.1+ won't activate galera replication.
150 # wsrep_cluster_address must only be set though, its value does not
151 # matter because it's overriden by the galera resource agent.
152 cat >> /etc/my.cnf.d/galera.cnf <<EOF
153 [mysqld]
154 wsrep_on = ON
155 wsrep_cluster_address = gcomm://localhost
156 EOF
157
158 if [ "$(hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid)" = "$(facter hostname)" ]; then
159     if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
160         # Scripts run via heat have no HOME variable set and this confuses
161         # mysqladmin
162         export HOME=/root
163         mkdir /var/lib/mysql || /bin/true
164         chown mysql:mysql /var/lib/mysql
165         chmod 0755 /var/lib/mysql
166         restorecon -R /var/lib/mysql/
167         mysql_install_db --datadir=/var/lib/mysql --user=mysql
168         chown -R mysql:mysql /var/lib/mysql/
169         mysqld_safe --wsrep-new-cluster &
170         # We have a populated /root/.my.cnf with root/password here so
171         # we need to temporarily rename it because the newly created
172         # db is empty and no root password is set
173         mv /root/.my.cnf /root/.my.cnf.temporary
174         timeout 60 sh -c 'while ! mysql -e "" &> /dev/null; do sleep 1; done'
175         mysql -u root < "$MYSQL_BACKUP_DIR/openstack_database.sql"
176         mv /root/.my.cnf.temporary /root/.my.cnf
177         mysqladmin -u root shutdown
178         # The import was successful so we may remove the folder
179         rm -r "$MYSQL_BACKUP_DIR"
180     fi
181 fi
182
183 # If we reached here without error we can safely blow away the origin
184 # mysql dir from every controller
185 if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
186     rm -r $MYSQL_TEMP_UPGRADE_BACKUP_DIR
187 fi
188
189 # Let's reset the stonith back to true if it was true, before starting the cluster
190 if [ $STONITH_STATE == "true" ]; then
191     pcs -f /var/lib/pacemaker/cib/cib.xml property set stonith-enabled=true
192 fi
193
194 # Pin messages sent to compute nodes to kilo, these will be upgraded later
195 crudini  --set /etc/nova/nova.conf upgrade_levels compute "$upgrade_level_nova_compute"
196 # https://bugzilla.redhat.com/show_bug.cgi?id=1284047
197 # Change-Id: Ib3f6c12ff5471e1f017f28b16b1e6496a4a4b435
198 crudini  --set /etc/ceilometer/ceilometer.conf DEFAULT rpc_backend rabbit
199 # https://bugzilla.redhat.com/show_bug.cgi?id=1284058
200 # Ifd1861e3df46fad0e44ff9b5cbd58711bbc87c97 Swift Ceilometer middleware no longer exists
201 crudini --set /etc/swift/proxy-server.conf pipeline:main pipeline "catch_errors healthcheck cache ratelimit tempurl formpost authtoken keystone staticweb proxy-logging proxy-server"