Skip to content

Commit

Permalink
feat(elasticsearch): add support for entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jun 22, 2024
1 parent 5ccf400 commit f41a8a9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
10 changes: 8 additions & 2 deletions features/src/elasticsearch/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "elasticsearch",
"name": "Elasticsearch",
"description": "Sets up Elasticsearch into the Dev Environment",
"version": "1.1.2",
"version": "1.2.0",
"containerEnv": {
"ES_TMPDIR": "/usr/share/elasticsearch/tmp"
},
Expand All @@ -22,9 +22,15 @@
"type": "boolean",
"default": false,
"description": "Set Elasticseatch data directory to /workspaces/es-data to persist data between container rebuilds (GHCS)"
},
"install-runit-service": {
"type": "boolean",
"default": true,
"description": "Whether to install a runit service for Elasticsearch"
}
},
"installsAfter": [
"ghcr.io/automattic/vip-codespaces/entrypoints",
"ghcr.io/automattic/vip-codespaces/wordpress"
]
}
}
18 changes: 18 additions & 0 deletions features/src/elasticsearch/entrypoint-alpine.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -e
exec 2>&1

# shellcheck disable=SC2312
ES_JAVA_HOME="$(dirname "$(dirname "$(readlink -f /usr/bin/java)")")"
ES_JAVA_OPTS="-Des.cgroups.hierarchy.override=/ ${ES_JAVA_OPTS:-}"
export ES_JAVA_HOME ES_JAVA_OPTS

# shellcheck disable=SC2154 # ES_USER and ES_DATADIR are substituted by `install.sh`.
chown -R "${ES_USER}:${ES_USER}" "${ES_DATADIR}" /usr/share/elasticsearch/config /usr/share/elasticsearch/logs /usr/share/elasticsearch/tmp /usr/share/elasticsearch/plugins
# shellcheck disable=SC2154
if [ -x /sbin/chpst ]; then
exec /sbin/chpst -u "${ES_USER}:root" /usr/bin/elasticsearch > /dev/null &
else
exec /sbin/su-exec "${ES_USER}:root" /usr/bin/elasticsearch > /dev/null &
fi
11 changes: 11 additions & 0 deletions features/src/elasticsearch/entrypoint-deb.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e
exec 2>&1

# shellcheck disable=SC2154 # ES_USER and ES_DATADIR are substituted by `install.sh`.
chown -R "${ES_USER}:${ES_USER}" "${ES_DATADIR}" /usr/share/elasticsearch/config /usr/share/elasticsearch/logs /usr/share/elasticsearch/tmp /usr/share/elasticsearch/plugins
# shellcheck disable=SC2154
ES_UID="$(id -u "${ES_USER}")"
ES_GID="$(id -g root)"
exec setpriv --reuid="${ES_UID}" --regid="${ES_GID}" --inh-caps=-all --init-groups /usr/bin/elasticsearch > /dev/null &
26 changes: 22 additions & 4 deletions features/src/elasticsearch/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fi
: "${_REMOTE_USER:?"_REMOTE_USER is required"}"
: "${ENABLED:=}"
: "${INSTALLDATATOWORKSPACES:?}"
: "${INSTALL_RUNIT_SERVICE:=true}"

ES_VERSION="${VERSION:-7.17.21}"

Expand All @@ -30,6 +31,7 @@ if [ "${ENABLED}" = "true" ]; then
fi

PACKAGES=""
ENTRYPOINT=""
if ! hash curl >/dev/null 2>&1; then
PACKAGES="${PACKAGES} curl"
fi
Expand Down Expand Up @@ -74,6 +76,8 @@ if [ "${ENABLED}" = "true" ]; then
if [ "${_REMOTE_USER}" = "root" ]; then
adduser --disabled-login --home /usr/share/elasticsearch --no-create-home --gecos '' "${ES_USER}"
fi

ENTRYPOINT=entrypoint-deb.tpl
;;

"alpine")
Expand All @@ -86,12 +90,18 @@ if [ "${ENABLED}" = "true" ]; then
PACKAGES="${PACKAGES} bash"
fi

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

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

if [ "${_REMOTE_USER}" = "root" ]; then
adduser -h /usr/share/elasticsearch -s /sbin/nologin -H -D "${ES_USER}"
fi

ENTRYPOINT=entrypoint-alpine.tpl
;;

*)
Expand Down Expand Up @@ -137,10 +147,18 @@ if [ "${ENABLED}" = "true" ]; then
chown "${ES_USER}:${ES_USER}" /usr/share/elasticsearch/config/elasticsearch.yml
chmod 0644 /usr/share/elasticsearch/config/elasticsearch.yml

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

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

if [ -d /var/lib/wordpress/postinstall.d ]; then
install -m 0755 -o root -g root post-wp-install.sh /var/lib/wordpress/postinstall.d/50-elasticsearch.sh
Expand Down
16 changes: 8 additions & 8 deletions features/test/elasticsearch/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"features": {
"elasticsearch": {
"version": "7.17.21"
"version": "7.17.22"
}
}
},
Expand All @@ -19,7 +19,7 @@
},
"features": {
"elasticsearch": {
"version": "8.13.4"
"version": "8.14.1"
}
}
},
Expand All @@ -31,7 +31,7 @@
},
"features": {
"elasticsearch": {
"version": "7.17.21",
"version": "7.17.22",
"installDataToWorkspaces": true
}
}
Expand All @@ -44,7 +44,7 @@
},
"features": {
"elasticsearch": {
"version": "8.13.4",
"version": "8.14.1",
"installDataToWorkspaces": true
}
}
Expand All @@ -57,7 +57,7 @@
},
"features": {
"elasticsearch": {
"version": "7.17.21"
"version": "7.17.22"
}
}
},
Expand All @@ -69,7 +69,7 @@
},
"features": {
"elasticsearch": {
"version": "8.13.4"
"version": "8.14.1"
}
}
},
Expand All @@ -81,7 +81,7 @@
},
"features": {
"elasticsearch": {
"version": "7.17.21",
"version": "7.17.22",
"installDataToWorkspaces": true
}
}
Expand All @@ -94,7 +94,7 @@
},
"features": {
"elasticsearch": {
"version": "8.13.4",
"version": "8.14.1",
"installDataToWorkspaces": true
}
}
Expand Down
6 changes: 4 additions & 2 deletions features/test/elasticsearch/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 "elasticsearch exists" which elasticsearch
check "/etc/sv/elasticsearch/run exists and is executable" test -x /etc/sv/elasticsearch/run
check "/etc/service/elasticsearch is a symlink" test -L /etc/service/elasticsearch
if [[ -d /etc/sv ]]; then
check "/etc/sv/elasticsearch/run exists and is executable" test -x /etc/sv/elasticsearch/run
check "/etc/service/elasticsearch is a symlink" test -L /etc/service/elasticsearch
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

0 comments on commit f41a8a9

Please sign in to comment.