From 77e24a8ec601e0de83b81ac1d7a2b7917b3eac10 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Sat, 30 Apr 2022 16:56:32 +0200 Subject: [PATCH] Init Percona XtraDB Cluster install script Signed-off-by: Georg Pfuetzenreuter --- scripts/sh/deploy_pxc.sh | 382 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100755 scripts/sh/deploy_pxc.sh diff --git a/scripts/sh/deploy_pxc.sh b/scripts/sh/deploy_pxc.sh new file mode 100755 index 0000000..dab277c --- /dev/null +++ b/scripts/sh/deploy_pxc.sh @@ -0,0 +1,382 @@ +#!/bin/sh +# Basic Percona XtraDB Cluster Server installation script +# Intended to be run on openSUSE Leap, but is easily adaptable by changing the package manager call in check_dependencies. +# +# Georg Pfuetzenreuter +# Created and last modified: 30/04/2022 + +SCRIPT="PXC_Boostrap" +VENDOR="Percona" +VERSION="8.0.27" +PRODUCT="$VENDOR-XtraDB-Cluster_$VERSION-18.1_Linux.x86_64.glibc2.17-minimal" +TARBALL="$PRODUCT.tar.gz" +URL="https://downloads.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-8.0.27/binary/tarball/$TARBALL" +TARGETPREFIX="/opt" +TARGETDIR="$TARGETPREFIX/$VENDOR" +TMPDIR="/tmp" +SHORTY="mysql" +DATADIR="/var/lib/$SHORTY" +HOMEDIR="/var/lib/${SHORTY}_home" +RUNDATE="`date +%m%d%Y-%H%M%S`" +servicefile="/etc/systemd/system/${SHORTY}d.service" +configfile="/etc/opt/my.cnf" +checkfile="/var/adm/pxc.installed" +ARGS="$@" + +check_args () { + if [ "$ARGS" = "--debug" ] + then + RUNMODE="debug" + # we do not use this here + echo "Not implemented." + exit 1 + fi + if [ ! "$ARGS" = "--debug" ] + then + RUNMODE="normal" + fi + if [ "$ARGS" = "--uninstall" ] + then + RUNMODE="uninstall" + fi +} + +check_dependencies () { + if ! command -v ex &>/dev/null + then + if zypper --non-interactive install vim-small + then + log "Satisfied dependencies." + else + log "Could not satisfy dependency \"ex\"." + return 1 + fi + fi + if ! command -v systemctl &>/dev/null + then + log "Unsure whether this actually is a systemd based system." + return 1 + fi +} + +init () { + local step="Preparation" + + if [ ! "`id -u`" = "0" ] + then + log "Script needs root privileges." + exit 1 + fi + log "Initializing $SCRIPT for $PRODUCT" + check_args + if [ "$RUNMODE" = "normal" ] + then + if [ -f "$checkfile" ] + then + log "Existing installation found. Consider --uninstall first." + exit 1 + fi + check_dependencies + check "$?" "dependencies" + cd $TMPDIR + download + setup + touch "$checkfile" + fi + if [ "$RUNMODE" = "uninstall" ] + then + uninstall + fi +} + + +log () { + local printmsg="$1" + local prefix="[$SCRIPT]" + local logmsg="$prefix $printmsg" + local logfile="$TMPDIR/$SCRIPT_$RUNDATE.log" + + echo "$printmsg" + echo "$logmsg" >> $logfile +} + +check () { + local return="$1" + local action="$2" + + if [ "$return" = "0" ] + then + log "$step: Action \"$action\" OK" + else + log "$step: Action \"$action\" failed" + abort + fi +} + +abort () { + local trash_link="$TARGETPREFIX/$SHORTY" + local trash_dir="$TARGETDIR/$PRODUCT" + local trash_dl1="$TMPDIR/$TARBALL" + local trash_dl2="$TMPDIR/$checksum" + + if [ ! "$RUNMODE" = "uninstall" ] + then + log "Fatal error, aborting script ..." + fi + + if [ -h "$trash_link" ] + then + log "Cleaning up installation ..." + rm $trash_link + if [ -d $trash_dir ] + then + rm -r $trash_dir + fi + fi + + if [ -f "$trash_dl1" ] + then + log "Cleaning up download ..." + rm $trash_dl1 + if [ -f $trash_dl2 ] + then + rm $trash_dl2 + fi + fi + + if [ -f "$servicefile" ] + then + log "Removing servicefile ..." + systemctl disable ${SHORTY}d.service &>/dev/null + rm $servicefile + fi + + if [ ! "$RUNMODE" = "uninstall" ] + then + log "Exit." + exit 1 + fi +} + +download () { + checksum="$TARBALL.sha256sum" + log "Downloading release tarball ..." + if [ -f $TARBALL ] + then + log "Removing stale files ..." + rm $TARBALL + fi + if [ -f $checksum ] + then + rm $checksum + fi + curl -sSLo $TARBALL $URL + curl -sSLo $checksum $URL.sha256sum + if ! download_verify $checksum + then + log "Corrupted download, verify URL." + exit 1 + fi +} + +download_verify () { + log "Verifying downloaded tarball ..." + sha256sum -c $1 +} + +setup () { + local step="Setup" + + for action in extract prepare install + do + setup_$action + check "$?" "$action" + done + +} + +setup_extract () { + log "Deflating ..." + if [ ! -d "$TARGETDIR" ] + then + mkdir "$TARGETDIR" + fi + tar -C $TARGETDIR -xzf $TARBALL +} + +setup_prepare () { + log "Preparing installation ..." + local target="$TARGETDIR/$PRODUCT" + local target_short="$TARGETPREFIX/$SHORTY" + ln -s $target $target_short + if ! getent group "$SHORTY" &>/dev/null + then + log "Creating group ..." + if ! groupadd -r $SHORTY + then + log "Failed to create group." + return 1 + fi + fi + if ! id "$SHORTY" &>/dev/null + then + log "Creating user ..." + if ! useradd -g $SHORTY -d $HOMEDIR -rs /usr/sbin/nologin $SHORTY + then + log "Failed to create user." + return 1 + fi + if [ ! -d "$HOMEDIR" ] + then + if mkdir $HOMEDIR + then + if chown $SHORTY:$SHORTY $HOMEDIR + then + if ! chmod 750 $HOMEDIR + then + log "Failed to set homedir permissions." + return 1 + fi + else + log "Failed to set homedir ownership." + return 1 + fi + else + log "Failed to create home directory." + return 1 + fi + fi + + fi + if ! chown -R $SHORTY:$SHORTY $target + then + log "Failed to change ownership." + return 1 + fi + if ! chmod 750 $target + then + log "Failed to change permissions." + return 1 + fi +} + +setup_install () { + log "Placing systemd service ..." + ex - $servicefile </dev/null + groupdel -f $SHORTY &>/dev/null + + log "Removing files and directories ..." + abort + + echo "Remove data? y/n" + read choice + if [ "$choice" = "y" ] + then + if [ -d "$DATADIR" ] + then + rm -r "$DATADIR" + fi + if [ -d "$HOMEDIR" ] + then + rm -r "$HOMEDIR" + fi + if [ -f "$configfile" ] + then + rm "$configfile" + fi + fi + + if [ -f "$checkfile" ] + then + rm "$checkfile" + fi + + log "OK" +} + +# Execute workflow +init +