Skip to content

Commit

Permalink
Merge pull request #207 from Automattic/refactor-mariadb
Browse files Browse the repository at this point in the history
refactor(mariadb): support Debian and Ubuntu
  • Loading branch information
sjinks authored May 31, 2024
2 parents 87465e4 + 3c1611a commit e1d6e23
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
2 changes: 0 additions & 2 deletions features/src/mariadb/conf-mariadb.tpl

This file was deleted.

2 changes: 1 addition & 1 deletion features/src/mariadb/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
61 changes: 53 additions & 8 deletions features/src/mariadb/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit e1d6e23

Please sign in to comment.