Skip to content

Commit 0f45eeb

Browse files
authored
Add set-env ctl action to persist agent environment variables (#2211)
1 parent 4cb0245 commit 0f45eeb

3 files changed

Lines changed: 118 additions & 5 deletions

File tree

packaging/darwin/amazon-cloudwatch-agent-ctl

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ readonly JSON="${CONFDIR}/amazon-cloudwatch-agent.json"
3232
readonly JSON_DIR="${CONFDIR}/amazon-cloudwatch-agent.d"
3333
readonly CV_LOG_FILE="${AGENTDIR}/logs/configuration-validation.log"
3434
readonly COMMON_CONIG="${CONFDIR}/common-config.toml"
35+
readonly ENV_CONFIG="${CONFDIR}/env-config.json"
3536

3637
readonly ALL_CONFIG='all'
3738

3839
UsageString="
3940
4041
41-
usage: amazon-cloudwatch-agent-ctl -a stop|start|status|status-with-workloads|fetch-config|append-config|remove-config [-m ec2|onPremise|onPrem|auto] [-c default|ssm:<parameter-store-name>|file:<file-path>] [-s] [-d]
42+
usage: amazon-cloudwatch-agent-ctl -a stop|start|status|status-with-workloads|fetch-config|append-config|remove-config|set-log-level|set-env [-m ec2|onPremise|onPrem|auto] [-c default|ssm:<parameter-store-name>|file:<file-path>] [-s] [-d] [-l INFO|DEBUG|WARN|ERROR|OFF] [-e KEY=VALUE]
4243
4344
e.g.
4445
1. apply a SSM parameter store config on EC2 instance and restart the agent afterwards:
@@ -58,6 +59,8 @@ UsageString="
5859
fetch-config: use this json config as the agent's only configuration.
5960
append-config: append json config with the existing json configs if any.
6061
remove-config: remove json config based on the location (ssm parameter store name, file name)
62+
set-log-level: sets the log level, followed by -l to provide the level in all caps.
63+
set-env: sets an environment variable for the agent process, followed by -e to provide KEY=VALUE.
6164
6265
-m: mode
6366
ec2: indicate this is on ec2 host.
@@ -76,6 +79,15 @@ UsageString="
7679
-d: enable dual-stack endpoints for AWS API calls during config download
7780
this parameter is used for 'fetch-config', 'append-config' actions only.
7881
82+
-l: log level to set the agent to INFO, DEBUG, WARN, ERROR, or OFF
83+
this parameter is used for 'set-log-level' only.
84+
85+
-e: environment variable to set for the agent process, in KEY=VALUE format
86+
this parameter is used for 'set-env' only.
87+
note: values that are also produced by agent config translation (such as
88+
proxy, CA bundle, and log level settings) are overwritten from the agent
89+
configuration on each fetch-config/append-config or agent restart.
90+
7991
"
8092

8193
cwa_start() {
@@ -265,15 +277,59 @@ cwa_config() {
265277
fi
266278
}
267279

280+
cwa_set_log_level() {
281+
log_level="${1:-}"
282+
case "${log_level}" in
283+
INFO) ;;
284+
DEBUG) ;;
285+
ERROR) ;;
286+
WARN) ;;
287+
OFF) ;;
288+
*)
289+
echo "Invalid log level: ${log_level} ${UsageString}" >&2
290+
exit 1
291+
;;
292+
esac
293+
294+
if ! runEnvConfigCommand=$("${CMDDIR}/amazon-cloudwatch-agent" -setenv "CWAGENT_LOG_LEVEL=${log_level}" -envconfig "${ENV_CONFIG}" 2>&1); then
295+
echo "${runEnvConfigCommand}" >&2
296+
echo "Failed to set CWAGENT_LOG_LEVEL to ${log_level}" >&2
297+
exit 1
298+
fi
299+
echo "${runEnvConfigCommand}"
300+
echo "Set CWAGENT_LOG_LEVEL to ${log_level}"
301+
}
302+
303+
cwa_set_env() {
304+
env_kv="${1:-}"
305+
case "${env_kv}" in
306+
?*=*) ;;
307+
*)
308+
echo "Invalid environment variable: '${env_kv}' (expected KEY=VALUE) ${UsageString}" >&2
309+
exit 1
310+
;;
311+
esac
312+
313+
if ! runEnvConfigCommand=$("${CMDDIR}/amazon-cloudwatch-agent" -setenv "${env_kv}" -envconfig "${ENV_CONFIG}" 2>&1); then
314+
echo "${runEnvConfigCommand}" >&2
315+
echo "Failed to set environment variable ${env_kv%%=*}" >&2
316+
exit 1
317+
fi
318+
echo "${runEnvConfigCommand}"
319+
echo "Set ${env_kv%%=*}"
320+
}
321+
268322
main() {
269323
action=''
270324
config_location='default'
271325
restart='false'
272326
mode='auto'
273327
use_dualstack='false'
328+
log_level=''
329+
env_kv=''
274330

275331
OPTIND=1
276-
while getopts ":hsa:r:c:m:d" opt; do
332+
while getopts ":hsa:r:c:m:dl:e:" opt; do
277333
case "${opt}" in
278334
h)
279335
echo "${UsageString}"
@@ -284,6 +340,8 @@ main() {
284340
c) config_location="${OPTARG}" ;;
285341
m) mode="${OPTARG}" ;;
286342
d) use_dualstack='true' ;;
343+
l) log_level="${OPTARG}" ;;
344+
e) env_kv="${OPTARG}" ;;
287345
\?)
288346
echo "Invalid option: -${OPTARG} ${UsageString}" >&2
289347
;;
@@ -320,6 +378,8 @@ main() {
320378
remove-config) cwa_config "${config_location}" "${restart}" "${mode}" 'remove' ;;
321379
status) cwa_status "false" ;;
322380
status-with-workloads) cwa_status "true" ;;
381+
set-log-level) cwa_set_log_level "${log_level}" ;;
382+
set-env) cwa_set_env "${env_kv}" ;;
323383
# helpers for ssm package scripts to workaround fact that it can't determine if invocation is due to
324384
# upgrade or install
325385
prep-restart) cwa_prep_restart ;;

packaging/dependencies/amazon-cloudwatch-agent-ctl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ UsageString="
3131
3232
3333
usage: amazon-cloudwatch-agent-ctl -a
34-
stop|start|status|status-with-workloads|fetch-config|append-config|remove-config|set-log-level
34+
stop|start|status|status-with-workloads|fetch-config|append-config|remove-config|set-log-level|set-env
3535
[-m ec2|onPremise|onPrem|auto]
3636
[-c default|all|ssm:<parameter-store-name>|file:<file-path>]
3737
[-s]
3838
[-d]
3939
[-l INFO|DEBUG|WARN|ERROR|OFF]
40+
[-e KEY=VALUE]
4041
4142
e.g.
4243
1. apply a SSM parameter store config on EC2 instance and restart the agent afterwards:
@@ -57,6 +58,7 @@ UsageString="
5758
append-config: append json config with the existing json configs if any, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'default'.
5859
remove-config: remove config for agent, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'all'.
5960
set-log-level: sets the log level, followed by -l to provide the level in all caps.
61+
set-env: sets an environment variable for the agent process, followed by -e to provide KEY=VALUE.
6062
6163
-m: mode
6264
ec2: indicate this is on ec2 host.
@@ -78,6 +80,12 @@ UsageString="
7880
-l: log level to set the agent to INFO, DEBUG, WARN, ERROR, or OFF
7981
this parameter is used for 'set-log-level' only.
8082
83+
-e: environment variable to set for the agent process, in KEY=VALUE format
84+
this parameter is used for 'set-env' only.
85+
note: values that are also produced by agent config translation (such as
86+
proxy, CA bundle, and log level settings) are overwritten from the agent
87+
configuration on each fetch-config/append-config or agent restart.
88+
8189
"
8290

8391
start_all() {
@@ -378,12 +386,32 @@ set_log_level_all() {
378386
echo "Set CWAGENT_LOG_LEVEL to ${log_level}"
379387
}
380388

389+
set_env() {
390+
env_kv="${1:-}"
391+
case "${env_kv}" in
392+
?*=*) ;;
393+
*)
394+
echo "Invalid environment variable: '${env_kv}' (expected KEY=VALUE) ${UsageString}" >&2
395+
exit 1
396+
;;
397+
esac
398+
399+
if ! runEnvConfigCommand=$("${CMDDIR}/amazon-cloudwatch-agent" -setenv "${env_kv}" -envconfig "${ENV_CONFIG}" 2>&1); then
400+
echo "${runEnvConfigCommand}" >&2
401+
echo "Failed to set environment variable ${env_kv%%=*}" >&2
402+
exit 1
403+
fi
404+
echo "${runEnvConfigCommand}"
405+
echo "Set ${env_kv%%=*}"
406+
}
407+
381408
main() {
382409
action=''
383410
cwa_config_location=''
384411
restart='false'
385412
mode='ec2'
386413
use_dualstack='false'
414+
env_kv=''
387415

388416
# detect which init system is in use
389417
if [ "$(/sbin/init --version 2>/dev/null | grep -c upstart)" = 1 ]; then
@@ -399,7 +427,7 @@ main() {
399427
fi
400428

401429
OPTIND=1
402-
while getopts ":hsa:c:m:l:d" opt; do
430+
while getopts ":hsa:c:m:l:de:" opt; do
403431
case "${opt}" in
404432
h)
405433
echo "${UsageString}"
@@ -411,6 +439,7 @@ main() {
411439
m) mode="${OPTARG}" ;;
412440
l) log_level="${OPTARG}" ;;
413441
d) use_dualstack='true' ;;
442+
e) env_kv="${OPTARG}" ;;
414443
\?)
415444
echo "Invalid option: -${OPTARG} ${UsageString}" >&2
416445
;;
@@ -448,6 +477,7 @@ main() {
448477
# helper for rpm+deb uninstallation hooks, not expected to be called manually
449478
preun) preun_all ;;
450479
set-log-level) set_log_level_all "${log_level}" ;;
480+
set-env) set_env "${env_kv}" ;;
451481
*)
452482
echo "Invalid action: ${action} ${UsageString}" >&2
453483
exit 1

packaging/windows/amazon-cloudwatch-agent-ctl.ps1

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Param (
1515
[Parameter(Mandatory = $false)]
1616
[string]$LogLevel = '',
1717
[Parameter(Mandatory = $false)]
18+
[string]$EnvVar = '',
19+
[Parameter(Mandatory = $false)]
1820
[switch]$d = $false,
1921
[parameter(ValueFromRemainingArguments=$true)]
2022
$unsupportedVars
@@ -27,12 +29,13 @@ $UsageString = @"
2729
2830
2931
usage: amazon-cloudwatch-agent-ctl.ps1 -a
30-
stop|start|status|status-with-workloads|fetch-config|append-config|remove-config|set-log-level
32+
stop|start|status|status-with-workloads|fetch-config|append-config|remove-config|set-log-level|set-env
3133
[-m ec2|onPremise|onPrem|auto]
3234
[-c default|all|ssm:<parameter-store-name>|file:<file-path>]
3335
[-s]
3436
[-d]
3537
[-l INFO|DEBUG|WARN|ERROR|OFF]
38+
[-e KEY=VALUE]
3639
3740
e.g.
3841
1. apply a SSM parameter store config on EC2 instance and restart the agent afterwards:
@@ -53,6 +56,7 @@ $UsageString = @"
5356
append-config: append json config with the existing json configs if any, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'default'.
5457
remove-config: remove config for agent, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'all'.
5558
set-log-level: sets the log level, followed by -l to provide the level in all caps.
59+
set-env: sets an environment variable for the agent process, followed by -e to provide KEY=VALUE.
5660
5761
-m: mode
5862
ec2: indicate this is on ec2 host.
@@ -74,6 +78,12 @@ $UsageString = @"
7478
-l: log level to set the agent to INFO, DEBUG, WARN, ERROR, or OFF
7579
this parameter is used for 'set-log-level' only.
7680
81+
-e: environment variable to set for the agent process, in KEY=VALUE format
82+
this parameter is used for 'set-env' only.
83+
note: values that are also produced by agent config translation (such as
84+
proxy, CA bundle, and log level settings) are overwritten from the agent
85+
configuration on each fetch-config/append-config or agent restart.
86+
7787
"@
7888

7989
$CWAServiceName = 'AmazonCloudWatchAgent'
@@ -442,6 +452,18 @@ Function SetLogLevelAll() {
442452
CheckCMDResult "" "Set CWAGENT_LOG_LEVEL to ${LogLevel}"
443453
}
444454

455+
Function SetEnv() {
456+
if ($EnvVar -notmatch '^[^=]+=') {
457+
Write-Output "Invalid environment variable: '${EnvVar}' (expected KEY=VALUE)`n${UsageString}"
458+
Exit 1
459+
}
460+
461+
# Invoke directly (not via cmd /c) so the value is passed as a single
462+
# argument and shell metacharacters or spaces in it are not interpreted.
463+
& "${CWAProgramFiles}\amazon-cloudwatch-agent.exe" --setenv $EnvVar --envconfig $ENV_CONFIG
464+
CheckCMDResult "" "Set $(${EnvVar}.Split('=')[0])"
465+
}
466+
445467
Function main() {
446468
if (Get-Command 'Get-CimInstance' -CommandType Cmdlet -ErrorAction SilentlyContinue) {
447469
$CIM = $true
@@ -479,6 +501,7 @@ Function main() {
479501
cond-restart { CondRestartAll }
480502
preun { PreunAll }
481503
set-log-level { SetLogLevelAll }
504+
set-env { SetEnv }
482505
default {
483506
Write-Output "Invalid action: ${Action}`n${UsageString}"
484507
Exit 1

0 commit comments

Comments
 (0)