forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
261 lines (237 loc) · 6.83 KB
/
migration.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package config
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"sort"
"strings"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
"github.com/influxdata/telegraf/migrations"
_ "github.com/influxdata/telegraf/migrations/all" // register all migrations
)
type section struct {
name string
begin int
content *ast.Table
raw *bytes.Buffer
}
func splitToSections(root *ast.Table) ([]section, error) {
var sections []section
for name, elements := range root.Fields {
switch name {
case "inputs", "outputs", "processors", "aggregators":
category, ok := elements.(*ast.Table)
if !ok {
return nil, fmt.Errorf("%q is not a table (%T)", name, category)
}
for plugin, elements := range category.Fields {
tbls, ok := elements.([]*ast.Table)
if !ok {
return nil, fmt.Errorf("elements of \"%s.%s\" is not a list of tables (%T)", name, plugin, elements)
}
for _, tbl := range tbls {
s := section{
name: name + "." + tbl.Name,
begin: tbl.Line,
content: tbl,
raw: &bytes.Buffer{},
}
sections = append(sections, s)
}
}
default:
tbl, ok := elements.(*ast.Table)
if !ok {
return nil, fmt.Errorf("%q is not a table (%T)", name, elements)
}
s := section{
name: name,
begin: tbl.Line,
content: tbl,
raw: &bytes.Buffer{},
}
sections = append(sections, s)
}
}
// Sort the TOML elements by begin (line-number)
sort.SliceStable(sections, func(i, j int) bool { return sections[i].begin < sections[j].begin })
return sections, nil
}
func assignTextToSections(data []byte, sections []section) ([]section, error) {
// Now assign the raw text to each section
if sections[0].begin > 0 {
sections = append([]section{{
name: "header",
begin: 0,
raw: &bytes.Buffer{},
}}, sections...)
}
var lineno int
scanner := bufio.NewScanner(bytes.NewBuffer(data))
for idx, next := range sections[1:] {
var buf bytes.Buffer
for lineno < next.begin-1 {
if !scanner.Scan() {
break
}
lineno++
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
buf.Write(scanner.Bytes())
buf.WriteString("\n")
continue
} else if buf.Len() > 0 {
if _, err := io.Copy(sections[idx].raw, &buf); err != nil {
return nil, fmt.Errorf("copying buffer failed: %w", err)
}
buf.Reset()
}
sections[idx].raw.Write(scanner.Bytes())
sections[idx].raw.WriteString("\n")
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("splitting by line failed: %w", err)
}
// If a comment is directly in front of the next section, without
// newline, the comment is assigned to the next section.
if buf.Len() > 0 {
if _, err := io.Copy(sections[idx+1].raw, &buf); err != nil {
return nil, fmt.Errorf("copying buffer failed: %w", err)
}
buf.Reset()
}
}
// Write the remaining to the last section
for scanner.Scan() {
sections[len(sections)-1].raw.Write(scanner.Bytes())
sections[len(sections)-1].raw.WriteString("\n")
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("splitting by line failed: %w", err)
}
return sections, nil
}
func ApplyMigrations(data []byte) ([]byte, uint64, error) {
root, err := toml.Parse(data)
if err != nil {
return nil, 0, fmt.Errorf("parsing failed: %w", err)
}
// Split the configuration into sections containing the location
// in the file.
sections, err := splitToSections(root)
if err != nil {
return nil, 0, fmt.Errorf("splitting to sections failed: %w", err)
}
if len(sections) == 0 {
return nil, 0, errors.New("no TOML configuration found")
}
// Assign the configuration text to the corresponding segments
sections, err = assignTextToSections(data, sections)
if err != nil {
return nil, 0, fmt.Errorf("assigning text failed: %w", err)
}
var applied uint64
// Do the actual global section migration(s)
for idx, s := range sections {
if strings.Contains(s.name, ".") {
continue
}
log.Printf("D! applying global migrations to section %q in line %d...", s.name, s.begin)
for _, migrate := range migrations.GlobalMigrations {
result, msg, err := migrate(s.name, s.content)
if err != nil {
if errors.Is(err, migrations.ErrNotApplicable) {
continue
}
return nil, 0, fmt.Errorf("migrating options of %q (line %d) failed: %w", s.name, s.begin, err)
}
if msg != "" {
log.Printf("I! Global section %q in line %d: %s", s.name, s.begin, msg)
}
s.raw = bytes.NewBuffer(result)
applied++
}
sections[idx] = s
}
// Do the actual plugin migration(s)
for idx, s := range sections {
migrate, found := migrations.PluginMigrations[s.name]
if !found {
continue
}
log.Printf("D! migrating plugin %q in line %d...", s.name, s.begin)
result, msg, err := migrate(s.content)
if err != nil {
return nil, 0, fmt.Errorf("migrating %q (line %d) failed: %w", s.name, s.begin, err)
}
if msg != "" {
log.Printf("I! Plugin %q in line %d: %s", s.name, s.begin, msg)
}
s.raw = bytes.NewBuffer(result)
tbl, err := toml.Parse(s.raw.Bytes())
if err != nil {
return nil, 0, fmt.Errorf("reparsing migrated %q (line %d) failed: %w", s.name, s.begin, err)
}
s.content = tbl
sections[idx] = s
applied++
}
// Do the actual plugin option migration(s)
for idx, s := range sections {
migrate, found := migrations.PluginOptionMigrations[s.name]
if !found {
continue
}
log.Printf("D! migrating options of plugin %q in line %d...", s.name, s.begin)
result, msg, err := migrate(s.content)
if err != nil {
if errors.Is(err, migrations.ErrNotApplicable) {
continue
}
return nil, 0, fmt.Errorf("migrating options of %q (line %d) failed: %w", s.name, s.begin, err)
}
if msg != "" {
log.Printf("I! Plugin %q in line %d: %s", s.name, s.begin, msg)
}
s.raw = bytes.NewBuffer(result)
sections[idx] = s
applied++
}
// Do general migrations applying to all plugins
for idx, s := range sections {
parts := strings.Split(s.name, ".")
if len(parts) != 2 {
continue
}
log.Printf("D! applying general migrations to plugin %q in line %d...", s.name, s.begin)
category, name := parts[0], parts[1]
for _, migrate := range migrations.GeneralMigrations {
result, msg, err := migrate(category, name, s.content)
if err != nil {
if errors.Is(err, migrations.ErrNotApplicable) {
continue
}
return nil, 0, fmt.Errorf("migrating options of %q (line %d) failed: %w", s.name, s.begin, err)
}
if msg != "" {
log.Printf("I! Plugin %q in line %d: %s", s.name, s.begin, msg)
}
s.raw = bytes.NewBuffer(result)
applied++
}
sections[idx] = s
}
// Reconstruct the config file from the sections
var buf bytes.Buffer
for _, s := range sections {
_, err = s.raw.WriteTo(&buf)
if err != nil {
return nil, applied, fmt.Errorf("joining output failed: %w", err)
}
}
return buf.Bytes(), applied, nil
}