|
10 | 10 | tomlFormat = pkgs.formats.toml { }; |
11 | 11 | jsonFormat = pkgs.formats.json { }; |
12 | 12 |
|
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; |
15 | 23 | # See https://docs.aws.amazon.com/prescriptive-guidance/latest/implementing-logging-monitoring-cloudwatch/create-store-cloudwatch-configurations.html#store-cloudwatch-configuration-s3. |
16 | 24 | # |
17 | 25 | # We don't use the multiple JSON configuration files feature, |
|
24 | 32 | options.services.amazon-cloudwatch-agent = { |
25 | 33 | enable = lib.mkEnableOption "Amazon CloudWatch Agent"; |
26 | 34 | 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; |
30 | 38 | description = '' |
31 | 39 | Amazon CloudWatch Agent common configuration. See |
32 | 40 | <https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html#CloudWatch-Agent-profile-instance-first> |
33 | 41 | 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`. |
34 | 59 | ''; |
35 | 60 | example = { |
36 | 61 | credentials = { |
|
44 | 69 | }; |
45 | 70 | }; |
46 | 71 | }; |
| 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 | + }; |
47 | 93 | configuration = lib.mkOption { |
48 | 94 | type = jsonFormat.type; |
49 | 95 | default = { }; |
50 | 96 | 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`. |
54 | 100 | ''; |
55 | 101 | # Subset of "CloudWatch agent configuration file: Complete examples" and "CloudWatch agent configuration file: Traces section" in the description link. |
56 | 102 | # |
|
110 | 156 | }; |
111 | 157 | }; |
112 | 158 | }; |
| 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 | + }; |
113 | 168 | mode = lib.mkOption { |
114 | 169 | type = lib.types.str; |
115 | 170 | default = "auto"; |
|
122 | 177 | }; |
123 | 178 |
|
124 | 179 | 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. |
126 | 181 | systemd.services.amazon-cloudwatch-agent = { |
127 | 182 | description = "Amazon CloudWatch Agent"; |
128 | 183 | after = [ "network.target" ]; |
|
140 | 195 | # 3. Runs "amazon-cloudwatch-agent" with the paths to these generated files. |
141 | 196 | # |
142 | 197 | # Re-implementing with systemd options. |
143 | | - User = lib.attrByPath [ |
144 | | - "agent" |
145 | | - "run_as_user" |
146 | | - ] "root" cfg.configuration; |
| 198 | + User = cfg.user; |
147 | 199 | RuntimeDirectory = "amazon-cloudwatch-agent"; |
148 | 200 | 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 | + ]; |
164 | 216 | KillMode = "process"; |
165 | 217 | Restart = "on-failure"; |
166 | 218 | RestartSec = 60; |
167 | 219 | }; |
168 | | - restartTriggers = [ |
169 | | - cfg.package |
170 | | - commonConfigurationFile |
171 | | - configurationFile |
172 | | - configurationDirectory |
173 | | - cfg.mode |
174 | | - ]; |
175 | 220 | }; |
176 | 221 | }; |
177 | | - |
178 | | - meta.maintainers = pkgs.amazon-cloudwatch-agent.meta.maintainers; |
179 | 222 | } |
0 commit comments