-
Notifications
You must be signed in to change notification settings - Fork 70
/
utils.go
200 lines (166 loc) · 5.81 KB
/
utils.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package migrate
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
metricArg = "metrics"
inventoryArg = "inventory"
eventsArg = "events"
exeSuffix = ".exe"
prefixArg = "--"
prefixArgShort = "-"
)
// V3toV4Result represents the result of a migration
type V3toV4Result struct {
V3toV4Result string `json:"migrateV3toV4Result"`
}
func V3toV4(pathConfiguration string, pathDefinition string, pathOutput string, overwrite bool) string {
if _, err := os.Stat(pathOutput); err == nil && !overwrite {
log.Fatal(fmt.Errorf("file '%s' already exist and overwrite option is set to false", pathOutput))
}
// Reading old Definition file
v3Definition := Plugin{}
err := readAndUnmarshallConfig(pathDefinition, &v3Definition)
if err != nil {
log.Fatal(fmt.Errorf("error reading old config definition: %w", err))
}
// Reading old Configuration file
v3Configuration := PluginInstanceWrapper{}
err = readAndUnmarshallConfig(pathConfiguration, &v3Configuration)
if err != nil {
log.Fatal(fmt.Errorf("error reading old config configuration: %w", err))
}
// Populating new config
v4config, err := populateV4Config(v3Definition, v3Configuration)
if err != nil {
log.Fatal(fmt.Errorf("error populating new config: %w", err))
}
// Writing output
err = writeOutput(v4config, pathDefinition, pathConfiguration, pathOutput)
if err != nil {
log.Fatal(fmt.Errorf("error writing output: %w", err))
}
return fmt.Sprintf("The config has been migrated and placed in: %s", pathOutput)
}
func readAndUnmarshallConfig(path string, out interface{}) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("opening %s, %w", path, err)
}
defer file.Close()
err = yaml.NewDecoder(file).Decode(out)
if err != nil {
return fmt.Errorf("decoding %s, %w", path, err)
}
return nil
}
func populateV4Config(v3Definition Plugin, v3Configuration PluginInstanceWrapper) (*v4, error) {
if v3Configuration.IntegrationName != v3Definition.Name {
return nil, fmt.Errorf("IntegrationName != Name: %s!=%s", v3Configuration.IntegrationName, v3Definition.Name)
}
// The field os does not have currently a simple way to be migrated
if v3Definition.OS != "" {
log.Debugf("The old definitions had a os directive, %s. Usually it is not needed, use `when` field otherwhise", v3Definition.OS)
}
v4Config := &v4{}
for commandName, pluginV1Command := range v3Definition.Commands {
for _, pluginV1Instance := range v3Configuration.Instances {
if commandName == pluginV1Instance.Command {
integrationInstance := populateConfigEntry(pluginV1Instance, pluginV1Command)
v4Config.Integrations = append(v4Config.Integrations, integrationInstance)
}
}
}
return v4Config, nil
}
func populateConfigEntry(pluginV1Instance *PluginV1Instance, pluginV1Command *PluginV1Command) ConfigEntry {
configEntry := ConfigEntry{}
if len(pluginV1Command.Command) == 0 {
return configEntry
}
executable := pluginV1Command.Command[0]
binaryName := filepath.Base(executable)
configEntry.InstanceName = strings.TrimSuffix(binaryName, exeSuffix)
configEntry.Interval = fmt.Sprintf("%ds", pluginV1Command.Interval)
configEntry.Labels = pluginV1Instance.Labels
configEntry.User = pluginV1Instance.IntegrationUser
configEntry.InventorySource = pluginV1Command.Prefix
configEntry.Env = map[string]string{}
for k, v := range pluginV1Instance.Arguments {
configEntry.Env[strings.ToUpper(k)] = v
}
// Please notice that this is a simplification. If it is an absolute path we are adding it to the exec
// if is a relative path or a integration name, we are assuming it is a standard integration included into the path
if filepath.IsAbs(executable) {
configEntry.Exec = pluginV1Command.Command
} else {
buildCLIArgs(pluginV1Command, &configEntry)
}
return configEntry
}
func buildCLIArgs(pluginV1Command *PluginV1Command, configEntry *ConfigEntry) {
for index, arg := range pluginV1Command.Command {
if index == 0 {
// the first arg in command is the binary name
continue
}
sanitized := strings.TrimPrefix(arg, prefixArg)
sanitized = strings.TrimPrefix(sanitized, prefixArgShort)
if sanitized == metricArg || sanitized == inventoryArg || sanitized == eventsArg {
configEntry.Env[strings.ToUpper(sanitized)] = "true"
} else {
configEntry.CLIArgs = append(configEntry.CLIArgs, arg)
}
}
}
func writeOutput(v4Config *v4, pathDefinition string, pathConfiguration string, pathOutput string) error {
if v4Config == nil {
return fmt.Errorf("v4Config pointer is nil")
}
file, err := os.OpenFile(pathOutput, os.O_RDWR|os.O_CREATE|syscall.O_TRUNC, 0666)
if err != nil {
return fmt.Errorf("opening File %s, %w", pathOutput, err)
}
defer file.Close()
err = writeV4Config(v4Config, file)
if err != nil {
return fmt.Errorf("writing v4 config, %w", err)
}
err = writeFileAsComment(file, pathDefinition)
if err != nil {
return fmt.Errorf("adding old definition as comment, %w", err)
}
err = writeFileAsComment(file, pathConfiguration)
if err != nil {
return fmt.Errorf("adding old configuration as comment, %w", err)
}
return nil
}
func writeV4Config(v4Config *v4, file *os.File) error {
// see https://github.com/go-yaml/yaml/commit/7649d4548cb53a614db133b2a8ac1f31859dda8c
yaml.FutureLineWrap()
err := yaml.NewEncoder(file).Encode(*v4Config)
if err != nil {
return fmt.Errorf("writing v4ConfigBytes, %w", err)
}
return nil
}
func writeFileAsComment(file *os.File, filename string) error {
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("reading file to add it as comment: %w", err)
}
fileCommented := strings.ReplaceAll(string(fileContent), "\n", "\n## ")
_, err = file.Write([]byte("\n\n## " + fileCommented))
if err != nil {
return fmt.Errorf("writing text as comment: %w", err)
}
return nil
}