Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

feat: Add util for waiting for container startup; use in dbcopyall8 #1198

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,14 @@ dev.dbshell:

DB_NAMES_LIST = credentials discovery ecommerce notes registrar xqueue edxapp edxapp_csmh dashboard analytics-api reports reports_v1
_db_copy8_targets = $(addprefix dev.dbcopy8.,$(DB_NAMES_LIST))
dev.dbcopyall8: | $(_db_copy8_targets) ## Copy data from old mysql 5.7 containers into new mysql8 dbs
dev.dbcopyall8: ## Copy data from old mysql 5.7 containers into new mysql8 dbs
$(MAKE) dev.up.mysql57+mysql80
./wait-ready.sh mysql57 mysql80
$(MAKE) $(_db_copy8_targets)

dev.dbcopy8.%: ## Copy data from old mysql 5.7 container into a new 8 db
docker compose exec mysql57 bash -c "mysqldump $*" > .dev/$*.sql
docker compose exec -T mysql80 bash -c "mysql $*" < .dev/$*.sql
docker compose exec mysql57 mysqldump "$*" > .dev/$*.sql
docker compose exec -T mysql80 mysql "$*" < .dev/$*.sql
rm .dev/$*.sql

dev.dbshell.%: ## Run a SQL shell on the given database.
Expand Down
26 changes: 4 additions & 22 deletions provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,22 @@ fi

# Ensure the MySQL5 server is online and usable
echo "${GREEN}Waiting for MySQL 5.7.${NC}"
until docker compose exec -T mysql57 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
do
printf "."
sleep 1
done
./wait-ready.sh mysql57

# Ensure the MySQL8 server is online and usable
echo "${GREEN}Waiting for MySQL 8.0.${NC}"
until docker compose exec -T mysql80 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
do
printf "."
sleep 1
done
./wait-ready.sh mysql80

# In the event of a fresh MySQL container, wait a few seconds for the server to restart
# See https://github.com/docker-library/mysql/issues/245 for why this is necessary.
sleep 10

echo "${GREEN}Waiting for MySQL 5.7 to restart.${NC}"
until docker compose exec -T mysql57 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
do
printf "."
sleep 1
done

./wait-ready.sh mysql57
echo -e "${GREEN}MySQL5 ready.${NC}"

echo "${GREEN}Waiting for MySQL 8.0 to restart.${NC}"
until docker compose exec -T mysql80 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
do
printf "."
sleep 1
done

./wait-ready.sh mysql80
echo -e "${GREEN}MySQL8 ready.${NC}"

# Ensure that the MySQL databases and users are created for all IDAs.
Expand Down
40 changes: 40 additions & 0 deletions wait-ready.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# Wait for the listed services to become ready.
#
# This does not start the containers; that should be performed separately
# via `make dev.up` in order to allow for parallel startup.

set -eu -o pipefail

function print_usage {
echo "Usage: $0 service1 service2 ..."
}

if [[ $# == 0 ]]; then
print_usage
exit 0
fi

function wait_db {
container_name="$1"
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
until docker compose exec -T "$container_name" mysql -uroot -se "$mysql_probe" &> /dev/null; do
printf "." >&2
sleep 1
done
echo >&2 "$container_name is ready"
}


for service_name in "$@"; do
case "$service_name" in
mysql*)
wait_db "$service_name"
;;
# TODO: Add other services...
*)
echo >&2 "Unknown service: $service_name"
exit 1
;;
esac
done
Loading