-
Notifications
You must be signed in to change notification settings - Fork 17
/
distroinfo.go
191 lines (163 loc) · 4.27 KB
/
distroinfo.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
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package series
import (
"encoding/csv"
"os"
"strings"
"sync"
"time"
"github.com/juju/errors"
)
// UbuntuDistroInfo references a csv that contains all the distro information
// about info. This includes what the names and versions of a distro and if the
// distro is supported or not.
var UbuntuDistroInfo = "/usr/share/distro-info/ubuntu.csv"
const dateFormat = "2006-01-02"
// FileSystem defines a interface for interacting with the host os.
type FileSystem interface {
Open(string) (*os.File, error)
Exists(string) bool
}
// DistroInfoSerie holds the information about each distro.
type DistroInfoSerie struct {
Version string
CodeName string
Series string
Created time.Time
Released time.Time
EOL time.Time
}
// Supported returns true if the underlying series is supported or not.
// It expects the time to be in UTC.
func (d *DistroInfoSerie) Supported(now time.Time) bool {
return now.After(d.Released.UTC()) && now.Before(d.EOL.UTC())
}
// LTS returns true if the series is an LTS or not.
func (d *DistroInfoSerie) LTS() bool {
return strings.HasSuffix(d.Version, "LTS")
}
// DistroInfo holds records of which distro is supported or not.
// Refreshing will cause the distro to go out and fetch new information from
// the local file system to update itself.
type DistroInfo struct {
mutex sync.RWMutex
path string
info map[string]DistroInfoSerie
fileSystem FileSystem
}
// NewDistroInfo creates a new DistroInfo for querying the distro.
func NewDistroInfo(path string) *DistroInfo {
return &DistroInfo{
path: path,
info: make(map[string]DistroInfoSerie),
fileSystem: defaultFileSystem{},
}
}
// Refresh will attempt to update the information it has about each distro and
// if the distro is supported or not.
func (d *DistroInfo) Refresh() error {
// On non-Ubuntu systems this file won't exist but that's expected.
if !d.fileSystem.Exists(d.path) {
return nil
}
f, err := d.fileSystem.Open(d.path)
if err != nil {
return errors.Trace(err)
}
defer func() {
_ = f.Close()
}()
csvReader := csv.NewReader(f)
csvReader.FieldsPerRecord = -1
records, err := csvReader.ReadAll()
if err != nil {
return errors.Annotatef(err, "reading %s", d.path)
}
fieldNames := records[0]
records = records[1:]
result := make(map[string]DistroInfoSerie)
// We ignore all series prior to precise.
var foundPrecise bool
for _, fields := range records {
record, ok := consumeRecord(fieldNames, fields)
if !ok {
continue
}
createdDate, err := time.Parse(dateFormat, record.Created)
if err != nil {
continue
}
releasedDate, err := time.Parse(dateFormat, record.Released)
if err != nil {
continue
}
eolDate, err := time.Parse(dateFormat, record.EOL)
if err != nil {
continue
}
if !foundPrecise {
if record.Series != "precise" {
continue
}
foundPrecise = true
}
result[record.Series] = DistroInfoSerie{
Version: record.Version,
CodeName: record.CodeName,
Series: record.Series,
Created: createdDate,
Released: releasedDate,
EOL: eolDate,
}
}
// Lock the distro info, as we're going to be updating it.
d.mutex.Lock()
d.info = result
d.mutex.Unlock()
return nil
}
// SeriesInfo returns the DistroInfoSerie for the series name.
func (d *DistroInfo) SeriesInfo(seriesName string) (DistroInfoSerie, bool) {
d.mutex.RLock()
defer d.mutex.RUnlock()
info, ok := d.info[seriesName]
return info, ok
}
// record defines a raw distro line that hasn't been parsed.
type record struct {
Version string
CodeName string
Series string
Created string
Released string
EOL string
}
func consumeRecord(headers []string, fields []string) (record, bool) {
var result record
var malformed bool
for i, field := range fields {
if i >= len(headers) {
break
}
if field == "" {
malformed = true
}
switch headers[i] {
case "version":
result.Version = field
case "codename":
result.CodeName = field
case "series":
result.Series = field
case "created":
result.Created = field
case "release":
result.Released = field
case "eol":
result.EOL = field
}
}
// If the record is malformed then the validity of the record is not ok.
return result, !malformed
}