aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/xml/config.go
blob: 0ce7413d67118df5ce7451e1d446b6856efe50ad (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package xml

import (
	"bufio"
	"bytes"
	"encoding/xml"
	"fmt"
	"io"
	"regexp"
	"strconv"
	"strings"

	"github.com/vespa-engine/vespa/client/go/internal/util"
	"github.com/vespa-engine/vespa/client/go/internal/vespa"
)

var DefaultDeployment Deployment

func init() {
	defaultDeploymentRaw := `<deployment version="1.0">
  <prod>
    <region>aws-us-east-1c</region>
  </prod>
</deployment>`
	d, err := ReadDeployment(strings.NewReader(defaultDeploymentRaw))
	if err != nil {
		util.JustExitWith(err)
	}
	DefaultDeployment = d
}

// Deployment represents the contents of a deployment.xml file.
type Deployment struct {
	Root     xml.Name   `xml:"deployment"`
	Version  string     `xml:"version,attr"`
	Instance []Instance `xml:"instance"`
	Prod     Prod       `xml:"prod"`
	rawXML   bytes.Buffer
}

type Instance struct {
	Prod Prod `xml:"prod"`
}

type Prod struct {
	Regions []Region `xml:"region"`
}

type Region struct {
	Name string `xml:",chardata"`
}

func (d Deployment) String() string { return d.rawXML.String() }

// Replace replaces any elements of name found under parentName with data.
func (s *Deployment) Replace(parentName, name string, data interface{}) error {
	rewritten, err := Replace(&s.rawXML, parentName, name, data)
	if err != nil {
		return err
	}
	newXML, err := ReadDeployment(strings.NewReader(rewritten))
	if err != nil {
		return err
	}
	*s = newXML
	return nil
}

// Services represents the contents of a services.xml file.
type Services struct {
	Root      xml.Name    `xml:"services"`
	Container []Container `xml:"container"`
	Content   []Content   `xml:"content"`
	rawXML    bytes.Buffer
}

type Container struct {
	Root  xml.Name `xml:"container"`
	ID    string   `xml:"id,attr"`
	Nodes Nodes    `xml:"nodes"`
}

type Content struct {
	ID    string `xml:"id,attr"`
	Nodes Nodes  `xml:"nodes"`
}

type Nodes struct {
	Count     string     `xml:"count,attr"`
	Resources *Resources `xml:"resources,omitempty"`
}

type Resources struct {
	Vcpu   string `xml:"vcpu,attr"`
	Memory string `xml:"memory,attr"`
	Disk   string `xml:"disk,attr"`
}

func (s Services) String() string { return s.rawXML.String() }

// Replace replaces any elements of name found under parentName with data.
func (s *Services) Replace(parentName, name string, data interface{}) error {
	rewritten, err := Replace(&s.rawXML, parentName, name, data)
	if err != nil {
		return err
	}
	newXML, err := ReadServices(strings.NewReader(rewritten))
	if err != nil {
		return err
	}
	*s = newXML
	return nil
}

func (r Resources) String() string {
	return fmt.Sprintf("vcpu=%s,memory=%s,disk=%s", r.Vcpu, r.Memory, r.Disk)
}

// ReadDeployment reads deployment.xml from reader r.
func ReadDeployment(r io.Reader) (Deployment, error) {
	var deployment Deployment
	var rawXML bytes.Buffer
	dec := xml.NewDecoder(io.TeeReader(r, &rawXML))
	if err := dec.Decode(&deployment); err != nil {
		return Deployment{}, err
	}
	deployment.rawXML = rawXML
	return deployment, nil
}

// ReadServices reads services.xml from reader r.
func ReadServices(r io.Reader) (Services, error) {
	var services Services
	var rawXML bytes.Buffer
	dec := xml.NewDecoder(io.TeeReader(r, &rawXML))
	if err := dec.Decode(&services); err != nil {
		return Services{}, err
	}
	services.rawXML = rawXML
	return services, nil
}

// Regions returns given region names as elements.
func Regions(names ...string) []Region {
	var regions []Region
	for _, z := range names {
		regions = append(regions, Region{Name: z})
	}
	return regions
}

// ParseResources parses nodes resources from string s.
func ParseResources(s string) (Resources, error) {
	var parts []string
	inRange := false
	var sb strings.Builder
	for _, c := range s {
		if inRange {
			if c == ']' {
				inRange = false
			}
		} else {
			if c == '[' {
				inRange = true
			} else if c == ',' {
				parts = append(parts, sb.String())
				sb.Reset()
				continue
			}
		}
		sb.WriteRune(c)
	}
	parts = append(parts, sb.String())
	if len(parts) != 3 {
		return Resources{}, fmt.Errorf("invalid resources: %q", s)
	}
	vcpu, err := parseResource("vcpu", parts[0])
	if err != nil {
		return Resources{}, err
	}
	memory, err := parseResource("memory", parts[1])
	if err != nil {
		return Resources{}, err
	}
	disk, err := parseResource("disk", parts[2])
	if err != nil {
		return Resources{}, err
	}
	return Resources{Vcpu: vcpu, Memory: memory, Disk: disk}, nil
}

// ParseNodeCount parses a node count range from string s.
func ParseNodeCount(s string) (int, int, error) {
	parseErr := fmt.Errorf("invalid node count: %q", s)
	min, max := 0, 0
	n, err := strconv.Atoi(s)
	if err == nil {
		min = n
		max = n
	} else if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
		parts := strings.Split(s[1:len(s)-1], ",")
		if len(parts) != 2 {
			return 0, 0, parseErr
		}
		min, err = strconv.Atoi(strings.TrimSpace(parts[0]))
		if err != nil {
			return 0, 0, parseErr
		}
		max, err = strconv.Atoi(strings.TrimSpace(parts[1]))
		if err != nil {
			return 0, 0, parseErr
		}
	} else {
		return 0, 0, parseErr
	}

	if min <= 0 || min > max {
		return 0, 0, parseErr
	}
	return min, max, nil
}

// IsProdRegion returns whether string s is a valid production region.
func IsProdRegion(s string, system vespa.System) bool {
	// TODO: Add support for cd and main systems
	if system.Name == vespa.PublicCDSystem.Name {
		return s == "aws-us-east-1c"
	}
	switch s {
	case "aws-us-east-1c", "aws-us-west-2a",
		"aws-eu-west-1a", "aws-ap-northeast-1a",
		"gcp-us-central1-f":
		return true
	}
	return false
}

func parseResource(field, s string) (string, error) {
	parts := strings.SplitN(s, "=", 2)
	if len(parts) != 2 || strings.TrimSpace(parts[0]) != field {
		return "", fmt.Errorf("invalid value for %s field: %q", field, s)
	}
	return strings.TrimSpace(parts[1]), nil
}

// ReplaceRaw finds all elements of name in rawXML and replaces their contents with value.
func ReplaceRaw(rawXML, name, value string) string {
	startElement := "<" + name + ">"
	endElement := "</" + name + ">"
	re := regexp.MustCompile(regexp.QuoteMeta(startElement) + ".*" + regexp.QuoteMeta(endElement))
	return re.ReplaceAllString(rawXML, startElement+value+endElement)
}

// Replace looks for an element name in the XML read from reader r, appearing inside a element named parentName.
//
// Any matching elements found are replaced with data. If parentName contains an ID selector, e.g. "email#my-id", only
// the elements inside the parent element with the attribute id="my-id" are replaced.
//
// If data is nil, any matching elements are removed instead of replaced.
func Replace(r io.Reader, parentName, name string, data interface{}) (string, error) {
	var buf bytes.Buffer
	dec := xml.NewDecoder(r)
	enc := xml.NewEncoder(&buf)
	enc.Indent("", "  ")

	parts := strings.SplitN(parentName, "#", 2)
	id := ""
	if len(parts) > 1 {
		parentName = parts[0]
		id = parts[1]
	}

	foundParent := false
	replacing := false
	done := false
	for {
		token, err := dec.Token()
		if err == io.EOF {
			break
		} else if err != nil {
			return "", err
		}
		token = joinNamespace(token)
		if isEndElement(parentName, token) {
			foundParent = false
			done = false
		}
		if _, ok := getStartElement(parentName, id, token); ok {
			foundParent = true
		}
		if foundParent {
			if isEndElement(name, token) {
				replacing = false
				continue
			}
			replacableElement, ok := getStartElement(name, "", token)
			if ok {
				replacing = true
			}
			if replacing {
				if !done && data != nil {
					replacableElement.Attr = nil // Clear any existing attributes as given data should contain the wanted ones
					if err := enc.EncodeElement(data, replacableElement); err != nil {
						return "", err
					}
					done = true
				}
				continue
			}
		}
		if err := enc.EncodeToken(token); err != nil {
			return "", err
		}
	}
	if err := enc.Flush(); err != nil {
		return "", err
	}
	var sb strings.Builder
	scanner := bufio.NewScanner(&buf)
	for scanner.Scan() {
		line := scanner.Text()
		// Skip lines containing only whitespace
		if strings.TrimSpace(line) == "" {
			continue
		}
		sb.WriteString(line)
		sb.WriteRune('\n')
	}
	if err := scanner.Err(); err != nil {
		return "", err
	}
	return sb.String(), nil
}

func joinNamespace(token xml.Token) xml.Token {
	// Hack to work around the broken namespace support in Go
	// https://github.com/golang/go/issues/13400
	if startElement, ok := token.(xml.StartElement); ok {
		attr := make([]xml.Attr, 0, len(startElement.Attr))
		for _, a := range startElement.Attr {
			if a.Name.Space != "" {
				a.Name.Space = ""
				a.Name.Local = "xmlns:" + a.Name.Local
			}
			attr = append(attr, a)
		}
		startElement.Attr = attr
		return startElement
	}
	return token
}

func getStartElement(name, id string, token xml.Token) (xml.StartElement, bool) {
	startElement, ok := token.(xml.StartElement)
	if !ok {
		return xml.StartElement{}, false
	}
	matchingID := id == ""
	for _, attr := range startElement.Attr {
		if attr.Name.Local == "id" && attr.Value == id {
			matchingID = true
		}
	}
	return startElement, startElement.Name.Local == name && matchingID
}

func isEndElement(name, token xml.Token) bool {
	endElement, ok := token.(xml.EndElement)
	return ok && endElement.Name.Local == name
}