diff --git a/features/src/mariadb/devcontainer-feature.json b/features/src/mariadb/devcontainer-feature.json index d716986c..e9b9befb 100644 --- a/features/src/mariadb/devcontainer-feature.json +++ b/features/src/mariadb/devcontainer-feature.json @@ -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" ] } diff --git a/features/src/mariadb/entrypoint.alpine.tpl b/features/src/mariadb/entrypoint.alpine.tpl new file mode 100755 index 00000000..c53d666b --- /dev/null +++ b/features/src/mariadb/entrypoint.alpine.tpl @@ -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 diff --git a/features/src/mariadb/entrypoint.deb.tpl b/features/src/mariadb/entrypoint.deb.tpl new file mode 100755 index 00000000..073f0ddd --- /dev/null +++ b/features/src/mariadb/entrypoint.deb.tpl @@ -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 & diff --git a/features/src/mariadb/install.sh b/features/src/mariadb/install.sh index 7f9469b5..40d3e0f0 100755 --- a/features/src/mariadb/install.sh +++ b/features/src/mariadb/install.sh @@ -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 @@ -34,6 +35,7 @@ fi . /etc/os-release : "${ID:=}" : "${ID_LIKE:=${ID}}" +ENTRYPOINT="" case "${ID_LIKE}" in "debian") @@ -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") @@ -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" ;; *) @@ -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!' diff --git a/features/src/mariadb/service-run.tpl b/features/src/mariadb/service-run.tpl index 2267eb8c..90cbf2d2 100755 --- a/features/src/mariadb/service-run.tpl +++ b/features/src/mariadb/service-run.tpl @@ -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}" diff --git a/features/test/mariadb/alpine-mcr.sh b/features/test/mariadb/alpine-mcr.sh new file mode 120000 index 00000000..a5954756 --- /dev/null +++ b/features/test/mariadb/alpine-mcr.sh @@ -0,0 +1 @@ +alpine-normal-nopersist.sh \ No newline at end of file diff --git a/features/test/mariadb/checks.sh b/features/test/mariadb/checks.sh index 02285306..0453255e 100755 --- a/features/test/mariadb/checks.sh +++ b/features/test/mariadb/checks.sh @@ -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 diff --git a/features/test/mariadb/scenarios.json b/features/test/mariadb/scenarios.json index d4639fbe..0deacb85 100644 --- a/features/test/mariadb/scenarios.json +++ b/features/test/mariadb/scenarios.json @@ -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" + ] } -} \ No newline at end of file +} diff --git a/features/test/mariadb/test.sh b/features/test/mariadb/test.sh index 332cd54a..e5df2a1d 100755 --- a/features/test/mariadb/test.sh +++ b/features/test/mariadb/test.sh @@ -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 diff --git a/features/test/mariadb/ubuntu-mcr.sh b/features/test/mariadb/ubuntu-mcr.sh new file mode 120000 index 00000000..a5954756 --- /dev/null +++ b/features/test/mariadb/ubuntu-mcr.sh @@ -0,0 +1 @@ +alpine-normal-nopersist.sh \ No newline at end of file