aegir-hostmaster2.postinst 6.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/sh
# postinst script for hostmaster
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# Source debconf library.
. /usr/share/debconf/confmodule

23
if [ "$DPKG_DEBUG" = "developer" ]; then
anarcat's avatar
anarcat committed
24 25 26
    set -x
fi

27 28 29 30
VARLIB="/var/aegir"

case "$1" in
    configure)
31 32
        # fetch the version number from provision. the line we're looking for looks like this
        # version=6.x-1.9
33 34 35
        # this obviously doesn't work for git releases
        VERSION=`sed -n '/^version/{s/^.*= *//;p}' /usr/share/drush/commands/provision/provision.info`

36
        FLAGS="--yes"
37
        if [ "$DPKG_DEBUG" = "developer" ]; then
38 39
            FLAGS="$FLAGS --debug"
        fi
anarcat's avatar
anarcat committed
40 41 42 43
        db_get "aegir/makefile"
        if [ ! -z "$RET" ]; then
            FLAGS="$FLAGS --makefile='$RET'"
        fi
anarcat's avatar
anarcat committed
44 45 46 47 48 49 50
        db_get "aegir/webserver"
        if [ ! -z "$RET" ]; then
            if [ "$RET" = 'apache2' ]; then
                RET='apache' # convert argument to something aegir can understand
            fi
            FLAGS="$FLAGS --http_service_type='$RET'"
        fi
51

52 53
        # flush the drush cache to find new commands
        su -s /bin/sh aegir -c 'drush cc drush'
54
        TEMPFILE=`tempfile`
55
        su -s /bin/sh aegir -c 'drush --pipe @hostmaster status 2>/dev/null | egrep "site_uri|drupal_root"' >> $TEMPFILE || true
56
        if grep -q 'site_uri' $TEMPFILE; then
57
            # upgrade
58
            db_stop
59 60
            . $TEMPFILE
            echo "Aegir frontend (@hostmaster) site detected in $drupal_root"
61 62 63
            # make those paths canonical to make sure we can compare correctly
            NEW_PLATFORM=`readlink -f "$VARLIB/hostmaster-$VERSION"`
            drupal_root=`readlink -f $drupal_root`
64 65
            # we upgrade only if the target platform doesn't exit *OR*
            # if it's not the current platform
66
            if [ -d "$NEW_PLATFORM" ] && [ "$drupal_root" = "$NEW_PLATFORM" ]; then
67 68
                echo "it seems to be the same version as the one we're trying to install, not upgrading"
            else
69
                echo "upgrading the frontend from $drupal_root to $NEW_PLATFORM"
70
                if su -s /bin/sh aegir -c 'drush @hostmaster pm-list --status=enabled --pipe' | grep -q hosting_queued; then
71 72
                    service hosting-queued stop
                fi
73
                cd "$drupal_root"
74
                su -s /bin/sh aegir -c "drush hostmaster-migrate $FLAGS '$site_uri' '$NEW_PLATFORM'"
75
                echo "upgrade finished, old platform left in $drupal_root"
76
                # restart daemon if enabled
77
                if su -s /bin/sh aegir -c 'drush @hostmaster pm-list --status=enabled --pipe' | grep -q hosting_queued; then
78 79
                    service hosting-queued start
                fi
80 81
            fi
        else
82
            # fresh install
83 84
            db_get "aegir/site"
            if [ ! -z "$RET" ]; then
85
                site_uri="$RET"
86 87 88 89 90 91 92 93 94 95 96
            fi
            db_get "aegir/db_host"
            AEGIR_DB_HOST="$RET"
            db_get "aegir/db_user"
            AEGIR_DB_USER="$RET"
            db_get "aegir/db_password"
            AEGIR_DB_PASS="$RET"
            db_get "aegir/email"
            EMAIL="$RET"

            db_go
anarcat's avatar
anarcat committed
97

98
            # forget the DB password in debconf storage
anarcat's avatar
anarcat committed
99 100
            db_reset aegir/db_password || true
            db_fset aegir/db_password "seen" "true" || true
101 102 103 104 105 106 107
            db_stop
            
            if [ -d $VARLIB/.drush/provision ]; then
                echo "existing provision in $VARLIB/.drush/provision detected, move away and try again"
                exit 1
            fi
            echo "installing the Aegir frontend (Drupal with the hostmaster profile), please wait..."
108 109 110
            if [ "$DPKG_DEBUG" = "developer" ]; then
                DEBUG="--debug"
            fi
111
            # pass data through JSON for extra security
112
            su -s /bin/sh aegir -c "cd $VARLIB && drush hostmaster-install $FLAGS --backend $site_uri 2>&1 | drush backend-parse $DEBUG" <<EOF
113 114 115 116 117 118 119 120
{ "yes": 1,
  "version": "$VERSION",
  "aegir_db_host": "$AEGIR_DB_HOST",
  "aegir_db_user": "$AEGIR_DB_USER",
  "aegir_db_pass": "$AEGIR_DB_PASS",
  "client_email": "$EMAIL"
}
EOF
121 122
            # on new installs, we default to having the daemon enabled
            echo 'Enabling hosting-queued daemon'
123
            su -s /bin/sh aegir -c 'drush @hostmaster pm-enable -y hosting_queued'
124
            service hosting-queued start
125 126
        fi
        rm -f $TEMPFILE
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

        # make sure we have apache configs settled in, for remote headless aegir servers
        touch $VARLIB/config/$WEBSERVER.conf
        chown aegir:aegir "$VARLIB/config/$WEBSERVER.conf"
        ln -sf $VARLIB/config/$WEBSERVER.conf /etc/apache2/conf.d/aegir.conf
        case $WEBSERVER in
            apache)
                a2enmod ssl rewrite
                apache2ctl graceful
                ;;
            nginx)
                service nginx reload
                ;;
        esac

142 143 144
        # this will ensure that this script aborts if the site can't be bootstrapped
        if su -s /bin/sh aegir -c 'drush @hostmaster status' 2>&1 | grep -q 'Drupal bootstrap.*Successful'; then
            echo 'Aegir frontend bootstrap correctly, operation was a success!'
anarcat's avatar
anarcat committed
145 146
            echo 'Use this URL to login on your new site:'
            su -s /bin/sh aegir -c 'drush @hostmaster uli'
147 148 149 150 151
        else
            echo 'Aegir frontend failed to bootstrap, something went wrong!'
            echo 'Look at the log above for clues or run with DPKG_DEBUG=developer'
            exit 1
        fi
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0