Add set-env ctl action to persist agent environment variables - #2211
Conversation
|
Addressed review findings in 742c887:
Additional re-test coverage on both platforms: Deferred (pre-existing or out of scope, tracked separately): |
|
Added darwin platform parity in a8b9586: the darwin ctl now supports both Tested on an EC2 mac2.metal (macOS Sonoma, arm64) with agent 1.300069.0b1529 plus this branch's binaries and ctl:
|
Adds integration test cases for the new set-env action in the ManageAgent document (synced in this branch, delegating to the ctl set-env action from aws/amazon-cloudwatch-agent#2211): - Happy path: custom key with a space-containing value; asserts command success, the ctl's "Set <KEY>" stdout, unchanged agent status/configstatus, and the pair persisted to env-config.json on disk (read back via AWS-RunShellScript/AWS-RunPowerShellScript with platform-specific paths). - Merge: a second key persists alongside the first. - Error path: empty optionalEnvironmentVariable fails with the document-level error message (new failure-path helper that requires terminal status Failed). Cases are appended after the existing configure flow so prior status assertions are unaffected, mirrored across the unix and windows implementations.
| @@ -448,6 +477,7 @@ main() { | |||
| # helper for rpm+deb uninstallation hooks, not expected to be called manually | |||
| preun) preun_all ;; | |||
| set-log-level) set_log_level_all "${log_level}" ;; | |||
There was a problem hiding this comment.
nit: Should we have set-log-level call set_env?
Adds integration test cases for the new set-env action in the ManageAgent document (synced in this branch, delegating to the ctl set-env action from aws/amazon-cloudwatch-agent#2211): - Happy path: custom key with a space-containing value; asserts command success, the ctl's "Set <KEY>" stdout, unchanged agent status/configstatus, and the pair persisted to env-config.json on disk (read back via AWS-RunShellScript/AWS-RunPowerShellScript with platform-specific paths). - Merge: a second key persists alongside the first. - Error path: empty optionalEnvironmentVariable fails with the document-level error message (new failure-path helper that requires terminal status Failed). Cases are appended after the existing configure flow so prior status assertions are unaffected, mirrored across the unix and windows implementations.
4efa2d0 to
6add08b
Compare
Adds a 'set-env' action to the Linux and Windows ctl scripts that wraps the agent binary's internal -setenv/-envconfig flags, so callers (e.g. setup scripts) no longer need to invoke the raw agent binary or hardcode the env-config.json path. Usage: amazon-cloudwatch-agent-ctl -a set-env -e KEY=VALUE amazon-cloudwatch-agent-ctl.ps1 -a set-env -e KEY=VALUE Keys set this way are retained across config translations (#2131) unless they are translator-managed keys.
…n set-env - Windows: invoke the agent binary directly instead of through cmd /c so values containing spaces or shell metacharacters are passed as a single argument and never interpreted by cmd.exe - Linux: check the agent binary exit code explicitly and fail with a message instead of relying on set -e - Document in usage text that translation-produced values are overwritten on fetch-config/restart
The darwin ctl predates set-log-level and never received it. Both actions are needed for parity now that the ManageAgent SSM document (which has a MacOS step) will offer set-env. Uses the same explicit exit-code handling as the Linux implementation.
6add08b to
7f063b4
Compare
Description of the issue
Setup/enablement scripts need to persist environment variables such as
CWAGENT_ROLE_ARNandAWS_REGIONintoenv-config.json. Today the only way to do that is invoking the agent binary's internal-setenv/-envconfigflags directly and hardcoding theenv-config.jsonpath, bypassing the public ctl interface. The ctl script already wraps this exact mechanism for one specific key viaset-log-level.Description of changes
Adds a generic
set-envaction to the Linux and Windows ctl scripts, following the existingset-log-levelpattern:amazon-cloudwatch-agent-ctl -a set-env -e KEY=VALUE(Linux)amazon-cloudwatch-agent-ctl.ps1 -a set-env -e KEY=VALUE(Windows)The action validates the KEY=VALUE format (rejects missing
=or empty key) and delegates to the agent binary's-setenv, which merges the key intoenv-config.json. Keys set this way are retained across config translations per #2131, unless they are translator-managed keys.License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Tests
Tested on EC2 with agent 1.300069.0b1529 plus this change, on both Amazon Linux 2023 (x86_64) and Windows Server 2022. On both platforms:
set-envcreatesenv-config.jsonwhen missing, merges additional keys, overwrites repeated keysNOEQUALS,=value, and missing-ewith exit 1 and usagefetch-configand full agent restart; translator-managedCWAGENT_LOG_LEVELis wiped/regenerated from the JSON config as expectedI! MY_CUSTOM_VAR is set to ...log lines) and the 30s env watcher hot-reloads changesset-log-levelregression: still works end to end, including hot reload