From 3c1611a8e9aac04235c493e88841dab0943d1016 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Fri, 31 May 2024 16:00:39 +0300 Subject: [PATCH] refactor(mariadb): support Debian and Ubuntu --- features/src/mariadb/conf-mariadb.tpl | 2 - .../src/mariadb/devcontainer-feature.json | 2 +- features/src/mariadb/install.sh | 61 ++++++++++++++++--- .../mariadb/{service-run => service-run.tpl} | 10 +-- 4 files changed, 59 insertions(+), 16 deletions(-) delete mode 100644 features/src/mariadb/conf-mariadb.tpl rename features/src/mariadb/{service-run => service-run.tpl} (59%) diff --git a/features/src/mariadb/conf-mariadb.tpl b/features/src/mariadb/conf-mariadb.tpl deleted file mode 100644 index 7d2f53a3..00000000 --- a/features/src/mariadb/conf-mariadb.tpl +++ /dev/null @@ -1,2 +0,0 @@ -MARIADB_DATADIR=${MARIADB_DATADIR} -MARIADB_USER=${MARIADB_USER} diff --git a/features/src/mariadb/devcontainer-feature.json b/features/src/mariadb/devcontainer-feature.json index 07c6773a..92224601 100644 --- a/features/src/mariadb/devcontainer-feature.json +++ b/features/src/mariadb/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "MariaDB", "id": "mariadb", - "version": "1.0.4", + "version": "1.1.0", "description": "Sets up MariaDB into the Dev Environment", "options": { "installDatabaseToWorkspaces": { diff --git a/features/src/mariadb/install.sh b/features/src/mariadb/install.sh index f8bf037c..e305392e 100755 --- a/features/src/mariadb/install.sh +++ b/features/src/mariadb/install.sh @@ -11,31 +11,76 @@ fi echo '(*) Installing MariaDB...' +: "${_REMOTE_USER:?"_REMOTE_USER is required"}" : "${INSTALLDATABASETOWORKSPACES:=}" -if [ -z "${_REMOTE_USER}" ] || [ "${_REMOTE_USER}" = "root" ]; then +if [ "${_REMOTE_USER}" = "root" ]; then MARIADB_USER=mysql + INSTALLDATABASETOWORKSPACES=false + + echo '(!) Cannot install databases to the workspace when remoteUser is root.' else MARIADB_USER="${_REMOTE_USER}" fi -apk add --no-cache mariadb-client mariadb - if [ "${INSTALLDATABASETOWORKSPACES}" != 'true' ]; then MARIADB_DATADIR=/var/lib/mysql else MARIADB_DATADIR=/workspaces/mysql-data + install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}" +fi + +# shellcheck source=/dev/null +. /etc/os-release +: "${ID:=}" +: "${ID_LIKE:=${ID}}" + +case "${ID_LIKE}" in + "debian") + PACKAGES="mariadb-client mariadb-server" + if ! hash envsubst >/dev/null 2>&1; then + PACKAGES="${PACKAGES} gettext" + fi + + apt-get update + # shellcheck disable=SC2086 + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ${PACKAGES} + apt-get clean + rm -rf /var/lib/apt/lists/* + update-rc.d mariadb remove + + if [ "${INSTALLDATABASETOWORKSPACES}" = 'true' ]; then + mv /var/lib/mysql/debian-*.flag "${MARIADB_DATADIR}" + fi + ;; + + "alpine") + PACKAGES="mariadb-client mariadb" + if ! hash envsubst >/dev/null 2>&1; then + PACKAGES="${PACKAGES} gettext" + fi + + # shellcheck disable=SC2086 + apk add --no-cache ${PACKAGES} + ;; + + *) + echo "(!) Unsupported distribution: ${ID}" + exit 1 + ;; +esac + +if [ "${INSTALLDATABASETOWORKSPACES}" = 'true' ]; then usermod -d /workspaces/mysql mysql rm -rf /var/lib/mysql fi -install -D -m 0755 -o root -g root service-run /etc/sv/mariadb/run -install -d -m 0755 -o root -g root /etc/service /etc/conf.d -ln -sf /etc/sv/mariadb /etc/service/mariadb - 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' < conf-mariadb.tpl > /etc/conf.d/mariadb +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 echo 'Done!' diff --git a/features/src/mariadb/service-run b/features/src/mariadb/service-run.tpl similarity index 59% rename from features/src/mariadb/service-run rename to features/src/mariadb/service-run.tpl index e15e01ea..27333a2c 100755 --- a/features/src/mariadb/service-run +++ b/features/src/mariadb/service-run.tpl @@ -11,17 +11,17 @@ fi : "${MARIADB_USER:=mysql}" : "${MARIADB_DATADIR:=/var/lib/mysql}" -/usr/bin/install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}" +install -d -D -m 02755 -o "${MARIADB_USER}" -g "${MARIADB_USER}" "${MARIADB_DATADIR}" chown -R "${MARIADB_USER}:${MARIADB_USER}" "${MARIADB_DATADIR}" -/usr/bin/install -d /run/mysqld -o "${MARIADB_USER}" -g "${MARIADB_USER}" +install -d /run/mysqld -o "${MARIADB_USER}" -g "${MARIADB_USER}" if [ ! -d "${MARIADB_DATADIR}/mysql" ]; then - /usr/bin/mysql_install_db --auth-root-authentication-method=normal --skip-test-db --user="${MARIADB_USER}" --datadir="${MARIADB_DATADIR}" + mysql_install_db --auth-root-authentication-method=normal --skip-test-db --user="${MARIADB_USER}" --datadir="${MARIADB_DATADIR}" fi -exec /sbin/su-exec "${MARIADB_USER}" \ - /usr/bin/mysqld \ +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 \