Skip to content

Commit 89fc334

Browse files
authored
fix: avoid stopping nginx-agent service on package upgrade (#352)
* fix: restart service on package upgrade on Debian/Ubuntu Previously it was stopped during the executing of package scripts due to incorrect behavior in prerm: https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html#summary-of-ways-maintainer-scripts-are-called * fix: do not stop service on upgrade (RHEL-based distros) * Avoid restarting nginx-agent service from pkg on FreeBSD during upgrades This will end up with a new process being killed immediately by pkg: https://github.com/freebsd/pkg/blob/1.19.1/libpkg/scripts.c#L100-L103 https://github.com/freebsd/pkg/blob/1.19.1/libpkg/scripts.c#L244-L261 See also: freebsd/pkg#2128 * feat: OpenRC package scripts for Alpine Linux * chore: add Alpine Linux instructions to README While here, removed extra trailing spaces across the doc. * chore: adjust service description, remove modelines * chore: remove leading underscore from shell func names
1 parent 0854fe6 commit 89fc334

File tree

8 files changed

+160
-34
lines changed

8 files changed

+160
-34
lines changed

README.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ To install NGINX Agent on your system, go to [Releases](https://github.com/nginx
8989

9090
Use your system's package manager to install the package. Some examples:
9191

92-
Debian, Ubuntu, and other distributions using the `dpkg` package manager.
92+
Debian, Ubuntu, and other distributions using the `dpkg` package manager.
9393

9494
```
9595
sudo dpkg -i nginx-agent-<agent-version>.deb
@@ -121,7 +121,16 @@ To enable the NGINX Agent to start on boot, run the following command:
121121
sudo systemctl enable nginx-agent
122122
```
123123

124-
## Logging
124+
On Alpine Linux, use the following command to start the agent:
125+
```
126+
sudo service nginx-agent start
127+
```
128+
To enable the agent to start at boot time on Alpine Linux, run this:
129+
```
130+
sudo rc-update add nginx-agent default
131+
```
132+
133+
## Logging
125134
NGINX Agent uses formatted log files to collect metrics. Expanding log formats and instance counts will also increase the size of NGINX Agent log files. We recommend adding a separate partition for `/var/log/nginx-agent`. Without log rotation or storage on a separate partition, log files could use up all the free drive space and cause your system to become unresponsive to certain services.
126135

127136
### Log Rotation
@@ -171,7 +180,7 @@ Follow steps in the [Installation](#installation) section to download, install,
171180
Using your preferred method, clone the NGINX Agent repository into your development directory. See [Cloning a GitHub Repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) for additional help.
172181

173182
## Installing Go
174-
NGINX Agent and the Mock Control Plane are written in Go. Go 1.21.0 or higher is required to build and run either application from the source code directory. You can [download Go from the official website](https://go.dev/dl/).
183+
NGINX Agent and the Mock Control Plane are written in Go. Go 1.21.0 or higher is required to build and run either application from the source code directory. You can [download Go from the official website](https://go.dev/dl/).
175184

176185
## Starting the gRPC Mock Control Plane
177186
Start the mock control plane by running the following command from the `agent` source code root directory:
@@ -185,12 +194,12 @@ INFO[0000] gRPC listening at 54789 # gRPC control plane port which NGINX Agent w
185194
```
186195

187196
## NGINX Agent Settings
188-
If it doesn't already exist, create the `/etc/nginx-agent/` directory and copy the `nginx-agent.conf` file into it from the project root directory.
197+
If it doesn't already exist, create the `/etc/nginx-agent/` directory and copy the `nginx-agent.conf` file into it from the project root directory.
189198
```
190199
sudo mkdir /etc/nginx-agent
191200
sudo cp <project_root_directory>/nginx-agent.conf /etc/nginx-agent/
192201
```
193-
Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run.
202+
Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run.
194203
```
195204
sudo touch /var/lib/nginx-agent/agent-dynamic.conf
196205
```
@@ -242,7 +251,7 @@ Open a web browser to view the Swagger UI at http://localhost:8082/docs.
242251
## Extensions
243252
An extension is a piece of code, not critical to the main functionality that the NGINX agent is responsible for. This generally falls outside the remit of managing NGINX Configuration and reporting NGINX metrics.
244253
245-
To enable an extension, it must be added to the extensions list in the `/etc/nginx-agent/nginx-agent.conf`.
254+
To enable an extension, it must be added to the extensions list in the `/etc/nginx-agent/nginx-agent.conf`.
246255
Here is an example of enabling the advanced metrics extension:
247256
248257
```yaml
@@ -258,24 +267,24 @@ Open another terminal window and start the NGINX Agent. Issue the following comm
258267
sudo make run
259268
260269
# Command Output snippet
261-
WARN[0000] Log level is info
262-
INFO[0000] setting displayName to XXX
270+
WARN[0000] Log level is info
271+
INFO[0000] setting displayName to XXX
263272
INFO[0000] NGINX Agent at with pid 12345, clientID=XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX name=XXX
264-
INFO[0000] NginxBinary initializing
265-
INFO[0000] Commander initializing
266-
INFO[0000] Comms initializing
267-
INFO[0000] OneTimeRegistration initializing
268-
INFO[0000] Registering XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX
269-
INFO[0000] Metrics initializing
270-
INFO[0000] MetricsThrottle initializing
271-
INFO[0000] DataPlaneStatus initializing
272-
INFO[0000] MetricsThrottle waiting for report ready
273-
INFO[0000] Metrics waiting for handshake to be completed
274-
INFO[0000] ProcessWatcher initializing
275-
INFO[0000] Extensions initializing
276-
INFO[0000] FileWatcher initializing
273+
INFO[0000] NginxBinary initializing
274+
INFO[0000] Commander initializing
275+
INFO[0000] Comms initializing
276+
INFO[0000] OneTimeRegistration initializing
277+
INFO[0000] Registering XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX
278+
INFO[0000] Metrics initializing
279+
INFO[0000] MetricsThrottle initializing
280+
INFO[0000] DataPlaneStatus initializing
281+
INFO[0000] MetricsThrottle waiting for report ready
282+
INFO[0000] Metrics waiting for handshake to be completed
283+
INFO[0000] ProcessWatcher initializing
284+
INFO[0000] Extensions initializing
285+
INFO[0000] FileWatcher initializing
277286
INFO[0000] FileWatchThrottle initializing
278-
INFO[0001] Events initializing
287+
INFO[0001] Events initializing
279288
INFO[0001] OneTimeRegistration completed
280289
```
281290

@@ -341,15 +350,15 @@ sudo make run
341350
## Supported Distributions
342351
NGINX Agent can run in most environments. For a list of supported distributions, see the [NGINX Technical Specs](https://docs.nginx.com/nginx/technical-specs/#supported-distributions) guide.
343352

344-
## Supported Deployment Environments
353+
## Supported Deployment Environments
345354
NGINX Agent can be deployed in the following environments:
346355

347356
- Bare Metal
348357
- Container
349358
- Public Cloud: AWS, Google Cloud Platform, and Microsoft Azure
350359
- Virtual Machine
351360

352-
## Supported NGINX Versions
361+
## Supported NGINX Versions
353362
NGINX Agent works with all supported versions of NGINX Open Source and NGINX Plus.
354363

355364
## Sizing Recommendations

scripts/.local-nfpm.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ contents:
1919
dst: /etc/systemd/system/nginx-agent.service
2020
- src: ./scripts/nginx-agent.logrotate
2121
dst: /etc/logrotate.d/nginx-agent
22+
- src: ./scripts/packages/nginx-agent.openrc
23+
dst: /etc/init.d/nginx-agent
24+
packager: apk
2225
- src: ./scripts/selinux/nginx_agent_selinux.8.gz
2326
dst: /usr/share/man/man8/nginx_agent_selinux.8.gz
2427
packager: rpm
@@ -39,3 +42,6 @@ scripts:
3942
postinstall: "./scripts/packages/postinstall.sh"
4043
preremove: "./scripts/packages/preremove.sh"
4144
postremove: "./scripts/packages/postremove.sh"
45+
apk:
46+
scripts:
47+
postupgrade: "./scripts/packages/postupgrade.sh"

scripts/packages/nginx-agent.openrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/sbin/openrc-run
2+
3+
description="NGINX Agent"
4+
command_background=true
5+
6+
cfgfile=${cfgfile:-/etc/nginx-agent/nginx-agent.conf}
7+
pidfile=/var/run/nginx-agent.pid
8+
command=/usr/bin/nginx-agent
9+
command_args=""
10+
required_files="$cfgfile"
11+
12+
depend() {
13+
need net
14+
use dns logger netmount
15+
}

scripts/packages/postinstall.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
2+
23
# Determine OS platform
34
# shellcheck source=/dev/null
45
. /etc/os-release
@@ -271,6 +272,17 @@ upgrade_config_file() {
271272
fi
272273
}
273274

275+
restart_agent_if_required() {
276+
if [ "${ID}" = "freebsd" ]; then
277+
# https://github.com/freebsd/pkg/pull/2128
278+
return
279+
fi
280+
if service nginx-agent status >/dev/null 2>&1; then
281+
printf "PostInstall: Restarting nginx agent\n"
282+
service nginx-agent restart || true
283+
fi
284+
}
285+
274286
summary() {
275287
echo "----------------------------------------------------------------------"
276288
echo " NGINX Agent package has been successfully installed."
@@ -301,5 +313,6 @@ summary() {
301313
update_unit_file
302314
add_default_config_file
303315
upgrade_config_file
316+
restart_agent_if_required
304317
summary
305318
}

scripts/packages/postremove.sh

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
#!/bin/sh
2+
23
# Determine OS platform
34
# shellcheck source=/dev/null
45
. /etc/os-release
56

6-
if [ "$ID" = "freebsd" ]; then
7-
echo "Stop and remove nginx-agent service"
7+
stop_agent_freebsd() {
8+
echo "Stopping nginx-agent service"
89
service nginx-agent onestop >/dev/null 2>&1 || true
10+
}
11+
12+
disable_agent_freebsd() {
13+
echo "Disabling nginx-agent service"
914
sysrc -x nginx_agent_enable >/dev/null 2>&1 || true
10-
elif command -V systemctl >/dev/null 2>&1; then
11-
echo "Stop and disable nginx-agent service"
15+
}
16+
17+
stop_agent_systemd() {
18+
echo "Stopping nginx-agent service"
1219
systemctl stop nginx-agent >/dev/null 2>&1 || true
20+
}
21+
22+
disable_agent_systemd() {
23+
echo "Disabling nginx-agent service"
1324
systemctl disable nginx-agent >/dev/null 2>&1 || true
25+
}
26+
27+
systemd_daemon_reload() {
1428
echo "Running daemon-reload"
1529
systemctl daemon-reload || true
16-
fi
30+
}
31+
32+
cleanup() {
33+
echo "Removing /var/run/nginx-agent directory"
34+
rm -rf "/var/run/nginx-agent"
35+
}
1736

18-
echo "Removing /var/run/nginx-agent directory"
19-
rm -rf "/var/run/nginx-agent"
20-
echo "Removing /var/log/nginx-agent directory"
21-
rm -rf "/var/log/nginx-agent"
37+
case "$ID" in
38+
freebsd)
39+
stop_agent_freebsd
40+
disable_agent_freebsd
41+
cleanup
42+
;;
43+
debian|ubuntu)
44+
if [ "$1" = "remove" ]; then
45+
stop_agent_systemd
46+
disable_agent_systemd
47+
systemd_daemon_reload
48+
cleanup
49+
fi
50+
;;
51+
rhel|fedora|centos|amzn|almalinux|rocky)
52+
if [ "$1" = "0" ]; then
53+
stop_agent_systemd
54+
disable_agent_systemd
55+
systemd_daemon_reload
56+
cleanup
57+
fi
58+
;;
59+
alpine)
60+
cleanup
61+
;;
62+
*)
63+
stop_agent_systemd
64+
disable_agent_systemd
65+
systemd_daemon_reload
66+
cleanup
67+
;;
68+
esac

scripts/packages/postupgrade.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
NEWVER="$1"
4+
OLDVER="$2"
5+
6+
restart_agent_if_required() {
7+
if service nginx-agent status >/dev/null 2>&1; then
8+
printf "PostUpgrade: Restarting nginx agent (upgraded to %s from %s)\n" "$NEWVER" "$OLDVER"
9+
service nginx-agent restart || true
10+
fi
11+
}
12+
13+
# Determine OS platform
14+
# shellcheck source=/dev/null
15+
. /etc/os-release
16+
17+
case "$ID" in
18+
alpine)
19+
restart_agent_if_required
20+
;;
21+
esac

scripts/packages/preinstall.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@ update_config_file() {
164164
ensure_sudo
165165
load_config_values
166166
update_config_file
167-
}
167+
}

scripts/packages/preremove.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
#!/bin/sh
22
# Pre Remove Steps
3+
4+
# Determine OS platform
5+
# shellcheck source=/dev/null
6+
. /etc/os-release
7+
8+
stop_agent_openrc() {
9+
echo "Stopping nginx-agent service"
10+
service nginx-agent stop 2>&1 || true
11+
}
12+
13+
case "$ID" in
14+
alpine)
15+
stop_agent_openrc
16+
;;
17+
esac

0 commit comments

Comments
 (0)