Skip to content

Commit 594da08

Browse files
authored
Fix Post Install Script Issues (#739)
1 parent 4e2579a commit 594da08

File tree

7 files changed

+103
-18
lines changed

7 files changed

+103
-18
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ jobs:
263263
--account-name ${{ secrets.AZURE_ACCOUNT_NAME }} --overwrite -n nginx-agent/${GIT_BRANCH}/${i##*/}
264264
done
265265
266-
267266
build-example:
268267
name: Build Grafana Dashboard Example
269268
runs-on: ubuntu-22.04

Makefile.containers

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ifeq ($(CONTAINER_CLITOOL), docker)
1212
CONTAINER_BUILDENV ?= DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain
1313
ifeq ($(shell docker-compose -v >/dev/null 2>&1 || echo FAIL),)
1414
CONTAINER_COMPOSE = docker-compose
15+
else ifeq ($(shell docker compose -v >/dev/null 2>&1 || echo FAIL),)
16+
CONTAINER_COMPOSE = docker compose
1517
endif
1618
else ifeq ($(CONTAINER_CLITOOL), podman)
1719
ifeq ($(shell podman-compose -v >/dev/null 2>&1 || echo FAIL),)
@@ -29,7 +31,7 @@ else
2931
CONTAINER_VOLUME_FLAGS =
3032
endif
3133
else
32-
CONTAINER_COMPOSETOOL = $(error Neither docker-compose nor podman-compose found)
34+
CONTAINER_COMPOSETOOL = $(error Neither docker-compose, docker compose or podman-compose were found)
3335
endif
3436

3537
ifeq ($(OS_RELEASE), ubuntu)

scripts/packages/nginx-agent.service

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ PermissionsStartOnly=true
2323
PIDFile=${AGENT_RUN_DIR}/nginx-agent.pid
2424
Environment=
2525

26-
StandardOutput=syslog
27-
StandardError=syslog
26+
StandardOutput=journal
27+
StandardError=journal
2828
SyslogIdentifier=nginx-agent
2929

3030
ExecStop=/bin/kill -2 $MAINPID

scripts/packages/postinstall.sh

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/sh
2+
set -e
3+
4+
# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
5+
# This is to hide expected error messages from being outputted.
26

37
# Determine OS platform
48
# shellcheck source=/dev/null
@@ -20,7 +24,7 @@ WORKER_USER=""
2024
AGENT_GROUP="nginx-agent"
2125

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

40-
if [ -z "${nginx_user}" ]; then
44+
if command -V ps >/dev/null 2>&1 && [ -z "${nginx_user}" ]; then
4145
printf "PostInstall: Reading NGINX process information to determine NGINX user\n"
42-
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $1}')
46+
if [ "$ID" = "alpine" ]; then
47+
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $2}')
48+
else
49+
nginx_user=$(ps aux | grep "nginx: master process" | grep -v grep | head -1 | awk '{print $1}')
50+
fi
51+
52+
if [ -z "${nginx_user}" ]; then
53+
printf "No NGINX user found\n"
54+
fi
55+
fi
56+
57+
if [ -z "${nginx_user}" ]; then
58+
printf "PostInstall: Reading NGINX process files to determine NGINX user\n"
59+
nginx_pid=""
60+
for pid in /proc/[0-9]*; do
61+
pid=${pid##*/}
62+
if [ -r /proc/"$pid"/cmdline ]; then
63+
if grep -q "nginx: master process" /proc/"$pid"/cmdline 2>/dev/null; then
64+
nginx_pid=$pid
65+
break
66+
fi
67+
fi
68+
done
69+
if [ "${nginx_pid}" ]; then
70+
nginx_user=$(awk '/^Uid:/ {print $2}' /proc/"$nginx_pid"/status | while read -r uid; do getent passwd "$uid"; done | cut -d: -f1)
71+
fi
4372

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

85+
if command -V ps >/dev/null 2>&1 && [ -z "${worker_user}" ]; then
86+
printf "PostInstall: Reading NGINX process information to determine NGINX worker user\n"
87+
if [ "$ID" = "alpine" ]; then
88+
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $2}')
89+
else
90+
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $1}')
91+
fi
92+
93+
if [ -z "${worker_user}" ]; then
94+
printf "No NGINX worker user found\n"
95+
fi
96+
fi
97+
5698
if [ -z "${worker_user}" ]; then
57-
printf "PostInstall: Reading NGINX process information to determine NGINX user\n"
58-
worker_user=$(ps aux | grep "nginx: worker process" | grep -v grep | head -1 | awk '{print $1}')
99+
printf "PostInstall: Reading NGINX process files to determine NGINX worker user\n"
100+
worker_pid=""
101+
for pid in /proc/[0-9]*; do
102+
pid=${pid##*/}
103+
if [ -r /proc/"$pid"/cmdline ]; then
104+
if grep -q "nginx: worker process" /proc/"$pid"/cmdline 2>/dev/null; then
105+
worker_pid=$pid
106+
break
107+
fi
108+
fi
109+
done
110+
if [ "${worker_pid}" ]; then
111+
worker_user=$(awk '/^Uid:/ {print $2}' /proc/"$worker_pid"/status | while read -r uid; do getent passwd "$uid"; done | cut -d: -f1)
112+
fi
59113

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

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

100154
if command -V groupadd >/dev/null 2>&1; then
101-
groupadd "${AGENT_GROUP}"
155+
if [ ! "$(getent group $AGENT_GROUP)" ]; then
156+
groupadd "${AGENT_GROUP}"
157+
fi
102158

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

142198
update_unit_file() {
143199
# Fill in data to unit file that's acquired post install
144-
if command -V systemctl >/dev/null 2>&1; then
200+
if command -V systemctl >/dev/null 2>&1 && [ "$(cat /proc/1/comm)" = "systemd" ]; then
145201
printf "PostInstall: Modifying NGINX Agent unit file with correct locations and user information\n"
146202
EXE_CMD="s|\${AGENT_EXE}|${AGENT_EXE}|g"
147203
sed -i -e $EXE_CMD ${AGENT_UNIT_LOCATION}/${AGENT_UNIT_FILE}
@@ -276,9 +332,16 @@ restart_agent_if_required() {
276332
# https://github.com/freebsd/pkg/pull/2128
277333
return
278334
fi
279-
if service nginx-agent status >/dev/null 2>&1; then
280-
printf "PostInstall: Restarting nginx agent\n"
281-
service nginx-agent restart || true
335+
if command -V service >/dev/null 2>&1; then
336+
if service nginx-agent status >/dev/null 2>&1; then
337+
printf "PostInstall: Restarting nginx agent\n"
338+
service nginx-agent restart || true
339+
fi
340+
else
341+
if systemctl status nginx-agent >/dev/null 2>&1; then
342+
printf "PostInstall: Restarting nginx agent\n"
343+
systemctl restart nginx-agent || true
344+
fi
282345
fi
283346
}
284347

scripts/packages/postremove.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/sh
2+
set -e
3+
4+
# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
5+
# This is to hide expected error messages from being outputted.
26

37
# Determine OS platform
4-
# shellcheck source=/dev/null
8+
59
. /etc/os-release
610

711
stop_agent_freebsd() {
@@ -30,6 +34,17 @@ systemd_daemon_reload() {
3034
}
3135

3236
cleanup() {
37+
echo "Removing nginx-agent group"
38+
if command -V groupdel >/dev/null 2>&1; then
39+
if [ "$(getent group nginx-agent)" ]; then
40+
groupdel nginx-agent
41+
fi
42+
fi
43+
44+
if [ "$ID" = "freebsd" ]; then
45+
pw groupdel nginx-agent
46+
fi
47+
3348
echo "Removing /var/run/nginx-agent directory"
3449
rm -rf "/var/run/nginx-agent"
3550
}

scripts/packages/postupgrade.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/sh
2+
set -e
3+
4+
# Note: >/dev/null 2>&1 is used in multiple if statements in this file.
5+
# This is to hide expected error messages from being outputted.
26

37
NEWVER="$1"
48
OLDVER="$2"
@@ -11,7 +15,7 @@ restart_agent_if_required() {
1115
}
1216

1317
# Determine OS platform
14-
# shellcheck source=/dev/null
18+
1519
. /etc/os-release
1620

1721
case "$ID" in

scripts/packages/preremove.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/bin/sh
2+
set -e
3+
24
# Pre Remove Steps
35

46
# Determine OS platform
5-
# shellcheck source=/dev/null
7+
68
. /etc/os-release
79

810
stop_agent_openrc() {

0 commit comments

Comments
 (0)