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
143 changes: 5 additions & 138 deletions scripts/packages/postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
# shellcheck source=/dev/null
. /etc/os-release

if [ "$ID" = "freebsd" ]; then
BSD_HIER="/usr/local"
AGENT_EXE="${BSD_HIER}/bin/nginx-agent"
else
AGENT_EXE="/usr/bin/nginx-agent"
BSD_HIER=""
fi

AGENT_EXE="/usr/bin/nginx-agent"
AGENT_RUN_DIR="/var/run/nginx-agent"
AGENT_LOG_DIR="/var/log/nginx-agent"
AGENT_UNIT_LOCATION="/etc/systemd/system"
Expand Down Expand Up @@ -107,18 +102,6 @@ create_agent_group() {
fi
fi

if [ "$ID" = "freebsd" ]; then
printf "PostInstall: Adding nginx-agent group %s\n" "${AGENT_GROUP}"
pw groupadd "${AGENT_GROUP}"

printf "PostInstall: Adding NGINX / agent user %s to group %s\n" "${AGENT_USER}" "${AGENT_GROUP}"
pw groupmod "${AGENT_GROUP}" -M "${AGENT_USER}"
if [ "${WORKER_USER}" ]; then
printf "PostInstall: Adding NGINX Worker user %s to group %s\n" "${WORKER_USER}" "${AGENT_GROUP}"
pw groupmod "${AGENT_GROUP}" -M "${WORKER_USER}"
fi
fi

if [ "$ID" = "alpine" ]; then
printf "PostInstall: Adding nginx-agent group %s\n" "${AGENT_GROUP}"
addgroup "${AGENT_GROUP}"
Expand Down Expand Up @@ -168,118 +151,9 @@ update_unit_file() {
printf "PostInstall: Set the enabled flag for the service unit\n"
systemctl enable "${AGENT_UNIT_FILE}"
fi

if [ "$ID" = "freebsd" ]; then
printf "PostInstall: Enabling NGINX Agent Service\n"
sysrc nginx_agent_enable=YES
fi
}

add_default_config_file() {
if [ ! -f "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf ]; then
printf "PostInstall: Creating default nginx-agent.conf file\n"
cat <<EOF > "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf
#
# /etc/nginx-agent/nginx-agent.conf
#
# Configuration file for NGINX Agent.
#
# This file is to track NGINX Agent configuration values that are meant to be statically set. There
# are additional NGINX Agent configuration values that are set via the API and NGINX Agent install script
# which can be found in /var/lib/nginx-agent/agent-dynamic.conf.

# specify the server grpc port to connect to
server:
# host of the control plane
host: 127.0.0.1
grpcPort: 54789
# provide servername overrides if using SNI
# metrics: ""
# command: ""
# tls options
tls:
# enable tls in the nginx-agent setup for grpcs
# default to enable to connect with tls connection but without client cert for mtls
enable: false
# specify the absolute path to the CA certificate file to use for verifying
# the server certificate (also requires 'skip_verify: false' below)
# by default, this will be the trusted root CAs found in the OS CA store
# ca: /etc/nginx-agent/ca.pem
# specify the absolute path to the client cert, when mtls is enabled
# cert: /etc/nginx-agent/client.crt
# specify the absolute path to the client cert key, when mtls is enabled
# key: /etc/nginx-agent/client.key
# controls whether the server certificate chain and host name are verified.
# for production use, see instructions for configuring TLS
skip_verify: true
log:
# set log level (panic, fatal, error, info, debug, trace; default "info")
level: info
# set log path. if empty, don't log to file.
path: /var/log/nginx-agent/
# data plane status message / 'heartbeat'
nginx:
# path of NGINX logs to exclude
exclude_logs: ""
socket: "unix:/var/run/nginx-agent/nginx.sock"

dataplane:
status:
# poll interval for data plane status - the frequency the NGINX Agent will query the dataplane for changes
poll_interval: 30s
# report interval for data plane status - the maximum duration to wait before syncing dataplane information if no updates have being observed
report_interval: 24h
metrics:
# specify the size of a buffer to build before sending metrics
bulk_size: 20
# specify metrics poll interval
report_interval: 1m
collection_interval: 15s
mode: aggregated

# OSS NGINX default config path
# path to aux file dirs can also be added
allowed_directories:
- /etc/nginx
- /usr/local/etc/nginx
- /usr/share/nginx/modules
- /etc/nms
api:
# default port for NGINX Agent API, this is for the server configuration of the REST API
port: 8081
EOF
printf "PostInstall: Updating file permissions for nginx-agent.conf to 0640\n"
chmod 0640 "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf
fi
}

upgrade_config_file() {
if [ -f "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf ]; then
extensions=""
if grep -q "advanced_metrics:" "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf; then
extensions="${extensions} advanced-metrics"
fi
if grep -q "nginx_app_protect:" "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf; then
extensions="${extensions} nginx-app-protect"
fi
if grep -q "nap_monitoring:" "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf; then
extensions="${extensions} nap-monitoring"
fi
if ! grep -q "extensions:" "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf && [ "${#extensions}" -ne "0" ]; then
printf "PostInstall: Updating nginx-agent.conf to include extensions array\n"
printf "\nextensions:\n" >> "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf
for extension in ${extensions}; do
echo " - $extension" >> "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf
done
fi
fi
}

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
Expand All @@ -291,15 +165,10 @@ summary() {
echo " NGINX Agent package has been successfully installed."
echo ""
echo " Please follow the next steps to start the software:"
if [ "$ID" = "freebsd" ]; then
echo " sudo service nginx-agent start"
echo ""
else
echo " sudo systemctl start nginx-agent"
echo ""
fi
echo " sudo systemctl start nginx-agent"
echo ""
echo " Configuration settings can be adjusted here:"
echo " ${BSD_HIER}/etc/nginx-agent/nginx-agent.conf"
echo " /etc/nginx-agent/nginx-agent.conf"
echo ""
echo "----------------------------------------------------------------------"
}
Expand All @@ -314,8 +183,6 @@ summary() {
create_agent_group
create_run_dir
update_unit_file
add_default_config_file
upgrade_config_file
restart_agent_if_required
summary
}
15 changes: 0 additions & 15 deletions scripts/packages/postremove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
# shellcheck source=/dev/null
. /etc/os-release

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
}

stop_agent_systemd() {
echo "Stopping nginx-agent service"
systemctl stop nginx-agent >/dev/null 2>&1 || true
Expand All @@ -35,11 +25,6 @@ cleanup() {
}

case "$ID" in
freebsd)
stop_agent_freebsd
disable_agent_freebsd
cleanup
;;
debian|ubuntu)
if [ "$1" = "remove" ]; then
stop_agent_systemd
Expand Down
83 changes: 1 addition & 82 deletions scripts/packages/preinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ set -e
################################

LOG_LEVEL=""
INSTANCE_GROUP=""

################################
###### Default variables
Expand All @@ -22,43 +21,11 @@ export AGENT_GROUP="${AGENT_GROUP:-$(id -ng)}"
# shellcheck source=/dev/null
. /etc/os-release

if [ "$ID" = "freebsd" ]; then
AGENT_CONFIG_FILE=${AGENT_CONFIG_FILE:-"/usr/local/etc/nginx-agent/nginx-agent.conf"}
AGENT_DYNAMIC_CONFIG_DIR="/var/db/nginx-agent"
# Old location of agent-dynamic.conf
OLD_DYNAMIC_CONFIG_DIR="/etc/nginx-agent"
mkdir -p /var/log/nginx-agent/
else
AGENT_CONFIG_FILE=${AGENT_CONFIG_FILE:-"/etc/nginx-agent/nginx-agent.conf"}
AGENT_DYNAMIC_CONFIG_DIR="/var/lib/nginx-agent"
# Old location of agent-dynamic.conf
OLD_DYNAMIC_CONFIG_DIR="/etc/nginx-agent"
fi

AGENT_DYNAMIC_CONFIG_FILE="${AGENT_DYNAMIC_CONFIG_DIR}/agent-dynamic.conf"
OLD_DYNAMIC_CONFIG_FILE="${OLD_DYNAMIC_CONFIG_DIR}/agent-dynamic.conf"
AGENT_DYNAMIC_CONFIG_COMMENT="#
# agent-dynamic.conf
#
# Dynamic configuration file for NGINX Agent.
#
# The purpose of this file is to track NGINX Agent configuration
# values that can be dynamically changed via the API and the NGINX Agent install script.
# You may edit this file, but API calls that modify the tags on this system will
# overwrite the tag values in this file.
#
# The NGINX Agent configuration values that API calls can modify are as follows:
# - tags
#
# The NGINX Agent configuration value(s) that the NGINX Agent install script can modify are as follows:
# - instance_group

"
AGENT_CONFIG_FILE=${AGENT_CONFIG_FILE:-"/etc/nginx-agent/nginx-agent.conf"}

#
# Functions
#

err_exit() {
printf "\n%b" "$1"
printf " exiting.\n"
Expand All @@ -77,68 +44,21 @@ ensure_sudo() {
fi
}

create_config_file() {
mkdir -p ${AGENT_DYNAMIC_CONFIG_DIR}
printf "%s" "${AGENT_DYNAMIC_CONFIG_COMMENT}" | tee ${AGENT_DYNAMIC_CONFIG_FILE} > /dev/null
chmod 0640 ${AGENT_DYNAMIC_CONFIG_FILE}
printf "Successfully created %s\n" "${AGENT_DYNAMIC_CONFIG_FILE}"
}

load_config_values() {
if [ ! -f "$AGENT_DYNAMIC_CONFIG_FILE" ]; then
if [ -f "$OLD_DYNAMIC_CONFIG_FILE" ]; then
printf "Moving %s to %s\n" "$OLD_DYNAMIC_CONFIG_FILE" "$AGENT_DYNAMIC_CONFIG_FILE"
mkdir -p ${AGENT_DYNAMIC_CONFIG_DIR}
mv "$OLD_DYNAMIC_CONFIG_FILE" "$AGENT_DYNAMIC_CONFIG_FILE"
printf "Creating symlink %s at %s\n" "$AGENT_DYNAMIC_CONFIG_FILE" "$OLD_DYNAMIC_CONFIG_FILE"
ln -s "$AGENT_DYNAMIC_CONFIG_FILE" "$OLD_DYNAMIC_CONFIG_FILE"
else
printf "Could not find %s ... Creating file\n" ${AGENT_DYNAMIC_CONFIG_FILE}
create_config_file
fi

fi

# Check if there are existing values
_instance_group="$(grep "^instance_group:" "${AGENT_DYNAMIC_CONFIG_FILE}" | head -n 1 | cut -d : -f 2 | sed "s/^[[:space:]]//")"

if [ "$_instance_group" ] && [ ! "${INSTANCE_GROUP}" ]; then
INSTANCE_GROUP=$_instance_group
fi
}

update_config_file() {
sed_cmd="sed -i.bak "

printf "Updating %s ...\n" "${AGENT_DYNAMIC_CONFIG_FILE}"

if [ ! -f "$AGENT_CONFIG_FILE" ]; then
printf "NGINX Agent config file %s does not exist. Could not be updated\n" "$AGENT_CONFIG_FILE"
exit 0
fi

if [ ! -f "$AGENT_DYNAMIC_CONFIG_FILE" ]; then
err_exit "$AGENT_DYNAMIC_CONFIG_FILE does not exist"
fi

if [ "${PACKAGE_HOST}" ]; then
printf "Updating %s ...\n" "${AGENT_CONFIG_FILE}"

# Replace Host
${sed_cmd} "s/host:.*$/host: ${PACKAGE_HOST}/" "${AGENT_CONFIG_FILE}"
fi

# Check the instance group and set accordingly
if [ "${INSTANCE_GROUP}" ]; then
if [ "$(grep -cP '^(?=[\s]*+[^#])[^#]*(instance_group)' "${AGENT_DYNAMIC_CONFIG_FILE}")" -ge 1 ]; then
printf "Setting existing instance_group: %s\n" "${INSTANCE_GROUP}"
${sed_cmd} "/^[[:space:]]*#/!s/\(instance_group:.*\)/instance_group: ${INSTANCE_GROUP}/g" "${AGENT_DYNAMIC_CONFIG_FILE}"
else
printf "Setting instance_group: %s\n" "${INSTANCE_GROUP}"
printf "instance_group: %s\n" "${INSTANCE_GROUP}" >> "${AGENT_DYNAMIC_CONFIG_FILE}"
fi
printf "Successfully updated %s\n" "${AGENT_DYNAMIC_CONFIG_FILE}"
fi
# Check the log-level and set accordingly
if [ "${LOG_LEVEL}" ]; then
if [ "$(grep -cP '^(?=[\s]*+[^#])[^#]*(level:)' "${AGENT_CONFIG_FILE}")" -ge 1 ]; then
Expand All @@ -162,6 +82,5 @@ update_config_file() {
{
title
ensure_sudo
load_config_values
update_config_file
}