__single_binary_service: envvars and user-reuse support #26

Merged
evilham merged 1 commit from single-binary-env into master 2023-04-21 12:54:35 +00:00
4 changed files with 26 additions and 3 deletions

View file

@ -90,6 +90,11 @@ binary
If `--unpack` is used, a binary with this name must be unpacked.
Otherwise, the contents of `--url` will be placed under this binary name.
env
An `env` file consiting of `ENVIRONMENT_VARIABLE=VALUE`, one variable per
line.
Empty lines and those starting with `#` are ignored.
service-args
Any extra arguments to pass along with `--service-exec`. Beware that any
service-args having the format `--config=/etc/foo.cfg` should be

View file

@ -112,7 +112,7 @@ if [ "${USER}" != "root" ] && \
--system \
--state "${STATE}" \
--home "${USER_HOME_DIR}" \
--comment "cdist-managed ${SERVICE_NAME} user" \
--comment "cdist-managed service user" \
${USER_CREATE_HOME}
# Track dependencies
service_require="${service_require} __user/${USER}"
@ -136,11 +136,21 @@ fi
# These messages will trigger a service restart (overridden for systemd)
service_config_reload_pattern="^__file${CONFIG_FILE_DEST}"
# This should setup the object in $service_definition_require
# See above.
case "${INIT}" in
systemd)
if [ -z "${SERVICE_DEFINITION}" ]; then
SYSTEMD_ENV_FILE="/etc/systemd/system/${SERVICE_NAME}.env"
__file "${SYSTEMD_ENV_FILE}" \
--mode 0400 \
--source "${__object}/parameter/env"
# We need to take into account the envionment file for systemd too
service_config_reload_pattern="(${service_config_reload_pattern}|^__file${SYSTEMD_ENV_FILE})"
SERVICE_DEFINITION="$(cat <<EOF
[Unit]
Description=${SERVICE_DESCRIPTION}
@ -153,6 +163,7 @@ User=${USER}
Group=${GROUP}
ExecStart=${SERVICE_EXEC}
Restart=always
EnvironmentFile=${SYSTEMD_ENV_FILE}
${WORKING_DIRECTORY_SYSTEMD}
[Install]
@ -169,12 +180,18 @@ EOF
;;
runit|sysvinit)
if [ -z "${SERVICE_DEFINITION}" ]; then
RUNIT_ENV="$(sed -Ee 's!^([[:alnum:]_]+)=(.*)$!export \1=\2!' "${__object}/parameter/env")"
SERVICE_DEFINITION="$(cat <<EOF
#!/bin/sh -e
${WORKING_DIRECTORY_RUNIT}
# User-provided environment
${RUNIT_ENV}
# System vars
export HOME="\$(getent passwd '${USER}' | cut -d: -f6)"
export USER="${USER}"
export GROUP="${GROUP}"
exec 2>&1
exec chpst -u "${USER}:${GROUP}" ${SERVICE_EXEC}
EOF
)"
@ -279,10 +296,10 @@ EOF
--onchange "${perform_service_upgrade}" \
--source "-"
else
# We only restart here if there was a config change
# We only restart here if there was a config or env change
# but there was not a version change
require="${service_require}" __check_messages \
"single_binary_service_${__object_id}" \
--pattern "^__file${CONFIG_FILE_DEST}" \
--pattern "${service_config_reload_pattern}" \
--execute "$(sv_cmd restart)"
fi

View file

@ -1,4 +1,5 @@
config-file-source
env
user
group
state