Skip to content

Commit 385bb02

Browse files
committed
Merge branch 'v3' into check-allowed-dir
2 parents 1ae2833 + 7bc297c commit 385bb02

File tree

15 files changed

+488
-56
lines changed

15 files changed

+488
-56
lines changed

internal/collector/otelcol.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
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 }}
913
cpu:
14+
metrics:
15+
system.cpu.utilization:
16+
enabled: true
17+
system.cpu.logical.count:
18+
enabled: true
1019
{{- end }}
1120
{{- if .Receivers.HostMetrics.Scrapers.Disk }}
1221
disk:

internal/command/command_plugin.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type (
3131
UpdateDataPlaneStatus(ctx context.Context, resource *mpi.Resource) error
3232
UpdateDataPlaneHealth(ctx context.Context, instanceHealths []*mpi.InstanceHealth) error
3333
SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error
34-
UpdateClient(client mpi.CommandServiceClient)
34+
UpdateClient(ctx context.Context, client mpi.CommandServiceClient) error
3535
Subscribe(ctx context.Context)
3636
IsConnected() bool
3737
CreateConnection(ctx context.Context, resource *mpi.Resource) (*mpi.CreateConnectionResponse, error)
@@ -68,7 +68,7 @@ func (cp *CommandPlugin) Init(ctx context.Context, messagePipe bus.MessagePipeIn
6868
}
6969

7070
func (cp *CommandPlugin) Close(ctx context.Context) error {
71-
slog.InfoContext(ctx, "Canceling subscribe context")
71+
slog.InfoContext(ctx, "Closing command plugin")
7272

7373
cp.subscribeMutex.Lock()
7474
if cp.subscribeCancel != nil {
@@ -178,12 +178,16 @@ func (cp *CommandPlugin) processDataPlaneResponse(ctx context.Context, msg *bus.
178178
func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Message) {
179179
slog.DebugContext(ctx, "Command plugin received connection reset")
180180
if newConnection, ok := msg.Data.(grpc.GrpcConnectionInterface); ok {
181-
err := cp.conn.Close(ctx)
182-
if err != nil {
183-
slog.ErrorContext(ctx, "Command plugin: unable to close connection", "error", err)
181+
connectionErr := cp.conn.Close(ctx)
182+
if connectionErr != nil {
183+
slog.ErrorContext(ctx, "Command plugin: unable to close connection", "error", connectionErr)
184184
}
185185
cp.conn = newConnection
186-
cp.commandService.UpdateClient(cp.conn.CommandServiceClient())
186+
err := cp.commandService.UpdateClient(ctx, cp.conn.CommandServiceClient())
187+
if err != nil {
188+
slog.ErrorContext(ctx, "Failed to reset connection", "error", err)
189+
return
190+
}
187191
slog.DebugContext(ctx, "Command service client reset successfully")
188192
}
189193
}

internal/command/command_service.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,19 @@ func (cs *CommandService) CreateConnection(
254254
return response, nil
255255
}
256256

257-
func (cs *CommandService) UpdateClient(client mpi.CommandServiceClient) {
257+
func (cs *CommandService) UpdateClient(ctx context.Context, client mpi.CommandServiceClient) error {
258258
cs.subscribeClientMutex.Lock()
259-
defer cs.subscribeClientMutex.Unlock()
260259
cs.commandServiceClient = client
260+
cs.subscribeClientMutex.Unlock()
261+
262+
cs.isConnected.Store(false)
263+
resp, err := cs.CreateConnection(ctx, cs.resource)
264+
if err != nil {
265+
return err
266+
}
267+
slog.InfoContext(ctx, "Successfully sent create connection request", "response", resp)
268+
269+
return nil
261270
}
262271

263272
// Retry callback for sending a data plane response to the Management Plane.

internal/command/command_service_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@ func TestCommandService_CreateConnection(t *testing.T) {
200200

201201
func TestCommandService_UpdateClient(t *testing.T) {
202202
commandServiceClient := &v1fakes.FakeCommandServiceClient{}
203+
ctx := context.Background()
203204

204205
commandService := NewCommandService(
205206
commandServiceClient,
206207
types.AgentConfig(),
207208
make(chan *mpi.ManagementPlaneRequest),
208209
)
209-
commandService.UpdateClient(commandServiceClient)
210+
err := commandService.UpdateClient(ctx, commandServiceClient)
211+
require.NoError(t, err)
210212
assert.NotNil(t, commandService.commandServiceClient)
211213
}
212214

internal/command/commandfakes/fake_command_service.go

Lines changed: 47 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/file/file_plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func (fp *FilePlugin) Init(ctx context.Context, messagePipe bus.MessagePipeInter
5151
}
5252

5353
func (fp *FilePlugin) Close(ctx context.Context) error {
54+
slog.InfoContext(ctx, "Closing file plugin")
5455
return fp.conn.Close(ctx)
5556
}
5657

internal/grpc/grpc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ func (gc *GrpcConnection) FileServiceClient() mpi.FileServiceClient {
115115
}
116116

117117
func (gc *GrpcConnection) Close(ctx context.Context) error {
118-
slog.InfoContext(ctx, "Closing grpc connection")
119-
120118
gc.mutex.Lock()
121119
defer gc.mutex.Unlock()
122120

123121
if gc.conn != nil {
122+
slog.InfoContext(ctx, "Closing grpc connection")
124123
err := gc.conn.Close()
125124
gc.conn = nil
126125
if err != nil {
@@ -354,7 +353,8 @@ func ProtoValidatorStreamClientInterceptor() (grpc.StreamClientInterceptor, erro
354353
func ValidateGrpcError(err error) error {
355354
if err != nil {
356355
if statusError, ok := status.FromError(err); ok {
357-
if statusError.Code() == codes.InvalidArgument || statusError.Code() == codes.Unimplemented {
356+
if statusError.Code() == codes.InvalidArgument || statusError.Code() == codes.Unimplemented ||
357+
statusError.Code() == codes.Canceled {
358358
return backoff.Permanent(err)
359359
}
360360
}

internal/resource/resource_plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *Resource) Init(ctx context.Context, messagePipe bus.MessagePipeInterfac
6060
}
6161

6262
func (*Resource) Close(ctx context.Context) error {
63-
slog.DebugContext(ctx, "Closing resource plugin")
63+
slog.InfoContext(ctx, "Closing resource plugin")
6464
return nil
6565
}
6666

internal/watcher/watcher_plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (w *Watcher) Init(ctx context.Context, messagePipe bus.MessagePipeInterface
112112
// nolint: unparam
113113
// error is always nil
114114
func (w *Watcher) Close(ctx context.Context) error {
115-
slog.DebugContext(ctx, "Closing watcher plugin")
115+
slog.InfoContext(ctx, "Closing watcher plugin")
116116

117117
w.cancel()
118118

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

0 commit comments

Comments
 (0)