Skip to content

Commit

Permalink
Merge pull request #12 from sergey-zabolotny/master
Browse files Browse the repository at this point in the history
Added healthcheck support. Updated entrypoint.sh for mysql 8.0
  • Loading branch information
lmakarov authored Nov 6, 2018
2 parents fe73d96 + 23b1b3d commit 5416825
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:
- VERSION=mysql-8.0

install:
- curl -fsSL get.docksal.io | sh
- curl -fsSL get.docksal.io | bash
- fin version
- fin sysinfo

Expand Down
7 changes: 6 additions & 1 deletion mysql-5.5/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FROM mysql:5.5
FROM mysql:5.5.61

# Docksal settings
COPY default.cnf /etc/mysql/conf.d/10-default.cnf

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /opt/healthcheck.sh

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]

# Health check script
HEALTHCHECK --interval=5s --timeout=1s --retries=12 CMD ["/opt/healthcheck.sh"]
5 changes: 5 additions & 0 deletions mysql-5.5/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

mysqladmin ping -u root --password=$MYSQL_ROOT_PASSWORD | grep -i 'mysqld is alive' || exit 1

exit 0
7 changes: 6 additions & 1 deletion mysql-5.6/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FROM mysql:5.6
FROM mysql:5.6.41

# Docksal settings
COPY default.cnf /etc/mysql/conf.d/10-default.cnf

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /opt/healthcheck.sh

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]

# Health check script
HEALTHCHECK --interval=5s --timeout=1s --retries=12 CMD ["/opt/healthcheck.sh"]
5 changes: 5 additions & 0 deletions mysql-5.6/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

mysqladmin ping -u root --password=$MYSQL_ROOT_PASSWORD | grep -i 'mysqld is alive' || exit 1

exit 0
7 changes: 6 additions & 1 deletion mysql-5.7/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FROM mysql:5.7
FROM mysql:5.7.24

# Docksal settings
COPY default.cnf /etc/mysql/conf.d/10-default.cnf

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /opt/healthcheck.sh

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]

# Health check script
HEALTHCHECK --interval=5s --timeout=1s --retries=12 CMD ["/opt/healthcheck.sh"]
5 changes: 5 additions & 0 deletions mysql-5.7/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

mysqladmin ping -u root --password=$MYSQL_ROOT_PASSWORD | grep -i 'mysqld is alive' || exit 1

exit 0
7 changes: 6 additions & 1 deletion mysql-8.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FROM mysql:8.0
FROM mysql:8.0.13

# Docksal settings
COPY default.cnf /etc/mysql/conf.d/10-default.cnf

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /opt/healthcheck.sh

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]

# Health check script
HEALTHCHECK --interval=5s --timeout=1s --retries=12 CMD ["/opt/healthcheck.sh"]
33 changes: 24 additions & 9 deletions mysql-8.0/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ file_env() {
unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

_check_config() {
toRun=( "$@" --verbose --help )
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
Expand All @@ -59,7 +77,9 @@ _check_config() {
# latter only show values present in config files, and not server defaults
_get_config() {
local conf="$1"; shift
"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }'
"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
| awk '$1 == "'"$conf"'" && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
}

# allow the container to be started with `--user`
Expand Down Expand Up @@ -152,7 +172,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
-- or products like mysql-fabric won't work
SET @@SESSION.SQL_LOG_BIN=0;
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
${rootCreate}
DROP DATABASE IF EXISTS test ;
Expand Down Expand Up @@ -182,14 +202,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
fi

echo
ls /docker-entrypoint-initdb.d/ > /dev/null
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
process_init_file "$f" "${mysql[@]}"
done

if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
Expand Down
5 changes: 5 additions & 0 deletions mysql-8.0/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

mysqladmin ping -u root --password=$MYSQL_ROOT_PASSWORD | grep -i 'mysqld is alive' || exit 1

exit 0
49 changes: 48 additions & 1 deletion tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,53 @@ teardown () {
echo "================================================================"
}

# Checks container health status (if available)
# @param $1 container id/name
_healthcheck ()
{
local health_status
health_status=$(docker inspect --format='{{json .State.Health.Status}}' "$1" 2>/dev/null)

# Wait for 5s then exit with 0 if a container does not have a health status property
# Necessary for backward compatibility with images that do not support health checks
if [[ $? != 0 ]]; then
echo "Waiting 10s for container to start..."
sleep 10
return 0
fi

# If it does, check the status
echo $health_status | grep '"healthy"' >/dev/null 2>&1
}

# Waits for containers to become healthy
# For reasoning why we are not using `depends_on` `condition` see here:
# https://github.com/docksal/docksal/issues/225#issuecomment-306604063
_healthcheck_wait ()
{
# Wait for cli to become ready by watching its health status
local container_name="${NAME}"
local delay=5
local timeout=30
local elapsed=0

until _healthcheck "$container_name"; do
echo "Waiting for $container_name to become ready..."
sleep "$delay";

# Give the container 30s to become ready
elapsed=$((elapsed + delay))
if ((elapsed > timeout)); then
echo-error "$container_name heathcheck failed" \
"Container did not enter a healthy state within the expected amount of time." \
"Try ${yellow}fin restart${NC}"
exit 1
fi
done

return 0
}

# Global skip
# Uncomment below, then comment skip in the test you want to debug. When done, reverse.
#SKIP=1
Expand All @@ -22,7 +69,7 @@ teardown () {

### Setup ###
make start
sleep 30 # TODO: replace with a healthcheck
_healthcheck_wait

### Tests ###
# MySQL does a restart, so there should be two of these in the logs after a successful start
Expand Down

0 comments on commit 5416825

Please sign in to comment.