Skip to content

Commit

Permalink
Merge pull request #263 from Automattic/add-mariadb-ep-support
Browse files Browse the repository at this point in the history
feat(mariadb): add support for entrypoints
  • Loading branch information
sjinks authored Jun 22, 2024
2 parents 94fbfd5 + 5beddef commit 950c67a
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 13 deletions.
9 changes: 7 additions & 2 deletions features/src/mariadb/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"name": "MariaDB",
"id": "mariadb",
"version": "1.1.5",
"version": "1.2.0",
"description": "Sets up MariaDB into the Dev Environment",
"options": {
"installDatabaseToWorkspaces": {
"type": "boolean",
"default": false,
"description": "Set MariaDB data directory to /workspaces/mysql-data to persist data between container rebuilds (GHCS)"
},
"install-runit-service": {
"type": "boolean",
"default": true,
"description": "Whether to install a runit service for Mailpit"
}
},
"installsAfter": [
"ghcr.io/automattic/vip-codespaces/base"
"ghcr.io/automattic/vip-codespaces/entrypoints"
]
}
34 changes: 34 additions & 0 deletions features/src/mariadb/entrypoint.alpine.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

set -eu
exec 2>&1

export LD_PRELOAD=

# shellcheck disable=SC2154
install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}"
chown -R "${MARIADB_USER}:${MARIADB_USER}" "${MARIADB_DATADIR}"

install -d /run/mysqld -o "${MARIADB_USER}" -g "${MARIADB_USER}"

if [ ! -d "${MARIADB_DATADIR}/mysql" ]; then
mysql_install_db --auth-root-authentication-method=normal --skip-test-db --user="${MARIADB_USER}" --datadir="${MARIADB_DATADIR}"
fi

if [ -x /sbin/chpst ]; then
exec chpst -u "${MARIADB_USER}:${MARIADB_USER}" \
mysqld \
--datadir="${MARIADB_DATADIR}" \
--sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION \
--max_allowed_packet=67M \
--skip_networking=0 \
--bind-address=127.0.0.1 &
else
exec su-exec "${MARIADB_USER}:${MARIADB_USER}" \
mysqld \
--datadir="${MARIADB_DATADIR}" \
--sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION \
--max_allowed_packet=67M \
--skip_networking=0 \
--bind-address=127.0.0.1 &
fi
27 changes: 27 additions & 0 deletions features/src/mariadb/entrypoint.deb.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

set -eu
exec 2>&1

export LD_PRELOAD=

# shellcheck disable=SC2154
install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}"
chown -R "${MARIADB_USER}:${MARIADB_USER}" "${MARIADB_DATADIR}"

install -d /run/mysqld -o "${MARIADB_USER}" -g "${MARIADB_USER}"

if [ ! -d "${MARIADB_DATADIR}/mysql" ]; then
mysql_install_db --auth-root-authentication-method=normal --skip-test-db --user="${MARIADB_USER}" --datadir="${MARIADB_DATADIR}"
fi

MY_UID="$(id -u "${MARIADB_USER}")"
MY_GID="$(id -g "${MARIADB_USER}")"

exec setpriv --reuid="${MY_UID}" --regid="${MY_GID}" --inh-caps=-all --init-groups \
mysqld \
--datadir="${MARIADB_DATADIR}" \
--sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION \
--max_allowed_packet=67M \
--skip_networking=0 \
--bind-address=127.0.0.1 &
26 changes: 22 additions & 4 deletions features/src/mariadb/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ echo '(*) Installing MariaDB...'

: "${_REMOTE_USER:?"_REMOTE_USER is required"}"
: "${INSTALLDATABASETOWORKSPACES:=}"
: "${INSTALL_RUNIT_SERVICE:=true}"

if [ "${_REMOTE_USER}" = "root" ]; then
MARIADB_USER=mysql
Expand All @@ -34,6 +35,7 @@ fi
. /etc/os-release
: "${ID:=}"
: "${ID_LIKE:=${ID}}"
ENTRYPOINT=""

case "${ID_LIKE}" in
"debian")
Expand All @@ -58,6 +60,8 @@ case "${ID_LIKE}" in
rm -rf /var/lib/mysql/mysql
rm -f /var/lib/mysql/aria_log* /var/lib/mysql/ib*
fi

ENTRYPOINT="entrypoint.deb.tpl"
;;

"alpine")
Expand All @@ -66,8 +70,14 @@ case "${ID_LIKE}" in
PACKAGES="${PACKAGES} gettext"
fi

if [ ! -x /sbin/chpst ] && [ ! -x /sbin/su-exec ]; then
PACKAGES="${PACKAGES} su-exec"
fi

# shellcheck disable=SC2086
apk add --no-cache ${PACKAGES}

ENTRYPOINT="entrypoint.alpine.tpl"
;;

*)
Expand All @@ -84,9 +94,17 @@ fi
export MARIADB_USER
export MARIADB_DATADIR

install -D -d -m 0755 -o root -g root /etc/service /etc/sv/mariadb
# shellcheck disable=SC2016
envsubst '$MARIADB_USER $MARIADB_DATADIR' < service-run.tpl > /etc/sv/mariadb/run && chmod 0755 /etc/sv/mariadb/run
ln -sf /etc/sv/mariadb /etc/service/mariadb
if [ "${INSTALL_RUNIT_SERVICE}" = 'true' ] && [ -d /etc/sv ]; then
install -D -d -m 0755 -o root -g root /etc/service /etc/sv/mariadb
# shellcheck disable=SC2016
envsubst '$MARIADB_USER $MARIADB_DATADIR' < service-run.tpl > /etc/sv/mariadb/run && chmod 0755 /etc/sv/mariadb/run
ln -sf /etc/sv/mariadb /etc/service/mariadb
fi

if [ -d /var/lib/entrypoint.d ]; then
# shellcheck disable=SC2016
envsubst '$MARIADB_USER $MARIADB_DATADIR' < "${ENTRYPOINT}" > /var/lib/entrypoint.d/50-mariadb
chmod 0755 /var/lib/entrypoint.d/50-mariadb
fi

echo 'Done!'
2 changes: 2 additions & 0 deletions features/src/mariadb/service-run.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
set -eu
exec 2>&1

export LD_PRELOAD=

# shellcheck disable=SC2154
install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}"
chown -R "${MARIADB_USER}:${MARIADB_USER}" "${MARIADB_DATADIR}"
Expand Down
1 change: 1 addition & 0 deletions features/test/mariadb/alpine-mcr.sh
19 changes: 15 additions & 4 deletions features/test/mariadb/checks.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#!/bin/bash

check "mariadb is running" sudo sh -c 'sv status mariadb | grep -E ^run:'
sudo sv stop mariadb
check "mariadb is stopped" sudo sh -c 'sv status mariadb | grep -E ^down:'
sudo sv start mariadb
if hash sv >/dev/null 2>&1; then
check "mariadb is running" sudo sh -c 'sv status mariadb | grep -E ^run:'
sudo sv stop mariadb
check "mariadb is stopped" sudo sh -c 'sv status mariadb | grep -E ^down:'
sudo sv start mariadb
else
check "mariadb is running" pgrep mariadbd
fi

second=0
while ! mysqladmin ping -u root -h 127.0.0.1 --silent && [[ "${second}" -lt 60 ]]; do
sleep 1
second=$((second+1))
done
check "MariaDB is online" mysqladmin ping -u root -h 127.0.0.1 --silent

# Microsoft's base images contain zsh. We don't want to run this check for MS images because we have no control over the installed services.
if test -d /etc/rc2.d && ! test -e /usr/bin/zsh; then
Expand Down
29 changes: 28 additions & 1 deletion features/test/mariadb/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,32 @@
"installDatabaseToWorkspaces": true
}
}
},

"ubuntu-mcr": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"entrypoints": {},
"mariadb": {
"install-runit-service": false
}
},
"overrideFeatureInstallOrder": [
"./entrypoints",
"./mariadb"
]
},
"alpine-mcr": {
"image": "mcr.microsoft.com/devcontainers/base:alpine",
"features": {
"entrypoints": {},
"mariadb": {
"install-runit-service": false
}
},
"overrideFeatureInstallOrder": [
"./entrypoints",
"./mariadb"
]
}
}
}
6 changes: 4 additions & 2 deletions features/test/mariadb/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
source dev-container-features-test-lib

check "mysqld exists" which mysqld
check "/etc/sv/mariadb/run exists and is executable" test -x /etc/sv/mariadb/run
check "/etc/service/mariadb is a symlink" test -L /etc/service/mariadb
if [[ -d /etc/sv ]]; then
check "/etc/sv/mariadb/run exists and is executable" test -x /etc/sv/mariadb/run
check "/etc/service/mariadb is a symlink" test -L /etc/service/mariadb
fi

# Microsoft's base images contain zsh. We don't want to run this check for MS images because we have no control over the installed services.
if test -d /etc/rc2.d && ! test -e /usr/bin/zsh; then
Expand Down
1 change: 1 addition & 0 deletions features/test/mariadb/ubuntu-mcr.sh

0 comments on commit 950c67a

Please sign in to comment.