Skip to content

Commit

Permalink
Merge pull request #261 from Automattic/mailpit-ep-support
Browse files Browse the repository at this point in the history
feat(mailpit): add support for entrypoints
  • Loading branch information
sjinks authored Jun 22, 2024
2 parents b657755 + d2dbab1 commit d28cbff
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 12 deletions.
8 changes: 7 additions & 1 deletion features/src/mailpit/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
"id": "mailpit",
"name": "Mailpit",
"description": "Sets up Mailpit into the Dev Environment",
"version": "1.1.2",
"version": "1.2.0",
"options": {
"enabled": {
"type": "boolean",
"default": true,
"description": "Enable Mailpit"
},
"install-runit-service": {
"type": "boolean",
"default": true,
"description": "Whether to install a runit service for Mailpit"
}
},
"installsAfter": [
"ghcr.io/automattic/vip-codespaces/entrypoints",
"ghcr.io/automattic/vip-codespaces/php"
]
}
17 changes: 17 additions & 0 deletions features/src/mailpit/entrypoint.alpine.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

set -eu
exec 2>&1

if [ -x /sbin/chpst ]; then
# shellcheck disable=SC2154
exec chpst -u "${_REMOTE_USER}:${_REMOTE_USER}" \
/usr/local/bin/mailpit \
--listen 127.0.0.1:8025 \
--smtp 127.0.0.1:1025 &
else
exec su-exec "${_REMOTE_USER}:${_REMOTE_USER}" \
/usr/local/bin/mailpit \
--listen 127.0.0.1:8025 \
--smtp 127.0.0.1:1025 &
fi
13 changes: 13 additions & 0 deletions features/src/mailpit/entrypoint.deb.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

set -eu
exec 2>&1

# shellcheck disable=SC2154
MY_UID="$(id -u "${_REMOTE_USER}")"
MY_GID="$(id -g "${_REMOTE_USER}")"

exec setpriv --reuid="${MY_UID}" --regid="${MY_GID}" --inh-caps=-all --init-groups \
/usr/local/bin/mailpit \
--listen 127.0.0.1:8025 \
--smtp 127.0.0.1:1025 &
26 changes: 22 additions & 4 deletions features/src/mailpit/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fi

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

if [ "${ENABLED}" = "true" ]; then
echo '(*) Installing Mailpit...'
Expand All @@ -22,6 +23,7 @@ if [ "${ENABLED}" = "true" ]; then
: "${ID_LIKE:=${ID}}"
PHP_INI_DIR=
NEED_ENMOD=
ENTRYPOINT=""

case "${ID_LIKE}" in
"debian")
Expand Down Expand Up @@ -52,6 +54,8 @@ if [ "${ENABLED}" = "true" ]; then
PHP_INI_DIR="/etc/php/$(php -r 'echo PHP_MAJOR_VERSION, ".", PHP_MINOR_VERSION;')/mods-available"
NEED_ENMOD=1
fi

ENTRYPOINT="entrypoint.deb.tpl"
;;

"alpine")
Expand All @@ -64,6 +68,10 @@ if [ "${ENABLED}" = "true" ]; then
PACKAGES="${PACKAGES} gettext"
fi

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

if [ -n "${PACKAGES}" ]; then
# shellcheck disable=SC2086
apk add --no-cache ${PACKAGES}
Expand All @@ -72,6 +80,8 @@ if [ "${ENABLED}" = "true" ]; then
if hash php >/dev/null 2>&1; then
PHP_INI_DIR="/etc/php$(php -r 'echo PHP_MAJOR_VERSION, PHP_MINOR_VERSION;')/conf.d"
fi

ENTRYPOINT="entrypoint.alpine.tpl"
;;

*)
Expand Down Expand Up @@ -106,10 +116,18 @@ if [ "${ENABLED}" = "true" ]; then
fi
fi

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

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

echo 'Done!'
fi
1 change: 1 addition & 0 deletions features/test/mailpit/alpine.sh
15 changes: 10 additions & 5 deletions features/test/mailpit/checks.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/bin/bash

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

check "Port 1025 is open" sh -c 'netstat -lnt | grep :1025 '
check "Port 8025 is open" sh -c 'netstat -lnt | grep :8025 '

Expand Down
26 changes: 26 additions & 0 deletions features/test/mailpit/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,31 @@
"features": {
"mailpit": {}
}
},
"alpine": {
"image": "mcr.microsoft.com/devcontainers/base:alpine",
"features": {
"entrypoints": {},
"mailpit": {
"install-runit-service": false
}
},
"overrideFeatureInstallOrder": [
"./entrypoints",
"./mailpit"
]
},
"ubuntu": {
"image": "mcr.microsoft.com/devcontainers/base:alpine",
"features": {
"entrypoints": {},
"mailpit": {
"install-runit-service": false
}
},
"overrideFeatureInstallOrder": [
"./entrypoints",
"./mailpit"
]
}
}
7 changes: 5 additions & 2 deletions features/test/mailpit/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
source dev-container-features-test-lib

check "mailpit exists and is executable" test -x /usr/local/bin/mailpit
check "/etc/sv/mailpit/run exists and is executable" test -x /etc/sv/mailpit/run
check "/etc/service/mailpit is a symlink" test -L /etc/service/mailpit

if [[ -d /etc/sv ]]; then
check "/etc/sv/mailpit/run exists and is executable" test -x /etc/sv/mailpit/run
check "/etc/service/mailpit is a symlink" test -L /etc/service/mailpit
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/mailpit/ubuntu.sh

0 comments on commit d28cbff

Please sign in to comment.