#!/bin/sh # # Cron wrapper for push-metrics.js: load the secrets, check the install, hand # over to node. Every five minutes, forever, unattended. # # NEVER add `set -x`. This script is meant to be piped into `logger`, and the # shell would echo the `. "$ENV_FILE"` expansion — the ingest token — straight # into /var/log/syslog and journald, where it would stay. Same reason nothing # below ever echoes a variable that holds a secret: the checks print the NAME of # what is missing, never its value. # # Suggested crontab line (see README.md): # */5 * * * * $HOME/.local/share/maximus-host-agent/agent/run-push.sh 2>&1 | logger -t host-agent set -eu SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) AGENT="$SCRIPT_DIR/push-metrics.js" # Overridable for testing only; cron uses the default. ENV_FILE="${HOST_AGENT_ENV_FILE:-$HOME/.config/maximus-host-agent.env}" if [ ! -f "$ENV_FILE" ]; then echo "host-agent: env file not found: $ENV_FILE" >&2 exit 1 fi # A warning, not a refusal: a wrong mode bit is worth shouting about, but it is # not worth silencing the heartbeat of the whole workstation over. FILE_MODE=$(stat -c "%a" "$ENV_FILE" 2>/dev/null || echo "") if [ -n "$FILE_MODE" ] && [ "$FILE_MODE" != "600" ]; then echo "host-agent: warning: $ENV_FILE is mode $FILE_MODE, expected 600" >&2 fi # shellcheck source=/dev/null . "$ENV_FILE" if [ -z "${HOSTS_API_URL:-}" ]; then echo "host-agent: HOSTS_API_URL is not set in $ENV_FILE" >&2 exit 1 fi if [ -z "${HOSTS_INGEST_TOKEN:-}" ]; then echo "host-agent: HOSTS_INGEST_TOKEN is not set in $ENV_FILE" >&2 exit 1 fi if [ -z "${HOST_ID:-}" ]; then echo "host-agent: HOST_ID is not set in $ENV_FILE" >&2 exit 1 fi if [ ! -f "$AGENT" ]; then echo "host-agent: agent not found: $AGENT" >&2 exit 1 fi # cron's PATH is famously short (/usr/bin:/bin), and a node installed through # nvm or /usr/local lives outside it. Set NODE_BIN in the env file when # `command -v node` comes up empty under cron but works in a login shell. NODE_BIN="${NODE_BIN:-node}" if ! command -v "$NODE_BIN" >/dev/null 2>&1; then echo "host-agent: node not found (NODE_BIN=$NODE_BIN); set NODE_BIN in $ENV_FILE" >&2 exit 1 fi export HOSTS_API_URL HOSTS_INGEST_TOKEN HOST_ID exec "$NODE_BIN" "$AGENT" "$@"