Skip to content

Commit a437da8

Browse files
chadpatelmovence
andauthored
Refactor helper binaries to save 161MB of disk space when the agent is installed and reduce RPM by 48MB (#1454)
Co-authored-by: Hyunsoo Kim <[email protected]>
1 parent 2855860 commit a437da8

File tree

19 files changed

+1354
-776
lines changed

19 files changed

+1354
-776
lines changed

cmd/amazon-cloudwatch-agent-config-wizard/wizard.go

Lines changed: 7 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -4,139 +4,18 @@
44
package main
55

66
import (
7-
"bufio"
8-
"flag"
9-
"fmt"
7+
"log"
108
"os"
119

12-
"github.com/aws/amazon-cloudwatch-agent/tool/data"
13-
"github.com/aws/amazon-cloudwatch-agent/tool/processors"
14-
"github.com/aws/amazon-cloudwatch-agent/tool/processors/basicInfo"
15-
"github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/linux"
16-
"github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/windows"
17-
"github.com/aws/amazon-cloudwatch-agent/tool/processors/serialization"
18-
"github.com/aws/amazon-cloudwatch-agent/tool/processors/tracesconfig"
19-
"github.com/aws/amazon-cloudwatch-agent/tool/runtime"
20-
"github.com/aws/amazon-cloudwatch-agent/tool/stdin"
21-
"github.com/aws/amazon-cloudwatch-agent/tool/testutil"
22-
"github.com/aws/amazon-cloudwatch-agent/tool/util"
10+
"github.com/aws/amazon-cloudwatch-agent/tool/cmdwrapper"
11+
"github.com/aws/amazon-cloudwatch-agent/tool/wizard/flags"
2312
)
2413

25-
type IMainProcessor interface {
26-
VerifyProcessor(processor interface{})
27-
}
28-
type MainProcessorStruct struct{}
29-
30-
var MainProcessorGlobal IMainProcessor = &MainProcessorStruct{}
31-
32-
var isNonInteractiveWindowsMigration *bool
33-
34-
var configOutputPath *string
35-
36-
var isNonInteractiveXrayMigration *bool
37-
3814
func main() {
39-
// Parse command line args for non-interactive Windows migration
40-
isNonInteractiveWindowsMigration = flag.Bool("isNonInteractiveWindowsMigration", false,
41-
"If true, it will use command line args to bypass the wizard. Default value is false.")
42-
43-
isNonInteractiveLinuxMigration := flag.Bool("isNonInteractiveLinuxMigration", false,
44-
"If true, it will do the linux config migration. Default value is false.")
45-
46-
tracesOnly := flag.Bool("tracesOnly", false, "If true, only trace configuration will be generated")
47-
useParameterStore := flag.Bool("useParameterStore", false,
48-
"If true, it will use the parameter store for the migrated config storage.")
49-
isNonInteractiveXrayMigration = flag.Bool("nonInteractiveXrayMigration", false, "If true, then this is part of non Interactive xray migration tool.")
50-
configFilePath := flag.String("configFilePath", "",
51-
fmt.Sprintf("The path of the old config file. Default is %s on Windows or %s on Linux", windows.DefaultFilePathWindowsConfiguration, linux.DefaultFilePathLinuxConfiguration))
52-
53-
configOutputPath = flag.String("configOutputPath", "", "Specifies where to write the configuration file generated by the wizard")
54-
parameterStoreName := flag.String("parameterStoreName", "", "The parameter store name. Default is AmazonCloudWatch-windows")
55-
parameterStoreRegion := flag.String("parameterStoreRegion", "", "The parameter store region. Default is us-east-1")
56-
57-
flag.Parse()
15+
log.Printf("Starting config-wizard, this will map back to a call to amazon-cloudwatch-agent")
5816

59-
if *isNonInteractiveWindowsMigration {
60-
addWindowsMigrationInputs(*configFilePath, *parameterStoreName, *parameterStoreRegion, *useParameterStore)
61-
} else if *isNonInteractiveLinuxMigration {
62-
ctx := new(runtime.Context)
63-
config := new(data.Config)
64-
ctx.HasExistingLinuxConfig = true
65-
ctx.ConfigFilePath = *configFilePath
66-
if ctx.ConfigFilePath == "" {
67-
ctx.ConfigFilePath = linux.DefaultFilePathLinuxConfiguration
68-
}
69-
process(ctx, config, linux.Processor, serialization.Processor)
70-
return
71-
} else if *tracesOnly {
72-
ctx := new(runtime.Context)
73-
config := new(data.Config)
74-
ctx.TracesOnly = true
75-
ctx.ConfigOutputPath = *configOutputPath
76-
if *isNonInteractiveXrayMigration {
77-
ctx.NonInteractiveXrayMigration = true
78-
}
79-
process(ctx, config, tracesconfig.Processor, serialization.Processor)
80-
return
81-
}
82-
83-
startProcessing()
84-
}
85-
86-
func init() {
87-
stdin.Scanln = func(a ...interface{}) (n int, err error) {
88-
scanner := bufio.NewScanner(os.Stdin)
89-
scanner.Scan()
90-
if len(a) > 0 {
91-
*a[0].(*string) = scanner.Text()
92-
n = len(*a[0].(*string))
93-
}
94-
err = scanner.Err()
95-
return
96-
}
97-
processors.StartProcessor = basicInfo.Processor
98-
}
99-
100-
func addWindowsMigrationInputs(configFilePath string, parameterStoreName string, parameterStoreRegion string, useParameterStore bool) {
101-
inputChan := testutil.SetUpTestInputStream()
102-
if useParameterStore {
103-
testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "1", parameterStoreName, parameterStoreRegion, "1")
104-
} else {
105-
testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "2")
106-
}
107-
}
108-
109-
func process(ctx *runtime.Context, config *data.Config, processors ...processors.Processor) {
110-
for _, processor := range processors {
111-
processor.Process(ctx, config)
112-
}
113-
}
114-
115-
func startProcessing() {
116-
ctx := new(runtime.Context)
117-
config := new(data.Config)
118-
ctx.ConfigOutputPath = *configOutputPath
119-
var processor interface{}
120-
processor = processors.StartProcessor
121-
if *isNonInteractiveWindowsMigration {
122-
ctx.WindowsNonInteractiveMigration = true
123-
}
124-
if *isNonInteractiveXrayMigration {
125-
ctx.NonInteractiveXrayMigration = true
126-
}
127-
for {
128-
if processor == nil {
129-
if util.CurOS() == util.OsTypeWindows && !*isNonInteractiveWindowsMigration {
130-
util.EnterToExit()
131-
}
132-
fmt.Println("Program exits now.")
133-
break
134-
}
135-
MainProcessorGlobal.VerifyProcessor(processor) // For testing purposes
136-
processor.(processors.Processor).Process(ctx, config)
137-
processor = processor.(processors.Processor).NextProcessor(ctx, config)
138-
}
139-
}
17+
fs, wizardFlags := cmdwrapper.CreateFlagSet(flags.Command, flags.WizardFlags)
18+
fs.Parse(os.Args[1:]) // Skip program name only
14019

141-
func (p *MainProcessorStruct) VerifyProcessor(processor interface{}) {
20+
_ = cmdwrapper.ExecuteSubcommand(flags.Command, wizardFlags)
14221
}

cmd/amazon-cloudwatch-agent-config-wizard/wizard_test.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ import (
4141
"github.com/aws/amazon-cloudwatch-agent/internal/version"
4242
cwaLogger "github.com/aws/amazon-cloudwatch-agent/logger"
4343
"github.com/aws/amazon-cloudwatch-agent/logs"
44-
_ "github.com/aws/amazon-cloudwatch-agent/plugins"
44+
_ "github.com/aws/amazon-cloudwatch-agent/plugins" // do not remove, necessary for telegraf to know what plugins are used
4545
"github.com/aws/amazon-cloudwatch-agent/profiler"
4646
"github.com/aws/amazon-cloudwatch-agent/receiver/adapter"
4747
"github.com/aws/amazon-cloudwatch-agent/service/configprovider"
4848
"github.com/aws/amazon-cloudwatch-agent/service/defaultcomponents"
4949
"github.com/aws/amazon-cloudwatch-agent/service/registry"
50+
"github.com/aws/amazon-cloudwatch-agent/tool/cmdwrapper"
51+
"github.com/aws/amazon-cloudwatch-agent/tool/downloader"
52+
downloaderflags "github.com/aws/amazon-cloudwatch-agent/tool/downloader/flags"
5053
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
54+
"github.com/aws/amazon-cloudwatch-agent/tool/translator"
55+
"github.com/aws/amazon-cloudwatch-agent/tool/wizard"
56+
wizardflags "github.com/aws/amazon-cloudwatch-agent/tool/wizard/flags"
57+
translatorflags "github.com/aws/amazon-cloudwatch-agent/translator/flags"
5158
"github.com/aws/amazon-cloudwatch-agent/translator/tocwconfig/toyamlconfig"
5259
)
5360

@@ -492,6 +499,40 @@ func (p *program) Stop(_ service.Service) error {
492499

493500
func main() {
494501
flag.Var(&fOtelConfigs, configprovider.OtelConfigFlagName, "YAML configuration files to run OTel pipeline")
502+
503+
// Check for subcommands first
504+
if len(os.Args) > 1 {
505+
subcommand := os.Args[1]
506+
if subcommand == translatorflags.TranslatorCommand || subcommand == downloaderflags.Command || subcommand == wizardflags.Command {
507+
subcommands := map[string]map[string]cmdwrapper.Flag{
508+
translatorflags.TranslatorCommand: translatorflags.TranslatorFlags,
509+
downloaderflags.Command: downloaderflags.DownloaderFlags,
510+
wizardflags.Command: wizardflags.WizardFlags,
511+
}
512+
handlers := map[string]func(map[string]*string) error{
513+
translatorflags.TranslatorCommand: translator.RunTranslator,
514+
downloaderflags.Command: downloader.RunDownloaderFromFlags,
515+
wizardflags.Command: wizard.RunWizardFromFlags,
516+
}
517+
518+
if err := cmdwrapper.HandleSubcommand(subcommands, handlers); err != nil {
519+
log.Fatalf("E! %s", err.Error())
520+
}
521+
return
522+
}
523+
}
524+
525+
// Override flag.Usage to include subcommand help
526+
flag.Usage = func() {
527+
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
528+
flag.PrintDefaults()
529+
fmt.Fprintf(os.Stderr, "\nAvailable subcommands:\n")
530+
fmt.Fprintf(os.Stderr, " %s\t\tTranslate configuration files\n", translatorflags.TranslatorCommand)
531+
fmt.Fprintf(os.Stderr, " %s\t\tDownload configuration from remote sources\n", downloaderflags.Command)
532+
fmt.Fprintf(os.Stderr, " %s\t\t\tInteractive configuration wizard\n", wizardflags.Command)
533+
fmt.Fprintf(os.Stderr, "\nUse '%s <subcommand> --help' for more information about a subcommand.\n", os.Args[0])
534+
}
535+
495536
flag.Parse()
496537
if len(fOtelConfigs) == 0 {
497538
_ = fOtelConfigs.Set(getFallbackOtelConfig(*fTomlConfig, paths.YamlConfigPath))
@@ -614,6 +655,7 @@ func main() {
614655
}
615656
}
616657
return
658+
617659
}
618660

619661
if runtime.GOOS == "windows" && windowsRunAsService() {

0 commit comments

Comments
 (0)