Skip to content

Commit 5712773

Browse files
committed
Update preinstall script to update agent config from V2 to V3
1 parent a6d5cc4 commit 5712773

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

scripts/packages/preinstall.sh

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,36 @@ load_config_values() {
112112
update_config_file() {
113113
echo "Checking what version of NGINX Agent is already installed"
114114
check_version="nginx-agent --version"
115-
nginx_agent_version=$($check_version 2>&1)
116-
if [ $? -eq 0 ]; then
117-
echo "Currently NGINX Agent version is $nginx_agent_version"
115+
nginx_agent_version=$($check_version 2>&1) || true
116+
if [ -z "${nginx_agent_version##nginx-agent version v2*}" ]; then
117+
echo "Updating NGINX Agent V2 configuration to V3 configuration"
118+
echo "Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf"
119+
cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf
118120

119-
if [ -z "${nginx_agent_version##nginx-agent version v2*}" ]; then
120-
echo "Updating NGINX Agent V2 configuration to V3 configuration"
121-
echo "Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf"
122-
cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf
123-
124-
nginx_one_host="agent.connect.nginx.com"
125-
v2_config_file=$AGENT_CONFIG_FILE
126-
v3_config_file=$AGENT_CONFIG_FILE
127-
128-
if grep -q "$nginx_one_host" ${v2_config_file}; then
129-
echo "N1 connected agent"
130-
else
131-
echo "${RED}Previous version of NGINX Agent was not connected to NGINX One. Stopping upgrade.${NC}"
132-
exit 1
133-
fi
134-
135-
token=`grep "token:" "${v2_config_file}"`
136-
token=`echo $token | cut -d ":" -f 2 | xargs`
137-
138-
config_dirs=`grep "config_dirs:" "${v2_config_file}"`
139-
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`
140-
141-
allowed_directories=""
142-
export IFS=":"
143-
for config_dir in $config_dirs; do
144-
allowed_directories="${allowed_directories}\n - ${config_dir}"
145-
done
146-
147-
v3_config_contents="
121+
nginx_one_host="agent.connect.nginx.com"
122+
v2_config_file=$AGENT_CONFIG_FILE
123+
v3_config_file=$AGENT_CONFIG_FILE
124+
125+
if grep -q "$nginx_one_host" ${v2_config_file}; then
126+
echo "N1 connected agent"
127+
else
128+
echo "${RED}Previous version of NGINX Agent was not connected to NGINX One. Stopping upgrade.${NC}"
129+
exit 1
130+
fi
131+
132+
token=`grep "token:" "${v2_config_file}"`
133+
token=`echo $token | cut -d ":" -f 2 | xargs`
134+
135+
config_dirs=`grep "config_dirs:" "${v2_config_file}"`
136+
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`
137+
138+
allowed_directories=""
139+
export IFS=":"
140+
for config_dir in $config_dirs; do
141+
allowed_directories="${allowed_directories}\n - ${config_dir}"
142+
done
143+
144+
v3_config_contents="
148145
#
149146
# /etc/nginx-agent/nginx-agent.conf
150147
#
@@ -195,8 +192,7 @@ collector:
195192
value: ${token}
196193
"
197194

198-
echo "${v3_config_contents}" > $v3_config_file
199-
fi
195+
echo "${v3_config_contents}" > $v3_config_file
200196
fi
201197
}
202198

0 commit comments

Comments
 (0)