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
|
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package series
import (
"sort"
"sync"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils/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",
"win2008r2": "win2008r2",
"win2012hvr2": "win2012hvr2",
"win2012hv": "win2012hv",
"win2012r2": "win2012r2",
"win2012": "win2012",
"win2016": "win2016",
"win2016hv": "win2016hv",
"win2016nano": "win2016nano",
"win7": "win7",
"win8": "win8",
"win81": "win81",
"win10": "win10",
"centos7": "centos7",
"opensuseleap": "opensuse42",
genericLinuxSeries: genericLinuxVersion,
}
// versionSeries provides a mapping between versions and series names.
var versionSeries = reverseSeriesVersion()
var centosSeries = map[string]string{
"centos7": "centos7",
}
var opensuseSeries = map[string]string{
"opensuseleap": "opensuse42",
}
var ubuntuSeries = 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",
}
// ubuntuLTS provides a lookup for current LTS series. Like seriesVersions,
// the values here are current at the time of writing. On Ubuntu systems this
// map is updated by updateDistroInfo, using data 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.
var ubuntuLTS = map[string]bool{
"precise": true,
"trusty": true,
"xenial": true,
// TODO(rogpeppe) add bionic when it's released.
}
// 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 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 Storage Server 2012 R2": "win2012r2",
"Windows Storage Server 2012": "win2012",
"Windows Storage Server 2016": "win2016",
"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
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
}
// 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
}
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 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))
}
// 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))
}
// SupportedLts are the current supported LTS series in ascending order.
func SupportedLts() []string {
seriesVersionsMutex.Lock()
defer seriesVersionsMutex.Unlock()
updateSeriesVersionsOnce()
versions := []string{}
for k := range ubuntuLTS {
versions = append(versions, ubuntuSeries[k])
}
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 := range ubuntuLTS {
if ubuntuSeries[k] > ubuntuSeries[latest] {
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 seriesVersion 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
}
// 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
}
}
|