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