|
| 1 | +# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation; version 2 of the License. |
| 6 | +# |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +# GNU General Public License for more details. |
| 11 | +# |
| 12 | +# You should have received a copy of the GNU General Public License |
| 13 | +# along with this program; if not, write to the Free Software |
| 14 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | + |
| 16 | +# Wait for server to start up (this requires the client package) |
| 17 | +pinger () { |
| 18 | + while /bin/true ; do |
| 19 | + sleep 1 |
| 20 | + mysqladmin ping >/dev/null 2>&1 && break |
| 21 | + done |
| 22 | +} |
| 23 | + |
| 24 | +# To avoid having hardcoded paths in the script, we do a search on the path, as suggested at: |
| 25 | +# https://www.debian.org/doc/manuals/developers-reference/ch06.en.html#bpp-debian-maint-scripts |
| 26 | +pathfind() { |
| 27 | + OLDIFS="$IFS" |
| 28 | + IFS=: |
| 29 | + for p in $PATH; do |
| 30 | + if [ -x "$p/$*" ]; then |
| 31 | + IFS="$OLDIFS" |
| 32 | + return 0 |
| 33 | + fi |
| 34 | + done |
| 35 | + IFS="$OLDIFS" |
| 36 | + return 1 |
| 37 | +} |
| 38 | + |
| 39 | +# Fetch value from config files |
| 40 | +# Usage: get_mysql_option [section] [option] [default value] |
| 41 | +get_mysql_option() { |
| 42 | + if pathfind my_print_defaults; then |
| 43 | + RESULT=$(my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1) |
| 44 | + fi |
| 45 | + if [ -z "$RESULT" ]; then |
| 46 | + RESULT="$3" |
| 47 | + fi |
| 48 | + echo $RESULT |
| 49 | +} |
| 50 | + |
| 51 | +# Check if server is running |
| 52 | +get_running () { |
| 53 | + PIDFILE=$(get_mysql_option mysqld_safe pid-file "") |
| 54 | + if [ -z "$PIDFILE" ]; then |
| 55 | + PIDFILE=$(get_mysql_option mysqld pid-file "$MYSQLDATA/$(hostname).pid") |
| 56 | + fi |
| 57 | + if [ -e "$PIDFILE" ] && [ -d "/proc/$(cat "$PIDFILE")" ]; then |
| 58 | + echo 1 |
| 59 | + else |
| 60 | + echo 0 |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# Runs an arbitrary init sql file supplied in $1. Does not require login access |
| 65 | +run_init_sql() { |
| 66 | + tmpdir=$(mktemp -d) |
| 67 | + chown mysql:mysql "$tmpdir" |
| 68 | + mysqld --bootstrap --user=mysql --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" > /dev/null 2>&1 < "$1" |
| 69 | + result=$? |
| 70 | + rm -rf "$tmpdir" |
| 71 | + return $result |
| 72 | +} |
| 73 | + |
| 74 | +# Verify that everything the server needs to run is set up |
| 75 | +verify_ready() { |
| 76 | + |
| 77 | + MYSQLDATA=/var/lib/mysql |
| 78 | + MYSQLFILES=/var/lib/mysql-files |
| 79 | + MYSQLKEYRING=/var/lib/mysql-keyring |
| 80 | + MYSQLLOG=/var/log/mysql |
| 81 | + MYSQLRUN=/var/run/mysqld |
| 82 | + |
| 83 | + if ! getent group mysql >/dev/null; then |
| 84 | + addgroup --system mysql >/dev/null |
| 85 | + fi |
| 86 | + |
| 87 | + if ! getent passwd mysql >/dev/null; then |
| 88 | + adduser --ingroup mysql --system --disabled-login --no-create-home --home ${MYSQLDATA} --shell /bin/false --gecos "MySQL Server" mysql >/dev/null |
| 89 | + fi |
| 90 | + |
| 91 | + if [ ! -d ${MYSQLDATA} -a ! -L ${MYSQLDATA} ]; then |
| 92 | + install -d -m0750 -omysql -gmysql ${MYSQLDATA} |
| 93 | + fi |
| 94 | + |
| 95 | + if [ ! -d ${MYSQLFILES} -a ! -L ${MYSQLFILES} ]; then |
| 96 | + install -d -m0770 -omysql -gmysql ${MYSQLFILES} |
| 97 | + fi |
| 98 | + |
| 99 | + if [ ! -d ${MYSQLKEYRING} -a ! -L ${MYSQLKEYRING} ]; then |
| 100 | + install -d -m0750 -omysql -gmysql ${MYSQLKEYRING} |
| 101 | + fi |
| 102 | + |
| 103 | + if [ ! -d ${MYSQLLOG} -a ! -L ${MYSQLLOG} ]; then |
| 104 | + install -d -m0750 -omysql -gadm ${MYSQLLOG} |
| 105 | + install /dev/null -m0640 -omysql -gadm ${MYSQLLOG}/error.log |
| 106 | + fi |
| 107 | + |
| 108 | + if [ ! -d ${MYSQLRUN} -a ! -L ${MYSQLRUN} ]; then |
| 109 | + install -d -m0755 -omysql -gmysql ${MYSQLRUN} |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +# Verify the database exists and is ssl ready |
| 114 | +verify_database() { |
| 115 | + MYSQLDATA=/var/lib/mysql |
| 116 | + MYSQLFILES=/var/lib/mysql-files |
| 117 | + |
| 118 | + if [ ! -d "${MYSQLDATA}/mysql" ] && [ -d "${MYSQLFILES}" ]; then |
| 119 | + su - mysql -s /bin/bash -c "mysql_install_db --user=mysql > /dev/null" |
| 120 | + SQL=$(mktemp -u ${MYSQLFILES}/XXXXXXXXXX) |
| 121 | + install /dev/null -m0600 -omysql -gmysql "${SQL}" |
| 122 | + cat << EOF > ${SQL} |
| 123 | +USE mysql; |
| 124 | +INSTALL PLUGIN auth_socket SONAME 'auth_socket.so'; |
| 125 | +UPDATE user SET plugin='auth_socket' WHERE user='root'; |
| 126 | +FLUSH PRIVILEGES; |
| 127 | +EOF |
| 128 | + run_init_sql "$SQL" |
| 129 | + rm -f "$SQL" |
| 130 | + fi |
| 131 | + |
| 132 | + if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${MYSQLDATA}/server-key.pem" ]; then |
| 133 | + mysql_ssl_rsa_setup --datadir="${MYSQLDATA}" --uid=mysql >/dev/null 2>&1 |
| 134 | + fi |
| 135 | +} |
| 136 | + |
| 137 | +verify_server () { |
| 138 | + TIMEOUT=0 |
| 139 | + if [ "${1}" = "start" ]; then |
| 140 | + TIMEOUT=${STARTTIMEOUT} |
| 141 | + elif [ "${1}" = "stop" ]; then |
| 142 | + TIMEOUT=${STOPTIMEOUT} |
| 143 | + fi |
| 144 | + |
| 145 | + COUNT=0 |
| 146 | + while [ ${COUNT} -lt ${TIMEOUT} ]; |
| 147 | + do |
| 148 | + COUNT=$(( COUNT+1 )) |
| 149 | + echo -n . |
| 150 | + if [ "${1}" = "start" ] && [ "$(get_running)" = 1 ]; then |
| 151 | + if [ -z ${2} ]; then |
| 152 | + echo |
| 153 | + fi |
| 154 | + return 0 |
| 155 | + fi |
| 156 | + if [ "${1}" = "stop" ] && [ "$(get_running)" = 0 ]; then |
| 157 | + if [ -z ${2} ]; then |
| 158 | + echo |
| 159 | + fi |
| 160 | + return 0 |
| 161 | + fi |
| 162 | + sleep 1 |
| 163 | + done |
| 164 | + return 1 |
| 165 | +} |
| 166 | + |
0 commit comments