Skip to content

Commit

Permalink
Match services docker workflow.
Browse files Browse the repository at this point in the history
Fixes OUT-2367

There are now two scripts:
- docker_dev_setup.sh: will create / recreate the service
  and all databases locally
- docker_dev_update.sh: can be used to migrate the local
  master branch. Will also pull plugin changes and run the
  relevant migrations.

Both scripts now directly use parts of canvas_update under the hood,
which will hopefully make future workflow changes less painful.

Test Plan:
- With docker:
  - optional: create a new canvas checkout
    (you can use a different directory name to avoid destroying your
     current database)
  - git fetch
  - check out this gerrit, and use `git checkout -b 2367` to
    create a branch for it
  - git checkout origin/master~200
  - git checkout 2367 -- script
  - ./script/docker_dev_setup.sh (follow all prompts)
  - docker-compose up -d
  - login, accept the terms of use
  - docker-compose down
  - git checkout master
  - ./script/docker_dev_update.sh -n code
  - docker-compose up -d
  - login, verify you are not asked for terms of use and the school name
    is the same as before.
  - docker-compose down
  - ./script/docker_dev_setup.sh again (nuke the old database,
    change the root account name)
  - docker-compose up -d
  - login, accept the terms of use, verify the new account name

Change-Id: Ie40600d7ea1e90633d9139b4cc1cf853695ac8f8
Reviewed-on: https://gerrit.instructure.com/151547
Tested-by: Jenkins
Reviewed-by: Michael Brewer-Davis <mbd@instructure.com>
Reviewed-by: Augusto Callejas <acallejas@instructure.com>
QA-Review: Dariusz Dzien <ddzien@instructure.com>
Product-Review: Neil Gupta <ngupta@instructure.com>
  • Loading branch information
AnIrishDuck committed Aug 14, 2018
1 parent 6390aa0 commit 4159e09
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 52 deletions.
16 changes: 11 additions & 5 deletions doc/docker/developing_with_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ open http://web.canvaslms.docker
Normally you can just start everything with `docker-compose up`, and
access Canvas at http://web.canvaslms.docker/.

After pulling new code, you'll probably want to run migrations and
update assets:
After pulling new code, you'll want to update all your local gems, rebuild your
docker images, pull plugin code, run migrations, and recompile assets. This can
all be done with one command:

```
$ docker-compose run --rm web bundle update
$ docker-compose run --rm web bundle exec rake db:migrate
$ docker-compose run --rm web bundle exec rake canvas:compile_assets
./script/docker_dev_update.sh
```

Note that this command will pull `master` and all plugin code by default. If you
want to update without switching from your local `HEAD`, run:

```
./script/docker_dev_update.sh -n code
```

Changes you're making are not showing up? See the Caveats section below.
Expand Down
71 changes: 51 additions & 20 deletions script/canvas_update
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

# TODO: improve usage / help
function usage {
echo "usage: canvas_update [[[-q]] | [-h]]"
echo "usage: canvas_update [-qhv] [-n phase]"
}

function bad_usage {
usage
exit 1
}

function echo_console_and_log {
Expand Down Expand Up @@ -113,18 +118,30 @@ function tips {
function update_canvas {
ensure_in_canvas_root_directory
intro_message
update_plugins
rebase_canvas
bundle_install_with_check
rake_db_migrate_dev_and_test
yarn_install

# skip if QUICK_MODE
if ! $1; then
compile_assets

if [[ -z "$SKIP_CODE" ]] ; then
update_plugins
rebase_canvas
fi

if [[ -z "$SKIP_DEPS" ]] ; then
bundle_install_with_check
yarn_install
fi

if [[ -z "$SKIP_DATA" ]] ; then
rake_db_migrate_dev_and_test

# skip if QUICK_MODE
if ! $QUICK_MODE; then
compile_assets
fi
fi

tips
# We should only display tips if this script is being run standalone
if [[ -n "$SKIP_CODE" && -n "$SKIP_DEPS" && -n "$SKIP_DATA" ]] ; then
tips
fi
}

LOG="$(pwd)/log/canvas_update.log"
Expand All @@ -135,24 +152,38 @@ QUICK_MODE=false

# parse options
# http://www.tldp.org/LDP/abs/html/internal.html#EX33
while getopts ":qh" Option
while getopts ":qhn:v" Option
do
case $Option in
q )
echo "Quick mode enabled (assumes you have guard running and don't want to generate docs)"
QUICK_MODE=true;;
h )
PERFORM_UPDATE=false
usage;;
usage
exit 0;;
n )
case $OPTARG in
code )
SKIP_CODE=true;;
deps )
SKIP_DEPS=true;;
data )
SKIP_DATA=true;;
assets )
QUICK_MODE=true;;
* )
bad_usage;;
esac
echo "Skipping $OPTARG";;
v )
set -x
LOG=/dev/stdout;;
* )
PERFORM_UPDATE=false
echo "Sorry, that's not a valid option!"
usage;;
echo "Sorry, -$OPTARG is not a valid option!"
bad_usage;;
esac
done

if $PERFORM_UPDATE; then
update_canvas $QUICK_MODE
update_canvas
fi


54 changes: 27 additions & 27 deletions script/docker_dev_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ echo '
Welcome! This script will guide you through the process of setting up a
Canvas development environment with docker and dinghy/dory.
When you git pull new changes, you can run this script again to bring
everything up to date.'
When you git pull new changes, you can run ./scripts/docker_dev_update.sh
to bring everything up to date.'

if [[ "$USER" == 'root' ]]; then
echo 'Please do not run this script as root!'
Expand Down Expand Up @@ -195,9 +195,7 @@ function build_images {
docker-compose build --pull
}

function install_gems {
message 'Installing gems...'

function check_gemfile {
if [[ -e Gemfile.lock ]]; then
message \
'For historical reasons, the Canvas Gemfile.lock is not tracked by git. We may
Expand All @@ -214,18 +212,14 @@ permissions so we can install gems."
touch Gemfile.lock
confirm_command 'chmod a+rw Gemfile.lock' || true
fi

docker-compose run --no-deps --rm web bundle install --jobs 8
}

function database_exists {
docker-compose run --rm web \
bundle exec rails runner 'ActiveRecord::Base.connection' &> /dev/null
}

function prepare_database {
message 'Setting up the development database...'

function create_db {
if ! docker-compose run --no-deps --rm web touch db/structure.sql; then
message \
"The 'docker' user is not allowed to write to db/structure.sql. We need write
Expand All @@ -235,31 +229,37 @@ permissions so we can run migrations."
fi

if database_exists; then
message 'Database exists. Migrating...'
docker-compose run --rm web bundle exec rake db:migrate
else
message 'Database does not exist. Running initial setup...'
docker-compose run --rm web bundle exec rake db:create db:migrate db:initial_setup
message \
'An existing database was found.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This script will destroy ALL EXISTING DATA if it continues
If you want to migrate the existing database, use docker_dev_update.sh
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
message 'About to run "bundle exec rake db:drop"'
prompt "type NUKE in all caps: " nuked
[[ ${nuked:-n} == 'NUKE' ]] || exit 1
docker-compose run --rm web bundle exec rake db:drop
fi

message 'Setting up the test database...'
docker-compose run --rm web bundle exec rake db:create db:migrate RAILS_ENV=test
}

function compile_assets {
message 'Compiling assets...'
docker-compose run --rm web bundle exec rake \
canvas:compile_assets_dev \
brand_configs:generate_and_upload_all
message "Creating new database"
docker-compose run --rm web \
bundle exec rake db:create
docker-compose run --rm web \
bundle exec rake db:migrate
docker-compose run --rm web \
bundle exec rake db:initial_setup
}

function setup_canvas {
message 'Now we can set up Canvas!'
copy_docker_config
build_images
install_gems
compile_assets
prepare_database

check_gemfile
docker-compose run --rm web ./script/canvas_update -n code -n data
create_db
docker-compose run --rm web ./script/canvas_update -n code -n deps
}

function display_next_steps {
Expand Down
48 changes: 48 additions & 0 deletions script/docker_dev_update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash -e

usage () {
echo "usage: $0 [-f] [-h] [-n phase]"
}

bad_usage () {
usage
exit 1
}

while getopts ":fhn:" opt
do
case $opt in
n )
case $OPTARG in
build )
SKIP_BUILD=true;;
code )
SKIP_CODE=true;;
* )
bad_usage;;
esac
echo "Skipping $OPTARG";;
f )
FORCE=yes;;
h )
usage;;
* )
echo "Sorry, -$OPTARG is not a valid option!"
bad_usage;;
esac
done

if [[ -z "$FORCE" && "$(docker-compose ps | wc -l)" -gt 2 ]] ; then
echo "You should probably stop services before running this command"
echo "(use -f to skip this check)"
exit 1
fi

[[ -z "$SKIP_CODE" ]] && ./script/canvas_update -n data
[[ -z "$SKIP_BUILD" ]] && docker-compose build --pull
if [[ -z "$SKIP_BUILD" ]] ; then
# assets are currently compiled during dc build --pull
docker-compose run --rm web ./script/canvas_update -n code -n assets
else
docker-compose run --rm web ./script/canvas_update -n code
fi

0 comments on commit 4159e09

Please sign in to comment.