a555f2
#!/bin/sh
a555f2
a555f2
# This script creates the mysql data directory during first service start.
a555f2
# In subsequent starts, it does nothing much.
a555f2
a555f2
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
a555f2
a555f2
export LC_ALL=C
a555f2
a555f2
# Returns content of the specified directory
a555f2
# If listing files fails, fake-file is returned so which means
a555f2
# we'll behave like there was some data initialized
a555f2
# Some files or directories are fine to be there, so those are
a555f2
# explicitly removed from the listing
a555f2
# @param <dir> datadir
a555f2
list_datadir ()
a555f2
{
a555f2
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
a555f2
    -e '^lost+found$' \
a555f2
    -e '\.err$' \
a555f2
    -e '^.bash_history$'
a555f2
}
a555f2
a555f2
# Checks whether datadir should be initialized
a555f2
# @param <dir> datadir
a555f2
should_initialize ()
a555f2
{
a555f2
    test -z "$(list_datadir "$1")"
a555f2
}
a555f2
a555f2
# If two args given first is user, second is group
a555f2
# otherwise the arg is the systemd service file
a555f2
if [ "$#" -eq 2 ]
a555f2
then
a555f2
    myuser="$1"
a555f2
    mygroup="$2"
a555f2
else
a555f2
    # Absorb configuration settings from the specified systemd service file,
a555f2
    # or the default service if not specified
a555f2
    SERVICE_NAME="$1"
a555f2
    if [ x"$SERVICE_NAME" = x ]
a555f2
    then
a555f2
        SERVICE_NAME=@DAEMON_NAME@.service
a555f2
    fi
a555f2
a555f2
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
a555f2
      sed 's/^User=//'`
a555f2
    if [ x"$myuser" = x ]
a555f2
    then
a555f2
        myuser=mysql
a555f2
    fi
a555f2
a555f2
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
a555f2
      sed 's/^Group=//'`
a555f2
    if [ x"$mygroup" = x ]
a555f2
    then
a555f2
        mygroup=mysql
a555f2
    fi
a555f2
fi
a555f2
a555f2
# Set up the errlogfile with appropriate permissions
a555f2
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
a555f2
    case $(basename "$errlogfile") in
a555f2
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
a555f2
        *) ;;
a555f2
    esac
a555f2
else
a555f2
    # Provide some advice if the log file cannot be created by this script
a555f2
    errlogdir=$(dirname "$errlogfile")
a555f2
    if ! [ -d "$errlogdir" ] ; then
a555f2
        echo "The directory $errlogdir does not exist." >&2
a555f2
        exit 1
a555f2
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
a555f2
        echo "The log file $errlogfile cannot be written, please, fix its permissions." >&2
a555f2
        echo "The daemon will be run under $myuser:$mygroup" >&2
a555f2
        exit 1
a555f2
    fi
a555f2
fi
a555f2
a555f2
# Make the data directory if doesn't exist or empty
a555f2
if should_initialize "$datadir" ; then
a555f2
    # First, make sure $datadir is there with correct permissions
a555f2
    # (note: if it's not, and we're not root, this'll fail ...)
a555f2
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
a555f2
    then
a555f2
        mkdir -p "$datadir" || exit 1
a555f2
    fi
a555f2
    chown "$myuser:$mygroup" "$datadir"
a555f2
    chmod 0755 "$datadir"
a555f2
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
a555f2
a555f2
    # Now create the database
a555f2
    echo "Initializing @NICE_PROJECT_NAME@ database" >&2
a555f2
    # Avoiding deletion of files not created by mysql_install_db is
a555f2
    # guarded by time check and sleep should help work-arounded
a555f2
    # potential issues on systems with 1 second resolution timestamps
a555f2
    # https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
a555f2
    INITDB_TIMESTAMP=`LANG=C date -u`
a555f2
    sleep 1
a555f2
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser" --skip-test-db >&2
a555f2
    ret=$?
a555f2
    if [ $ret -ne 0 ] ; then
a555f2
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
a555f2
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
a555f2
        # Clean up any partially-created database files
a555f2
        if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
a555f2
            echo "Initialization of @NICE_PROJECT_NAME@ database was not finished successfully." >&2
a555f2
            echo "Files created so far will be removed." >&2
a555f2
            find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
a555f2
                 -not -name "lost+found" -exec rm -rf {} +
a555f2
            if [ $? -ne 0 ] ; then
a555f2
                echo "Removing of created files was not successfull." >&2
a555f2
                echo "Please, clean directory $datadir manually." >&2
a555f2
            fi
a555f2
        else
a555f2
            echo "However, part of data has been initialized and those will not be removed." >&2
a555f2
            echo "Please, clean directory $datadir manually." >&2
a555f2
        fi
a555f2
        exit $ret
a555f2
    fi
a555f2
    # upgrade does not need to be run on a fresh datadir
a555f2
    echo "@VERSION@-MariaDB" >"$datadir/mysql_upgrade_info"
a555f2
else
a555f2
    if [ -d "$datadir/mysql/" ] ; then
a555f2
        # mysql dir exists, it seems data are initialized properly
a555f2
        echo "Database @NICE_PROJECT_NAME@ is probably initialized in $datadir already, nothing is done."
a555f2
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
a555f2
    else
a555f2
        # if the directory is not empty but mysql/ directory is missing, then
a555f2
        # print error and let user to initialize manually or empty the directory
a555f2
        echo "Database @NICE_PROJECT_NAME@ is not initialized, but the directory $datadir is not empty, so initialization cannot be done." >&2
a555f2
        echo "Make sure the $datadir is empty before running `basename $0`." >&2
a555f2
        exit 1
a555f2
    fi
a555f2
fi
a555f2
a555f2
exit 0