/
supportedseries.go
708 lines (644 loc) · 17.7 KB
/
supportedseries.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package series
import (
"sort"
"strings"
"sync"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/os"
)
var (
// TODO(katco): Remove globals (lp:1633571)
logger = loggo.GetLogger("juju.juju.series")
)
type unknownOSForSeriesError string
func (e unknownOSForSeriesError) Error() string {
return `unknown OS for series: "` + string(e) + `"`
}
// IsUnknownOSForSeriesError returns true if err is of type unknownOSForSeriesError.
func IsUnknownOSForSeriesError(err error) bool {
_, ok := errors.Cause(err).(unknownOSForSeriesError)
return ok
}
type unknownSeriesVersionError string
func (e unknownSeriesVersionError) Error() string {
return `unknown version for series: "` + string(e) + `"`
}
// IsUnknownSeriesVersionError returns true if err is of type unknownSeriesVersionError.
func IsUnknownSeriesVersionError(err error) bool {
_, ok := errors.Cause(err).(unknownSeriesVersionError)
return ok
}
type unknownVersionSeriesError string
func (e unknownVersionSeriesError) Error() string {
return `unknown series for version: "` + string(e) + `"`
}
// IsUnknownVersionSeriesError returns true if err is of type unknownVersionSeriesError.
func IsUnknownVersionSeriesError(err error) bool {
_, ok := errors.Cause(err).(unknownVersionSeriesError)
return ok
}
// seriesVersions provides a mapping between series names and versions.
// The values here are current as of the time of writing. On Ubuntu systems, we update
// these values from /usr/share/distro-info/ubuntu.csv to ensure we have the latest values.
// On non-Ubuntu systems, these values provide a nice fallback option.
// Exported so tests can change the values to ensure the distro-info lookup works.
var seriesVersions = map[string]string{
"precise": "12.04",
"quantal": "12.10",
"raring": "13.04",
"saucy": "13.10",
"trusty": "14.04",
"utopic": "14.10",
"vivid": "15.04",
"wily": "15.10",
"xenial": "16.04",
"yakkety": "16.10",
"zesty": "17.04",
"artful": "17.10",
"bionic": "18.04",
"cosmic": "18.10",
"disco": "19.04",
"eoan": "19.10",
"focal": "20.04",
"groovy": "20.10",
"win2008r2": "win2008r2",
"win2012hvr2": "win2012hvr2",
"win2012hv": "win2012hv",
"win2012r2": "win2012r2",
"win2012": "win2012",
"win2016": "win2016",
"win2016hv": "win2016hv",
"win2016nano": "win2016nano",
"win2019": "win2019",
"win7": "win7",
"win8": "win8",
"win81": "win81",
"win10": "win10",
"centos7": "centos7",
"centos8": "centos8",
"opensuseleap": "opensuse42",
genericLinuxSeries: genericLinuxVersion,
}
// versionSeries provides a mapping between versions and series names.
var versionSeries = reverseSeriesVersion()
var centosSeries = map[string]string{
"centos7": "centos7",
"centos8": "centos8",
}
var opensuseSeries = map[string]string{
"opensuseleap": "opensuse42",
}
var kubernetesSeries = map[string]string{
"kubernetes": "kubernetes",
}
// DefaultSupportedLTS returns the latest LTS that Juju supports and is
// compatible with.
func DefaultSupportedLTS() string {
return "bionic"
}
// SeriesVersionInfo represents a ubuntu series that includes the version, if the
// series is an LTS and the supported defines if Juju supports the series
// version.
type SeriesVersionInfo struct {
Version string
// LTS provides a lookup for a LTS series. Like seriesVersions,
// the values here are current at the time of writing.
LTS bool
// Supported defines if Juju classifies the series as officially supported.
Supported bool
// Extended security maintenance for customers, extends the supported bool
// for how Juju classifies the series.
ESMSupported bool
// WarningInfo shows any potential issues when parsing the series version
// information.
WarningInfo []string
// CreatedByLocalDistroInfo indicates that the series version was created
// by the local distro-info information on the system.
// This is useful to understand why a version appears yet is not supported.
CreatedByLocalDistroInfo bool
}
var ubuntuSeries = map[string]SeriesVersionInfo{
"precise": {
Version: "12.04",
},
"quantal": {
Version: "12.10",
},
"raring": {
Version: "13.04",
},
"saucy": {
Version: "13.10",
},
"trusty": {
Version: "14.04",
LTS: true,
ESMSupported: true,
},
"utopic": {
Version: "14.10",
},
"vivid": {
Version: "15.04",
},
"wily": {
Version: "15.10",
},
"xenial": {
Version: "16.04",
LTS: true,
Supported: true,
ESMSupported: true,
},
"yakkety": {
Version: "16.10",
},
"zesty": {
Version: "17.04",
},
"artful": {
Version: "17.10",
},
"bionic": {
Version: "18.04",
LTS: true,
Supported: true,
ESMSupported: true,
},
"cosmic": {
Version: "18.10",
},
"disco": {
Version: "19.04",
},
"eoan": {
Version: "19.10",
Supported: true,
},
"focal": {
Version: "20.04",
LTS: true,
// TODO - hard code to true when focal is released (fallback is to rely on distro-info.csv)
Supported: false,
},
"groovy": {
Version: "20.10",
Supported: false,
},
}
var nonUbuntuSeries = map[string]SeriesVersionInfo{
"win2008r2": {
Version: "win2008r2",
Supported: true,
},
"win2012hvr2": {
Version: "win2012hvr2",
Supported: true,
},
"win2012hv": {
Version: "win2012hv",
Supported: true,
},
"win2012r2": {
Version: "win2012r2",
Supported: true,
},
"win2012": {
Version: "win2012",
Supported: true,
},
"win2016": {
Version: "win2016",
Supported: true,
},
"win2016hv": {
Version: "win2016hv",
Supported: true,
},
"win2016nano": {
Version: "win2016nano",
Supported: true,
},
"win2019": {
Version: "win2019",
Supported: true,
},
"win7": {
Version: "win7",
Supported: true,
},
"win8": {
Version: "win8",
Supported: true,
},
"win81": {
Version: "win81",
Supported: true,
},
"win10": {
Version: "win10",
Supported: true,
},
"centos7": {
Version: "centos7",
Supported: true,
},
"centos8": {
Version: "centos8",
Supported: true,
},
"opensuseleap": {
Version: "opensuse42",
Supported: true,
},
genericLinuxSeries: {
Version: genericLinuxVersion,
Supported: true,
},
"kubernetes": {
Version: "kubernetes",
Supported: true,
},
}
// Windows versions come in various flavors:
// Standard, Datacenter, etc. We use string prefix match them to one
// of the following. Specify the longest name in a particular series first
// For example, if we have "Win 2012" and "Win 2012 R2", we specify "Win 2012 R2" first.
// We need to make sure we manually update this list with each new windows release.
var windowsVersionMatchOrder = []string{
"Hyper-V Server 2012 R2",
"Hyper-V Server 2012",
"Windows Server 2008 R2",
"Windows Server 2012 R2",
"Windows Server 2012",
"Hyper-V Server 2016",
"Windows Server 2016",
"Windows Server 2019",
"Windows Storage Server 2012 R2",
"Windows Storage Server 2012",
"Windows Storage Server 2016",
"Windows 7",
"Windows 8.1",
"Windows 8",
"Windows 10",
}
// windowsVersions is a mapping consisting of the output from
// the following WMI query: (gwmi Win32_OperatingSystem).Name
var windowsVersions = map[string]string{
"Hyper-V Server 2012 R2": "win2012hvr2",
"Hyper-V Server 2012": "win2012hv",
"Windows Server 2008 R2": "win2008r2",
"Windows Server 2012 R2": "win2012r2",
"Windows Server 2012": "win2012",
"Hyper-V Server 2016": "win2016hv",
"Windows Server 2016": "win2016",
"Windows Server 2019": "win2019",
"Windows Storage Server 2012 R2": "win2012r2",
"Windows Storage Server 2012": "win2012",
"Windows Storage Server 2016": "win2016",
"Windows Storage Server 2019": "win2019",
"Windows 7": "win7",
"Windows 8.1": "win81",
"Windows 8": "win8",
"Windows 10": "win10",
}
// windowsNanoVersions is a mapping from the product name
// stored in registry to a juju defined nano-series
// On the nano version so far the product name actually
// is identical to the correspondent main windows version
// and the information about it being nano is stored in
// a different place.
var windowsNanoVersions = map[string]string{
"Windows Server 2016": "win2016nano",
}
// WindowsVersions returns all windows versions as a map
// If we have nan and windows version in common, nano takes precedence
func WindowsVersions() map[string]string {
save := make(map[string]string)
for i, val := range windowsVersions {
save[i] = val
}
for i, val := range windowsNanoVersions {
save[i] = val
}
return save
}
func OverwrittenWindowsVersions() []string {
var overwrittenValues []string
for i, _ := range windowsNanoVersions {
if overwritten, ok := windowsVersions[i]; ok {
overwrittenValues = append(overwrittenValues, overwritten)
}
}
return overwrittenValues
}
// IsWindowsNano tells us whether the provided series is a
// nano series. It may seem futile at this point, but more
// nano series will come up with time.
// This is here and not in a windows specific package
// because we might want to take decisions dependant on
// whether we have a nano series or not in more general code.
func IsWindowsNano(series string) bool {
for _, val := range windowsNanoVersions {
if val == series {
return true
}
}
return false
}
// GetOSFromSeries will return the operating system based
// on the series that is passed to it
func GetOSFromSeries(series string) (os.OSType, error) {
if series == "" {
return os.Unknown, errors.NotValidf("series %q", series)
}
osType, err := getOSFromSeries(series)
if err == nil {
return osType, nil
}
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
return getOSFromSeries(series)
}
func getOSFromSeries(series string) (os.OSType, error) {
if _, ok := ubuntuSeries[series]; ok {
return os.Ubuntu, nil
}
if _, ok := centosSeries[series]; ok {
return os.CentOS, nil
}
if _, ok := opensuseSeries[series]; ok {
return os.OpenSUSE, nil
}
if _, ok := kubernetesSeries[series]; ok {
return os.Kubernetes, nil
}
if series == genericLinuxSeries {
return os.GenericLinux, nil
}
for _, val := range windowsVersions {
if val == series {
return os.Windows, nil
}
}
for _, val := range windowsNanoVersions {
if val == series {
return os.Windows, nil
}
}
for _, val := range macOSXSeries {
if val == series {
return os.OSX, nil
}
}
return os.Unknown, errors.Trace(unknownOSForSeriesError(series))
}
var (
seriesVersionsMutex sync.Mutex
)
// SeriesVersion returns the version for the specified series.
func SeriesVersion(series string) (string, error) {
if series == "" {
return "", errors.Trace(unknownSeriesVersionError(""))
}
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
if vers, ok := seriesVersions[series]; ok {
return vers, nil
}
updateSeriesVersionsOnce()
if vers, ok := seriesVersions[series]; ok {
return vers, nil
}
return "", errors.Trace(unknownSeriesVersionError(series))
}
// UbuntuSeriesVersion returns the ubuntu version for the specified series.
func UbuntuSeriesVersion(series string) (string, error) {
if series == "" {
return "", errors.Trace(unknownSeriesVersionError(""))
}
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
if vers, ok := ubuntuSeries[series]; ok {
return vers.Version, nil
}
updateSeriesVersionsOnce()
if vers, ok := ubuntuSeries[series]; ok {
return vers.Version, nil
}
return "", errors.Trace(unknownSeriesVersionError(series))
}
// VersionSeries returns the series (e.g.trusty) for the specified version (e.g. 14.04).
func VersionSeries(version string) (string, error) {
if version == "" {
return "", errors.Trace(unknownVersionSeriesError(""))
}
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
if series, ok := versionSeries[version]; ok {
return series, nil
}
updateSeriesVersionsOnce()
if series, ok := versionSeries[version]; ok {
return series, nil
}
return "", errors.Trace(unknownVersionSeriesError(version))
}
// WindowsVersionSeries returns the series (eg: win2012r2) for the specified version
// (eg: Windows Server 2012 R2 Standard)
func WindowsVersionSeries(version string) (string, error) {
if version == "" {
return "", errors.Trace(unknownVersionSeriesError(""))
}
for _, val := range windowsVersionMatchOrder {
if strings.HasPrefix(version, val) {
return windowsVersions[val], nil
}
}
return "", errors.Trace(unknownVersionSeriesError(""))
}
// CentOSVersionSeries validates that the supplied series (eg: centos7)
// is supported.
func CentOSVersionSeries(version string) (string, error) {
if version == "" {
return "", errors.Trace(unknownVersionSeriesError(""))
}
if series, ok := centosSeries[version]; ok {
return series, nil
}
return "", errors.Trace(unknownVersionSeriesError(""))
}
// SupportedLts are the current supported LTS series in ascending order.
func SupportedLts() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
versions := []string{}
for _, version := range ubuntuSeries {
if !version.LTS || !version.Supported {
continue
}
versions = append(versions, version.Version)
}
sort.Strings(versions)
sorted := []string{}
for _, v := range versions {
sorted = append(sorted, versionSeries[v])
}
return sorted
}
// latestLtsSeries is used to ensure we only do
// the work to determine the latest lts series once.
var latestLtsSeries string
// LatestLts returns the Latest LTS Series found in distro-info
func LatestLts() string {
if latestLtsSeries != "" {
return latestLtsSeries
}
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
var latest string
for k, version := range ubuntuSeries {
if !version.LTS || !version.Supported {
continue
}
if version.Version > ubuntuSeries[latest].Version {
latest = k
}
}
latestLtsSeries = latest
return latest
}
// SetLatestLtsForTesting is provided to allow tests to override the lts series
// used and decouple the tests from the host by avoiding calling out to
// distro-info. It returns the previous setting so that it may be set back to
// the original value by the caller.
func SetLatestLtsForTesting(series string) string {
old := latestLtsSeries
latestLtsSeries = series
return old
}
func updateVersionSeries() {
versionSeries = reverseSeriesVersion()
}
// reverseSeriesVersion returns reverse of SeriesVersionInfo map,
// keyed on versions with series as values.
func reverseSeriesVersion() map[string]string {
reverse := make(map[string]string, len(seriesVersions))
for k, v := range seriesVersions {
reverse[v] = k
}
return reverse
}
// SupportedSeries returns the series on which we can run Juju workloads.
func SupportedSeries() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
var series []string
for s := range seriesVersions {
series = append(series, s)
}
return series
}
func allSeriesVersions() map[string]SeriesVersionInfo {
all := map[string]SeriesVersionInfo{}
for k, v := range ubuntuSeries {
all[k] = v
}
for k, v := range nonUbuntuSeries {
all[k] = v
}
return all
}
// SupportedJujuControllerSeries returns a slice of juju supported series that
// target a controller (bootstrapping).
func SupportedJujuControllerSeries() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
var series []string
for s, version := range ubuntuSeries {
if !version.Supported {
continue
}
series = append(series, s)
}
return series
}
// SupportedJujuWorkloadSeries returns a slice of juju supported series that
// target a workload (deploying a charm).
func SupportedJujuWorkloadSeries() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
var series []string
for s, version := range allSeriesVersions() {
if !version.Supported {
continue
}
series = append(series, s)
}
return series
}
// SupportedJujuSeries returns a slice of juju supported series that also
// target a workload.
func SupportedJujuSeries() []string {
return SupportedJujuWorkloadSeries()
}
// ESMSupportedJujuSeries returns a slice of just juju extended security
// maintenance supported ubuntu series.
func ESMSupportedJujuSeries() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
var series []string
for s, version := range ubuntuSeries {
if !version.ESMSupported {
continue
}
series = append(series, s)
}
return series
}
// OSSupportedSeries returns the series of the specified OS on which we
// can run Juju workloads.
func OSSupportedSeries(os os.OSType) []string {
var osSeries []string
for _, series := range SupportedSeries() {
seriesOS, err := GetOSFromSeries(series)
if err != nil || seriesOS != os {
continue
}
osSeries = append(osSeries, series)
}
return osSeries
}
// UpdateSeriesVersions forces an update of the series versions by querying
// distro-info if possible.
func UpdateSeriesVersions() error {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
err := updateLocalSeriesVersions()
if err != nil {
return err
}
updateVersionSeries()
latestLtsSeries = ""
return nil
}
var updatedseriesVersions bool
func updateSeriesVersionsOnce() {
if !updatedseriesVersions {
if err := updateLocalSeriesVersions(); err != nil {
logger.Warningf("failed to update distro info: %v", err)
}
updateVersionSeries()
updatedseriesVersions = true
}
}