Skip to content

Commit 43caf2e

Browse files
committed
amazon-cloudwatch-agent: let users specify configuration file paths
1 parent 5f02383 commit 43caf2e

File tree

4 files changed

+95
-49
lines changed

4 files changed

+95
-49
lines changed

ci/OWNERS

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,16 @@ nixos/modules/installer/tools/nix-fallback-paths.nix @NixOS/nix-team @raitobeza
134134
/nixos/modules/installer/sd-card/
135135

136136
# Amazon
137-
/nixos/modules/virtualisation/amazon-init.nix @arianvp
138-
/nixos/modules/virtualisation/ec2-data.nix @arianvp
139-
/nixos/modules/virtualisation/amazon-options.nix @arianvp
140-
/nixos/modules/virtualisation/amazon-image.nix @arianvp
141-
/nixos/maintainers/scripts/ec2/ @arianvp
142-
/nixos/modules/services/misc/amazon-ssm-agent.nix @arianvp
143-
/nixos/tests/amazon-ssm-agent.nix @arianvp
144-
/nixos/modules/system/boot/grow-partition.nix @arianvp
137+
/nixos/modules/virtualisation/amazon-init.nix @arianvp
138+
/nixos/modules/virtualisation/ec2-data.nix @arianvp
139+
/nixos/modules/virtualisation/amazon-options.nix @arianvp
140+
/nixos/modules/virtualisation/amazon-image.nix @arianvp
141+
/nixos/maintainers/scripts/ec2/ @arianvp
142+
/nixos/modules/services/misc/amazon-ssm-agent.nix @arianvp
143+
/nixos/tests/amazon-ssm-agent.nix @arianvp
144+
/nixos/modules/system/boot/grow-partition.nix @arianvp
145+
/nixos/modules/services/monitoring/amazon-cloudwatch-agent.nix @philipmw
146+
/nixos/tests/amazon-cloudwatch-agent.nix @philipmw
145147

146148
# nixos-rebuild-ng
147149
/pkgs/by-name/ni/nixos-rebuild-ng @thiagokokada

nixos/modules/services/monitoring/amazon-cloudwatch-agent.nix

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ let
1010
tomlFormat = pkgs.formats.toml { };
1111
jsonFormat = pkgs.formats.json { };
1212

13-
commonConfigurationFile = tomlFormat.generate "common-config.toml" cfg.commonConfiguration;
14-
configurationFile = jsonFormat.generate "amazon-cloudwatch-agent.json" cfg.configuration;
13+
commonConfigurationFile =
14+
if (cfg.commonConfigurationFile == null) then
15+
(tomlFormat.generate "common-config.toml" cfg.commonConfiguration)
16+
else
17+
cfg.commonConfigurationFile;
18+
configurationFile =
19+
if (cfg.configurationFile == null) then
20+
(jsonFormat.generate "amazon-cloudwatch-agent.json" cfg.configuration)
21+
else
22+
cfg.configurationFile;
1523
# See https://docs.aws.amazon.com/prescriptive-guidance/latest/implementing-logging-monitoring-cloudwatch/create-store-cloudwatch-configurations.html#store-cloudwatch-configuration-s3.
1624
#
1725
# We don't use the multiple JSON configuration files feature,
@@ -24,13 +32,30 @@ in
2432
options.services.amazon-cloudwatch-agent = {
2533
enable = lib.mkEnableOption "Amazon CloudWatch Agent";
2634
package = lib.mkPackageOption pkgs "amazon-cloudwatch-agent" { };
27-
commonConfiguration = lib.mkOption {
28-
type = tomlFormat.type;
29-
default = { };
35+
commonConfigurationFile = lib.mkOption {
36+
type = lib.types.nullOr lib.types.path;
37+
default = null;
3038
description = ''
3139
Amazon CloudWatch Agent common configuration. See
3240
<https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html#CloudWatch-Agent-profile-instance-first>
3341
for supported values.
42+
43+
{option}`commonConfigurationFile` takes precedence over {option}`commonConfiguration`.
44+
45+
Note: Restricted evaluation blocks access to paths outside the Nix store.
46+
This means detecting content changes for mutable paths (i.e. not input or content-addressed) can't be done.
47+
As a result, `nixos-rebuild` won't reload/restart the systemd unit when mutable path contents change.
48+
`systemctl restart amazon-cloudwatch-agent.service` must be used instead.
49+
'';
50+
example = "/etc/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json";
51+
};
52+
commonConfiguration = lib.mkOption {
53+
type = tomlFormat.type;
54+
default = { };
55+
description = ''
56+
See {option}`commonConfigurationFile`.
57+
58+
{option}`commonConfigurationFile` takes precedence over {option}`commonConfiguration`.
3459
'';
3560
example = {
3661
credentials = {
@@ -44,13 +69,34 @@ in
4469
};
4570
};
4671
};
72+
configurationFile = lib.mkOption {
73+
type = lib.types.nullOr lib.types.path;
74+
default = null;
75+
description = ''
76+
Amazon CloudWatch Agent configuration file. See
77+
<https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html>
78+
for supported values.
79+
80+
The following options aren't supported:
81+
* `agent.run_as_user`
82+
* Use {option}`user` instead.
83+
84+
{option}`configurationFile` takes precedence over {option}`configuration`.
85+
86+
Note: Restricted evaluation blocks access to paths outside the Nix store.
87+
This means detecting content changes for mutable paths (i.e. not input or content-addressed) can't be done.
88+
As a result, `nixos-rebuild` won't reload/restart the systemd unit when mutable path contents change.
89+
`systemctl restart amazon-cloudwatch-agent.service` must be used instead.
90+
'';
91+
example = "/etc/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json";
92+
};
4793
configuration = lib.mkOption {
4894
type = jsonFormat.type;
4995
default = { };
5096
description = ''
51-
Amazon CloudWatch Agent configuration. See
52-
<https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html>
53-
for supported values.
97+
See {option}`configurationFile`.
98+
99+
{option}`configurationFile` takes precedence over {option}`configuration`.
54100
'';
55101
# Subset of "CloudWatch agent configuration file: Complete examples" and "CloudWatch agent configuration file: Traces section" in the description link.
56102
#
@@ -110,6 +156,15 @@ in
110156
};
111157
};
112158
};
159+
# Replaces "agent.run_as_user" from the configuration file.
160+
user = lib.mkOption {
161+
type = lib.types.str;
162+
default = "root";
163+
description = ''
164+
The user that runs the Amazon CloudWatch Agent.
165+
'';
166+
example = "amazon-cloudwatch-agent";
167+
};
113168
mode = lib.mkOption {
114169
type = lib.types.str;
115170
default = "auto";
@@ -122,7 +177,7 @@ in
122177
};
123178

124179
config = lib.mkIf cfg.enable {
125-
# See https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300048.1/packaging/dependencies/amazon-cloudwatch-agent.service.
180+
# See https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300049.1/packaging/dependencies/amazon-cloudwatch-agent.service.
126181
systemd.services.amazon-cloudwatch-agent = {
127182
description = "Amazon CloudWatch Agent";
128183
after = [ "network.target" ];
@@ -140,40 +195,28 @@ in
140195
# 3. Runs "amazon-cloudwatch-agent" with the paths to these generated files.
141196
#
142197
# Re-implementing with systemd options.
143-
User = lib.attrByPath [
144-
"agent"
145-
"run_as_user"
146-
] "root" cfg.configuration;
198+
User = cfg.user;
147199
RuntimeDirectory = "amazon-cloudwatch-agent";
148200
LogsDirectory = "amazon-cloudwatch-agent";
149-
ExecStartPre = ''
150-
${cfg.package}/bin/config-translator \
151-
-config ${commonConfigurationFile} \
152-
-input ${configurationFile} \
153-
-input-dir ${configurationDirectory} \
154-
-mode ${cfg.mode} \
155-
-output ''${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.toml
156-
'';
157-
ExecStart = ''
158-
${cfg.package}/bin/amazon-cloudwatch-agent \
159-
-config ''${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.toml \
160-
-envconfig ''${RUNTIME_DIRECTORY}/env-config.json \
161-
-otelconfig ''${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.yaml \
162-
-pidfile ''${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.pid
163-
'';
201+
ExecStartPre = builtins.concatStringsSep " " [
202+
"${cfg.package}/bin/config-translator"
203+
"-config ${commonConfigurationFile}"
204+
"-input ${configurationFile}"
205+
"-input-dir ${configurationDirectory}"
206+
"-mode ${cfg.mode}"
207+
"-output \${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.toml"
208+
];
209+
ExecStart = builtins.concatStringsSep " " [
210+
"${cfg.package}/bin/amazon-cloudwatch-agent"
211+
"-config \${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.toml"
212+
"-envconfig \${RUNTIME_DIRECTORY}/env-config.json"
213+
"-otelconfig \${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.yaml"
214+
"-pidfile \${RUNTIME_DIRECTORY}/amazon-cloudwatch-agent.pid"
215+
];
164216
KillMode = "process";
165217
Restart = "on-failure";
166218
RestartSec = 60;
167219
};
168-
restartTriggers = [
169-
cfg.package
170-
commonConfigurationFile
171-
configurationFile
172-
configurationDirectory
173-
cfg.mode
174-
];
175220
};
176221
};
177-
178-
meta.maintainers = pkgs.amazon-cloudwatch-agent.meta.maintainers;
179222
}

nixos/tests/amazon-cloudwatch-agent.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import ./make-test-python.nix (
2727
in
2828
{
2929
name = "amazon-cloudwatch-agent";
30-
meta.maintainers = pkgs.amazon-cloudwatch-agent.meta.maintainers;
3130

3231
nodes.machine =
3332
{ config, pkgs, ... }:

pkgs/by-name/am/amazon-cloudwatch-agent/package.nix

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ buildGoModule rec {
1616
src = fetchFromGitHub {
1717
owner = "aws";
1818
repo = "amazon-cloudwatch-agent";
19-
rev = "refs/tags/v${version}";
19+
tag = "v${version}";
2020
hash = "sha256-gJrK+ai+EEKvBErjOyvu677WykUPuxYy9NrR+qV2yyo=";
2121
};
2222

2323
vendorHash = "sha256-OQSl7nFvnDjJbs756QN5ZE/Dx/AZqxsijG0Ks7FYCB8=";
2424

25-
# See the list in https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300048.1/Makefile#L68-L77.
25+
# See the list in https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300049.1/Makefile#L68-L77.
2626
subPackages = [
2727
"cmd/config-downloader"
2828
"cmd/config-translator"
@@ -32,7 +32,7 @@ buildGoModule rec {
3232
"cmd/amazon-cloudwatch-agent-config-wizard"
3333
];
3434

35-
# See https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300048.1/Makefile#L57-L64.
35+
# See https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300049.1/Makefile#L57-L64.
3636
#
3737
# Needed for "amazon-cloudwatch-agent -version" to not show "Unknown".
3838
postInstall = ''
@@ -43,6 +43,8 @@ buildGoModule rec {
4343

4444
nativeInstallCheckInputs = [ versionCheckHook ];
4545

46+
versionCheckProgram = "${builtins.placeholder "out"}/bin/amazon-cloudwatch-agent";
47+
4648
versionCheckProgramArg = "-version";
4749

4850
passthru = {

0 commit comments

Comments
 (0)