Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move agent-dynamic.conf to /var/lib/nginx-agent #268

Merged
merged 21 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ sudo cp <project_root_directory>/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
Expand Down
2 changes: 1 addition & 1 deletion examples/grafana-metrics/nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions hugo/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <project_root_directory>/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
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions scripts/packages/postremove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 etc directory"
aphralG marked this conversation as resolved.
Show resolved Hide resolved
rm -rf "/usr/local/etc/nginx-agent"
echo "Removing db 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 directory"
rm -rf "/etc/nginx-agent"
echo "Removing lib directory"
rm -rf "/var/lib/nginx-agent"
fi

echo "Removing run directory"
rm -rf "/var/run/nginx-agent"
echo "Removing log directory"
rm -rf "/var/log/nginx-agent"
23 changes: 17 additions & 6 deletions scripts/packages/preinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand Down
48 changes: 31 additions & 17 deletions sdk/client/commander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 8 additions & 7 deletions src/core/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/integration/install/install_uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.