upgrade.sh.txt 6.51 KB
Newer Older
1
#!/bin/sh
2 3 4 5 6 7 8 9 10

########################################################################
# Aegir quick upgrade script
#
# This script also *DOES NOT CHECK* if the requirements have been met.
# It's up to the admin to follow the proper upgrade instructions or use
# the packages provided by their platform.
########################################################################

helmo's avatar
helmo committed
11
# Basic variables, change before running.
12
AEGIR_VERSION="7.x-3.x"
13
NEW_DRUSH_VERSION="stable"  # e.g stable, unstable, ignore or a version number like 8.1.3
14

15
# Extra variables, no changes needed for most use-cases.
16
DRUSH_DIR=$HOME/drush
17
DRUSH=$DRUSH_DIR/drush.php
helmo's avatar
helmo committed
18
MIGRATE_OPTIONS="" # Optional: --working-copy
19

20 21 22 23 24 25 26
BACKUP_DIR=$HOME/pre-upgrade-$(date '+%F-%H%M')

if [ "$AEGIR_VERSION" = "7.x-3.x" ]; then
  DRUPAL_DIR=$HOME/hostmaster-${AEGIR_VERSION}-$(date +'%F-%H%M')
else
  DRUPAL_DIR=$HOME/hostmaster-$AEGIR_VERSION
fi
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

########################################################################
# Functions

# Indent printed messages.
msg() {
  echo "==> $*"
}

# simple prompt
prompt_yes_no() {
  while true ; do
    printf "$* [Y/n] "
    read answer
    if [ -z "$answer" ] ; then
      return 0
    fi
    case $answer in
      [Yy]|[Yy][Ee][Ss])
        return 0
        ;;
      [Nn]|[Nn][Oo])
        return 1
        ;;
      *)
        echo "Please answer yes or no"
        ;;
    esac
 done
}

########################################################################
# Preparation

61
# Test if we have Drush.
62 63
if which drush 2> /dev/null > /dev/null && which drush | grep -v 'no drush in' > /dev/null; then
  msg "Drush is in the path, good"
64
  # we do not set DRUSH to `which drush` because we're not sure how 'which' will behave
65
  DRUSH=drush
66 67 68 69 70 71
  DRUSH_DIR=`which drush`
  # yes, this will fail on non-GNU readlink, but we don't care - it
  # just means drush won't be upgraded if it's the path on those
  # platforms
  DRUSH_DIR=`readlink -f $DRUSH_DIR`
  DRUSH_DIR=`dirname $DRUSH_DIR`
72 73 74 75 76 77 78 79
elif [ -x $DRUSH ] ; then
  msg "Drush found in $DRUSH, good"
  DRUSH="php $DRUSH"
else
  msg "Could not find drush in $DRUSH or in $PATH"
  exit 1
fi

helmo's avatar
helmo committed
80
# Detect the current Drush version to work with.
81
CURRENT_DRUSH_VERSION=`drush --version --pipe`
82

83
case "$CURRENT_DRUSH_VERSION" in
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  5*)
  TEMPFILE=`mktemp`
  $DRUSH --pipe @hostmaster status | egrep "site_uri|drupal_root" >> $TEMPFILE || true
  if grep -q 'site_uri' $TEMPFILE; then
      # this sources the result of drush --pipe so we initialise shell variables used later
      . $TEMPFILE
  else
      msg 'could not find running hostmaster site'
      msg 'try running "drush @hostmaster status" to diagnose and repair'
      exit 1
  fi
  AEGIR_DOMAIN="$site_uri"
  OLD_DRUPAL_DIR="$drupal_root"
  ;;

99
  [6789]*)
helmo's avatar
helmo committed
100 101
  AEGIR_DOMAIN=`drush @hostmaster status --fields="uri" --field-labels=0 | sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'`
  OLD_DRUPAL_DIR=`drush @hostmaster status --fields="root" --field-labels=0 | sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'`
102 103

esac
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120


########################################################################
# Main script

AEGIR_HOST=`uname -n`
AEGIR_DOMAIN=${1:-$AEGIR_DOMAIN}

msg "Aegir $AEGIR_VERSION automated upgrade script"

if [ `whoami` = "root" ] ; then
  msg "This script should be run as the aegir user, not as root."
  exit 1
fi

msg "This script makes the following assumptions: "
cat <<EOF
helmo's avatar
helmo committed
121
 * you have read http://docs.aegirproject.org/en/3.x/install/upgrade/ and have prepared the server accordingly
122 123 124 125
 * you are executing this script as your "aegir" user
 * that the following settings are correct

The Aegir version to upgrade to is $AEGIR_VERSION
126
The Aegir home dir is $HOME
127 128 129 130
The new hostmaster platform will be $DRUPAL_DIR
Your Aegir domain is $AEGIR_DOMAIN
Your old hostmaster platform was $OLD_DRUPAL_DIR
The Drush command is "$DRUSH"
131
EOF
132

133
if [ "$NEW_DRUSH_VERSION" != "$CURRENT_DRUSH_VERSION" -o "$DRUSH_VERSION" != "ignore" ]; then
134 135
  if [ -w $DRUSH_DIR ]; then
    echo "The version of Drush will be $NEW_DRUSH_VERSION"
136 137 138 139 140 141 142 143 144
    if prompt_yes_no "Do you want to upgrade Drush to the specified PHAR package?" ; then
      true
    else
      echo "If you wish to upgrade Drush manually you can best abort now and start this script again when ready."
      echo

      # Set the version equal to not upgrade Drush later in this script.
      NEW_DRUSH_VERSION=$CURRENT_DRUSH_VERSION
    fi
145 146
  else
    echo "Drush will not be upgraded as its directory is not writable: $DRUSH_DIR"
147 148
    # Set the version equal to not upgrade Drush later in this script.
    NEW_DRUSH_VERSION=$CURRENT_DRUSH_VERSION
149
  fi
150
else
151
  msg "Not upgrading Drush, $CURRENT_DRUSH_VERSION will be used."
152 153
  # Set the version equal to not upgrade Drush later in this script.
  NEW_DRUSH_VERSION=$CURRENT_DRUSH_VERSION
154
fi
155 156 157 158 159 160 161 162 163 164

if prompt_yes_no "Do you want to proceed with the upgrade?" ; then
  true
else
  echo "Upgrade aborted by user."
  exit 1
fi

mkdir -p $BACKUP_DIR

165
# Upgrade drush if desired.
166
if [ "$NEW_DRUSH_VERSION" != "$CURRENT_DRUSH_VERSION" -o "$DRUSH_VERSION" != "ignore" ]; then
167 168 169 170
  if [ -d $DRUSH_DIR ]; then
    msg "Moving existing drush into $BACKUP_DIR"
    mv $DRUSH_DIR/drush $BACKUP_DIR
  fi
171

172 173 174
  # Cleanup composer Drush (< 8)
  if [ -f $HOME/.composer/vendor/bin/drush ]; then
    mv HOME/.composer/vendor/bin/drush HOME/.composer/vendor/bin/drush-old-$(date +'%Y-%m-%d')
175
  fi
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  DRUSH_MAJOR_VERSION=$(echo $NEW_DRUSH_VERSION| cut -d. -f1)

  if [ "$DRUSH_VERSION" = "stable" ]; then

      DRUSH_PHAR_URL="http://files.drush.org/drush.phar"

  elif [ "$DRUSH_VERSION" = "unstable" ]; then

      DRUSH_PHAR_URL="http://files.drush.org/drush-unstable.phar"

  elif [ "$DRUSH_MAJOR_VERSION" -ge "8" ]; then

      DRUSH_PHAR_URL="https://github.com/drush-ops/drush/releases/download/$NEW_DRUSH_VERSION/drush.phar"

  fi
192

193 194
  curl -SsL $DRUSH_PHAR_URL -o $DRUSH_DIR/drush
  chmod +x $DRUSH_DIR/drush
195

196
else
helmo's avatar
helmo committed
197
  msg "Not upgrading Drush, $CURRENT_DRUSH_VERSION will be used."
198 199
fi

helmo's avatar
helmo committed
200
# Fetch new version of provision.
201 202 203 204 205 206 207 208
# move existing provision
cd $HOME/.drush

if [ -d "provision" ] ; then
  msg "Moving existing provision into $BACKUP_DIR"
  mv provision $BACKUP_DIR
fi

209
if [ "$AEGIR_VERSION" = "7.x-3.x" ]; then
210
  git clone --branch "$AEGIR_VERSION" http://git.drupal.org/project/provision.git $HOME/.drush/provision
211
else
212 213 214
  wget http://ftp.drupal.org/files/projects/provision-$AEGIR_VERSION.tar.gz
  gunzip -c provision-$AEGIR_VERSION.tar.gz | tar -xf -
  rm provision-$AEGIR_VERSION.tar.gz
215 216
fi

helmo's avatar
helmo committed
217
# Clear the drush command cache.
218 219
drush cache-clear drush

helmo's avatar
helmo committed
220
# Start the actual upgrade of Aegir itself.
221
cd $OLD_DRUPAL_DIR
helmo's avatar
helmo committed
222
$DRUSH hostmaster-migrate $MIGRATE_OPTIONS $AEGIR_DOMAIN $DRUPAL_DIR
helmo's avatar
helmo committed
223 224

# All should be done.