44# Copyright (C) Nginx, Inc. 2022.
55#
66# Description:
7- # NGINX Agent install script for downloading the NGINX Agent package from the appropriate repository
7+ # NGINX Agent pre install script
88#
99# ###############################
1010# ##### Default variables
@@ -44,64 +44,78 @@ ensure_sudo() {
4444}
4545
4646update_config_file () {
47+ # Check for existing agent binary and version
4748 echo " Checking what version of NGINX Agent is already installed"
48- check_version=" nginx-agent --version"
49- nginx_agent_version=$( $check_version 2>&1 ) || true
50- if [ -z " ${nginx_agent_version## nginx-agent version v2* } " ]; then
51- echo " Updating NGINX Agent V2 configuration to V3 configuration"
52- echo " Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf"
53- cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf
54-
55- v2_config_file=$AGENT_CONFIG_FILE
56- v3_config_file=$AGENT_CONFIG_FILE
57-
58- echo " NGINX Agent server host should be ${NGINX_ONE_HOST} "
59-
60- if grep -q " $NGINX_ONE_HOST " ${v2_config_file} ; then
49+ if command -v nginx-agent > /dev/null 2>&1 ; then
50+ nginx_agent_version=$( nginx-agent -v 2>&1 )
51+ echo " Existing NGINX Agent version: $nginx_agent_version "
52+ else
53+ echo " No existing NGINX Agent installation found, skipping migration"
54+ return 0
55+ fi
56+
57+ # Only proceed if it's v2
58+ if echo " $nginx_agent_version " | grep -qE ' ^nginx-agent version v2' ; then
59+ echo " Migrating NGINX Agent configuration from V2 to V3 format"
60+
61+ # Backup v2 config if present
62+ if [ -f " $AGENT_CONFIG_FILE " ]; then
63+ echo " Backing up existing NGINX Agent V2 configuration to $AGENT_CONFIG_FILE .v2-backup"
64+ cp -v " $AGENT_CONFIG_FILE " " $AGENT_CONFIG_FILE .v2-backup" \
65+ || err_exit " Failed to back up v2 config"
66+ else
67+ echo " No existing NGINX Agent V2 config file found, skipping backup"
68+ fi
69+
70+ v2_config_file=" $AGENT_CONFIG_FILE "
71+ v3_config_file=" $AGENT_CONFIG_FILE "
72+
73+ echo " Verifying configured NGINX One host: ${NGINX_ONE_HOST} "
74+ if [ -f " $v2_config_file " ] && grep -q " $NGINX_ONE_HOST " " $v2_config_file " ; then
6175 echo " NGINX Agent is configured to connect to NGINX One"
62- else
63- echo " ${RED_COLOUR} Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR} "
64- exit 1
76+ else
77+ err_exit " Upgrade aborted: existing NGINX Agent V2 is not configured for NGINX One"
6578 fi
66-
67- token=` grep " token:" " ${v2_config_file} " `
68- token=` echo $token | cut -d " :" -f 2 | xargs`
69-
70- instance_group=` grep " instance_group:" " ${AGENT_DYNAMIC_CONFIG_FILE} " `
71- instance_group=` echo $instance_group | cut -d " :" -f 2 | xargs`
72-
73- labels=" "
74-
75- if [ -n " ${instance_group} " ]; then
76- echo " Adding config sync group to NGINX Agent configuration"
79+
80+ # Extract token
81+ if token_line=$( grep " token:" " $v2_config_file " ) ; then
82+ token=$( echo " $token_line " | cut -d " :" -f 2 | xargs)
83+ else
84+ err_exit " Upgrade aborted: no token found in v2 config"
85+ fi
86+
87+ # Extract instance_group if present
88+ instance_group=" "
89+ if [ -f " $AGENT_DYNAMIC_CONFIG_FILE " ] && instance_line=$( grep " instance_group:" " $AGENT_DYNAMIC_CONFIG_FILE " ) ; then
90+ instance_group=$( echo " $instance_line " | cut -d " :" -f 2 | xargs)
91+ echo " Migrating existing Config Sync Group: $instance_group "
7792 labels="
7893labels:
7994 config-sync-group: ${instance_group}
8095"
96+ else
97+ labels=" "
8198 fi
82-
83- config_dirs=` grep " config_dirs:" " ${v2_config_file} " `
84- config_dirs=` echo $config_dirs | cut -d " \" " -f 2`
85-
99+
100+ # Extract config_dirs if present
101+ config_dirs=" "
102+ if config_line=$( grep " config_dirs:" " $v2_config_file " ) ; then
103+ config_dirs=$( echo " $config_line " | cut -d ' "' -f 2)
104+ fi
105+
86106 allowed_directories=" "
87107 IFS=" :"
88108 for config_dir in $config_dirs ; do
89- allowed_directories=" ${allowed_directories} \n - ${config_dir} "
109+ allowed_directories=" ${allowed_directories} \n - ${config_dir} "
90110 done
91-
92111 allowed_directories=" ${allowed_directories} \n - /var/log/nginx"
93-
112+
113+ echo " Writing new v3 configuration to $v3_config_file "
94114 v3_config_contents="
95- #
96115# /etc/nginx-agent/nginx-agent.conf
97- #
98- # Configuration file for NGINX Agent.
99- #
100116
101117log:
102- # set log level (error, info, debug; default \" info\" )
103118 level: info
104- # set log path. if empty, don't log to file.
105119 path: /var/log/nginx-agent/
106120
107121allowed_directories: ${allowed_directories}
@@ -114,9 +128,12 @@ command:
114128 token: ${token}
115129 tls:
116130 skip_verify: false
117- "
118-
119- echo " ${v3_config_contents} " > $v3_config_file
131+ "
132+
133+ echo " $v3_config_contents " > " $v3_config_file " \
134+ || err_exit " Failed to write v3 config"
135+ else
136+ echo " Existing NGINX Agent version is not v2, skipping config migration"
120137 fi
121138}
122139
0 commit comments