From d6381de6d8efc2447e7c2c3302a9c3b4012760b2 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 21 May 2022 16:08:05 -0400 Subject: [PATCH 1/4] Change use cmd/c for creating new shell to package all stderr -> stdout --- cmd/config-downloader/downloader.go | 2 +- .../windows/amazon-cloudwatch-agent-ctl.ps1 | 24 +++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/config-downloader/downloader.go b/cmd/config-downloader/downloader.go index 151826e998..ca5102a42b 100644 --- a/cmd/config-downloader/downloader.go +++ b/cmd/config-downloader/downloader.go @@ -150,7 +150,7 @@ func main() { region = util.DetectRegion(mode, cc.CredentialsMap()) if region == "" && downloadLocation != locationDefault { - fmt.Println("Unable to determine aws-region.") + log.Println("E! Unable to determine aws-region.") if mode == config.ModeEC2 { errorMessage = fmt.Sprintf("E! Please check if you can access the metadata service. For example, on linux, run 'wget -q -O - http://169.254.169.254/latest/meta-data/instance-id && echo' ") } else { diff --git a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 index b1de1725d9..a5845d4b85 100644 --- a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 +++ b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 @@ -356,27 +356,27 @@ Function CWAConfig() { } if ($ConfigLocation -eq $AllConfig -And $multi_config -ne 'remove') { - Write-Output "ignore cwa configuration `"$AllConfig`" as it is only supported by action `"remove-config`"" + Write-Output "Ignore amazon-cloudwatch-agent's configuration ${AllConfig} as it is only supported by action `"remove-config`"" return } if ($ConfigLocation -eq $AllConfig) { Remove-Item -Path "${JSON_DIR}\*" -Force -ErrorAction SilentlyContinue } else { - & $CWAProgramFiles\config-downloader.exe --output-dir "${JSON_DIR}" --download-source "${ConfigLocation}" --mode "${param_mode}" --config "${COMMON_CONIG}" --multi-config "${multi_config}" + & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${JSON_DIR} --download-source ${ConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" CheckCMDResult } $jsonDirContent = Get-ChildItem "${JSON_DIR}" | Measure-Object if ($jsonDirContent.count -eq 0) { - Write-Output "all amazon-cloudwatch-agent configurations have been removed" + Write-Output "All amazon-cloudwatch-agent configurations have been removed" Remove-Item "${TOML}" -Force -ErrorAction SilentlyContinue } else { Write-Output "Start configuration validation..." & cmd /c "`"$CWAProgramFiles\config-translator.exe`" --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" CheckCMDResult - # Let command pass so we can check return code and give user-friendly error-message + $ErrorActionPreference = "Continue" & cmd /c "`"${CWAProgramFiles}\amazon-cloudwatch-agent.exe`" --schematest --config ${TOML} 2>&1" | Out-File $CVLogFile if ($LASTEXITCODE -ne 0) { @@ -425,11 +425,11 @@ Function CWOCConfig() { ) if (Test-Path -LiteralPath "${Env:ProgramFiles}\Amazon\AWSOTelCollector\aws-otel-collector-ctl.ps1") { - Write-Output "REMINDER: you are configuring `"cwagent-otel-collector`" instead of `"aws-otel-collector`"." + Write-Output "REMINDER: You are configuring `"cwagent-otel-collector`" instead of `"aws-otel-collector`"." } if ($multi_config -eq 'append') { - Write-Output "ignore `"-o`" as cwagent-otel-collector doesn't support append-config" + Write-Output "Ignore `"-o`" as cwagent-otel-collector doesn't support append-config" return } @@ -439,7 +439,7 @@ Function CWOCConfig() { } if ($OtelConfigLocation -eq $AllConfig -And $multi_config -ne 'remove') { - Write-Output "ignore cwoc configuration `"$AllConfig`" as it is only supported by action `"remove-config`"" + Write-Output "Ignore cwagent-otel-collector's configuration `"$AllConfig`" as it is only supported by action `"remove-config`"" return } @@ -449,15 +449,13 @@ Function CWOCConfig() { Copy-Item "${PREDEFINED_CONFIG_DATA}" -Destination "${YAML_DIR}/default.tmp" Write-Output "Successfully fetched the config and saved in ${YAML_DIR}\default.tmp" } else { - & $CWAProgramFiles\config-downloader.exe --output-dir "${YAML_DIR}" --download-source "${OtelConfigLocation}" --mode "${param_mode}" --config "${COMMON_CONIG}" --multi-config "${multi_config}" - if ($LASTEXITCODE -ne 0) { - return - } + & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${YAML_DIR} --download-source ${OtelConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config}" + CheckCMDResult } $yamlDirContent = Get-ChildItem "${YAML_DIR}" | Measure-Object if ($yamlDirContent.count -eq 0) { - Write-Output "all cwagent-otel-collector configurations have been removed" + Write-Output "All cwagent-otel-collector configurations have been removed" Remove-Item "${YAML}" -Force -ErrorAction SilentlyContinue } else { # delete old file which are without .tmp suffix @@ -469,7 +467,7 @@ Function CWOCConfig() { $destination = Join-Path -Path $_.Directory.FullName -ChildPath "${newName}" Move-Item $_.FullName -Destination "${destination}" -Force Copy-Item "${destination}" -Destination "${YAML}" -Force - Write-Output "cwagent-otel-collector config has been successfully fetched." + Write-Output "cwagent-otel-collector's configuration has been successfully fetched." } } From a77b9842894e1aa13e3e7518a0670ee4b8537c54 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sat, 21 May 2022 18:46:08 -0400 Subject: [PATCH 2/4] Add 2>&1 for otel collector and fix some typo --- packaging/windows/amazon-cloudwatch-agent-ctl.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 index a5845d4b85..3fc376da75 100644 --- a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 +++ b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 @@ -113,10 +113,10 @@ $EC2 = $false $CIM = $false Function StartAll() { - Write-Output "****** processing cwagent-otel-collector ******" + Write-Output "****** Processing cwagent-otel-collector ******" AgentStart -service_name $CWOCServiceName -service_display_name $CWOCServiceDisplayName - Write-Output "`r`n****** processing amazon-cloudwatch-agent ******" + Write-Output "`r`n****** Processing amazon-cloudwatch-agent ******" AgentStart -service_name $CWAServiceName -service_display_name $CWAServiceDisplayName } @@ -169,10 +169,10 @@ Function AgentStart() { } Function StopAll() { - Write-Output "****** processing cwagent-otel-collector ******" + Write-Output "****** Processing cwagent-otel-collector ******" AgentStop -service_name $CWOCServiceName - Write-Output "`r`n****** processing amazon-cloudwatch-agent ******" + Write-Output "`r`n****** Processing amazon-cloudwatch-agent ******" AgentStop -service_name $CWAServiceName } @@ -449,7 +449,7 @@ Function CWOCConfig() { Copy-Item "${PREDEFINED_CONFIG_DATA}" -Destination "${YAML_DIR}/default.tmp" Write-Output "Successfully fetched the config and saved in ${YAML_DIR}\default.tmp" } else { - & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${YAML_DIR} --download-source ${OtelConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config}" + & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${YAML_DIR} --download-source ${OtelConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" CheckCMDResult } From a306cfa5bd9c445a017bf64ac5af51a96b39a411 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Tue, 24 May 2022 12:26:10 -0400 Subject: [PATCH 3/4] Add comments for config-translator --- packaging/windows/amazon-cloudwatch-agent-ctl.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 index 3fc376da75..e6e8279e47 100644 --- a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 +++ b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 @@ -377,6 +377,9 @@ Function CWAConfig() { & cmd /c "`"$CWAProgramFiles\config-translator.exe`" --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" CheckCMDResult + # Use "Continue" to continue when having an error https://www.tutorialspoint.com/how-to-use-the-erroractionpreference-variable-in-powershell + # However, error will not be displayed in the console since we catch the error in the new shell with cmd /c and send more UX-friendly wordings + # to the customers $ErrorActionPreference = "Continue" & cmd /c "`"${CWAProgramFiles}\amazon-cloudwatch-agent.exe`" --schematest --config ${TOML} 2>&1" | Out-File $CVLogFile if ($LASTEXITCODE -ne 0) { From cc7977406bc446e26aaafd8688188e729133c85d Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Wed, 25 May 2022 16:40:30 -0400 Subject: [PATCH 4/4] Add comment and fix comment for config translator, config downloader and also schema test --- packaging/windows/amazon-cloudwatch-agent-ctl.ps1 | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 index e6e8279e47..3ecff63083 100644 --- a/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 +++ b/packaging/windows/amazon-cloudwatch-agent-ctl.ps1 @@ -364,7 +364,7 @@ Function CWAConfig() { Remove-Item -Path "${JSON_DIR}\*" -Force -ErrorAction SilentlyContinue } else { & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${JSON_DIR} --download-source ${ConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" - CheckCMDResult + CheckCMDResult # Exit immediately if config-downloader outputs any error } $jsonDirContent = Get-ChildItem "${JSON_DIR}" | Measure-Object @@ -375,11 +375,10 @@ Function CWAConfig() { } else { Write-Output "Start configuration validation..." & cmd /c "`"$CWAProgramFiles\config-translator.exe`" --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" - CheckCMDResult + CheckCMDResult # Exit immediately if config-translator outputs any error - # Use "Continue" to continue when having an error https://www.tutorialspoint.com/how-to-use-the-erroractionpreference-variable-in-powershell - # However, error will not be displayed in the console since we catch the error in the new shell with cmd /c and send more UX-friendly wordings - # to the customers + # Set ErrorActionPreference as Continue to continue on error when schema-test on toml file fails and + # return a UX-friendly message $ErrorActionPreference = "Continue" & cmd /c "`"${CWAProgramFiles}\amazon-cloudwatch-agent.exe`" --schematest --config ${TOML} 2>&1" | Out-File $CVLogFile if ($LASTEXITCODE -ne 0) { @@ -453,7 +452,7 @@ Function CWOCConfig() { Write-Output "Successfully fetched the config and saved in ${YAML_DIR}\default.tmp" } else { & cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir ${YAML_DIR} --download-source ${OtelConfigLocation} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1" - CheckCMDResult + CheckCMDResult # Exit immediately if config-downloader outputs any error } $yamlDirContent = Get-ChildItem "${YAML_DIR}" | Measure-Object