upgrade.sh 10.8 KB
Newer Older
1 2
#!/usr/bin/env bash

3 4 5 6 7 8
# Setting OPENEDX_DEBUG makes this more verbose.
if [[ $OPENEDX_DEBUG ]]; then
    set -x
fi

# Stop if any command fails.
9 10
set -e

Ned Batchelder committed
11 12 13 14
# Logging: write all the output to a timestamped log file.
sudo mkdir -p /var/log/edx
exec > >(sudo tee /var/log/edx/upgrade-$(date +%Y%m%d-%H%M%S).log) 2>&1

15
# defaults
16
CONFIGURATION="none"
17
TARGET=${OPENEDX_RELEASE-none}
18
INTERACTIVE=true
19
OPENEDX_ROOT="/edx"
20

Ned Batchelder committed
21 22 23 24 25 26 27 28
# Use this function to exit the script: it helps keep the output right with the
# exec-logging we started above.
exit_cleanly () {
  sleep .25
  echo
  exit $@
}

29 30 31
# check_pip succeeds if its first argument is found in the output of pip freeze.
PIP_EDXAPP="sudo -u edxapp -H $OPENEDX_ROOT/bin/pip.edxapp --disable-pip-version-check"
check_pip () {
32
  how_many=$($PIP_EDXAPP list 2>&- | grep -c "^$1 ")
33 34 35 36 37 38 39
  if (( $how_many > 0 )); then
    return 0
  else
    return 1
  fi
}

40
show_help () {
Ned Batchelder committed
41
  cat << EOM
42

Ned Batchelder committed
43
Upgrades your Open edX installation to a newer release.
44 45

-c CONFIGURATION
Ned Batchelder committed
46
    Use the given configuration. Either "devstack" or "fullstack". You
47
    must specify this.
Ned Batchelder committed
48

49
-t TARGET
50 51 52
    Upgrade to the given git ref. Open edX releases are called
    "open-release/eucalyptus.1", "open-release/eucalyptus.latest", and so on.
    Defaults to \$OPENEDX_RELEASE if it is defined.
Ned Batchelder committed
53

54
-y
Ned Batchelder committed
55 56
    Run in non-interactive mode (reply "yes" to all questions)

David Baumgold committed
57 58
-r OPENEDX_ROOT
    The root directory under which all Open edX applications are installed.
Ned Batchelder committed
59 60
    Defaults to "$OPENEDX_ROOT"

61 62
-h
    Show this help and exit.
63

64
EOM
65
}
66 67 68 69 70

# override defaults with options
while getopts "hc:t:y" opt; do
  case "$opt" in
    h)
71
      show_help
Ned Batchelder committed
72
      exit_cleanly 0
73 74 75 76 77 78 79 80 81 82
      ;;
    c)
      CONFIGURATION=$OPTARG
      ;;
    t)
      TARGET=$OPTARG
      ;;
    y)
      INTERACTIVE=false
      ;;
83 84 85
    r)
      OPENEDX_ROOT=$OPTARG
      ;;
86 87 88
  esac
done

89 90 91 92
# Helper to ask to proceed.
confirm_proceed () {
  echo "Do you wish to proceed?"
  read input
93
  if [[ "$input" != "yes" && "$input" != "y" ]]; then
94
    echo "Quitting"
Ned Batchelder committed
95
    exit_cleanly 1
96 97 98 99
  fi
}

# Check we are in the right place, and have the info we need.
Ned Batchelder committed
100
if [[ ! -d ${OPENEDX_ROOT}/app/edxapp ]]; then
101
  echo "Run this on your Open edX machine."
Ned Batchelder committed
102
  exit_cleanly 1
103 104 105 106
fi

if [[ $TARGET == none ]]; then
  cat <<"EOM"
107 108 109
You must specify a target. This should be the next Open edX release after the
one you are currently running.  This script can only move forward one release
at a time.
110 111
EOM
  show_help
Ned Batchelder committed
112
  exit_cleanly 1
113 114 115 116
fi

if [[ $CONFIGURATION == none ]]; then
  echo "You must specify a configuration, either fullstack or devstack."
Ned Batchelder committed
117
  exit_cleanly 1
118 119
fi

120 121 122 123 124
APPUSER=edxapp
if [[ $CONFIGURATION == fullstack ]] ; then
  APPUSER=www-data
fi

125 126
# Birch details

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
if [[ $TARGET == *birch* && $INTERACTIVE == true ]] ; then
  cat <<"EOM"
          WARNING WARNING WARNING WARNING WARNING
The Birch release of Open edX depends on MySQL 5.6 and MongoDB 2.6.4.
The Aspen release of Open edX depended on MySQL 5.5 and MongoDB 2.4.7.
Please make sure that you have already upgraded MySQL and MongoDB
before continuing.

If MySQL or MongoDB are not at the correct version, this script will
attempt to automatically upgrade them for you. However, this process
can fail, and IT RUNS THE RISK OF CORRUPTING ALL YOUR DATA.
Here there be dragons.

         .>   )\;`a__
        (  _ _)/ /-." ~~
         `( )_ )/
          <_  <_

Once you have verified that your MySQL and MongoDB versions are correct,
or you have decided to risk the automatic upgrade process, type "yes"
followed by enter to continue. Otherwise, press ctrl-c to quit. You can
also run this script with the -y flag to skip this check.

EOM
151
  confirm_proceed
152 153
fi

154 155
# Cypress details

156 157 158
if [[ $TARGET == *cypress* && $INTERACTIVE == true ]] ; then
  cat <<"EOM"
          WARNING WARNING WARNING WARNING WARNING
159 160
Due to the changes introduced between Birch and Cypress, you may encounter
some problems in this migration. If so, check this webpage for solutions:
161

162
https://openedx.atlassian.net/wiki/display/OpenOPS/Potential+Problems+Migrating+from+Birch+to+Cypress
163

164
EOM
165
  confirm_proceed
166 167
fi

168
if [[ $TARGET == *cypress* ]] ; then
169
  # Needed if transitioning to Cypress.
170 171 172 173 174 175 176 177 178 179 180 181 182 183
  echo "Killing all celery worker processes."
  sudo ${OPENEDX_ROOT}/bin/supervisorctl stop edxapp_worker:* &
  sleep 3
  # Supervisor restarts the process a couple of times so we have to kill it multiple times.
  sudo pgrep -lf celery | grep worker | awk '{ print $1}' | sudo xargs -I {} kill -9 {}
  sleep 3
  sudo pgrep -lf celery | grep worker | awk '{ print $1}' | sudo xargs -I {} kill -9 {}
  sleep 3
  sudo pgrep -lf celery | grep worker | awk '{ print $1}' | sudo xargs -I {} kill -9 {}
  sleep 3
  sudo pgrep -lf celery | grep worker | awk '{ print $1}' | sudo xargs -I {} kill -9 {}
  sudo -u forum git -C ${OPENEDX_ROOT}/app/forum/.rbenv reset --hard
fi

Ned Batchelder committed
184
if [[ -f ${OPENEDX_ROOT}/app/edx_ansible/server-vars.yml ]]; then
David Baumgold committed
185
  SERVER_VARS="--extra-vars=\"@${OPENEDX_ROOT}/app/edx_ansible/server-vars.yml\""
186 187
fi

188 189 190 191 192 193
# When tee'ing to a log, ansible (like many programs) buffers its output. This
# makes it hard to tell what is actually happening during the upgrade.
# "stdbuf -oL" will run ansible with line-buffered stdout, which makes the
# messages scroll in the way people expect.
ANSIBLE_PLAYBOOK="sudo stdbuf -oL ansible-playbook --inventory-file=localhost, --connection=local "

194 195 196 197 198 199 200
make_config_venv () {
  virtualenv venv
  source venv/bin/activate
  pip install -r configuration/pre-requirements.txt
  pip install -r configuration/requirements.txt
}

201
TEMPDIR=`mktemp -d`
202
echo "Working in $TEMPDIR"
203 204
chmod 777 $TEMPDIR
cd $TEMPDIR
205 206 207 208 209 210 211 212 213
# Set the CONFIGURATION_TARGET environment variable to use a different branch
# in the configuration repo, defaults to $TARGET.
git clone https://github.com/edx/configuration.git \
  --depth=1 --single-branch --branch=${CONFIGURATION_TARGET-$TARGET}
make_config_venv

# Dogwood details

if [[ $TARGET == *dogwood* ]] ; then
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  # Run the forum migrations.
  cat > migrate-008-context.js <<"EOF"
    // from: https://github.com/edx/cs_comments_service/blob/master/scripts/db/migrate-008-context.js
    print ("Add the new indexes for the context field");
    db.contents.ensureIndex({ _type: 1, course_id: 1, context: 1, pinned: -1, created_at: -1 }, {background: true})
    db.contents.ensureIndex({ _type: 1, commentable_id: 1, context: 1, pinned: -1, created_at: -1 }, {background: true})

    print ("Adding context to all comment threads where it does not yet exist\n");
    var bulk = db.contents.initializeUnorderedBulkOp();
    bulk.find( {_type: "CommentThread", context: {$exists: false}} ).update(  {$set: {context: "course"}} );
    bulk.execute();
    printjson (db.runCommand({ getLastError: 1, w: "majority", wtimeout: 5000 } ));
EOF

  mongo cs_comments_service migrate-008-context.js

230
  # We are upgrading Python from 2.7.3 to 2.7.10, so remake the venvs.
Ned Batchelder committed
231
  sudo rm -rf ${OPENEDX_ROOT}/app/*/v*envs/*
232 233 234

  echo "Upgrading to the end of Django 1.4"
  cd configuration/playbooks/vagrant
235
  $ANSIBLE_PLAYBOOK \
236 237 238 239 240 241 242 243 244 245 246 247 248
    $SERVER_VARS \
    --extra-vars="edx_platform_version=release-2015-11-09" \
    --extra-vars="xqueue_version=named-release/cypress" \
    --extra-vars="migrate_db=yes" \
    --skip-tags="edxapp-sandbox" \
    vagrant-$CONFIGURATION-delta.yml
  cd ../../..

  # Remake our own venv because of the Python 2.7.10 upgrade.
  rm -rf venv
  make_config_venv

  # Need to get rid of South from edx-platform, or things won't work.
249
  $PIP_EDXAPP uninstall -y South
250 251 252

  echo "Upgrading to the beginning of Django 1.8"
  cd configuration/playbooks/vagrant
253
  $ANSIBLE_PLAYBOOK \
254
    $SERVER_VARS \
255
    --extra-vars="edx_platform_version=dogwood-first-18" \
256 257 258 259 260 261 262 263
    --extra-vars="xqueue_version=dogwood-first-18" \
    --extra-vars="migrate_db=no" \
    --skip-tags="edxapp-sandbox" \
    vagrant-$CONFIGURATION-delta.yml
  cd ../../..

  echo "Running the Django 1.8 faked migrations"
  for item in lms cms; do
Ned Batchelder committed
264 265
    sudo -u $APPUSER -E ${OPENEDX_ROOT}/bin/python.edxapp \
      ${OPENEDX_ROOT}/bin/manage.edxapp $item migrate --settings=aws --noinput --fake-initial
266 267 268 269 270
  done

  if [[ $CONFIGURATION == fullstack ]] ; then
    sudo -u xqueue \
    SERVICE_VARIANT=xqueue \
Ned Batchelder committed
271 272
    ${OPENEDX_ROOT}/app/xqueue/venvs/xqueue/bin/python \
    ${OPENEDX_ROOT}/app/xqueue/xqueue/manage.py migrate \
273 274 275 276
    --settings=xqueue.aws_settings --noinput --fake-initial
  fi
fi

Ned Batchelder committed
277 278 279
# Eucalyptus details

if [[ $TARGET == *eucalyptus* ]] ; then
280 281
  if check_pip edx-oauth2-provider ; then
    echo "Uninstall edx-oauth2-provider"
282 283 284 285 286
    $PIP_EDXAPP uninstall -y edx-oauth2-provider
  fi
  if check_pip django-oauth2-provider ; then
    echo "Uninstall django-oauth2-provider"
    $PIP_EDXAPP uninstall -y django-oauth2-provider
287 288 289 290 291 292 293 294 295 296 297 298
  fi

  # edx-milestones changed how it was installed, so it is possible to have it
  # installed twice.  Try to uninstall it twice.
  if check_pip edx-milestones ; then
    echo "Uninstall edx-milestones"
    $PIP_EDXAPP uninstall -y edx-milestones
  fi
  if check_pip edx-milestones ; then
    echo "Uninstall edx-milestones again"
    $PIP_EDXAPP uninstall -y edx-milestones
  fi
Ned Batchelder committed
299 300 301 302 303 304 305 306

  if [[ $CONFIGURATION == devstack ]] ; then
    echo "Remove old Firefox"
    sudo apt-get purge -y firefox
  fi

  echo "Upgrade the code"
  cd configuration/playbooks/vagrant
307
  $ANSIBLE_PLAYBOOK \
Ned Batchelder committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    $SERVER_VARS \
    --extra-vars="edx_platform_version=$TARGET" \
    --extra-vars="xqueue_version=$TARGET" \
    --extra-vars="migrate_db=no" \
    --skip-tags="edxapp-sandbox,gather_static_assets" \
    vagrant-$CONFIGURATION-delta.yml
  cd ../../..

  echo "Migrate to fix oauth2_provider"
  ${OPENEDX_ROOT}/bin/edxapp-migrate-lms --fake oauth2_provider zero
  ${OPENEDX_ROOT}/bin/edxapp-migrate-lms --fake-initial

  echo "Clean up forums Ruby detritus"
  sudo rm -rf ${OPENEDX_ROOT}/app/forum/.rbenv ${OPENEDX_ROOT}/app/forum/.gem
fi

# Update to target.

326
echo "Updating to final version of code"
327 328
cd configuration/playbooks
echo "edx_platform_version: $TARGET" > vars.yml
329 330 331
echo "certs_version: $TARGET" >> vars.yml
echo "forum_version: $TARGET" >> vars.yml
echo "xqueue_version: $TARGET" >> vars.yml
332
echo "demo_version: $TARGET" >> vars.yml
333 334 335 336
echo "NOTIFIER_VERSION: $TARGET" >> vars.yml
echo "ECOMMERCE_VERSION: $TARGET" >> vars.yml
echo "ECOMMERCE_WORKER_VERSION: $TARGET" >> vars.yml
echo "PROGRAMS_VERSION: $TARGET" >> vars.yml
337
$ANSIBLE_PLAYBOOK \
338
    --extra-vars="@vars.yml" \
339 340
    $SERVER_VARS \
    vagrant-$CONFIGURATION.yml
341 342
cd ../..

Ned Batchelder committed
343 344
# Post-upgrade work.

345 346
if [[ $TARGET == *dogwood* ]] ; then
  echo "Running data fixup management commands"
Ned Batchelder committed
347 348
  sudo -u $APPUSER -E ${OPENEDX_ROOT}/bin/python.edxapp \
    ${OPENEDX_ROOT}/bin/manage.edxapp lms --settings=aws generate_course_overview --all
349

Ned Batchelder committed
350 351
  sudo -u $APPUSER -E ${OPENEDX_ROOT}/bin/python.edxapp \
    ${OPENEDX_ROOT}/bin/manage.edxapp lms --settings=aws post_cohort_membership_fix --commit
352 353 354 355 356

  # Run the forums migrations again to catch things made while this script
  # was running.
  mongo cs_comments_service migrate-008-context.js
fi
357

358
cd /
359
sudo rm -rf $TEMPDIR
Ned Batchelder committed
360
echo "Upgrade complete. Please reboot your machine."