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=""
2024AGENT_GROUP=" nginx-agent"
2125
2226detect_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
142198update_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
0 commit comments