Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 15 additions & 46 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,17 +151,12 @@ 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
if [ ! -f /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
cat <<EOF > /etc/nginx-agent/nginx-agent.conf
#
# /etc/nginx-agent/nginx-agent.conf
#
Expand Down Expand Up @@ -249,37 +227,33 @@ 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
chmod 0640 /etc/nginx-agent/nginx-agent.conf
fi
}

upgrade_config_file() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the functions upgrade_config_file and add_default_config_file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default nginx-agent.conf is packaged with the agent so I actually dont know why these functions are here. Its probably for some legacy reasons. I'd say remove them and then test out the default install to make sure things still work.

if [ -f "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf ]; then
if [ -f /etc/nginx-agent/nginx-agent.conf ]; then
extensions=""
if grep -q "advanced_metrics:" "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf; then
if grep -q "advanced_metrics:" /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
if grep -q "nginx_app_protect:" /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
if grep -q "nap_monitoring:" /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
if ! grep -q "extensions:" /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
printf "\nextensions:\n" >> /etc/nginx-agent/nginx-agent.conf
for extension in ${extensions}; do
echo " - $extension" >> "${BSD_HIER}"/etc/nginx-agent/nginx-agent.conf
echo " - $extension" >> /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 +265,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 Down
82 changes: 1 addition & 81 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,38 +21,7 @@ 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
Expand All @@ -77,68 +45,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 +83,5 @@ update_config_file() {
{
title
ensure_sudo
load_config_values
update_config_file
}