diff --git a/script/common/canvas/build_helpers.sh b/script/common/canvas/build_helpers.sh index 288d926c54c..f433d8d9750 100755 --- a/script/common/canvas/build_helpers.sh +++ b/script/common/canvas/build_helpers.sh @@ -20,12 +20,13 @@ function compile_assets { } function build_images { - message 'Building docker images...' + start_spinner 'Building docker images...' if [[ "$(uname)" == 'Linux' && -z "${CANVAS_SKIP_DOCKER_USERMOD:-}" ]]; then - _canvas_lms_track docker-compose build --pull --build-arg USER_ID=$(id -u) + _canvas_lms_track_with_log docker-compose build --pull --build-arg USER_ID=$(id -u) else - _canvas_lms_track docker-compose build --pull + _canvas_lms_track_with_log docker-compose build --pull fi + stop_spinner } function check_gemfile { @@ -38,7 +39,7 @@ errors.' fi # Fixes 'error while trying to write to `/usr/src/app/Gemfile.lock`' - if ! docker-compose run --no-deps --rm web touch Gemfile.lock; then + if ! _canvas_lms_track_with_log docker-compose run --no-deps --rm web touch Gemfile.lock; then message \ "The 'docker' user is not allowed to write to Gemfile.lock. We need write permissions so we can install gems." @@ -49,9 +50,15 @@ permissions so we can install gems." function build_assets { message "Building assets..." - _canvas_lms_track docker-compose run --rm web ./script/install_assets.sh -c bundle - _canvas_lms_track docker-compose run --rm web ./script/install_assets.sh -c yarn - _canvas_lms_track docker-compose run --rm web ./script/install_assets.sh -c compile + start_spinner "> Bundle install..." + _canvas_lms_track_with_log docker-compose run --rm web ./script/install_assets.sh -c bundle + stop_spinner + start_spinner "> Yarn install...." + _canvas_lms_track_with_log docker-compose run --rm web ./script/install_assets.sh -c yarn + stop_spinner + start_spinner "> Compile assets...." + _canvas_lms_track_with_log docker-compose run --rm web ./script/install_assets.sh -c compile + stop_spinner } function database_exists { @@ -59,7 +66,7 @@ function database_exists { } function create_db { - if ! docker-compose run --no-deps --rm web touch db/structure.sql; then + if ! _canvas_lms_track_with_log 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 permissions so we can run migrations." @@ -67,7 +74,9 @@ permissions so we can run migrations." confirm_command 'chmod a+rw db/structure.sql' || true fi + start_spinner "Checking for existing db..." if database_exists; then + stop_spinner message \ 'An existing database was found. @@ -80,18 +89,26 @@ If you want to migrate the existing database, use docker_dev_update prompt "type NUKE in all caps: " nuked [[ ${nuked:-n} == 'NUKE' ]] || exit 1 fi - _canvas_lms_track docker-compose run --rm web bundle exec rake db:drop + start_spinner "Deleting db....." + _canvas_lms_track_with_log docker-compose run --rm web bundle exec rake db:drop + stop_spinner fi + stop_spinner - message "Creating new database" - _canvas_lms_track docker-compose run --rm web \ + start_spinner "Creating new database...." + _canvas_lms_track_with_log docker-compose run --rm web \ bundle exec rake db:create + stop_spinner # Rails db:migrate only runs on development by default # https://discuss.rubyonrails.org/t/db-drop-create-migrate-behavior-with-rails-env-development/74435 - _canvas_lms_track docker-compose run --rm web \ + start_spinner "Migrating (Development env)...." + _canvas_lms_track_with_log docker-compose run --rm web \ bundle exec rake db:migrate RAILS_ENV=development - _canvas_lms_track docker-compose run --rm web \ + stop_spinner + start_spinner "Migrating (Test env)...." + _canvas_lms_track_with_log docker-compose run --rm web \ bundle exec rake db:migrate RAILS_ENV=test + stop_spinner _canvas_lms_track docker-compose run -e TELEMETRY_OPT_IN --rm web \ bundle exec rake db:initial_setup } diff --git a/script/common/utils/common.sh b/script/common/utils/common.sh index 0dc0462588d..cf66c3db44d 100755 --- a/script/common/utils/common.sh +++ b/script/common/utils/common.sh @@ -1,4 +1,5 @@ #!/bin/bash +source script/common/utils/spinner.sh # This file contains commonly used BASH functions for scripting in canvas-lms, # particularly script/canvas_update and script/rebase_canvas_and_plugins . As such, @@ -22,10 +23,10 @@ function echo_console_and_log { echo "$1" |tee -a "$LOG" } -function print_results { +function trap_result { exit_code=$? set +e - + stop_spinner $exit_code if [ "${exit_code}" == "0" ]; then echo "" echo_console_and_log " \o/ Success!" @@ -35,7 +36,6 @@ function print_results { echo_console_and_log " /o\ Something went wrong. Check ${LOG} for details." _canvas_lms_telemetry_report_status $exit_code fi - exit ${exit_code} } @@ -76,8 +76,10 @@ function _canvas_lms_track_with_log { command="$@" if _canvas_lms_telemetry_enabled; then _inst_telemetry_with_log $command - else + elif [[ -z "${JENKINS}" ]]; then $command >> "$LOG" 2>&1 + else + $command fi } @@ -111,6 +113,7 @@ function _canvas_lms_telemetry_report_status() { if [[ ! -z ${1-} ]]; then exit_status=$1 fi + stop_spinner $exit_status if installed _inst_report_status && _canvas_lms_telemetry_enabled; then _inst_report_status $exit_status fi diff --git a/script/common/utils/dinghy_setup.sh b/script/common/utils/dinghy_setup.sh index 50ad9abeaa4..bd57b1101f6 100755 --- a/script/common/utils/dinghy_setup.sh +++ b/script/common/utils/dinghy_setup.sh @@ -37,7 +37,8 @@ function create_dinghy_vm { prompt "How big should the VM's disk be (in GB)? [$DINGHY_DISK]" disk message "OK let's do this." - _canvas_lms_track dinghy create \ + message "Creating dinghy machine..." + _canvas_lms_track_with_log dinghy create \ --provider=virtualbox \ --memory "${memory:-$DINGHY_MEMORY}" \ --cpus "${cpus:-$DINGHY_CPUS}" \ @@ -46,7 +47,8 @@ function create_dinghy_vm { function start_dinghy_vm { if dinghy status | grep -q 'stopped'; then - _canvas_lms_track dinghy up + message "Starting dinghy VM..." + _canvas_lms_track_with_log dinghy up else message 'Looks like the dinghy VM is already running. Moving on...' fi diff --git a/script/common/utils/spinner.sh b/script/common/utils/spinner.sh new file mode 100644 index 00000000000..19781edc7df --- /dev/null +++ b/script/common/utils/spinner.sh @@ -0,0 +1,86 @@ +# +# Copyright (C) 2021 - present Instructure, Inc. +# +# This file is part of Canvas. +# +# Canvas is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, version 3 of the License. +# +# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along +# with this program. If not, see . +# + +function _inst_spinner() { + + local SUCCESS="DONE" + local FAILURE="FAIL" + local RED='\e[31m' + local GREEN='\e[32m' + local YELLOW='\e[33m' + local NC='\e[0m' # No Color + local bold=$(tput bold) + local normal=$(tput sgr0) + + case $1 in + start) + printf "\n${bold}> ${@:2}${normal}" + if [[ ! -z ${LOG-} ]]; then + echo "${@:2}" >> "$LOG" + fi + printf "\t" + + i=1 + sp='\|/-' + delay=${SPINNER_DELAY:-0.15} + + while : + do + printf "\b${sp:i++%${#sp}:1}" + sleep $delay + done + ;; + stop) + if [[ -z ${3:-} ]]; then + # no pid to kill + return + fi + + kill $3 > /dev/null 2>&1 + + printf "\b[" + if [[ $2 -eq 0 ]]; then + printf "${GREEN}${SUCCESS}${NC}" + elif [[ $2 -eq 130 ]]; then + printf "${YELLOW}INTERRUPTED${NC}" + else + printf "${RED}${FAILURE}${NC}" + fi + printf "]\n" + ;; + *) + echo "invalid argument, try {start/stop}" + exit 1 + ;; + esac +} + +function start_spinner { + _inst_spinner "start" "$1" & + export _sp_pid=$! + disown +} + +function stop_spinner { + exit_status=$? + if [[ ! -z ${1-} ]]; then + exit_status=$1 + fi + _inst_spinner "stop" $exit_status ${_sp_pid:-} + unset _sp_pid +} diff --git a/script/docker_dev_setup.sh b/script/docker_dev_setup.sh index 5930ad0654a..bf5a08492fc 100755 --- a/script/docker_dev_setup.sh +++ b/script/docker_dev_setup.sh @@ -3,12 +3,14 @@ set -e source script/common/utils/common.sh source script/common/canvas/build_helpers.sh -trap '_canvas_lms_telemetry_report_status' ERR EXIT +trap 'trap_result' ERR EXIT +trap "printf '\nTerminated\n' && exit 130" SIGINT +LOG="$(pwd)/log/docker_dev_setup.log" SCRIPT_NAME=$0 OS="$(uname)" DOCKER='y' -_canvas_lms_opt_in_telemetry "$SCRIPT_NAME" +_canvas_lms_opt_in_telemetry "$SCRIPT_NAME" "$LOG" # shellcheck disable=1004 echo ' @@ -44,6 +46,8 @@ function setup_docker_environment { fi } +create_log_file +init_log_file "Docker Dev Setup" setup_docker_environment message 'Now we can set up Canvas!' copy_docker_config