#!/bin/bash set -e . /usr/share/debconf/confmodule PACKAGE=`basename $0 .postinst` bold=`tput bold` normal=`tput sgr0` # Get the user input case "$1" in configure) db_get maharadocs/installdir DIRNAME=$RET db_get maharadocs/giturl GITURL=$RET db_get maharadocs/logdir LOGDIR=$RET db_get maharadocs/overwrite OVERWRITEDIR=$RET ;; *) echo "postinst called with unknown argument \`$1'" >&2 exit 1 ;; esac function update_venv { echo "Updating Python virtual environment:" # If the Python virtual environment doesn't exist, try creating it. if [ ! -d $DIRNAME/venv ]; then echo "Virtual environment missing, creating a new one." virtualenv $DIRNAME/venv || \ echo "${bold}*** ERROR: failed to create a virtual environment in $DIRNAME/venv${normal}" fi echo "${bold}Updating Python virtual environment...${normal}" . $DIRNAME/venv/bin/activate pip install --upgrade -r $DIRNAME/requirements.txt || \ echo "${bold}*** ERROR: pip install failed, you may need to use a wheelhouse.${normal}" deactivate } # Check if we seem to have the correct git contents, not very trustworthy # but better than no check :D if [ -d $DIRNAME ] then if grep -Fq "url = $GITURL" $DIRNAME/.git/config then # If we have what appears to be the correct git contents, # just quit the script cleanly and let the rest of the # packaging do its thing. echo "Data already exists pulling from the correct source." echo "Not doing any alteration of $DIRNAME." # Update the Python virtual environment first, though update_venv exit else # However if it looks wrong, check if its a-ok to overwrite if [ "$OVERWRITEDIR" == "ok" ] then echo "${bold}*******************************************************************${normal}" echo "${bold}* Deleting and re-fetching the Mahara manual code. The build dir *${normal}" echo "${bold}* will not be rebuilt until the cron job(s) run, so you probably *${normal}" echo "${bold}* want to run them manually after this installation. *${normal}" echo "${bold}*******************************************************************${normal}" # Buhbye! rm -rf $DIRNAME else # Throw an error stating that we don't feel right about # nuking the data we can see. echo "${bold}The directory '$DIRNAME' already exists." echo "We were not told to overwrite, so we will not." echo "Exiting now. Retry with the overwrite option.${normal}" exit fi fi fi # Make our lovely new install from the git address. # Despite all our checking, this can fail horribly after a long wait; # there's not really much we can do about that now. echo "Downloading the manual into $DIRNAME from git.mahara.org:" echo "$GITURL" echo "This may take a while..." git clone $GITURL $DIRNAME # Did we succeed? if [ -d $DIRNAME ] then # Yay! echo "Manual successfully downloaded into '$DIRNAME'." else # In theory, we should not be here. Throw an error if we did, guessing what might # have happened. Git _should_ have reported all failures to download, but if the # directory is missing, then _something_ went wrong. echo "${bold}The installation failed; the manual directory could not be found.${normal}" echo "Please check that:" echo "* The directory is indeed missing. If not, it is a packaging error." echo "* $GITURL is accessible and downloadable on your network. Find a friendly sysadmin." echo "If this appears to be a packaging error, report it to info@mahara.org with all" echo "the text output since you tried to install this. " exit -1 fi # Update the Python virtual environment update_venv # Make the sitelogs directory... mkdir -p /var/log/sitelogs/mahara-manual-sphinx echo "${bold}Setting up Apache configuration...${normal}" cat > /etc/apache2/sites-available/mahara-manual-sphinx.conf << EOF ServerAdmin maharahost@catalyst.net.nz ServerName manual.mahara.org DocumentRoot ${DIRNAME}/build/html Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all ErrorLog ${LOGDIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${LOGDIR}/access.log combined # Convert wild existing links from before we vacated readthedocs. RewriteEngine on RewriteRule ^(.*)/1.4_STABLE/(.*)$ $1/1.4/$2 RewriteRule ^(.*)/1.5_STABLE/(.*)$ $1/1.5/$2 RewriteRule ^/$ /en/16.10/ [R] EOF # Enable the site a2ensite mahara-manual-sphinx.conf # Restart apache # Nicked from http://www.ilestis.ch/2011/03/03/apache-auto-restart-if-config-check/ # Check current apachectl status # - configtest sends to str.err, so map it back to str.out CHECKCONF=`/usr/sbin/apachectl -t 2>&1` # httpd service HTTPDS='httpd' # Is Syntax OK: restart echo "Check syntax files..." if [ "$CHECKCONF" = "Syntax OK" ] then echo "- Config seems OK." echo "Restarting apache..." RESTARTCTL=`/usr/sbin/apachectl restart 2>&1 | grep -v "httpd not running, trying to start"` # If restart isn't empty, an error occured if [ "$RESTARTCTL" != "" ] then echo "Error on restart: $RESTARTCTL" echo "" fi # Check if apache is running successfully echo "Check if httpd is running...." if ps ax | grep -v grep | grep $HTTPDS > /dev/null then echo "- HTTPD is running" else echo "- Error: HTTPD isn't running!" fi else echo "Configcheck error: $CHECKCONF" echo "" fi