From 3b481902c4b1a1248f28d45f97ab6a1776d49a16 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Thu, 25 May 2023 15:21:12 +0400 Subject: [PATCH 1/7] 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 --- scripts/packages/postinstall.sh | 10 ++++++ scripts/packages/postremove.sh | 55 +++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/scripts/packages/postinstall.sh b/scripts/packages/postinstall.sh index 36cadc71e9..5b64a7cda4 100755 --- a/scripts/packages/postinstall.sh +++ b/scripts/packages/postinstall.sh @@ -1,4 +1,6 @@ #!/bin/sh +# vim:sw=4:ts=4:et: + # Determine OS platform # shellcheck source=/dev/null . /etc/os-release @@ -271,6 +273,13 @@ upgrade_config_file() { fi } +restart_agent_if_required() { + if service nginx-agent status >/dev/null 2>&1; then + printf "PostInstall: Restarting nginx agent\n" + service nginx-agent restart || true + fi +} + summary() { echo "----------------------------------------------------------------------" echo " NGINX Agent package has been successfully installed." @@ -301,5 +310,6 @@ summary() { update_unit_file add_default_config_file upgrade_config_file + restart_agent_if_required summary } diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index 6a8bb8e7d9..7495753250 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -1,21 +1,58 @@ #!/bin/sh +# vim:sw=4:ts=4:et: + # Determine OS platform # shellcheck source=/dev/null . /etc/os-release -if [ "$ID" = "freebsd" ]; then - echo "Stop and remove nginx-agent service" +_stop_agent_freebsd() { + echo "Stopping nginx-agent service" service nginx-agent onestop >/dev/null 2>&1 || true +} + +_disable_agent_freebsd() { + echo "Disabling nginx-agent service" sysrc -x nginx_agent_enable >/dev/null 2>&1 || true -elif command -V systemctl >/dev/null 2>&1; then - echo "Stop and disable nginx-agent service" +} + +_stop_agent_systemd() { + echo "Stopping nginx-agent service" systemctl stop nginx-agent >/dev/null 2>&1 || true +} + +_disable_agent_systemd() { + echo "Disabling nginx-agent service" systemctl disable nginx-agent >/dev/null 2>&1 || true +} + +_systemd_daemon_reload() { echo "Running daemon-reload" systemctl daemon-reload || true -fi +} + +_cleanup() { + echo "Removing /var/run/nginx-agent directory" + rm -rf "/var/run/nginx-agent" +} -echo "Removing /var/run/nginx-agent directory" -rm -rf "/var/run/nginx-agent" -echo "Removing /var/log/nginx-agent directory" -rm -rf "/var/log/nginx-agent" +case "$ID" in + freebsd) + _stop_agent_freebsd + _disable_agent_freebsd + _cleanup + ;; + debian|ubuntu) + if [ "$1" = "remove" ]; then + _stop_agent_systemd + _disable_agent_systemd + _systemd_daemon_reload + _cleanup + fi + ;; + *) + _stop_agent_systemd + _disable_agent_systemd + _systemd_daemon_reload + _cleanup + ;; +esac From 58413741c38acd6040d8786e38d7295070fe1281 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Tue, 20 Jun 2023 12:39:01 +0400 Subject: [PATCH 2/7] fix: do not stop service on upgrade (RHEL-based distros) --- scripts/packages/postremove.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index 7495753250..3043f32f9f 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -49,6 +49,14 @@ case "$ID" in _cleanup fi ;; + rhel|fedora|centos|amzn|almalinux|rocky) + if [ "$1" = "0" ]; then + _stop_agent_systemd + _disable_agent_systemd + _systemd_daemon_reload + _cleanup + fi + ;; *) _stop_agent_systemd _disable_agent_systemd From 41b3d94e3f1d477ed1e30fa99e06ff2e052666f4 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Tue, 20 Jun 2023 16:57:36 +0400 Subject: [PATCH 3/7] 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: https://github.com/freebsd/pkg/pull/2128 --- scripts/packages/postinstall.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/packages/postinstall.sh b/scripts/packages/postinstall.sh index 5b64a7cda4..46919c7d3d 100755 --- a/scripts/packages/postinstall.sh +++ b/scripts/packages/postinstall.sh @@ -274,6 +274,10 @@ upgrade_config_file() { } restart_agent_if_required() { + if [ "${ID}" = "freebsd" ]; then + # 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 From 5285e6a4a91f9c2da7702982a407b258a8d21553 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Wed, 21 Jun 2023 17:12:00 +0400 Subject: [PATCH 4/7] feat: OpenRC package scripts for Alpine Linux --- scripts/.local-nfpm.yaml | 6 ++++++ scripts/packages/nginx-agent.openrc | 15 +++++++++++++++ scripts/packages/postremove.sh | 3 +++ scripts/packages/postupgrade.sh | 22 ++++++++++++++++++++++ scripts/packages/preinstall.sh | 2 +- scripts/packages/preremove.sh | 16 ++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 scripts/packages/nginx-agent.openrc create mode 100644 scripts/packages/postupgrade.sh diff --git a/scripts/.local-nfpm.yaml b/scripts/.local-nfpm.yaml index 3d6fec373a..b7df26c33c 100644 --- a/scripts/.local-nfpm.yaml +++ b/scripts/.local-nfpm.yaml @@ -19,6 +19,9 @@ contents: dst: /etc/systemd/system/nginx-agent.service - src: ./scripts/nginx-agent.logrotate dst: /etc/logrotate.d/nginx-agent + - src: ./scripts/packages/nginx-agent.openrc + dst: /etc/init.d/nginx-agent + packager: apk - src: ./scripts/selinux/nginx_agent_selinux.8.gz dst: /usr/share/man/man8/nginx_agent_selinux.8.gz packager: rpm @@ -39,3 +42,6 @@ scripts: postinstall: "./scripts/packages/postinstall.sh" preremove: "./scripts/packages/preremove.sh" postremove: "./scripts/packages/postremove.sh" +apk: + scripts: + postupgrade: "./scripts/packages/postupgrade.sh" diff --git a/scripts/packages/nginx-agent.openrc b/scripts/packages/nginx-agent.openrc new file mode 100755 index 0000000000..dfa753572d --- /dev/null +++ b/scripts/packages/nginx-agent.openrc @@ -0,0 +1,15 @@ +#!/sbin/openrc-run + +description="nginx-agent" +command_background=true + +cfgfile=${cfgfile:-/etc/nginx-agent/nginx-agent.conf} +pidfile=/var/run/nginx-agent.pid +command=/usr/bin/nginx-agent +command_args="" +required_files="$cfgfile" + +depend() { + need net + use dns logger netmount +} diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index 3043f32f9f..3ff11576bc 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -57,6 +57,9 @@ case "$ID" in _cleanup fi ;; + alpine) + _cleanup + ;; *) _stop_agent_systemd _disable_agent_systemd diff --git a/scripts/packages/postupgrade.sh b/scripts/packages/postupgrade.sh new file mode 100644 index 0000000000..dac612165d --- /dev/null +++ b/scripts/packages/postupgrade.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# vim:sw=4:ts=4:et: + +NEWVER="$1" +OLDVER="$2" + +_restart_agent_if_required() { + if service nginx-agent status >/dev/null 2>&1; then + printf "PostUpgrade: Restarting nginx agent (upgraded to %s from %s)\n" "$NEWVER" "$OLDVER" + service nginx-agent restart || true + fi +} + +# Determine OS platform +# shellcheck source=/dev/null +. /etc/os-release + +case "$ID" in + alpine) + _restart_agent_if_required + ;; +esac diff --git a/scripts/packages/preinstall.sh b/scripts/packages/preinstall.sh index 7464c1ef25..972a2507ad 100644 --- a/scripts/packages/preinstall.sh +++ b/scripts/packages/preinstall.sh @@ -164,4 +164,4 @@ update_config_file() { ensure_sudo load_config_values update_config_file -} \ No newline at end of file +} diff --git a/scripts/packages/preremove.sh b/scripts/packages/preremove.sh index 67bb50f422..0643646db0 100644 --- a/scripts/packages/preremove.sh +++ b/scripts/packages/preremove.sh @@ -1,2 +1,18 @@ #!/bin/sh +# vim:sw=4:ts=4:et: # Pre Remove Steps + +# Determine OS platform +# shellcheck source=/dev/null +. /etc/os-release + +_stop_agent_openrc() { + echo "Stopping nginx-agent service" + service nginx-agent stop 2>&1 || true +} + +case "$ID" in + alpine) + _stop_agent_openrc + ;; +esac From 920ad17daac32ea7a1953abe5543c376cc6c3948 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Wed, 21 Jun 2023 17:34:10 +0400 Subject: [PATCH 5/7] chore: add Alpine Linux instructions to README While here, removed extra trailing spaces across the doc. --- README.md | 57 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e37db68406..599fb5f458 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ To install NGINX Agent on your system, go to [Releases](https://github.com/nginx Use your system's package manager to install the package. Some examples: -Debian, Ubuntu, and other distributions using the `dpkg` package manager. +Debian, Ubuntu, and other distributions using the `dpkg` package manager. ``` sudo dpkg -i nginx-agent-.deb @@ -121,7 +121,16 @@ To enable the NGINX Agent to start on boot, run the following command: sudo systemctl enable nginx-agent ``` -## Logging +On Alpine Linux, use the following command to start the agent: +``` +sudo service nginx-agent start +``` +To enable the agent to start at boot time on Alpine Linux, run this: +``` +sudo rc-update add nginx-agent default +``` + +## Logging 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. ### Log Rotation @@ -171,7 +180,7 @@ Follow steps in the [Installation](#installation) section to download, install, 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. ## Installing Go -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/). +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/). ## Starting the gRPC Mock Control Plane 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 ``` ## NGINX Agent Settings -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. +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. ``` sudo mkdir /etc/nginx-agent sudo cp /nginx-agent.conf /etc/nginx-agent/ ``` -Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run. +Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run. ``` sudo touch /var/lib/nginx-agent/agent-dynamic.conf ``` @@ -242,7 +251,7 @@ Open a web browser to view the Swagger UI at http://localhost:8082/docs. ## Extensions 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. -To enable an extension, it must be added to the extensions list in the `/etc/nginx-agent/nginx-agent.conf`. +To enable an extension, it must be added to the extensions list in the `/etc/nginx-agent/nginx-agent.conf`. Here is an example of enabling the advanced metrics extension: ```yaml @@ -258,24 +267,24 @@ Open another terminal window and start the NGINX Agent. Issue the following comm sudo make run # Command Output snippet -WARN[0000] Log level is info -INFO[0000] setting displayName to XXX +WARN[0000] Log level is info +INFO[0000] setting displayName to XXX INFO[0000] NGINX Agent at with pid 12345, clientID=XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX name=XXX -INFO[0000] NginxBinary initializing -INFO[0000] Commander initializing -INFO[0000] Comms initializing -INFO[0000] OneTimeRegistration initializing -INFO[0000] Registering XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX -INFO[0000] Metrics initializing -INFO[0000] MetricsThrottle initializing -INFO[0000] DataPlaneStatus initializing -INFO[0000] MetricsThrottle waiting for report ready -INFO[0000] Metrics waiting for handshake to be completed -INFO[0000] ProcessWatcher initializing -INFO[0000] Extensions initializing -INFO[0000] FileWatcher initializing +INFO[0000] NginxBinary initializing +INFO[0000] Commander initializing +INFO[0000] Comms initializing +INFO[0000] OneTimeRegistration initializing +INFO[0000] Registering XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX +INFO[0000] Metrics initializing +INFO[0000] MetricsThrottle initializing +INFO[0000] DataPlaneStatus initializing +INFO[0000] MetricsThrottle waiting for report ready +INFO[0000] Metrics waiting for handshake to be completed +INFO[0000] ProcessWatcher initializing +INFO[0000] Extensions initializing +INFO[0000] FileWatcher initializing INFO[0000] FileWatchThrottle initializing -INFO[0001] Events initializing +INFO[0001] Events initializing INFO[0001] OneTimeRegistration completed ``` @@ -341,7 +350,7 @@ sudo make run ## Supported Distributions 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. -## Supported Deployment Environments +## Supported Deployment Environments NGINX Agent can be deployed in the following environments: - Bare Metal @@ -349,7 +358,7 @@ NGINX Agent can be deployed in the following environments: - Public Cloud: AWS, Google Cloud Platform, and Microsoft Azure - Virtual Machine -## Supported NGINX Versions +## Supported NGINX Versions NGINX Agent works with all supported versions of NGINX Open Source and NGINX Plus. ## Sizing Recommendations From 35a82dc16f4623cd2789e61a3ab5bd70a53d539d Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Mon, 26 Jun 2023 18:55:11 +0400 Subject: [PATCH 6/7] chore: adjust service description, remove modelines --- scripts/packages/nginx-agent.openrc | 2 +- scripts/packages/postinstall.sh | 1 - scripts/packages/postremove.sh | 1 - scripts/packages/postupgrade.sh | 1 - scripts/packages/preremove.sh | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts/packages/nginx-agent.openrc b/scripts/packages/nginx-agent.openrc index dfa753572d..b7c92963d2 100755 --- a/scripts/packages/nginx-agent.openrc +++ b/scripts/packages/nginx-agent.openrc @@ -1,6 +1,6 @@ #!/sbin/openrc-run -description="nginx-agent" +description="NGINX Agent" command_background=true cfgfile=${cfgfile:-/etc/nginx-agent/nginx-agent.conf} diff --git a/scripts/packages/postinstall.sh b/scripts/packages/postinstall.sh index 46919c7d3d..c44686b32f 100755 --- a/scripts/packages/postinstall.sh +++ b/scripts/packages/postinstall.sh @@ -1,5 +1,4 @@ #!/bin/sh -# vim:sw=4:ts=4:et: # Determine OS platform # shellcheck source=/dev/null diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index 3ff11576bc..4e2051302a 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -1,5 +1,4 @@ #!/bin/sh -# vim:sw=4:ts=4:et: # Determine OS platform # shellcheck source=/dev/null diff --git a/scripts/packages/postupgrade.sh b/scripts/packages/postupgrade.sh index dac612165d..54915a0d21 100644 --- a/scripts/packages/postupgrade.sh +++ b/scripts/packages/postupgrade.sh @@ -1,5 +1,4 @@ #!/bin/sh -# vim:sw=4:ts=4:et: NEWVER="$1" OLDVER="$2" diff --git a/scripts/packages/preremove.sh b/scripts/packages/preremove.sh index 0643646db0..f49a76f888 100644 --- a/scripts/packages/preremove.sh +++ b/scripts/packages/preremove.sh @@ -1,5 +1,4 @@ #!/bin/sh -# vim:sw=4:ts=4:et: # Pre Remove Steps # Determine OS platform From 9d70b92304b324205db4619f4d42ea432e7a2a28 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Tue, 27 Jun 2023 19:03:45 +0400 Subject: [PATCH 7/7] chore: remove leading underscore from shell func names --- scripts/packages/postremove.sh | 44 ++++++++++++++++----------------- scripts/packages/postupgrade.sh | 4 +-- scripts/packages/preremove.sh | 4 +-- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index 4e2051302a..6b2c739f2f 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -4,65 +4,65 @@ # shellcheck source=/dev/null . /etc/os-release -_stop_agent_freebsd() { +stop_agent_freebsd() { echo "Stopping nginx-agent service" service nginx-agent onestop >/dev/null 2>&1 || true } -_disable_agent_freebsd() { +disable_agent_freebsd() { echo "Disabling nginx-agent service" sysrc -x nginx_agent_enable >/dev/null 2>&1 || true } -_stop_agent_systemd() { +stop_agent_systemd() { echo "Stopping nginx-agent service" systemctl stop nginx-agent >/dev/null 2>&1 || true } -_disable_agent_systemd() { +disable_agent_systemd() { echo "Disabling nginx-agent service" systemctl disable nginx-agent >/dev/null 2>&1 || true } -_systemd_daemon_reload() { +systemd_daemon_reload() { echo "Running daemon-reload" systemctl daemon-reload || true } -_cleanup() { +cleanup() { echo "Removing /var/run/nginx-agent directory" rm -rf "/var/run/nginx-agent" } case "$ID" in freebsd) - _stop_agent_freebsd - _disable_agent_freebsd - _cleanup + stop_agent_freebsd + disable_agent_freebsd + cleanup ;; debian|ubuntu) if [ "$1" = "remove" ]; then - _stop_agent_systemd - _disable_agent_systemd - _systemd_daemon_reload - _cleanup + stop_agent_systemd + disable_agent_systemd + systemd_daemon_reload + cleanup fi ;; rhel|fedora|centos|amzn|almalinux|rocky) if [ "$1" = "0" ]; then - _stop_agent_systemd - _disable_agent_systemd - _systemd_daemon_reload - _cleanup + stop_agent_systemd + disable_agent_systemd + systemd_daemon_reload + cleanup fi ;; alpine) - _cleanup + cleanup ;; *) - _stop_agent_systemd - _disable_agent_systemd - _systemd_daemon_reload - _cleanup + stop_agent_systemd + disable_agent_systemd + systemd_daemon_reload + cleanup ;; esac diff --git a/scripts/packages/postupgrade.sh b/scripts/packages/postupgrade.sh index 54915a0d21..6b95c3b528 100644 --- a/scripts/packages/postupgrade.sh +++ b/scripts/packages/postupgrade.sh @@ -3,7 +3,7 @@ NEWVER="$1" OLDVER="$2" -_restart_agent_if_required() { +restart_agent_if_required() { if service nginx-agent status >/dev/null 2>&1; then printf "PostUpgrade: Restarting nginx agent (upgraded to %s from %s)\n" "$NEWVER" "$OLDVER" service nginx-agent restart || true @@ -16,6 +16,6 @@ _restart_agent_if_required() { case "$ID" in alpine) - _restart_agent_if_required + restart_agent_if_required ;; esac diff --git a/scripts/packages/preremove.sh b/scripts/packages/preremove.sh index f49a76f888..6ee1c2611c 100644 --- a/scripts/packages/preremove.sh +++ b/scripts/packages/preremove.sh @@ -5,13 +5,13 @@ # shellcheck source=/dev/null . /etc/os-release -_stop_agent_openrc() { +stop_agent_openrc() { echo "Stopping nginx-agent service" service nginx-agent stop 2>&1 || true } case "$ID" in alpine) - _stop_agent_openrc + stop_agent_openrc ;; esac