diff --git a/README.md b/README.md index 5f29d0321..1d9f18a45 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ sudo cp /nginx-agent.conf /etc/nginx-agent/ ``` Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run. ``` -sudo touch /etc/nginx-agent/agent-dynamic.conf +sudo touch /var/lib/nginx-agent/agent-dynamic.conf ``` ### Enabling the gRPC interface diff --git a/examples/grafana-metrics/nginx-agent.conf b/examples/grafana-metrics/nginx-agent.conf index 48464cd82..d310f1866 100644 --- a/examples/grafana-metrics/nginx-agent.conf +++ b/examples/grafana-metrics/nginx-agent.conf @@ -5,7 +5,7 @@ # # This file is to track agent configuration values that are meant to be statically set. There # are additional agent configuration values that are set via the API and agent install script -# which can be found in /etc/nginx-agent/agent-dynamic.conf. +# which can be found in /var/lib/nginx-agent/agent-dynamic.conf. api: # port to expose http api diff --git a/hugo/content/getting-started.md b/hugo/content/getting-started.md index 891baf883..a6f800e67 100644 --- a/hugo/content/getting-started.md +++ b/hugo/content/getting-started.md @@ -42,9 +42,9 @@ If it doesn't already exist, create the `/etc/nginx-agent/` directory and copy t sudo mkdir /etc/nginx-agent sudo cp /nginx-agent.conf /etc/nginx-agent/ ``` -Create the `agent-dynamic.conf` file in the `/etc/nginx-agent/` directory, which is required for NGINX Agent to run. +Create the `agent-dynamic.conf` file in the `/var/lib/nginx-agent/` directory, which is required for NGINX Agent to run. ``` -sudo touch /etc/nginx-agent/agent-dynamic.conf +sudo touch /var/lib/nginx-agent/agent-dynamic.conf ``` ### Enable the gRPC interface diff --git a/main.go b/main.go index 373436939..2c802eb0a 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "context" "os" "os/signal" + "runtime" "strconv" "strings" "syscall" @@ -42,7 +43,11 @@ func init() { config.SetVersion(version, commit) config.SetDefaults() config.RegisterFlags() - configPath, err := config.RegisterConfigFile(config.DynamicConfigFileAbsPath, config.ConfigFileName, config.ConfigFilePaths()...) + dynamicConfigPath := config.DynamicConfigFileAbsPath + if runtime.GOOS == "freebsd" { + dynamicConfigPath = config.DynamicConfigFileAbsFreeBsdPath + } + configPath, err := config.RegisterConfigFile(dynamicConfigPath, config.ConfigFileName, config.ConfigFilePaths()...) if err != nil { log.Fatalf("Failed to load configuration file: %v", err) } diff --git a/nginx-agent.conf b/nginx-agent.conf index 6be903301..adee227ab 100644 --- a/nginx-agent.conf +++ b/nginx-agent.conf @@ -5,7 +5,7 @@ # # This file is to track agent configuration values that are meant to be statically set. There # are additional agent configuration values that are set via the API and agent install script -# which can be found in /etc/nginx-agent/agent-dynamic.conf. +# which can be found in /var/lib/nginx-agent/agent-dynamic.conf. log: # set log level (panic, fatal, error, info, debug, trace; default "info") diff --git a/scripts/packages/postinstall.sh b/scripts/packages/postinstall.sh index 6a7993e7b..b7c92f499 100755 --- a/scripts/packages/postinstall.sh +++ b/scripts/packages/postinstall.sh @@ -185,7 +185,7 @@ add_default_config_file() { # # This file is to track agent configuration values that are meant to be statically set. There # are additional agent configuration values that are set via the API and agent install script -# which can be found in /etc/nginx-agent/agent-dynamic.conf. +# which can be found in /var/lib/nginx-agent/agent-dynamic.conf. # specify the server grpc port to connect to server: diff --git a/scripts/packages/postremove.sh b/scripts/packages/postremove.sh index a8e050073..75e8379dc 100755 --- a/scripts/packages/postremove.sh +++ b/scripts/packages/postremove.sh @@ -7,13 +7,23 @@ if [ "$ID" = "freebsd" ]; then echo "Stop and remove nginx-agent service" service nginx-agent onestop >/dev/null 2>&1 || true sysrc -x nginx_agent_enable >/dev/null 2>&1 || true + echo "Removing /usr/local/etc/nginx-agent directory" + rm -rf "/usr/local/etc/nginx-agent" + echo "Removing /var/db/nginx-agent directory" + rm -rf "/var/db/nginx-agent" elif command -V systemctl >/dev/null 2>&1; then echo "Stop and disable nginx-agent service" systemctl stop nginx-agent >/dev/null 2>&1 || true systemctl disable nginx-agent >/dev/null 2>&1 || true echo "Running daemon-reload" systemctl daemon-reload || true + echo "Removing /etc/nginx-agent directory" + rm -rf "/etc/nginx-agent" + echo "Removing /var/lib/nginx-agent directory" + rm -rf "/var/lib/nginx-agent" fi -echo "Removing run directory" +echo "Removing /var/run/nginx-agent directory" rm -rf "/var/run/nginx-agent" +echo "Removing /var/log/nginx-agent directory" +rm -rf "/var/log/nginx-agent" diff --git a/scripts/packages/preinstall.sh b/scripts/packages/preinstall.sh index 16db11f60..e1238a329 100644 --- a/scripts/packages/preinstall.sh +++ b/scripts/packages/preinstall.sh @@ -24,14 +24,19 @@ export AGENT_GROUP="${AGENT_GROUP:-$(id -ng)}" if [ "$ID" = "freebsd" ]; then AGENT_CONFIG_FILE=${AGENT_CONFIG_FILE:-"/usr/local/etc/nginx-agent/nginx-agent.conf"} - AGENT_DYNAMIC_CONFIG_DIR="/usr/local/etc/nginx-agent" + 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="/etc/nginx-agent" + 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="# # dynamic-agent.conf # @@ -77,10 +82,16 @@ ensure_sudo() { load_config_values() { # If the file doesn't exist attempt to create it if [ ! -f "$AGENT_DYNAMIC_CONFIG_FILE" ]; then - printf "Could not find %s ... Creating file\n" ${AGENT_DYNAMIC_CONFIG_FILE} - mkdir -p ${AGENT_DYNAMIC_CONFIG_DIR} - printf "%s" "${AGENT_DYNAMIC_CONFIG_COMMENT}" | tee ${AGENT_DYNAMIC_CONFIG_FILE} > /dev/null - printf "Successfully created %s\n" "${AGENT_DYNAMIC_CONFIG_FILE}" + if [ -f "$OLD_DYNAMIC_CONFIG_FILE" ]; then + mkdir -p ${AGENT_DYNAMIC_CONFIG_DIR} + ln "$OLD_DYNAMIC_CONFIG_FILE" "$AGENT_DYNAMIC_CONFIG_FILE" + else + printf "Could not find %s ... Creating file\n" ${AGENT_DYNAMIC_CONFIG_FILE} + mkdir -p ${AGENT_DYNAMIC_CONFIG_DIR} + printf "%s" "${AGENT_DYNAMIC_CONFIG_COMMENT}" | tee ${AGENT_DYNAMIC_CONFIG_FILE} > /dev/null + printf "Successfully created %s\n" "${AGENT_DYNAMIC_CONFIG_FILE}" + fi + fi # Check if there are existing values diff --git a/sdk/client/commander_test.go b/sdk/client/commander_test.go index b6d3ec795..7da6162c9 100644 --- a/sdk/client/commander_test.go +++ b/sdk/client/commander_test.go @@ -85,7 +85,9 @@ func TestCommander_Recv(t *testing.T) { select { case actual := <-commanderClient.Recv(): - assert.Equal(t, "1234", actual.Meta().MessageId) + if actual != nil { + assert.Equal(t, "1234", actual.Meta().MessageId) + } case <-time.After(1 * time.Second): t.Fatalf("No message received from commander") } @@ -112,7 +114,9 @@ func TestCommander_Send(t *testing.T) { select { case actual := <-commandService.handler.fromClient: - assert.Equal(t, "1234", actual.GetMeta().MessageId) + if actual != nil { + assert.Equal(t, "1234", actual.GetMeta().MessageId) + } case <-time.After(1 * time.Second): t.Fatalf("No message received from commander") } @@ -249,7 +253,9 @@ func TestCommander_Recv_Reconnect(t *testing.T) { select { case actual := <-commanderClient.Recv(): - assert.Equal(t, "1234", actual.Meta().MessageId) + if actual != nil { + assert.Equal(t, "1234", actual.Meta().MessageId) + } case <-time.After(1 * time.Second): t.Fatalf("No message received from commander") } @@ -613,10 +619,12 @@ func (c *mockCommanderService) Download(request *proto.DownloadRequest, server p for { data := <-c.downloadChannel fmt.Printf("Download Send: %v\n", data) - err := server.Send(data) - if err != nil { - fmt.Printf("Download Send Error: %v\n", err) - return err + if data != nil { + err := server.Send(data) + if err != nil { + fmt.Printf("Download Send Error: %v\n", err) + return err + } } } } @@ -661,25 +669,31 @@ func (c *mockCommanderService) ensureHandler() *handler { func (h *handler) recvHandle(server proto.Commander_CommandChannelServer, wg *sync.WaitGroup) { for { cmd, err := server.Recv() - fmt.Printf("Recv Command: %v\n", cmd) - if err != nil { - fmt.Printf("Recv Command Error: %v\n", err) + if cmd != nil { + fmt.Printf("Recv Command: %v\n", cmd) + if err != nil { + fmt.Printf("Recv Command Error: %v\n", err) + wg.Done() + return + } + h.fromClient <- cmd wg.Done() - return } - h.fromClient <- cmd } } func (h *handler) sendHandle(server proto.Commander_CommandChannelServer, wg *sync.WaitGroup) { for { cmd := <-h.toClient - err := server.Send(cmd) - fmt.Printf("Send Command: %v\n", cmd) - if err != nil { - fmt.Printf("Send Command Error: %v\n", err) + if cmd != nil { + err := server.Send(cmd) + fmt.Printf("Send Command: %v\n", cmd) + if err != nil { + fmt.Printf("Send Command Error: %v\n", err) + wg.Done() + return + } wg.Done() - return } } } diff --git a/src/core/config/config.go b/src/core/config/config.go index 34aa9168d..508bce77d 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "sort" "strings" "time" @@ -235,7 +236,11 @@ func UpdateAgentConfig(systemId string, updateTags []string, updateFeatures []st // already set. dynamicCfgPath := Viper.GetString(DynamicConfigPathKey) if dynamicCfgPath == "" { - dynamicCfgPath = DynamicConfigFileAbsPath + if runtime.GOOS == "freebsd" { + dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath + } else { + dynamicCfgPath = DynamicConfigFileAbsPath + } } // Overwrite existing nginx-agent.conf with updated config @@ -334,7 +339,11 @@ func LoadPropertiesFromFile(cfg string) error { // already set. dynamicCfgPath := Viper.GetString(DynamicConfigPathKey) if dynamicCfgPath == "" { - dynamicCfgPath = DynamicConfigFileAbsPath + if runtime.GOOS == "freebsd" { + dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath + } else { + dynamicCfgPath = DynamicConfigFileAbsPath + } } dynamicCfgDir, dynamicCfgFile := filepath.Split(dynamicCfgPath) diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 73955f6c7..a89563bbf 100644 --- a/src/core/config/defaults.go +++ b/src/core/config/defaults.go @@ -81,13 +81,14 @@ var ( ) const ( - DynamicConfigFileName = "agent-dynamic.conf" - DynamicConfigFileAbsPath = "/etc/nginx-agent/agent-dynamic.conf" - ConfigFileName = "nginx-agent.conf" - ConfigFileType = "yaml" - EnvPrefix = "nms" - ConfigPathKey = "path" - DynamicConfigPathKey = "dynamic-config-path" + DynamicConfigFileName = "agent-dynamic.conf" + DynamicConfigFileAbsPath = "/var/lib/nginx-agent/agent-dynamic.conf" + DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" + ConfigFileName = "nginx-agent.conf" + ConfigFileType = "yaml" + EnvPrefix = "nms" + ConfigPathKey = "path" + DynamicConfigPathKey = "dynamic-config-path" CloudAccountIdKey = "cloudaccountid" LocationKey = "location" diff --git a/test/integration/api/nginx-agent.conf b/test/integration/api/nginx-agent.conf index b0212e62f..4c6129f0d 100644 --- a/test/integration/api/nginx-agent.conf +++ b/test/integration/api/nginx-agent.conf @@ -5,7 +5,7 @@ # # This file is to track agent configuration values that are meant to be statically set. There # are additional agent configuration values that are set via the API and agent install script -# which can be found in /etc/nginx-agent/agent-dynamic.conf. +# which can be found in /var/lib/nginx-agent/agent-dynamic.conf. api: # port to expose http api diff --git a/test/integration/install/install_uninstall_test.go b/test/integration/install/install_uninstall_test.go index 29a5288ab..38bbfb401 100644 --- a/test/integration/install/install_uninstall_test.go +++ b/test/integration/install/install_uninstall_test.go @@ -42,7 +42,7 @@ func TestAgentManualInstallUninstall(t *testing.T) { expectedAgentPaths := map[string]string{ "AgentConfigFile": "/etc/nginx-agent/nginx-agent.conf", - "AgentDynamicConfigFile": "/etc/nginx-agent/agent-dynamic.conf", + "AgentDynamicConfigFile": "/var/lib/nginx-agent/agent-dynamic.conf", } // Check the environment variable $PACKAGE_NAME is set diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go index 34aa9168d..508bce77d 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "sort" "strings" "time" @@ -235,7 +236,11 @@ func UpdateAgentConfig(systemId string, updateTags []string, updateFeatures []st // already set. dynamicCfgPath := Viper.GetString(DynamicConfigPathKey) if dynamicCfgPath == "" { - dynamicCfgPath = DynamicConfigFileAbsPath + if runtime.GOOS == "freebsd" { + dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath + } else { + dynamicCfgPath = DynamicConfigFileAbsPath + } } // Overwrite existing nginx-agent.conf with updated config @@ -334,7 +339,11 @@ func LoadPropertiesFromFile(cfg string) error { // already set. dynamicCfgPath := Viper.GetString(DynamicConfigPathKey) if dynamicCfgPath == "" { - dynamicCfgPath = DynamicConfigFileAbsPath + if runtime.GOOS == "freebsd" { + dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath + } else { + dynamicCfgPath = DynamicConfigFileAbsPath + } } dynamicCfgDir, dynamicCfgFile := filepath.Split(dynamicCfgPath) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go index 73955f6c7..a89563bbf 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go @@ -81,13 +81,14 @@ var ( ) const ( - DynamicConfigFileName = "agent-dynamic.conf" - DynamicConfigFileAbsPath = "/etc/nginx-agent/agent-dynamic.conf" - ConfigFileName = "nginx-agent.conf" - ConfigFileType = "yaml" - EnvPrefix = "nms" - ConfigPathKey = "path" - DynamicConfigPathKey = "dynamic-config-path" + DynamicConfigFileName = "agent-dynamic.conf" + DynamicConfigFileAbsPath = "/var/lib/nginx-agent/agent-dynamic.conf" + DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" + ConfigFileName = "nginx-agent.conf" + ConfigFileType = "yaml" + EnvPrefix = "nms" + ConfigPathKey = "path" + DynamicConfigPathKey = "dynamic-config-path" CloudAccountIdKey = "cloudaccountid" LocationKey = "location"