Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ jobs:
--account-name ${{ secrets.AZURE_ACCOUNT_NAME }} --overwrite -n nginx-agent/${GIT_BRANCH}/${i##*/}
done


build-example:
name: Build Grafana Dashboard Example
runs-on: ubuntu-22.04
Expand Down
4 changes: 3 additions & 1 deletion Makefile.containers
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ ifeq ($(CONTAINER_CLITOOL), docker)
CONTAINER_BUILDENV ?= DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain
ifeq ($(shell docker-compose -v >/dev/null 2>&1 || echo FAIL),)
CONTAINER_COMPOSE = docker-compose
else ifeq ($(shell docker compose -v >/dev/null 2>&1 || echo FAIL),)
CONTAINER_COMPOSE = docker compose
endif
else ifeq ($(CONTAINER_CLITOOL), podman)
ifeq ($(shell podman-compose -v >/dev/null 2>&1 || echo FAIL),)
Expand All @@ -29,7 +31,7 @@ else
CONTAINER_VOLUME_FLAGS =
endif
else
CONTAINER_COMPOSETOOL = $(error Neither docker-compose nor podman-compose found)
CONTAINER_COMPOSETOOL = $(error Neither docker-compose, docker compose or podman-compose were found)
endif

ifeq ($(OS_RELEASE), ubuntu)
Expand Down
4 changes: 2 additions & 2 deletions scripts/packages/nginx-agent.service
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ PermissionsStartOnly=true
PIDFile=${AGENT_RUN_DIR}/nginx-agent.pid
Environment=

StandardOutput=syslog
StandardError=syslog
StandardOutput=journal
StandardError=journal
SyslogIdentifier=nginx-agent

ExecStop=/bin/kill -2 $MAINPID
Expand Down
85 changes: 74 additions & 11 deletions scripts/packages/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
set -e

# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
# This is to hide expected error messages from being outputted.

# Determine OS platform
# shellcheck source=/dev/null
Expand All @@ -20,7 +24,7 @@ WORKER_USER=""
AGENT_GROUP="nginx-agent"

detect_nginx_users() {
if command -V systemctl >/dev/null 2>&1; then
if command -V systemctl >/dev/null 2>&1 && [ "$(cat /proc/1/comm)" = "systemd" ]; then
printf "PostInstall: Reading NGINX systemctl unit file for user information\n"
nginx_unit_file=$(systemctl status nginx | grep -Po "\(\K\/.*service")
pid_file=$(grep -Po "PIDFile=\K.*$" "${nginx_unit_file}")
Expand All @@ -37,9 +41,34 @@ detect_nginx_users() {
fi
fi

if [ -z "${nginx_user}" ]; then
if command -V ps >/dev/null 2>&1 && [ -z "${nginx_user}" ]; then
printf "PostInstall: Reading NGINX process information to determine NGINX user\n"
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $1}')
if [ "$ID" = "alpine" ]; then
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $2}')
else
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $1}')
fi

if [ -z "${nginx_user}" ]; then
printf "No NGINX user found\n"
fi
fi

if [ -z "${nginx_user}" ]; then
printf "PostInstall: Reading NGINX process files to determine NGINX user\n"
nginx_pid=""
for pid in /proc/[0-9]*; do
pid=${pid##*/}
if [ -r /proc/"$pid"/cmdline ]; then
if grep -q "nginx: master process" /proc/"$pid"/cmdline 2>/dev/null; then
nginx_pid=$pid
break
fi
fi
done
if [ "${nginx_pid}" ]; then
nginx_user=$(awk '/^Uid:/ {print $2}' /proc/"$nginx_pid"/status | while read -r uid; do getent passwd "$uid"; done | cut -d: -f1)
fi

if [ -z "${nginx_user}" ]; then
printf "No NGINX user found\n"
Expand All @@ -53,17 +82,42 @@ detect_nginx_users() {
echo "WARNING: No NGINX processes detected."
fi

if command -V ps >/dev/null 2>&1 && [ -z "${worker_user}" ]; then
printf "PostInstall: Reading NGINX process information to determine NGINX worker user\n"
if [ "$ID" = "alpine" ]; then
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $2}')
else
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $1}')
fi

if [ -z "${worker_user}" ]; then
printf "No NGINX worker user found\n"
fi
fi

if [ -z "${worker_user}" ]; then
printf "PostInstall: Reading NGINX process information to determine NGINX user\n"
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $1}')
printf "PostInstall: Reading NGINX process files to determine NGINX worker user\n"
worker_pid=""
for pid in /proc/[0-9]*; do
pid=${pid##*/}
if [ -r /proc/"$pid"/cmdline ]; then
if grep -q "nginx: worker process" /proc/"$pid"/cmdline 2>/dev/null; then
worker_pid=$pid
break
fi
fi
done
if [ "${worker_pid}" ]; then
worker_user=$(awk '/^Uid:/ {print $2}' /proc/"$worker_pid"/status | while read -r uid; do getent passwd "$uid"; done | cut -d: -f1)
fi

if [ -z "${worker_user}" ]; then
printf "No NGINX worker user found\n"
fi
fi

if [ "${worker_user}" ]; then
echo "NGINX processes running as user '${worker_user}'. nginx-agent will try add that user to '${AGENT_GROUP}'"
echo "NGINX worker processes running as user '${worker_user}'. nginx-agent will try add that user to '${AGENT_GROUP}'"
WORKER_USER=${worker_user}
else
echo "WARNING: No NGINX worker processes detected."
Expand Down Expand Up @@ -98,7 +152,9 @@ create_agent_group() {
printf "PostInstall: Adding nginx-agent group %s\n" "${AGENT_GROUP}"

if command -V groupadd >/dev/null 2>&1; then
groupadd "${AGENT_GROUP}"
if [ ! "$(getent group $AGENT_GROUP)" ]; then
groupadd "${AGENT_GROUP}"
fi

printf "PostInstall: Adding NGINX / agent user %s to group %s\n" "${AGENT_USER}" "${AGENT_GROUP}"
usermod -a -G "${AGENT_GROUP}" "${AGENT_USER}"
Expand Down Expand Up @@ -141,7 +197,7 @@ create_run_dir() {

update_unit_file() {
# Fill in data to unit file that's acquired post install
if command -V systemctl >/dev/null 2>&1; then
if command -V systemctl >/dev/null 2>&1 && [ "$(cat /proc/1/comm)" = "systemd" ]; then
printf "PostInstall: Modifying NGINX Agent unit file with correct locations and user information\n"
EXE_CMD="s|\${AGENT_EXE}|${AGENT_EXE}|g"
sed -i -e $EXE_CMD ${AGENT_UNIT_LOCATION}/${AGENT_UNIT_FILE}
Expand Down Expand Up @@ -276,9 +332,16 @@ restart_agent_if_required() {
# https://github.com/freebsd/pkg/pull/2128
return
fi
if service nginx-agent status >/dev/null 2>&1; then
printf "PostInstall: Restarting nginx agent\n"
service nginx-agent restart || true
if command -V service >/dev/null 2>&1; then
if service nginx-agent status >/dev/null 2>&1; then
printf "PostInstall: Restarting nginx agent\n"
service nginx-agent restart || true
fi
else
if systemctl status nginx-agent >/dev/null 2>&1; then
printf "PostInstall: Restarting nginx agent\n"
systemctl restart nginx-agent || true
fi
fi
}

Expand Down
17 changes: 16 additions & 1 deletion scripts/packages/postremove.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/bin/sh
set -e

# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
# This is to hide expected error messages from being outputted.

# Determine OS platform
# shellcheck source=/dev/null

. /etc/os-release

stop_agent_freebsd() {
Expand Down Expand Up @@ -30,6 +34,17 @@ systemd_daemon_reload() {
}

cleanup() {
echo "Removing nginx-agent group"
if command -V groupdel >/dev/null 2>&1; then
if [ "$(getent group nginx-agent)" ]; then
groupdel nginx-agent
fi
fi

if [ "$ID" = "freebsd" ]; then
pw groupdel nginx-agent
fi

echo "Removing /var/run/nginx-agent directory"
rm -rf "/var/run/nginx-agent"
}
Expand Down
6 changes: 5 additions & 1 deletion scripts/packages/postupgrade.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
set -e

# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
# This is to hide expected error messages from being outputted.

NEWVER="$1"
OLDVER="$2"
Expand All @@ -11,7 +15,7 @@ restart_agent_if_required() {
}

# Determine OS platform
# shellcheck source=/dev/null

. /etc/os-release

case "$ID" in
Expand Down
4 changes: 3 additions & 1 deletion scripts/packages/preremove.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/sh
set -e

# Pre Remove Steps

# Determine OS platform
# shellcheck source=/dev/null

. /etc/os-release

stop_agent_openrc() {
Expand Down