Commit 00138135 authored by Project Open's avatar Project Open

- Updated ~projop/bin/ scripts

parent a0f9d965
################################################################
# (c) christof.Damian@project-open.com
# klaus.hofeditz@project-open.com
# frank.bergmann@project-open.com
#
# v0.9 - USE WITH CAUTION
#
# Last changed: 2020-12-10
#
################################################################
# Required software:
# - pwgen for random password if not supplied
# - htpasswd2 to change the cvs passwd file, this comes with
# the apache2 rpm
################################################################
# Configuration changes required (not part of current dump)
# - Change SuppressHttpPort to 1
# - various update scripts
#
################################################################
echo "##########################################################"
echo "# Defaults and parameters"
echo "##########################################################"
#
# Parameters
#
DRY=0
PGPORT=5432
COM_FREELANCE=0
COM_FIN_REPORTS=0
COM_TRANS_REPORTS=0
BRANCH=master
TIMESTAMP=`date +"%Y-%m-%d.%H-%M-%S"`
GIT_SERVER=gitlab.project-open.net
GIT_BASE=https://$GIT_SERVER/project-open/
#
# Directories
#
PGDIR=/usr/bin
PGDUMP=/web/po51patches/pg_dump.5.1.0.0.0.enterprise.sql.gz
HOMEDUMP=/web/po51patches/home51.tgz
CONFFILE=/web/po51patches/config51.conf
PACKAGES_DIR=packages
NGINX_CONF_DIR=/etc/nginx/config.d
function usage () {
cat <<EOF
Usage: createserver.sh [options] user [password]
-h, --help help
-d, --dry dry run, no actions taken
-f, --freelance clone commercial freelance packages
-i, --finance clone commercial finance packages
-t, --translation clone commercial translation packages
-p, --port=port define port for webservice
-b, --branch=tag branch to clone (default=$BRANCH)
Examples:
createserver51.sh --port=80 --finance testuser secret
createserver51.sh -ifr -p 9000 anotheruser
EOF
exit
}
#
# option parsing
#
TEMP=`getopt -o dfitp:hrb: --long "dry,freelance,finance,translation,port:,help,branch" -n $0 -- "$@"`
if [ $? != 0 ] ; then
echo "wrong option..." >&2 ;
usage
fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-d|--dry)
DRY=1
shift
;;
-f|--freelance)
COM_FREELANCE=1
shift
;;
-i|--finance)
COM_FIN_REPORTS=1
shift
;;
-t|--translation)
PACKAGES_DIR=packages-trans
PGDUMP=dump_5_0_translation_with_commercial_packages.sql.gz
COM_TRANS_REPORTS=1
shift
;;
-p|--port)
PGPORT=$2
shift 2
;;
-b|--branch)
BRANCH=$2
shift 2
;;
-h|--help)
usage
;;
--) shift ; break ;;
*) echo "Internal error!"; exit ;;
esac
done
POUSER=$1
POPASS=$2
if test "$POUSER" = ""; then
usage
fi
if test "$POPASS" = ""; then
if ! test -x "/usr/bin/pwgen"; then
echo "pwgen not found, please specify a password"
usage
fi
POPASS=`pwgen 8 1`
fi
# find the directory which contains the inital home tar and sql dump
tmp=`dirname $0`
SCRIPTDIR=`( cd $tmp; pwd )`
cat <<EOF
creating server for '$POUSER'
dry-run : $DRY
password : $POPASS
home : $HOMEDIR
port : $PGPORT
freelance : $COM_FREELANCE
finance : $COM_FIN_REPORTS
translation : $COM_TRANS_REPORTS
branch : $BRANCH
scriptdir : $SCRIPTDIR
pgdump : $PGDUMP
timestamp : $TIMESTAMP
EOF
#
# sanity checks
#
if ! test -e $PGDUMP; then
echo "pgdump $PGDUMP doesn't exist"
exit
fi
echo ""
echo "##########################################################"
echo "# Create users / groups"
echo "##########################################################"
echo ""
HOMEDIR=/web/$POUSER
echo "- groupadd $POUSER"
echo "- useradd -m -g $POUSER -d $HOMEDIR $POUSER"
echo "- chown -R $POUSER:$POUSER $HOMEDIR"
if [ $DRY != 1 ] ; then
groupadd $POUSER
useradd -m -g $POUSER -d $HOMEDIR $POUSER
# chown -R $POUSER:$POUSER $HOMEDIR
fi
# echo "- /bin/su --login $POUSER --command 'source $HOMEDIR/.bash_profile'"
# /bin/su --login $POUSER --command "source $HOMEDIR/.bash_profile"
# Check if the database exists and perform a backup
DB_EXISTS_P= `su - postgres -c "psql -lqt | cut -d \| -f 1 | grep $POUSER | wc -l"`
echo "- DB_EXISTS_P=$DB_EXISTS_P"
if [ $DB_EXISTS_P != 1 ] ; then
echo ""
echo "##########################################################"
echo "# Dropping DB"
echo "##########################################################"
echo ""
echo "- Creating a database backup - just in case..."
echo "- /bin/su --login $POUSER --command 'pg_dump --no-owner --clean --disable-dollar-quoting --format=p --file=$HOMEDIR/pg_dump.$TIMESTAMP.sql'"
/bin/su --login $POUSER --command "pg_dump --no-owner --clean --disable-dollar-quoting --format=p --file=$HOMEDIR/pg_dump.$TIMESTAMP.sql"
echo "- Creating database backup - just in case..."
echo "- dropping database $POUSER"
echo "- dropdb $POUSER"
dropdb $POUSER
fi
echo ""
echo "##########################################################"
echo "# creating db"
echo "##########################################################"
echo ""
echo "- /bin/su --login postgres --command "$PGDIR/createuser $POUSER""
echo "- /bin/su --login postgres --command '$PGDIR/createdb --owner $POUSER --encoding=utf8 $POUSER'"
echo "- /bin/su --login $POUSER --command 'zcat $PGDUMP | $PGDIR/psql --quiet --dbname $POUSER > $HOMEDIR/db-init.log 2>&1'"
if [ $DRY != 1 ] ; then
/bin/su --login postgres --command "$PGDIR/createuser $POUSER"
/bin/su --login postgres --command "$PGDIR/createdb --owner $POUSER --encoding=utf8 $POUSER"
echo "-
/bin/su --login $POUSER --command "zcat $PGDUMP | $PGDIR/psql --quiet --dbname $POUSER > $HOMEDIR/db-init.log 2>&1 "
fi
echo ""
echo "##########################################################"
echo "# Replacing database parameters"
echo "##########################################################"
echo ""
echo "- Set /web/*"
echo "- Set Localhost"
echo "- Fix issues"
if [ $DRY != 1 ] ; then
/bin/su --login $POUSER --command "$PGDIR/psql --quiet -c \"UPDATE apm_parameter_values SET attr_value=REPLACE(attr_value,'/web/projop/','/web/$POUSER/') WHERE attr_value LIKE '/web/%';\"";
/bin/su --login $POUSER --command "$PGDIR/psql --quiet -c \"UPDATE apm_parameter_values SET attr_value='http://$POUSER.project-open.net' where parameter_id in (select parameter_id from apm_parameters where package_key = 'acs-kernel' and parameter_name = 'SystemURL');\"";
/bin/su --login $POUSER --command "$PGDIR/psql --quiet -c \"update im_menus set (url, label, parent_menu_id) = ('/intranet-reporting/timesheet-productivity-calendar-view-workdays-simple.tcl', 'timesheet-productivity-calendar-view-workdays-simple', 25975) where menu_id = 30178;\"";
fi
echo ""
echo "##########################################################"
echo "# Create dir/files"
echo "##########################################################"
echo ""
echo "- unpacking home directory"
echo "- tar -zxf $HOMEDUMP -C $HOMEDIR"
echo ""
echo "- creating ~/packages"
echo "- mv $HOMEDIR/packages $HOMEDIR/packages.$TIMESTAMP"
echo "- ln -s /web/po51patches/$PACKAGES_DIR/ $HOMEDIR/packages"
if [ $DRY != 1 ] ; then
tar -zxf $HOMEDUMP -C $HOMEDIR
mv $HOMEDIR/packages $HOMEDIR/packages.$TIMESTAMP
ln -s /web/po51patches/$PACKAGES_DIR/ $HOMEDIR/packages
fi
echo ""
echo "##########################################################"
echo "# Create config: NaviServer: ~/etc/config.tcl"
echo "##########################################################"
echo ""
echo "- sed 's/@POUSER@/$POUSER/g; s/@PGPORT@/$PGPORT/g;' < $CONFFILE > /web/$POUSER/etc/config.tcl"
if [ $DRY != 1 ] ; then
sed "s/@POUSER@/$POUSER/g; s/@PGPORT@/$PGPORT/g;" < $CONFFILE > /web/$POUSER/etc/config.tcl
fi
echo ""
echo "##########################################################"
echo "# Creating config: NGINX: /etc/nginx/conf.d/$POUSER.conf"
echo "##########################################################"
echo ""
echo "- creating config in $NGINX_CONF_DIR/$POUSER.conf"
echo "- chmod 644 $NGINX_CONF_DIR/$POUSER.conf"
echo "- systemctl restart nginx"
if [ $DRY != 1 ] ; then
cat > $NGINX_CONF_DIR/$POUSER.conf <<EOF
server {
listen 80;
listen 443 ssl;
server_name ponet $POUSER.*;
location / {
proxy_pass http://127.0.0.1:$PORT;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header Host \$host;
client_max_body_size 1024M;
}
ssl_certificate /etc/nginx/certificates/fullchain.pem;
ssl_certificate_key /etc/nginx/certificates/privkey.pem;
error_page 500 502 503 504 /err/50x.html;
error_page 404 /err/404.html;
location /err/ {
root /etc/nginx/html;
}
rewrite_log on;
# if (\$scheme != "https") { return 301 https://\$host\$request_uri; }
}
EOF
systemctl restart nginx
fi
echo ""
echo "##########################################################"
echo "# Changing ownership of $HOMEDIR"
echo "##########################################################"
echo ""
echo "- changing home directory ownership"
echo "- chown -R $POUSER:$POUSER $HOMEDIR"
if [ $DRY != 1 ] ; then
chown -R $POUSER:$POUSER $HOMEDIR
fi
echo ""
echo "##########################################################"
echo "# Enable and start service using systemctl"
echo "##########################################################"
echo ""
echo "- systemctl enable po@$POUSER"
echo "- systemctl start po@$POUSER"
if [ $DRY != 1 ] ; then
systemctl enable po@$POUSER
systemctl start po@$POUSER
fi
echo ""
echo "##########################################################"
echo "# Finished"
echo "##########################################################"
echo ""
exit 0
################################################################
# v0.1 - USE WITH CAUTION
# Last changed: 2016-10-12
################################################################
#
# Drops ]po[ 4.x and ]po[ 5.x servers
#
################################################################
# ToDo:
################################################################
# defaults
# function usage () {
# cat <<EOF
# Usage: createserver.sh [options] user [password]
# -h, --help help
# Example: dropserver.sh worldbank
# EOF
# exit
# }
# Must run as root so that we can shutdown backuppc and mount drives
if [ $(whoami) != "root" ]; then
echo "You need to run this script as root."
echo "Use 'sudo ./$script_name' then enter your password when prompted."
exit 1
fi
POUSER=$1
if [ $? != 0 ] ; then
echo "wrong option..." >&2 ;
# usage
fi
if test "$POUSER" = ""; then
echo "param missing"
# usage
fi
#
# directories
#
WEBDIR=/web
HOMEDIR=$WEBDIR/$POUSER
SERVICEDIR=/web/service/$POUSER
GRAVEYARD=/web/garbage/backup_canceled_servers
echo "drop server for '$POUSER'"
read -p "Continue (y/n)?"
if [ $REPLY == "n" ]; then
exit 1
fi
echo "Remove ]po[ v4 (PostgreSQL 8.4) or v5 (PostgreSQL 9.x)? "
echo "]po[ Server (4/5)?"
read pgversion
if [ $pgversion = 4 ]; then
echo "Setting executables for PostgreSQL 8.4"
PGDUMP=/usr/bin/pg_dump
DROPDB=/usr/bin/dropdb
DROPUSER=/usr/bin/dropuser
elif [ $pgversion = 5 ]; then
echo "Setting executables for PostgreSQL 9.x"
PGDUMP="/usr/local/pgsql95/bin/pg_dump -p 5433"
DROPDB="/usr/local/pgsql95/bin/dropdb -p 5433"
DROPUSER="/usr/bin/dropuser -p 5433"
else
echo "Unable to set executables. Quitting"
exit 1
fi
# Alternatives:
# PGDUMP="/usr/local/pgsql/bin/pg_dump -p 5433"
# DROPDB="/usr/local/pgsql/bin/dropdb -p 5433"
# DROPUSER="/usr/local/pgsql/bin/dropuser -p 5433"
NOW=$(date +"%y%m%d%H%M%S")
echo "Creating unique folder in /tmp: $NOW"
mkdir /tmp/$NOW
if ! test -e $SCRIPTDIR/$DBDUMP; then
echo "$SCRIPTDIR/$DBDUMP doesn't exist (SCRIPTDIR=$SCRIPTDIR)"
exit
fi
# shut down service
# svc -d /web/service/$POUSER
# delete old backups & dump database
echo ""
echo "Now Shut down server ..."
echo "svc -d /web/service/$POUSER"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
svc -d /web/service/$POUSER
echo "server shut down"
fi
echo ""
echo "Now move old backups to temp folder ...."
echo "mv /web/$POUSER/filestorage/backup/*.sql /tmp/$NOW/"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
mv /web/$POUSER/filestorage/backup/*.sql /tmp/$NOW/
echo "moved files to tmp folder"
fi
echo ""
echo "Creating DB dump:"
echo "/bin/su --login $POUSER --command $PGDUMP --no-owner --clean --disable-dollar-quoting --format=p --file=/web/$POUSER/filestorage/backup/pg_dump.aachen.project-open.net.$POUSER.final.sql"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
/bin/su --login $POUSER --command "$PGDUMP --no-owner --clean --disable-dollar-quoting --format=p --file=/web/$POUSER/filestorage/backup/pg_dump.aachen.project-open.net.$POUSER.final.sql"
echo "created new db_dump"
fi
echo ""
echo "Drop DB:"
echo "/bin/su --login $POUSER --command '$DROPDB $POUSER'"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
/bin/su --login $POUSER --command "$DROPDB $POUSER"
echo "dropped db"
fi
echo ""
echo "Now tar filstorage and move to grave yard ..."
echo "tar czvf $GRAVEYARD/filestorage_$POUSER.tgz /web/$POUSER/filestorage"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
tar czvf $GRAVEYARD/filestorage_$POUSER.tgz /web/$POUSER/filestorage
echo "Filestorage moved to grave yard"
fi
echo ""
echo "Now removing server:"
echo "mv /web/$POUSER /tmp/$NOW/"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
mv /web/$POUSER /tmp/$NOW/
echo "Moved server to tmp folder"
fi
echo ""
# echo "Now removing service ..."
# echo "mv /web/service/$POUSER /tmp/$NOW"
# read -p "Continue (y/n)?"
# if [ $REPLY == "y" ]; then
# mv /web/service/$POUSER /tmp/$NOW
# echo "Moved server to tmp folder"
# fi
# echo ""
echo "Now delete user ..."
echo "userdel $POUSER"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
userdel $POUSER
echo "User deleted"
fi
echo ""
echo " /bin/su --login postgres --command '$DROPUSER $POUSER'"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
/bin/su --login postgres --command "$DROPUSER $POUSER"
echo "db user dropped"
fi
echo "Now removing service ..."
echo "mv /web/service/$POUSER /tmp/$NOW/ttt"
read -p "Continue (y/n)?"
if [ $REPLY == "y" ]; then
mv /web/service/$POUSER /tmp/$NOW/ttt
echo "Moved service directory to tmp folder"
fi
echo ""
echo ""
echo ""
echo "*** Finished script ***"
echo ""
echo ""
exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment