Rework the pacemaker_common_functions for M..N upgrades
[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 check_cluster
8 check_pcsd
9 check_clean_cluster
10 check_python_rpm
11 check_galera_root_password
12 check_disk_for_mysql_dump
13
14 # We want to disable fencing during the cluster --stop as it might fence
15 # nodes where a service fails to stop, which could be fatal during an upgrade
16 # procedure. So we remember the stonith state. If it was enabled we reenable it
17 # at the end of this script
18 STONITH_STATE=$(pcs property show stonith-enabled | grep "stonith-enabled" | awk '{ print $2 }')
19 pcs property set stonith-enabled=false
20
21 # In case the mysql package is updated, the database on disk must be
22 # upgraded as well. This typically needs to happen during major
23 # version upgrades (e.g. 5.5 -> 5.6, 5.5 -> 10.1...)
24 #
25 # Because in-place upgrades are not supported across 2+ major versions
26 # (e.g. 5.5 -> 10.1), we rely on logical upgrades via dump/restore cycle
27 # https://bugzilla.redhat.com/show_bug.cgi?id=1341968
28 #
29 # The default is to determine automatically if upgrade is needed based
30 # on mysql package versionning, but this can be overriden manually
31 # to support specific upgrade scenario
32
33 if [ "$(hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid)" = "$(facter hostname)" ]; then
34     if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
35         mysqldump $backup_flags > "$MYSQL_BACKUP_DIR/openstack_database.sql"
36         cp -rdp /etc/my.cnf* "$MYSQL_BACKUP_DIR"
37     fi
38
39     pcs resource disable httpd
40     check_resource httpd stopped 1800
41     pcs resource disable redis
42     check_resource redis stopped 600
43     pcs resource disable rabbitmq
44     check_resource rabbitmq stopped 600
45     pcs resource disable galera
46     check_resource galera stopped 600
47     # Disable all VIPs before stopping the cluster, so that pcs doesn't use one as a source address:
48     #   https://bugzilla.redhat.com/show_bug.cgi?id=1330688
49     for vip in $(pcs resource show | grep ocf::heartbeat:IPaddr2 | grep Started | awk '{ print $1 }'); do
50       pcs resource disable $vip
51       check_resource $vip stopped 60
52     done
53     pcs cluster stop --all
54 fi
55
56 stop_or_disable_service mongod
57 check_resource mongod stopped 600
58 stop_or_disable_service memcached
59 check_resource memcached stopped 600
60
61
62
63
64
65 # Swift isn't controled by pacemaker
66 systemctl_swift stop
67
68 tstart=$(date +%s)
69 while systemctl is-active pacemaker; do
70     sleep 5
71     tnow=$(date +%s)
72     if (( tnow-tstart > cluster_sync_timeout )) ; then
73         echo_error "ERROR: cluster shutdown timed out"
74         exit 1
75     fi
76 done
77
78 # The reason we do an sql dump *and* we move the old dir out of
79 # the way is because it gives us an extra level of safety in case
80 # something goes wrong during the upgrade. Once the restore is
81 # successful we go ahead and remove it. If the directory exists
82 # we bail out as it means the upgrade process had issues in the last
83 # run.
84 if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
85     if [ -d $MYSQL_TEMP_UPGRADE_BACKUP_DIR ]; then
86         echo_error "ERROR: mysql backup dir already exist"
87         exit 1
88     fi
89     mv /var/lib/mysql $MYSQL_TEMP_UPGRADE_BACKUP_DIR
90 fi
91
92 yum -y install python-zaqarclient  # needed for os-collect-config
93 yum -y -q update
94
95 # We need to ensure at least those two configuration settings, otherwise
96 # mariadb 10.1+ won't activate galera replication.
97 # wsrep_cluster_address must only be set though, its value does not
98 # matter because it's overriden by the galera resource agent.
99 cat >> /etc/my.cnf.d/galera.cnf <<EOF
100 [mysqld]
101 wsrep_on = ON
102 wsrep_cluster_address = gcomm://localhost
103 EOF
104
105 if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
106     # Scripts run via heat have no HOME variable set and this confuses
107     # mysqladmin
108     export HOME=/root
109
110     mkdir /var/lib/mysql || /bin/true
111     chown mysql:mysql /var/lib/mysql
112     chmod 0755 /var/lib/mysql
113     restorecon -R /var/lib/mysql/
114     mysql_install_db --datadir=/var/lib/mysql --user=mysql
115     chown -R mysql:mysql /var/lib/mysql/
116
117     if [ "$(hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid)" = "$(facter hostname)" ]; then
118         mysqld_safe --wsrep-new-cluster &
119         # We have a populated /root/.my.cnf with root/password here so
120         # we need to temporarily rename it because the newly created
121         # db is empty and no root password is set
122         mv /root/.my.cnf /root/.my.cnf.temporary
123         timeout 60 sh -c 'while ! mysql -e "" &> /dev/null; do sleep 1; done'
124         mysql -u root < "$MYSQL_BACKUP_DIR/openstack_database.sql"
125         mv /root/.my.cnf.temporary /root/.my.cnf
126         mysqladmin -u root shutdown
127         # The import was successful so we may remove the folder
128         rm -r "$MYSQL_BACKUP_DIR"
129     fi
130 fi
131
132 # If we reached here without error we can safely blow away the origin
133 # mysql dir from every controller
134
135 # TODO: What if the upgrade fails on the bootstrap node, but not on
136 # this controller.  Data may be lost.
137 if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
138     rm -r $MYSQL_TEMP_UPGRADE_BACKUP_DIR
139 fi
140
141 # Let's reset the stonith back to true if it was true, before starting the cluster
142 if [ $STONITH_STATE == "true" ]; then
143     pcs -f /var/lib/pacemaker/cib/cib.xml property set stonith-enabled=true
144 fi
145
146 # Pin messages sent to compute nodes to kilo, these will be upgraded later
147 crudini  --set /etc/nova/nova.conf upgrade_levels compute "$upgrade_level_nova_compute"