Skip to content

Commit

Permalink
Move wizard and downloader to the amazon-cloudwatch-agent binary to f…
Browse files Browse the repository at this point in the history
…urther decrease deployment size
  • Loading branch information
chadpatel committed Dec 16, 2024
1 parent f5f3198 commit aa25c3f
Show file tree
Hide file tree
Showing 18 changed files with 795 additions and 484 deletions.
133 changes: 8 additions & 125 deletions cmd/amazon-cloudwatch-agent-config-wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,22 @@
package main

import (
"bufio"
"flag"
"fmt"
"log"
"os"

"github.com/aws/amazon-cloudwatch-agent/tool/data"
"github.com/aws/amazon-cloudwatch-agent/tool/processors"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/basicInfo"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/linux"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/windows"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/serialization"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/tracesconfig"
"github.com/aws/amazon-cloudwatch-agent/tool/runtime"
"github.com/aws/amazon-cloudwatch-agent/tool/stdin"
"github.com/aws/amazon-cloudwatch-agent/tool/testutil"
"github.com/aws/amazon-cloudwatch-agent/tool/util"
"github.com/aws/amazon-cloudwatch-agent/tool/cmdwrapper"
"github.com/aws/amazon-cloudwatch-agent/tool/wizard/flags"
)

type IMainProcessor interface {
VerifyProcessor(processor interface{})
}
type MainProcessorStruct struct{}

var MainProcessorGlobal IMainProcessor = &MainProcessorStruct{}

var isNonInteractiveWindowsMigration *bool

var configOutputPath *string

var isNonInteractiveXrayMigration *bool

func main() {
// Parse command line args for non-interactive Windows migration
isNonInteractiveWindowsMigration = flag.Bool("isNonInteractiveWindowsMigration", false,
"If true, it will use command line args to bypass the wizard. Default value is false.")

isNonInteractiveLinuxMigration := flag.Bool("isNonInteractiveLinuxMigration", false,
"If true, it will do the linux config migration. Default value is false.")

tracesOnly := flag.Bool("tracesOnly", false, "If true, only trace configuration will be generated")
useParameterStore := flag.Bool("useParameterStore", false,
"If true, it will use the parameter store for the migrated config storage.")
isNonInteractiveXrayMigration = flag.Bool("nonInteractiveXrayMigration", false, "If true, then this is part of non Interactive xray migration tool.")
configFilePath := flag.String("configFilePath", "",
fmt.Sprintf("The path of the old config file. Default is %s on Windows or %s on Linux", windows.DefaultFilePathWindowsConfiguration, linux.DefaultFilePathLinuxConfiguration))

configOutputPath = flag.String("configOutputPath", "", "Specifies where to write the configuration file generated by the wizard")
parameterStoreName := flag.String("parameterStoreName", "", "The parameter store name. Default is AmazonCloudWatch-windows")
parameterStoreRegion := flag.String("parameterStoreRegion", "", "The parameter store region. Default is us-east-1")
log.Printf("Starting config-wizard, this will map back to a call to amazon-cloudwatch-agent")

translatorFlags := cmdwrapper.AddFlags("", flags.WizardFlags)
flag.Parse()

if *isNonInteractiveWindowsMigration {
addWindowsMigrationInputs(*configFilePath, *parameterStoreName, *parameterStoreRegion, *useParameterStore)
} else if *isNonInteractiveLinuxMigration {
ctx := new(runtime.Context)
config := new(data.Config)
ctx.HasExistingLinuxConfig = true
ctx.ConfigFilePath = *configFilePath
if ctx.ConfigFilePath == "" {
ctx.ConfigFilePath = linux.DefaultFilePathLinuxConfiguration
}
process(ctx, config, linux.Processor, serialization.Processor)
return
} else if *tracesOnly {
ctx := new(runtime.Context)
config := new(data.Config)
ctx.TracesOnly = true
ctx.ConfigOutputPath = *configOutputPath
if *isNonInteractiveXrayMigration {
ctx.NonInteractiveXrayMigration = true
}
process(ctx, config, tracesconfig.Processor, serialization.Processor)
return
}

startProcessing()
}

func init() {
stdin.Scanln = func(a ...interface{}) (n int, err error) {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if len(a) > 0 {
*a[0].(*string) = scanner.Text()
n = len(*a[0].(*string))
}
err = scanner.Err()
return
err := cmdwrapper.ExecuteAgentCommand(flags.Command, translatorFlags)
if err != nil {
os.Exit(1)
}
processors.StartProcessor = basicInfo.Processor
}

func addWindowsMigrationInputs(configFilePath string, parameterStoreName string, parameterStoreRegion string, useParameterStore bool) {
inputChan := testutil.SetUpTestInputStream()
if useParameterStore {
testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "1", parameterStoreName, parameterStoreRegion, "1")
} else {
testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "2")
}
}

func process(ctx *runtime.Context, config *data.Config, processors ...processors.Processor) {
for _, processor := range processors {
processor.Process(ctx, config)
}
}

func startProcessing() {
ctx := new(runtime.Context)
config := new(data.Config)
ctx.ConfigOutputPath = *configOutputPath
var processor interface{}
processor = processors.StartProcessor
if *isNonInteractiveWindowsMigration {
ctx.WindowsNonInteractiveMigration = true
}
if *isNonInteractiveXrayMigration {
ctx.NonInteractiveXrayMigration = true
}
for {
if processor == nil {
if util.CurOS() == util.OsTypeWindows && !*isNonInteractiveWindowsMigration {
util.EnterToExit()
}
fmt.Println("Program exits now.")
break
}
MainProcessorGlobal.VerifyProcessor(processor) // For testing purposes
processor.(processors.Processor).Process(ctx, config)
processor = processor.(processors.Processor).NextProcessor(ctx, config)
}
}

func (p *MainProcessorStruct) VerifyProcessor(processor interface{}) {
}
78 changes: 0 additions & 78 deletions cmd/amazon-cloudwatch-agent-config-wizard/wizard_test.go

This file was deleted.

40 changes: 26 additions & 14 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"syscall"
"time"

"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/logger"
Expand All @@ -42,13 +41,19 @@ import (
"github.com/aws/amazon-cloudwatch-agent/internal/version"
cwaLogger "github.com/aws/amazon-cloudwatch-agent/logger"
"github.com/aws/amazon-cloudwatch-agent/logs"
_ "github.com/aws/amazon-cloudwatch-agent/plugins"
"github.com/aws/amazon-cloudwatch-agent/profiler"
"github.com/aws/amazon-cloudwatch-agent/receiver/adapter"
"github.com/aws/amazon-cloudwatch-agent/service/configprovider"
"github.com/aws/amazon-cloudwatch-agent/service/defaultcomponents"
"github.com/aws/amazon-cloudwatch-agent/service/registry"
"github.com/aws/amazon-cloudwatch-agent/tool/cmdwrapper"
"github.com/aws/amazon-cloudwatch-agent/tool/downloader"
downloaderflags "github.com/aws/amazon-cloudwatch-agent/tool/downloader/flags"
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
"github.com/aws/amazon-cloudwatch-agent/tool/wizard"
wizardflags "github.com/aws/amazon-cloudwatch-agent/tool/wizard/flags"
"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
translatorflags "github.com/aws/amazon-cloudwatch-agent/translator/flags"
"github.com/aws/amazon-cloudwatch-agent/translator/tocwconfig/toyamlconfig"
)

Expand Down Expand Up @@ -96,15 +101,10 @@ var fRunAsConsole = flag.Bool("console", false, "run as console application (win
var fSetEnv = flag.String("setenv", "", "set an env in the configuration file in the format of KEY=VALUE")
var fStartUpErrorFile = flag.String("startup-error-file", "", "file to touch if agent can't start")

// config-translator
var fConfigTranslator = flag.Bool("config-translator", false, "run in config-translator mode")
var fTranslatorOs = flag.String("ct-os", "", "Please provide the os preference, valid value: windows/linux.")
var fTranslatorInput = flag.String("ct-input", "", "Please provide the path of input agent json config file")
var fTranslatorInputDir = flag.String("ct-input-dir", "", "Please provide the path of input agent json config directory.")
var fTranslatorOutput = flag.String("ct-output", "", "Please provide the path of the output CWAgent config file")
var fTranslatorMode = flag.String("ct-mode", "ec2", "Please provide the mode, i.e. ec2, onPremise, onPrem, auto")
var fTranslatorConfig = flag.String("ct-config", "", "Please provide the common-config file")
var fTranslatorMultiConfig = flag.String("ct-multi-config", "remove", "valid values: default, append, remove")
// sub-commands
var fConfigTranslator = flag.Bool(translatorflags.TranslatorCommand, false, "run in config-translator mode")
var fConfigDownloader = flag.Bool(downloaderflags.Command, false, "run in config-downloader mode")
var fConfigWizard = flag.Bool(wizardflags.Command, false, "run in config-wizard mode")

var stop chan struct{}

Expand Down Expand Up @@ -496,6 +496,10 @@ func (p *program) Stop(_ service.Service) error {

func main() {
flag.Var(&fOtelConfigs, configprovider.OtelConfigFlagName, "YAML configuration files to run OTel pipeline")
translatorFlags := cmdwrapper.AddFlags(translatorflags.TranslatorCommand, translatorflags.TranslatorFlags)
downloaderFlags := cmdwrapper.AddFlags(downloaderflags.Command, downloaderflags.DownloaderFlags)
wizardFlags := cmdwrapper.AddFlags(wizardflags.Command, wizardflags.WizardFlags)

flag.Parse()
if len(fOtelConfigs) == 0 {
_ = fOtelConfigs.Set(paths.YamlConfigPath)
Expand Down Expand Up @@ -619,13 +623,21 @@ func main() {
}
return
case *fConfigTranslator:
ct, err := cmdutil.NewConfigTranslator(*fTranslatorOs, *fTranslatorInput, *fTranslatorInputDir, *fTranslatorOutput, *fTranslatorMode, *fTranslatorConfig, *fTranslatorMultiConfig)
err := cmdutil.RunTranslator(translatorFlags)
if err != nil {
log.Fatalf("E! Failed to initialize config translator: %v", err)
}
err = ct.Translate()
return
case *fConfigDownloader:
err := downloader.RunDownloaderFromFlags(downloaderFlags)
if err != nil {
log.Fatalf("E! Failed to initialize config downloader: %v", err)
}
return
case *fConfigWizard:
err := wizard.RunWizardFromFlags(wizardFlags)
if err != nil {
log.Fatalf("E! Failed to translate config: %v", err)
log.Fatalf("E! Failed to run config wizard: %v", err)
}
return
}
Expand Down
Loading

0 comments on commit aa25c3f

Please sign in to comment.