maharadocs.postinst 6.18 KB
Newer Older
Melissa Draper's avatar
Melissa Draper committed
1 2 3 4 5 6 7 8 9
#!/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
10 11 12 13 14 15 16 17 18 19
    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
    ;;
Melissa Draper's avatar
Melissa Draper committed
20

21 22 23 24
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
Melissa Draper's avatar
Melissa Draper committed
25 26
esac

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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
}
Melissa Draper's avatar
Melissa Draper committed
42 43 44

# Check if we seem to have the correct git contents, not very trustworthy
# but better than no check :D
Melissa Draper's avatar
Melissa Draper committed
45
if [ -d $DIRNAME ]
Melissa Draper's avatar
Melissa Draper committed
46
then
47
    if grep -Fq "url = $GITURL" $DIRNAME/.git/config
48 49 50 51 52 53
    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."
54 55 56 57

        # Update the Python virtual environment first, though
        update_venv

58 59 60 61 62
        exit
    else
        # However if it looks wrong, check if its a-ok to overwrite
        if [ "$OVERWRITEDIR" == "ok" ]
        then
63 64 65 66 67
            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}"
68 69 70 71 72 73 74 75 76 77 78
            # 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
Melissa Draper's avatar
Melissa Draper committed
79 80 81
fi

# Make our lovely new install from the git address.
Melissa Draper's avatar
Melissa Draper committed
82
# Despite all our checking, this can fail horribly after a long wait;
Melissa Draper's avatar
Melissa Draper committed
83
# there's not really much we can do about that now.
84
echo "Downloading the manual into $DIRNAME from git.mahara.org:"
Melissa Draper's avatar
Melissa Draper committed
85 86 87 88 89
echo "$GITURL"
echo "This may take a while..."
git clone $GITURL $DIRNAME

# Did we succeed?
Melissa Draper's avatar
Melissa Draper committed
90
if [ -d $DIRNAME ]
Melissa Draper's avatar
Melissa Draper committed
91
then
92 93
    # Yay!
    echo "Manual successfully downloaded into '$DIRNAME'."
Melissa Draper's avatar
Melissa Draper committed
94
else
95 96 97 98 99 100 101 102 103 104
    # 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
Melissa Draper's avatar
Melissa Draper committed
105
fi
Melissa Draper's avatar
Melissa Draper committed
106

107 108
# Update the Python virtual environment
update_venv
109

Melissa Draper's avatar
Melissa Draper committed
110 111 112 113 114 115 116
# 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
<VirtualHost *:80>
117
        ServerAdmin maharahost@catalyst.net.nz
Melissa Draper's avatar
Melissa Draper committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        ServerName manual.mahara.org

        DocumentRoot ${DIRNAME}/build/html
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory ${DIRNAME}/build/html/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        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.
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^(.*)/1.4_STABLE/(.*)$ $1/1.4/$2
        RewriteRule ^(.*)/1.5_STABLE/(.*)$ $1/1.5/$2
145
        RewriteRule   ^/$  /en/16.10/  [R]
Melissa Draper's avatar
Melissa Draper committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    </IfModule>

</VirtualHost>
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