Skip to content

Instantly share code, notes, and snippets.

@fraktik
Created October 25, 2019 19:45
Show Gist options
  • Save fraktik/29ca1f378cb206dba4bb995bf0098d05 to your computer and use it in GitHub Desktop.
Save fraktik/29ca1f378cb206dba4bb995bf0098d05 to your computer and use it in GitHub Desktop.
Perl skript na vytvoření nové CENTER instance (primárně) na stejném serveru s Koha - zkopíruje konfigy, provede jejich nastavením, vytvoří linky na společné soubory.
#!/bin/bash
echo "**********************************************"
echo "* Center2 instance creator *"
echo "* Do NOT use in single-instance environment! *"
echo "**********************************************"
echo
BASE_PATH=$(dirname "$(dirname "$(readlink -f "$0")")")
read -e -p "Continue creation in multi-instance environment? (Y/n): " -i "Y" yn
yn=`echo $yn | tr '[:upper:]' '[:lower:]'`
if [ "$yn" != "y" ];
then exit;
fi
read -e -p "Center2 main installation folder (used as a source; must NOT be configured): " -i "$BASE_PATH" center
if [ ! -d "${center}" ]; then
echo "Folder ${center} does NOT exist. Exiting."
exit
fi
read -e -p "Common root folder for all instances: " -i $(dirname $BASE_PATH)"/knihovna" root
if [ ! -d "${root}" ]; then
echo "Folder ${root} does NOT exist. Exiting."
exit
fi
read -e -p "Instance name: " instance;
if [ -d "${root}/${instance}" ]; then
echo "Folder ${root}/${instance} already exists. Instance probably created earlier. Exiting."
exit
else
mkdir "${root}/${instance}"
fi
read -e -p "Creating new instance in ${root}/${instance} by cloning from ${center}. Is it OK? (Y/n) " -i "Y" yn
yn=`echo $yn | tr '[:upper:]' '[:lower:]'`
if [ "$yn" != "y" ];
then exit;
fi
MY_DIR="`dirname \"$0\"`"
CURR_DIR=${PWD}
cd "$MY_DIR/.."
shopt -s nullglob # causes the array to be empty if there are no matches
FILES=(*)
for FILE in "${FILES[@]}"
do
if [ "$FILE" == "config" ] || [ "$FILE" == "public" ]; then
continue
fi
echo "Creating symlink: ${center}/${FILE} -> ${root}/${instance}/${FILE}"
ln -s "${center}/${FILE}" "${root}/${instance}/${FILE}"
done
LOCAL_DIR=()
LOCAL_DIRS+=("config")
LOCAL_DIRS+=("config/autoload")
LOCAL_DIRS+=("public")
LOCAL_DIRS+=("public/upload")
LOCAL_DIRS+=("public/upload/RF")
LOCAL_LINKS=()
LOCAL_LINKS+=("config/application.config.php")
LOCAL_LINKS+=("config/autoload/global.php")
LOCAL_LINKS+=("config/autoload/README.md")
LOCAL_LINKS+=("public/css")
LOCAL_LINKS+=("public/fonts")
LOCAL_LINKS+=("public/js")
LOCAL_LINKS+=("public/upload/readme.txt")
echo "Creating local directories..."
for LDIR in "${LOCAL_DIRS[@]}"
do
echo " ${root}/${instance}/${LDIR}"
mkdir "${root}/${instance}/${LDIR}"
done
echo "Setting write permissions to ${root}/${instance}/public/upload/RF...";
chmod a+w "${root}/${instance}/public/upload/RF";
echo "Creating symlinks in local directories..."
for LLINK in "${LOCAL_LINKS[@]}"
do
echo " ${root}/${instance}/${LLINK}"
ln -s "${center}/${LLINK}" "${root}/${instance}/${LLINK}"
done
echo "Copying ${center}/config/autoload/local.php.dist -> ${root}/${instance}/config/autoload/local.php"
cp "${center}/config/autoload/local.php.dist" "${root}/${instance}/config/autoload/local.php"
echo "Copying ${center}/public/.htaccess-dist -> ${root}/${instance}/public/.htaccess"
cp "${center}/public/.htaccess-dist" "${root}/${instance}/public/.htaccess"
echo "Copying ${center}/public/index.php -> ${root}/${instance}/public/index.php"
cp "${center}/public/index.php" "${root}/${instance}/public/index.php"
echo "Copying ${center}/public/img -> ${root}/${instance}/public/"
cp -R "${center}/public/img" "${root}/${instance}/public/"
echo "Adding instance ${instance} to ${center}/systools/instance-list"
echo "${root}/${instance}/config/autoload/local.php" >> "${center}/systools/instance-list"
echo "Modifying .htaccess"
sed -i "s/^RewriteBase.*/RewriteBase \/${instance}/" "${root}/${instance}/public/.htaccess"
echo "Creating SSH tunnel file"
CLIENT="/root/tunnel/clients/${instance}.client"
LASTFILE=`ls -1 /root/tunnel/port-?????-* | sort | tail -n1`
IFS='-'
tokens=( $LASTFILE )
TUNNEL_PORT_HTTP=$((${tokens[1]}+1))
TUNNEL_PORT_MYSQL=$((${TUNNEL_PORT_HTTP}-10000))
TUNNEL="${tokens[0]}-${TUNNEL_PORT_HTTP}-${instance}.sh"
cp /root/tunnel/template-port-4000x-test.sh "${TUNNEL}"
sed -i "s/?????/${instance}/" "${TUNNEL}"
cp /root/tunnel/clients/_____.template "${CLIENT}"
sed -i "/^client_name/s/_____/${instance}/" "${CLIENT}"
sed -i "/^mysql_local_port/s/300__/${TUNNEL_PORT_MYSQL}/" "${CLIENT}"
sed -i "/^http_local_port/s/400__/${TUNNEL_PORT_HTTP}/" "${CLIENT}"
sed -i "/^http_remote_port/s/400__/${TUNNEL_PORT_HTTP}/" "${CLIENT}"
read -e -p "SSH server for tunel: " ssh_server
sed -i "/^tunnel_end/s/_____/${ssh_server}/" "${CLIENT}"
echo " ${TUNNEL} created"
"${TUNNEL}" key
LOCAL_CFG="${root}/${instance}/config/autoload/local.php"
echo "Modifying ${LOCAL_CFG}"
read -e -p "Koha DB username (max. 16): " -i "tunel" koha_uid
read -e -p "Koha DB password: " koha_pwd
read -e -p "Koha DB DSN: " -i "mysql:dbname=koha_${instance};host=127.0.0.1;port=${TUNNEL_PORT_MYSQL}" koha_dsn
dbinstance="${instance/-/_}"
read -e -p "Center DB username (max. 16): " -i "${dbinstance:0:16}" center_uid
read -e -p "Center DB password: " center_pwd
read -e -p "Center DB DSN: " -i "mysql:dbname=center_${dbinstance};host=127.0.0.1" center_dsn
sed -i "s/koha_user/${koha_uid}/" "${LOCAL_CFG}"
sed -i "s/\*\*\* koha \*\*\*/${koha_pwd}/" "${LOCAL_CFG}"
sed -i "s/'mysql:dbname=koha_test;host=127.0.0.1;port=30666'/'${koha_dsn}'/" "${LOCAL_CFG}"
sed -i "s/center_user/${center_uid}/" "${LOCAL_CFG}"
sed -i "s/\*\*\* center \*\*\*/${center_pwd}/" "${LOCAL_CFG}"
sed -i "s/'mysql:dbname=center_test;host=127.0.0.1'/'${center_dsn}'/" "${LOCAL_CFG}"
sed -i "s/CENTER_SESSID/${instance}_SESSID/" "${LOCAL_CFG}"
echo
echo "o-----------------------------------------------------------------------------"
echo "| Follow these steps to finish instance creation:"
echo "|"
echo "| - create instance database"
echo "| CREATE DATABASE center_${dbinstance};"
echo "| GRANT ALL PRIVILEGES ON center_${dbinstance}.* TO '${center_uid}'@'localhost' IDENTIFIED BY '${center_pwd}';"
echo "| - Create SSH tunnel using manual: Center SaaS_ technické řešení zapojení.pdf"
echo "| ls -1 ls -1 /root/tunnel/port-*.sh"
echo "| mcedit ${TUNNEL}"
echo "| mcedit ${CLIENT}"
echo "| get key: ${TUNNEL} key"
echo "| - Check local config"
echo "| mcedit ${root}/${instance}/config/autoload/local.php"
echo "| - ${center}/systools/update"
echo "| - ${root}/${instance}/systools/first-install --instance=${instance} --config=${root}/${instance}/config/autoload/local.php"
echo "| - mcedit /etc/httpd/sites-enabled/center.koha.cz"
echo "| add new alias to Center's site configuration in apache"
echo "| Alias /${instance} ${root}/${instance}/public"
echo "| - service httpd reload"
echo "o-----------------------------------------------------------------------------"
cd "$CURR_DIR"
echo
echo "**********************************************"
echo "* Center2 instance creator finished *"
echo "**********************************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment