Merge "M/N upgrade sahara-api fails to restart."
[apex-tripleo-heat-templates.git] / extraconfig / tasks / major_upgrade_check.sh
1 #!/bin/bash
2
3 set -eu
4
5 check_cluster()
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 check_pcsd()
14 {
15     if pcs status 2>&1 | grep -E 'Offline'; then
16         echo_error "ERROR: upgrade cannot start with some pcsd daemon offline"
17         exit 1
18     fi
19 }
20
21 check_disk_for_mysql_dump()
22 {
23     # Where to backup current database if mysql need to be upgraded
24     MYSQL_BACKUP_DIR=/var/tmp/mysql_upgrade_osp
25     MYSQL_TEMP_UPGRADE_BACKUP_DIR=/var/lib/mysql-temp-upgrade-backup
26     # Spare disk ratio for extra safety
27     MYSQL_BACKUP_SIZE_RATIO=1.2
28
29     # Shall we upgrade mysql data directory during the stack upgrade?
30     if [ "$mariadb_do_major_upgrade" = "auto" ]; then
31         ret=$(is_mysql_upgrade_needed)
32         if [ $ret = "1" ]; then
33             DO_MYSQL_UPGRADE=1
34         else
35             DO_MYSQL_UPGRADE=0
36         fi
37         echo "mysql upgrade required: $DO_MYSQL_UPGRADE"
38     elif [ "$mariadb_do_major_upgrade" = "no" ]; then
39         DO_MYSQL_UPGRADE=0
40     else
41         DO_MYSQL_UPGRADE=1
42     fi
43
44     if [ "$(hiera -c /etc/puppet/hiera.yaml bootstrap_nodeid)" = "$(facter hostname)" ]; then
45         if [ $DO_MYSQL_UPGRADE -eq 1 ]; then
46
47             if [ -d "$MYSQL_BACKUP_DIR" ]; then
48                 echo_error "Error: $MYSQL_BACKUP_DIR exists already. Likely an upgrade failed previously"
49                 exit 1
50             fi
51             mkdir "$MYSQL_BACKUP_DIR"
52             if [ $? -ne 0 ]; then
53                 echo_error "Error: could not create temporary backup directory $MYSQL_BACKUP_DIR"
54                 exit 1
55             fi
56
57             # the /root/.my.cnf is needed because we set the mysql root
58             # password from liberty onwards
59             backup_flags="--defaults-extra-file=/root/.my.cnf -u root --flush-privileges --all-databases --single-transaction"
60             # While not ideal, this step allows us to calculate exactly how much space the dump
61             # will need. Our main goal here is avoiding any chance of corruption due to disk space
62             # exhaustion
63             backup_size=$(mysqldump $backup_flags 2>/dev/null | wc -c)
64             database_size=$(du -cb /var/lib/mysql | tail -1 | awk '{ print $1 }')
65             free_space=$(df -B1 --output=avail "$MYSQL_BACKUP_DIR" | tail -1)
66
67             # we need at least space for a new mysql database + dump of the existing one,
68             # times a small factor for additional safety room
69             # note: bash doesn't do floating point math or floats in if statements,
70             # so use python to apply the ratio and cast it back to integer
71             required_space=$(python -c "from __future__ import print_function; print(\"%d\" % int((($database_size + $backup_size) * $MYSQL_BACKUP_SIZE_RATIO)))")
72             if [ $required_space -ge $free_space ]; then
73                 echo_error "Error: not enough free space in $MYSQL_BACKUP_DIR ($required_space bytes required)"
74                 exit 1
75             fi
76         fi
77     fi
78 }
79
80 check_python_rpm()
81 {
82     # If for some reason rpm-python are missing we want to error out early enough
83     if ! rpm -q rpm-python &> /dev/null; then
84         echo_error "ERROR: upgrade cannot start without rpm-python installed"
85         exit 1
86     fi
87 }
88
89 check_clean_cluster()
90 {
91     if crm_mon -1 | grep -A3 Failed; then
92         echo_error "ERROR: upgrade cannot start with failed resources on the cluster. Clean them up before starting: pcs resource cleanup."
93         exit 1
94     fi
95 }
96
97 check_galera_root_password()
98 {
99     # BZ: 1357112
100     if [ ! -e /root/.my.cnf ]; then
101         echo_error "ERROR: upgrade cannot be started, the galera password is missing. The overcloud needs update."
102         exit 1
103     fi
104 }