0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-10 08:07:34 +00:00
netdata_netdata/packaging/docker/run.sh
Austin S. Hemmelgarn 56e22c16e8
Cleanly reimplement system/edit-config.in. ()
* Cleanly reimplement system/edit-config.in

- Added support for pulling config files from Docker containers.
- Added auto-detection for Docker containers.
- Use directory the script is in for target directory for config files
  instead of templating it in at build time.
- Prefix error messages with `ERROR:`.
- Robustly check for a valid editor _before_ invoking it.
- Add support for actual command-line options, including a proper
  `--help` option.
- Use prefix matching of absolute paths to determine file validity
  instead of blindly excluding certain path types.
- If editing a non-existing file we do not provide a stock copy of,
  create an empty file instead of throwing an error.
- Make the whole script properly modular.

* Improve robustness of container autodetection.

Instead of relying on the lack of certain directories on a host system,
use some relatively standard checks to determine if we appear to be
running in a container.

* Auto-detect stock config paths at runtinme instead of hard-coding them at build time.

THis will simplify testing of the script, as well as making it a bit
more resilient to users moving things around.

* Add an option to list known config files.

* Fix container environment check to not require root.

* Fix help output.

* Fix path prefix check.

* Fix file path handling.

* Use correct variable when editing files.

* Use correct path for `env`.

* Source profile before running `set -e`.

To prevent questionablly written profiles from causing the script to
exit.

* Produce columnar output when listing valid files.

* Fix copy check.

* Fix build issues.

* fix build issues

* formatting

Co-authored-by: ilyam8 <ilya@netdata.cloud>
2022-12-06 10:53:30 -05:00

68 lines
2.6 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Entry point script for netdata
#
# Copyright: 2018 and later Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Author : Pavlos Emm. Katsoulakis <paul@netdata.cloud>
# Author : Austin S. Hemmelgarn <austin@netdata.cloud>
set -e
if [ ! -w / ] && [ "${EUID}" -eq 0 ]; then
echo >&2 "WARNING: This Docker host appears to not properly support newer stat system calls. This is known to cause issues with Netdata (most notably, nodes running on such hosts **cannot be claimed**)."
echo >&2 "WARNING: For more information, see https://learn.netdata.cloud/docs/agent/claim#known-issues-on-older-hosts-with-seccomp-enabled"
fi
if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
[ -n "$DISABLE_TELEMETRY" ] ||
[ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
[ -n "$DO_NOT_TRACK" ]; then
touch /etc/netdata/.opt-out-from-anonymous-statistics
fi
BALENA_PGID=$(stat -c %g /var/run/balena.sock 2>/dev/null || true)
DOCKER_PGID=$(stat -c %g /var/run/docker.sock 2>/dev/null || true)
re='^[0-9]+$'
if [[ $BALENA_PGID =~ $re ]]; then
echo "Netdata detected balena-engine.sock"
DOCKER_HOST='/var/run/balena-engine.sock'
PGID="$BALENA_PGID"
elif [[ $DOCKER_PGID =~ $re ]]; then
echo "Netdata detected docker.sock"
DOCKER_HOST="/var/run/docker.sock"
PGID="$DOCKER_PGID"
fi
export PGID
export DOCKER_HOST
if [ -n "${PGID}" ]; then
echo "Creating docker group ${PGID}"
addgroup -g "${PGID}" "docker" || echo >&2 "Could not add group docker with ID ${PGID}, its already there probably"
echo "Assign netdata user to docker group ${PGID}"
usermod -a -G "${PGID}" "${DOCKER_USR}" || echo >&2 "Could not add netdata user to group docker with ID ${PGID}"
fi
if mountpoint -q /etc/netdata && [ -z "$(ls -A /etc/netdata)" ]; then
echo "Copying stock configuration to /etc/netdata"
cp -a /etc/netdata.stock/. /etc/netdata
fi
if mountpoint -q /etc/netdata; then
hostname > /etc/netdata/.container-hostname
else
rm -f /etc/netdata/.container-hostname
fi
if [ -n "${NETDATA_CLAIM_URL}" ] && [ -n "${NETDATA_CLAIM_TOKEN}" ] && [ ! -f /var/lib/netdata/cloud.d/claimed_id ]; then
# shellcheck disable=SC2086
/usr/sbin/netdata-claim.sh -token="${NETDATA_CLAIM_TOKEN}" \
-url="${NETDATA_CLAIM_URL}" \
${NETDATA_CLAIM_ROOMS:+-rooms="${NETDATA_CLAIM_ROOMS}"} \
${NETDATA_CLAIM_PROXY:+-proxy="${NETDATA_CLAIM_PROXY}"} \
${NETDATA_EXTRA_CLAIM_OPTS} \
-daemon-not-running
fi
exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT}" "$@"