Skip to content

Commit 7bc297c

Browse files
authored
Update preinstall script to update agent config from V2 to V3 (#999)
1 parent 346d778 commit 7bc297c

File tree

5 files changed

+203
-32
lines changed

5 files changed

+203
-32
lines changed

internal/collector/otelcol.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
receivers:
22
{{- if ne .Receivers.HostMetrics nil }}
33
hostmetrics:
4+
{{- if .Receivers.HostMetrics.CollectionInterval }}
45
collection_interval: {{ .Receivers.HostMetrics.CollectionInterval }}
6+
{{- end}}
7+
{{- if .Receivers.HostMetrics.InitialDelay }}
58
initial_delay: {{ .Receivers.HostMetrics.InitialDelay }}
9+
{{- end}}
610
scrapers:
711
{{- if .Receivers.HostMetrics.Scrapers }}
812
{{- if .Receivers.HostMetrics.Scrapers.CPU }}

scripts/packages/preinstall.sh

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ set -e
66
# Description:
77
# NGINX Agent install script for downloading the NGINX Agent package from the appropriate repository
88
#
9-
################################
10-
###### Changeable variables
11-
################################
12-
13-
LOG_LEVEL=""
14-
159
################################
1610
###### Default variables
1711
################################
12+
export NGINX_ONE_HOST="${NGINX_AGENT_SERVER_HOST:-agent.connect.nginx.com}"
1813
export AGENT_GROUP="${AGENT_GROUP:-$(id -ng)}"
1914

15+
RED_COLOUR='\033[0;31m'
16+
NO_COLOUR='\033[0m'
17+
2018
# Determine OS platform
2119
# shellcheck source=/dev/null
2220
. /etc/os-release
@@ -45,34 +43,92 @@ ensure_sudo() {
4543
}
4644

4745
update_config_file() {
48-
sed_cmd="sed -i.bak "
49-
50-
if [ ! -f "$AGENT_CONFIG_FILE" ]; then
51-
printf "NGINX Agent config file %s does not exist. Could not be updated\n" "$AGENT_CONFIG_FILE"
52-
exit 0
53-
fi
46+
echo "Checking what version of NGINX Agent is already installed"
47+
check_version="nginx-agent --version"
48+
nginx_agent_version=$($check_version 2>&1) || true
49+
if [ -z "${nginx_agent_version##nginx-agent version v2*}" ]; then
50+
echo "Updating NGINX Agent V2 configuration to V3 configuration"
51+
echo "Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf"
52+
cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf
53+
54+
v2_config_file=$AGENT_CONFIG_FILE
55+
v3_config_file=$AGENT_CONFIG_FILE
56+
57+
echo "NGINX Agent server host should be ${NGINX_ONE_HOST}"
58+
59+
if grep -q "$NGINX_ONE_HOST" ${v2_config_file}; then
60+
echo "NGINX Agent is configured to connect to NGINX One"
61+
else
62+
echo "${RED_COLOUR}Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR}"
63+
exit 1
64+
fi
65+
66+
token=`grep "token:" "${v2_config_file}"`
67+
token=`echo $token | cut -d ":" -f 2 | xargs`
68+
69+
config_dirs=`grep "config_dirs:" "${v2_config_file}"`
70+
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`
71+
72+
allowed_directories=""
73+
IFS=":"
74+
for config_dir in $config_dirs; do
75+
allowed_directories="${allowed_directories}\n - ${config_dir}"
76+
done
77+
78+
allowed_directories="${allowed_directories}\n - /var/log/nginx"
79+
80+
v3_config_contents="
81+
#
82+
# /etc/nginx-agent/nginx-agent.conf
83+
#
84+
# Configuration file for NGINX Agent.
85+
#
5486
55-
if [ "${PACKAGE_HOST}" ]; then
56-
printf "Updating %s ...\n" "${AGENT_CONFIG_FILE}"
87+
log:
88+
# set log level (error, info, debug; default \"info\")
89+
level: info
90+
# set log path. if empty, don't log to file.
91+
path: /var/log/nginx-agent/
5792
58-
# Replace Host
59-
${sed_cmd} "s/host:.*$/host: ${PACKAGE_HOST}/" "${AGENT_CONFIG_FILE}"
60-
fi
61-
62-
# Check the log-level and set accordingly
63-
if [ "${LOG_LEVEL}" ]; then
64-
if [ "$(grep -cP '^(?=[\s]*+[^#])[^#]*(level:)' "${AGENT_CONFIG_FILE}")" -ge 1 ]; then
65-
printf "Setting existing log level: %s\n" "${LOG_LEVEL}"
66-
${sed_cmd} "/^[[:space:]]*#/!s/\(level:.*\)/level: ${LOG_LEVEL}/g" "${AGENT_CONFIG_FILE}"
67-
else
68-
printf "Setting log level: %s\n" "${LOG_LEVEL}"
69-
_log_level_replacement="s/^log:/log:\\
70-
level: ${LOG_LEVEL}/"
93+
allowed_directories: ${allowed_directories}
7194
72-
${sed_cmd} "${_log_level_replacement}" "${AGENT_CONFIG_FILE}"
73-
printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}"
74-
fi
75-
printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}"
95+
command:
96+
server:
97+
host: ${NGINX_ONE_HOST}
98+
port: 443
99+
auth:
100+
token: ${token}
101+
tls:
102+
skip_verify: false
103+
104+
collector:
105+
receivers:
106+
host_metrics:
107+
scrapers:
108+
cpu: {}
109+
memory: {}
110+
disk: {}
111+
network: {}
112+
filesystem: {}
113+
processors:
114+
batch: {}
115+
exporters:
116+
otlp_exporters:
117+
- server:
118+
host: ${NGINX_ONE_HOST}
119+
port: 443
120+
authenticator: headers_setter
121+
tls:
122+
skip_verify: false
123+
extensions:
124+
headers_setter:
125+
headers:
126+
- action: insert
127+
key: \"authorization\"
128+
value: ${token}
129+
"
130+
131+
echo "${v3_config_contents}" > $v3_config_file
76132
fi
77133
}
78134

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Copyright (C) Nginx, Inc. 2022.
5+
#
6+
# Description:
7+
# NGINX Agent script for converting NGINX AGENT V2 config format to V3 config format
8+
9+
export NGINX_ONE_HOST="${NGINX_AGENT_SERVER_HOST:-agent.connect.nginx.com}"
10+
11+
RED_COLOUR='\033[0;31m'
12+
NO_COLOUR='\033[0m'
13+
14+
for i in "$@"; do
15+
case $i in
16+
--v2-config-file=*)
17+
v2_config_file="${i#*=}"
18+
shift
19+
;;
20+
--v3-config-file=*)
21+
v3_config_file="${i#*=}"
22+
shift
23+
;;
24+
-*|--*)
25+
echo "Unknown option $i"
26+
exit 1
27+
;;
28+
*)
29+
;;
30+
esac
31+
done
32+
33+
echo "NGINX Agent server host should be ${NGINX_ONE_HOST}"
34+
35+
if grep -q "$NGINX_ONE_HOST" ${v2_config_file}; then
36+
echo "NGINX Agent is configured to connect to NGINX One"
37+
else
38+
echo "${RED_COLOUR}Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR}"
39+
exit 1
40+
fi
41+
42+
token=`grep "token:" "${v2_config_file}"`
43+
token=`echo $token | cut -d ":" -f 2 | xargs`
44+
45+
config_dirs=`grep "config_dirs:" "${v2_config_file}"`
46+
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`
47+
48+
allowed_directories=""
49+
IFS=":"
50+
for config_dir in $config_dirs; do
51+
allowed_directories="${allowed_directories}\n - ${config_dir}"
52+
done
53+
54+
allowed_directories="${allowed_directories}\n - /var/log/nginx"
55+
56+
v3_config_contents="
57+
#
58+
# /etc/nginx-agent/nginx-agent.conf
59+
#
60+
# Configuration file for NGINX Agent.
61+
#
62+
63+
log:
64+
# set log level (error, info, debug; default \"info\")
65+
level: info
66+
# set log path. if empty, don't log to file.
67+
path: /var/log/nginx-agent/
68+
69+
allowed_directories: ${allowed_directories}
70+
71+
command:
72+
server:
73+
host: ${NGINX_ONE_HOST}
74+
port: 443
75+
auth:
76+
token: ${token}
77+
tls:
78+
skip_verify: false
79+
80+
collector:
81+
receivers:
82+
host_metrics:
83+
scrapers:
84+
cpu: {}
85+
memory: {}
86+
disk: {}
87+
network: {}
88+
filesystem: {}
89+
processors:
90+
batch: {}
91+
exporters:
92+
otlp_exporters:
93+
- server:
94+
host: ${NGINX_ONE_HOST}
95+
port: 443
96+
authenticator: headers_setter
97+
tls:
98+
skip_verify: false
99+
extensions:
100+
headers_setter:
101+
headers:
102+
- action: insert
103+
key: \"authorization\"
104+
value: ${token}
105+
"
106+
107+
echo "${v3_config_contents}" > $v3_config_file
108+
109+
echo "NGINX Agent configuration successfully upgraded"

test/docker/nginx-official-image/apk/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ WORKDIR /agent
1212
COPY ./build/${PACKAGE_NAME}.apk /agent/build/${PACKAGE_NAME}.apk
1313
COPY ./ /agent
1414

15+
RUN apk del nginx-agent
1516
RUN apk add --allow-untrusted /agent/build/${PACKAGE_NAME}.apk
1617

1718
RUN unlink /var/log/nginx/access.log

test/docker/nginx-official-image/deb/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ WORKDIR /agent
1313
COPY ./build/${PACKAGE_NAME}.deb /agent/build/${PACKAGE_NAME}.deb
1414
COPY ./ /agent
1515

16-
RUN apt-get update \
16+
RUN apt-get update \
17+
&& apt purge -y nginx-agent \
1718
&& apt install --no-install-recommends --no-install-suggests -y /agent/build/${PACKAGE_NAME}.deb \
1819
&& rm /agent/build/${PACKAGE_NAME}.deb
1920

0 commit comments

Comments
 (0)