forked from juju/utils
/
series_linux.go
116 lines (105 loc) · 3.12 KB
/
series_linux.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package series
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
jujuos "github.com/juju/utils/os"
)
var (
// osReleaseFile is the name of the file that is read in order to determine
// the linux type release version.
osReleaseFile = "/etc/os-release"
)
func readSeries() (string, error) {
values, err := jujuos.ReadOSRelease(osReleaseFile)
if err != nil {
return "unknown", err
}
updateSeriesVersionsOnce()
return seriesFromOSRelease(values)
}
func seriesFromOSRelease(values map[string]string) (string, error) {
switch values["ID"] {
case strings.ToLower(jujuos.Ubuntu.String()):
return getValue(ubuntuSeries, values["VERSION_ID"])
case strings.ToLower(jujuos.Arch.String()):
return getValue(archSeries, values["VERSION_ID"])
case strings.ToLower(jujuos.CentOS.String()):
codename := fmt.Sprintf("%s%s", values["ID"], values["VERSION_ID"])
return getValue(centosSeries, codename)
default:
return "unknown", nil
}
}
func getValue(from map[string]string, val string) (string, error) {
for serie, ver := range from {
if ver == val {
return serie, nil
}
}
return "unknown", errors.New("Could not determine series")
}
// ReleaseVersion looks for the value of VERSION_ID in the content of
// the os-release. If the value is not found, the file is not found, or
// an error occurs reading the file, an empty string is returned.
func ReleaseVersion() string {
release, err := jujuos.ReadOSRelease(osReleaseFile)
if err != nil {
return ""
}
return release["VERSION_ID"]
}
func updateLocalSeriesVersions() error {
return updateDistroInfo()
}
var distroInfo = "/usr/share/distro-info/ubuntu.csv"
// updateDistroInfo updates seriesVersions from /usr/share/distro-info/ubuntu.csv if possible..
func updateDistroInfo() error {
// We need to find the series version eg 12.04 from the series eg precise. Use the information found in
// /usr/share/distro-info/ubuntu.csv provided by distro-info-data package.
f, err := os.Open(distroInfo)
if err != nil {
// On non-Ubuntu systems this file won't exist but that's expected.
return nil
}
defer f.Close()
bufRdr := bufio.NewReader(f)
// Only find info for precise or later.
// TODO: only add in series that are supported (i.e. before end of life)
preciseOrLaterFound := false
for {
line, err := bufRdr.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("reading distro info file file: %v", err)
}
// lines are of the form: "12.04 LTS,Precise Pangolin,precise,2011-10-13,2012-04-26,2017-04-26"
parts := strings.Split(line, ",")
// Ignore any malformed lines.
if len(parts) < 3 {
continue
}
series := parts[2]
if series == "precise" {
preciseOrLaterFound = true
}
if series != "precise" && !preciseOrLaterFound {
continue
}
// the numeric version may contain a LTS moniker so strip that out.
seriesInfo := strings.Split(parts[0], " ")
seriesVersions[series] = seriesInfo[0]
ubuntuSeries[series] = seriesInfo[0]
if len(seriesInfo) == 2 && seriesInfo[1] == "LTS" {
ubuntuLts[series] = true
}
}
return nil
}