aegir3-hostmaster.postinst 9.12 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
AEGIRHOME="$(su aegir -s /bin/sh -c 'echo $HOME')"
28 29 30

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
40 41 42 43
        db_get "aegir/makefile"
        if [ ! -z "$RET" ]; then
            FLAGS="$FLAGS --makefile='$RET'"
        fi
44 45
        db_get "aegir/profile"
        if [ ! -z "$RET" ]; then
46 47 48 49
            PROFILE=$RET
            FLAGS="$FLAGS --profile='$PROFILE'"
          else
            PROFILE='hostmaster'
50
        fi
51 52 53 54 55 56
        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'"
57
            WEBSERVER="$RET"
58
        fi
59

60 61 62 63 64
        db_get "aegir/working-copy"
        if [ ! -z "$RET" ]; then
            FLAGS="$FLAGS --working-copy"
        fi

65
        # make sure the configuration file exists before symlinking it in place (below)
66
        touch $AEGIRHOME/config/$WEBSERVER.conf
67
        # fix permissions on installed directories
68
        chown aegir:aegir "$AEGIRHOME" "$AEGIRHOME/config" "$AEGIRHOME/config/$WEBSERVER.conf"
69

70
        # flush the drush cache to find new commands
helmo's avatar
helmo committed
71
        su -s /bin/sh aegir -c 'drush cache-clear drush'
72

73 74
        site_uri=`su -s /bin/sh aegir -c 'drush @hostmaster status --fields="uri" --field-labels=0 2>/dev/null | tr "\n" " " | sed -e "s/^[[:space:]]*//g" -e "s/[[:space:]]*\$//g"'`
        drupal_root=`su -s /bin/sh aegir -c 'drush @hostmaster status --fields="root" --field-labels=0 2>/dev/null | tr "\n" " " | sed -e "s/^[[:space:]]*//g" -e "s/[[:space:]]*\$//g"'`
75

76
        if [ -d "$drupal_root" ]; then
77
            # upgrade
78
            db_stop
79
            echo "Aegir frontend (@hostmaster) site detected in $drupal_root"
80

helmo's avatar
helmo committed
81
            # determin target directory for the new hostmaster profile.
82 83 84 85 86
            if [ "$VERSION" = "7.x-3.x" ]; then
                NEW_PLATFORM="$AEGIRHOME/hostmaster-$VERSION-$(date +'%F-%H%M')"
            else
                NEW_PLATFORM="$AEGIRHOME/hostmaster-$VERSION"
            fi
helmo's avatar
helmo committed
87 88 89

            # make those paths canonical with readlink to make sure we can compare correctly
            NEW_PLATFORM=`readlink -f "$NEW_PLATFORM"`
90
            drupal_root=`readlink -f $drupal_root`
helmo's avatar
helmo committed
91 92

            # we upgrade only if the target platform doesn't exist *OR*
93
            # if it's not the current platform
94
            if [ -d "$NEW_PLATFORM" ] && [ "$drupal_root" = "$NEW_PLATFORM" ]; then
95 96
                echo "it seems to be the same version as the one we're trying to install, not upgrading"
            else
97
                echo "upgrading the frontend from $drupal_root to $NEW_PLATFORM"
98
                if su -s /bin/sh aegir -c 'drush @hostmaster pm-list --status=enabled --pipe' | grep -q hosting_queued; then
99 100
                    service hosting-queued stop
                fi
101
                cd "$drupal_root"
102
                su -s /bin/sh aegir -c "drush hostmaster-migrate $FLAGS '$site_uri' '$NEW_PLATFORM'"
103
                echo "upgrade finished, old platform left in $drupal_root"
104
                # restart daemon if enabled
105
                if su -s /bin/sh aegir -c 'drush @hostmaster pm-list --status=enabled --pipe' | grep -q hosting_queued; then
106 107
                    service hosting-queued start
                fi
108 109
            fi
        else
110
            # fresh install
111 112
            db_get "aegir/site"
            if [ ! -z "$RET" ]; then
113
                site_uri="$RET"
114 115 116
            fi
            db_get "aegir/db_host"
            AEGIR_DB_HOST="$RET"
117

118 119
            # Read /etc/os-release to properly handle differences between distributions
            OS_ID=$(grep ^ID /etc/os-release | cut -d= -f2)
120

121 122 123
            # Determin if we create a db accoount or ask for it.
            # Starting from MariaDB 10.1... https://www.drupal.org/node/2770819
            # TODO: Work with future versions
124
            if mysql --database=mysql --execute="show tables;" > /dev/null 2>&1
125
            then
126
              # MariaDB = 10.1
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
              CREATEUSER=1
            else
              # Mysql & MariaDB < 10.1
              CREATEUSER=0
            fi


            if [ $CREATEUSER = 0 ]; then
              db_get "aegir/db_user"
              AEGIR_DB_USER="$RET"
              db_get "aegir/db_password"
              AEGIR_DB_PASS="$RET"
              if [ -z "$AEGIR_DB_PASS" ]; then
                echo EMPTY database password, can not be good.
              fi
142
            fi
143

144 145 146 147
            db_get "aegir/email"
            EMAIL="$RET"

            db_go
148

149 150 151 152 153 154
            if [ $CREATEUSER = 0 ]; then
              # forget the DB password in debconf storage
              db_reset aegir/db_password || true
              db_fset aegir/db_password "seen" "true" || true
            fi

155
            db_stop
156 157 158 159 160 161 162 163 164

            if [ $CREATEUSER = 1 ]; then
              AEGIR_DB_USER="aegir_root"

              # Random password, will be stored in /var/aegir/.drush/server_localhost.alias.drushrc.php
              AEGIR_DB_PASS=$(openssl rand -base64 12)

              /usr/bin/mysql -e "GRANT ALL ON *.* TO '$AEGIR_DB_USER'@'localhost' IDENTIFIED BY '$AEGIR_DB_PASS' WITH GRANT OPTION"
            fi
165
            
166 167
            if [ -d $AEGIRHOME/.drush/provision ]; then
                echo "existing provision in $AEGIRHOME/.drush/provision detected, move away and try again"
168 169
                exit 1
            fi
170
            echo "installing the Aegir frontend (Drupal with the $PROFILE profile), please wait..."
171 172 173
            if [ "$DPKG_DEBUG" = "developer" ]; then
                DEBUG="--debug"
            fi
174

175
            # pass data through JSON for extra security
176
            su -s /bin/sh aegir -c "cd $AEGIRHOME && drush hostmaster-install $FLAGS --backend $site_uri 2>&1 | drush backend-parse $DEBUG" <<EOF
177 178 179 180 181 182 183 184
{ "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
helmo's avatar
helmo committed
185 186 187
            # flush the drush cache to find new commands
            su -s /bin/sh aegir -c 'drush cache-clear drush'

188 189
            # on new installs, we default to having the daemon enabled
            echo 'Enabling hosting-queued daemon'
190
            su -s /bin/sh aegir -c 'drush @hostmaster pm-enable -y hosting_queued'
191
            service hosting-queued start
192 193 194 195 196
            if [ -f /bin/systemctl ]; then
              # There must be a better way, but we're trying to stay compatible with Debian Wheezy and Jessie.
              # See https://www.drupal.org/node/2347557 for discussion.
              /bin/systemctl enable hosting-queued
            fi
197
        fi
198 199 200

        case $WEBSERVER in
            apache)
201 202 203 204 205 206 207
                if [ -d /etc/apache2/conf-enabled ]; then
                  # Apache 2.4
                  ln -sf $AEGIRHOME/config/$WEBSERVER.conf /etc/apache2/conf-enabled/aegir.conf
                else
                  # Apache 2.2
                  ln -sf $AEGIRHOME/config/$WEBSERVER.conf /etc/apache2/conf.d/aegir.conf
                fi
208 209 210 211
                a2enmod ssl rewrite
                apache2ctl graceful
                ;;
            nginx)
212
                ln -sf $AEGIRHOME/config/$WEBSERVER.conf /etc/nginx/conf.d/aegir.conf
213 214 215 216
                service nginx reload
                ;;
        esac

217 218 219
        # 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
220 221
            echo 'Use this URL to login on your new site:'
            su -s /bin/sh aegir -c 'drush @hostmaster uli'
222 223 224 225 226
        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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    ;;

    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