/
hardwarecharacteristics.go
398 lines (361 loc) · 10.7 KB
/
hardwarecharacteristics.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package instance
import (
"fmt"
"math"
"strconv"
"strings"
"text/scanner"
"unicode"
"github.com/juju/errors"
"github.com/juju/juju/core/arch"
)
// HardwareCharacteristics represents the characteristics of the instance (if known).
// Attributes that are nil are unknown or not supported.
type HardwareCharacteristics struct {
// Arch is the architecture of the processor.
Arch *string `json:"arch,omitempty" yaml:"arch,omitempty"`
// Mem is the size of RAM in megabytes.
Mem *uint64 `json:"mem,omitempty" yaml:"mem,omitempty"`
// RootDisk is the size of the disk in megabytes.
RootDisk *uint64 `json:"root-disk,omitempty" yaml:"rootdisk,omitempty"`
// RootDiskSource is where the disk storage resides.
RootDiskSource *string `json:"root-disk-source,omitempty" yaml:"rootdisksource,omitempty"`
// CpuCores is the number of logical cores the processor has.
CpuCores *uint64 `json:"cpu-cores,omitempty" yaml:"cpucores,omitempty"`
// CpuPower is a relative representation of the speed of the processor.
CpuPower *uint64 `json:"cpu-power,omitempty" yaml:"cpupower,omitempty"`
// Tags is a list of strings that identify the machine.
Tags *[]string `json:"tags,omitempty" yaml:"tags,omitempty"`
// AvailabilityZone defines the zone in which the machine resides.
AvailabilityZone *string `json:"availability-zone,omitempty" yaml:"availabilityzone,omitempty"`
// VirtType is the virtualisation type of the instance.
VirtType *string `json:"virt-type,omitempty" yaml:"virttype,omitempty"`
}
// quoteIfNeeded quotes s (according to Go string quoting rules) if it
// contains a comma or quote or whitespace character, otherwise it returns the
// original string.
func quoteIfNeeded(s string) string {
i := strings.IndexFunc(s, func(c rune) bool {
return c == ',' || c == '"' || unicode.IsSpace(c)
})
if i < 0 {
// No space or comma or quote in string, return as is
return s
}
return strconv.Quote(s)
}
func (hc HardwareCharacteristics) String() string {
var strs []string
if hc.Arch != nil {
strs = append(strs, fmt.Sprintf("arch=%s", quoteIfNeeded(*hc.Arch)))
}
if hc.CpuCores != nil {
strs = append(strs, fmt.Sprintf("cores=%d", *hc.CpuCores))
}
if hc.CpuPower != nil {
strs = append(strs, fmt.Sprintf("cpu-power=%d", *hc.CpuPower))
}
if hc.Mem != nil {
strs = append(strs, fmt.Sprintf("mem=%dM", *hc.Mem))
}
if hc.RootDisk != nil {
strs = append(strs, fmt.Sprintf("root-disk=%dM", *hc.RootDisk))
}
if hc.RootDiskSource != nil {
strs = append(strs, fmt.Sprintf("root-disk-source=%s", quoteIfNeeded(*hc.RootDiskSource)))
}
if hc.Tags != nil && len(*hc.Tags) > 0 {
escapedTags := make([]string, len(*hc.Tags))
for i, tag := range *hc.Tags {
escapedTags[i] = quoteIfNeeded(tag)
}
strs = append(strs, fmt.Sprintf("tags=%s", strings.Join(escapedTags, ",")))
}
if hc.AvailabilityZone != nil && *hc.AvailabilityZone != "" {
strs = append(strs, fmt.Sprintf("availability-zone=%s", quoteIfNeeded(*hc.AvailabilityZone)))
}
if hc.VirtType != nil && *hc.VirtType != "" {
strs = append(strs, fmt.Sprintf("virt-type=%s", quoteIfNeeded(*hc.VirtType)))
}
return strings.Join(strs, " ")
}
// Clone returns a copy of the hardware characteristics.
func (hc *HardwareCharacteristics) Clone() *HardwareCharacteristics {
if hc == nil {
return nil
}
clone := *hc
if hc.Tags != nil {
tags := make([]string, len(*hc.Tags))
copy(tags, *hc.Tags)
clone.Tags = &tags
}
return &clone
}
// MustParseHardware constructs a HardwareCharacteristics from the supplied arguments,
// as Parse, but panics on failure.
func MustParseHardware(args ...string) HardwareCharacteristics {
hc, err := ParseHardware(args...)
if err != nil {
panic(err)
}
return hc
}
// ParseHardware constructs a HardwareCharacteristics from the supplied arguments,
// each of which must contain only spaces and name=value pairs. If any
// name is specified more than once, an error is returned.
func ParseHardware(args ...string) (HardwareCharacteristics, error) {
hc := HardwareCharacteristics{}
for _, arg := range args {
arg = strings.TrimSpace(arg)
for arg != "" {
var err error
arg, err = hc.parseField(arg)
if err != nil {
return hc, errors.Trace(err)
}
arg = strings.TrimSpace(arg)
}
}
return hc, nil
}
// parseField parses a single name=value (or name="value") field into the
// corresponding field of the receiver.
func (hc *HardwareCharacteristics) parseField(s string) (rest string, err error) {
eq := strings.IndexByte(s, '=')
if eq <= 0 {
return s, errors.Errorf("malformed characteristic %q", s)
}
name, rest := s[:eq], s[eq+1:]
switch name {
case "tags":
// Tags is a multi-valued field (comma separated)
var values []string
values, rest, err = parseMulti(rest)
if err != nil {
return rest, errors.Errorf("%s: %v", name, err)
}
err = hc.setTags(values)
default:
// All other fields are single-valued
var value string
value, rest, err = parseSingle(rest, " ")
if err != nil {
return rest, errors.Errorf("%s: %v", name, err)
}
switch name {
case "arch":
err = hc.setArch(value)
case "cores":
err = hc.setCpuCores(value)
case "cpu-power":
err = hc.setCpuPower(value)
case "mem":
err = hc.setMem(value)
case "root-disk":
err = hc.setRootDisk(value)
case "root-disk-source":
err = hc.setRootDiskSource(value)
case "availability-zone":
err = hc.setAvailabilityZone(value)
case "virt-type":
err = hc.setVirtType(value)
default:
return rest, errors.Errorf("unknown characteristic %q", name)
}
}
if err != nil {
return rest, errors.Errorf("bad %q characteristic: %v", name, err)
}
return rest, nil
}
// parseSingle parses a single (optionally quoted) value from s and returns
// the value and the remainder of the string.
func parseSingle(s string, seps string) (value, rest string, err error) {
if len(s) > 0 && s[0] == '"' {
value, rest, err = parseQuotedString(s)
if err != nil {
return "", rest, errors.Trace(err)
}
} else {
sepPos := strings.IndexAny(s, seps)
value = s
if sepPos >= 0 {
value, rest = value[:sepPos], value[sepPos:]
}
}
return value, rest, nil
}
// parseMulti parses multiple (optionally quoted) comma-separated values from s
// and returns the values and the remainder of the string.
func parseMulti(s string) (values []string, rest string, err error) {
needComma := false
rest = s
for rest != "" && rest[0] != ' ' {
if needComma {
if rest[0] != ',' {
return values, rest, errors.New("expected comma after quoted value")
}
rest = rest[1:]
}
needComma = true
var value string
value, rest, err = parseSingle(rest, ", ")
if err != nil {
return values, rest, errors.Trace(err)
}
if value != "" {
values = append(values, value)
}
}
return values, rest, nil
}
// parseQuotedString parses a string name=value argument, returning the
// unquoted value and the remainder of the string.
func parseQuotedString(input string) (value, rest string, err error) {
// Use text/scanner to find end of quoted string
var s scanner.Scanner
s.Init(strings.NewReader(input))
s.Mode = scanner.ScanStrings
s.Whitespace = 0
var errMsg string
s.Error = func(s *scanner.Scanner, msg string) {
// Record first error
if errMsg == "" {
errMsg = msg
}
}
tok := s.Scan()
rest = input[s.Pos().Offset:]
if s.ErrorCount > 0 {
return "", rest, errors.Errorf("parsing quoted string: %s", errMsg)
}
if tok != scanner.String {
// Shouldn't happen; we only asked for strings
return "", rest, errors.Errorf("parsing quoted string: unexpected token %s", scanner.TokenString(tok))
}
// And then strconv to unquote it (oddly, text/scanner doesn't unquote)
unquoted, err := strconv.Unquote(s.TokenText())
if err != nil {
// Shouldn't happen; scanner should only return valid quoted strings
return "", rest, errors.Errorf("parsing quoted string: %v", err)
}
return unquoted, rest, nil
}
func (hc *HardwareCharacteristics) setArch(str string) error {
if hc.Arch != nil {
return errors.Errorf("already set")
}
if str != "" && !arch.IsSupportedArch(str) {
return errors.Errorf("%q not recognized", str)
}
hc.Arch = &str
return nil
}
func (hc *HardwareCharacteristics) setCpuCores(str string) (err error) {
if hc.CpuCores != nil {
return errors.Errorf("already set")
}
hc.CpuCores, err = parseUint64(str)
return
}
func (hc *HardwareCharacteristics) setCpuPower(str string) (err error) {
if hc.CpuPower != nil {
return errors.Errorf("already set")
}
hc.CpuPower, err = parseUint64(str)
return
}
func (hc *HardwareCharacteristics) setMem(str string) (err error) {
if hc.Mem != nil {
return errors.Errorf("already set")
}
hc.Mem, err = parseSize(str)
return
}
func (hc *HardwareCharacteristics) setRootDisk(str string) (err error) {
if hc.RootDisk != nil {
return errors.Errorf("already set")
}
hc.RootDisk, err = parseSize(str)
return
}
func (hc *HardwareCharacteristics) setRootDiskSource(str string) (err error) {
if hc.RootDiskSource != nil {
return errors.Errorf("already set")
}
if str != "" {
hc.RootDiskSource = &str
}
return
}
func (hc *HardwareCharacteristics) setTags(strs []string) (err error) {
if hc.Tags != nil {
return errors.Errorf("already set")
}
if len(strs) > 0 {
hc.Tags = &strs
}
return
}
func (hc *HardwareCharacteristics) setAvailabilityZone(str string) error {
if hc.AvailabilityZone != nil {
return errors.Errorf("already set")
}
if str != "" {
hc.AvailabilityZone = &str
}
return nil
}
func (hc *HardwareCharacteristics) setVirtType(str string) error {
if hc.VirtType != nil {
return errors.Errorf("already set")
}
// TODO (stickupkid): We potentially will want to allow "" to be a valid
// container virt-type, converting all empty strings to the default instance
// type. For now, allow LXD to fallback to the default instance type.
if str == "" {
return nil
}
if _, err := ParseVirtType(str); err != nil {
return errors.Trace(err)
}
hc.VirtType = &str
return nil
}
func parseUint64(str string) (*uint64, error) {
var value uint64
if str != "" {
val, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return nil, errors.Errorf("must be a non-negative integer")
}
value = val
}
return &value, nil
}
func parseSize(str string) (*uint64, error) {
var value uint64
if str != "" {
mult := 1.0
if m, ok := mbSuffixes[str[len(str)-1:]]; ok {
str = str[:len(str)-1]
mult = m
}
val, err := strconv.ParseFloat(str, 64)
if err != nil || val < 0 {
return nil, errors.Errorf("must be a non-negative float with optional M/G/T/P suffix")
}
val *= mult
value = uint64(math.Ceil(val))
}
return &value, nil
}
var mbSuffixes = map[string]float64{
"M": 1,
"G": 1024,
"T": 1024 * 1024,
"P": 1024 * 1024 * 1024,
}