Skip to content

Commit

Permalink
Using cmd/c to avoid PowerShell ISE does not accept stderr (#473)
Browse files Browse the repository at this point in the history
* Change use cmd/c for creating new shell to package all stderr -> stdout

* Add 2>&1 for otel collector and fix some typo

* Add comments for config-translator

* Add comment and fix comment for config translator, config downloader and also schema test
  • Loading branch information
khanhntd authored May 26, 2022
1 parent aa9b3a3 commit 89d1912
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/config-downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 19 additions & 19 deletions packaging/windows/amazon-cloudwatch-agent-ctl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -356,27 +356,29 @@ 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}"
CheckCMDResult
& 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 # Exit immediately if config-downloader outputs any error
}

$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
CheckCMDResult # Exit immediately if config-translator outputs any error

# 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) {
Expand Down Expand Up @@ -425,11 +427,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
}

Expand All @@ -439,7 +441,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
}

Expand All @@ -449,15 +451,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} 2>&1"
CheckCMDResult # Exit immediately if config-downloader outputs any error
}

$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
Expand All @@ -469,7 +469,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."
}
}

Expand Down

0 comments on commit 89d1912

Please sign in to comment.