forked from aws/amazon-cloudwatch-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-amazon-cloudwatch-agent.go
136 lines (115 loc) · 3.59 KB
/
start-amazon-cloudwatch-agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package main
import (
"errors"
"io"
"io/fs"
"log"
"os"
"os/exec"
"syscall"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/aws/private-amazon-cloudwatch-agent-staging/translator/config"
)
const (
COMMON_CONFIG = "common-config.toml"
JSON = "amazon-cloudwatch-agent.json"
TOML = "amazon-cloudwatch-agent.toml"
YAML = "amazon-cloudwatch-agent.yaml"
ENV = "env-config.json"
AGENT_LOG_FILE = "amazon-cloudwatch-agent.log"
//TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing
CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig"
)
var (
jsonConfigPath string
jsonDirPath string
envConfigPath string
tomlConfigPath string
commonConfigPath string
yamlConfigPath string
agentLogFilePath string
translatorBinaryPath string
agentBinaryPath string
)
// We use an environment variable here because we need this condition before the translator reads agent config json file.
var runInContainer = os.Getenv(config.RUN_IN_CONTAINER)
func translateConfig() error {
args := []string{"--output", tomlConfigPath, "--mode", "auto"}
var stdoutIOWriter io.Writer
var stderrIOWriter io.Writer
if runInContainer == config.RUN_IN_CONTAINER_TRUE {
args = append(args, "--input-dir", CONFIG_DIR_IN_CONTAINER)
stdoutIOWriter = os.Stdout
stderrIOWriter = os.Stderr
} else {
args = append(args, "--input", jsonConfigPath, "--input-dir", jsonDirPath, "--config", commonConfigPath)
stdoutIOWriter = io.MultiWriter(log.Writer(), os.Stdout)
stderrIOWriter = io.MultiWriter(log.Writer(), os.Stderr)
}
cmd := exec.Command(translatorBinaryPath, args...)
cmd.Stdout = stdoutIOWriter
cmd.Stderr = stderrIOWriter
err := cmd.Run()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
status := exitErr.Sys().(syscall.WaitStatus)
switch {
case status.Exited():
log.Printf("I! Return exit error: exit code=%d\n", status.ExitStatus())
if status.ExitStatus() == config.ERR_CODE_NOJSONFILE {
log.Printf("I! No json config files found, please provide config, exit now\n")
os.Exit(0)
}
}
} else {
log.Printf("Return other error: %s\n", err)
}
}
return err
}
func main() {
var writer io.WriteCloser
if runInContainer != config.RUN_IN_CONTAINER_TRUE {
writer = &lumberjack.Logger{
Filename: agentLogFilePath,
MaxSize: 100, //MB
MaxBackups: 5, //backup files
MaxAge: 7, //days
Compress: true,
}
log.SetOutput(writer)
}
if err := translateConfig(); err != nil {
log.Fatalf("E! Cannot translate JSON, ERROR is %v \n", err)
}
log.Printf("I! Config has been translated into TOML %s \n", tomlConfigPath)
printFileContents(tomlConfigPath)
log.Printf("I! Config has been translated into YAML %s \n", yamlConfigPath)
printFileContents(yamlConfigPath)
if err := startAgent(writer); err != nil {
log.Printf("E! Error when starting Agent, Error is %v \n", err)
os.Exit(1)
}
}
func printFileContents(path string) {
file, err := os.Open(path)
if err != nil {
// YAML file may or may not exist and that is okay.
if !errors.Is(err, fs.ErrNotExist) {
log.Printf("E! Error when printing file(%s) contents, Error is %v \n", path, err)
}
return
}
defer func() {
if err = file.Close(); err != nil {
log.Printf("E! Error when closing file, Error is %v \n", err)
}
}()
b, err := io.ReadAll(file)
if err != nil {
log.Printf("E! Error when reading file(%s), Error is %v \n", path, err)
}
log.Printf("D! config %v", string(b))
}