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

Commit fa758e4

Browse files
authored
feat: Add util for waiting for container startup; use in dbcopyall8 (#1198)
`make dev.dbcopyall8` doesn't work unless the mysql containers have been started and are ready to run. This is not the only place we need to wait for a functional check on a container, so in this commit I've gone ahead and started a utility for doing so. This first pass only supports MySQL, but we should at least add Mongo as well, as that's also needed in one of the provisioning scripts. I've moved the _db_copy8_targets from being an order-only prerequisite to being a recursive make call. I'm not sure why the order-only syntax was in use here, but in any case I needed to add in a script call before those targets were called, so it's likely moot. Also: - Drop unneeded `bash -c` wrappers from db copy docker exec calls
1 parent c396e1a commit fa758e4

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,14 @@ dev.dbshell:
461461

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

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

471474
dev.dbshell.%: ## Run a SQL shell on the given database.

provision.sh

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,40 +130,22 @@ fi
130130

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

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

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

151143
echo "${GREEN}Waiting for MySQL 5.7 to restart.${NC}"
152-
until docker compose exec -T mysql57 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
153-
do
154-
printf "."
155-
sleep 1
156-
done
157-
144+
./wait-ready.sh mysql57
158145
echo -e "${GREEN}MySQL5 ready.${NC}"
159146

160147
echo "${GREEN}Waiting for MySQL 8.0 to restart.${NC}"
161-
until docker compose exec -T mysql80 bash -e -c "mysql -uroot -se \"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')\"" &> /dev/null
162-
do
163-
printf "."
164-
sleep 1
165-
done
166-
148+
./wait-ready.sh mysql80
167149
echo -e "${GREEN}MySQL8 ready.${NC}"
168150

169151
# Ensure that the MySQL databases and users are created for all IDAs.

wait-ready.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# Wait for the listed services to become ready.
3+
#
4+
# This does not start the containers; that should be performed separately
5+
# via `make dev.up` in order to allow for parallel startup.
6+
7+
set -eu -o pipefail
8+
9+
function print_usage {
10+
echo "Usage: $0 service1 service2 ..."
11+
}
12+
13+
if [[ $# == 0 ]]; then
14+
print_usage
15+
exit 0
16+
fi
17+
18+
function wait_db {
19+
container_name="$1"
20+
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
21+
until docker compose exec -T "$container_name" mysql -uroot -se "$mysql_probe" &> /dev/null; do
22+
printf "." >&2
23+
sleep 1
24+
done
25+
echo >&2 "$container_name is ready"
26+
}
27+
28+
29+
for service_name in "$@"; do
30+
case "$service_name" in
31+
mysql*)
32+
wait_db "$service_name"
33+
;;
34+
# TODO: Add other services...
35+
*)
36+
echo >&2 "Unknown service: $service_name"
37+
exit 1
38+
;;
39+
esac
40+
done

0 commit comments

Comments
 (0)