/
list.go
405 lines (355 loc) · 11.5 KB
/
list.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
// Copyright (c) 2021 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
// Package repo defines software repository functions like listing, removing
// packages from software repository.
package repo
import (
"flag"
logutil "github.com/VeritasOS/plugin-manager/utils/log"
"github.com/VeritasOS/plugin-manager/utils/output"
"github.com/VeritasOS/software-update-manager/utils/rpm"
"github.com/VeritasOS/software-update-manager/validate/version"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"gopkg.in/yaml.v2"
)
// FormatVersionName is the RPM format version string that's embedded
// into RPM used for identifying the JSON format version.
const FormatVersionName = "RPM Format Version"
// RPMInfo is the list of RPM package info
type RPMInfo interface {
GetMatchedVersion() string
GetRPMName() string
GetRPMRelease() string
GetRPMType() string
GetRPMVersion() string
}
// Version 2 RPM Information related fields & helper functions below:
// v2productVersion is the details for a given product-version from the
// version compatibility matrix/info JSON.
type v2productVersion struct {
Install struct {
ConfirmationMessage []string `yaml:"confirmation-message"`
EstimatedMinutes uint `yaml:"estimated-minutes"`
RequiresRestart bool `yaml:"requires-restart"`
SupportsRollback bool `yaml:"supports-rollback"`
} `yaml:",omitempty"`
Rollback struct {
ConfirmationMessage []string `yaml:"confirmation-message"`
EstimatedMinutes uint `yaml:"estimated-minutes"`
RequiresRestart bool `yaml:"requires-restart"`
} `yaml:",omitempty"`
Commit struct {
ConfirmationMessage []string `yaml:"confirmation-message"`
EstimatedMinutes uint `yaml:"estimated-minutes"`
} `yaml:",omitempty"`
}
// v2RPMInfo is the list of RPM package info
type v2RPMInfo struct {
// Name of the RPM
Name string
// RPM file name
FileName string
Description []string
Type string
URL string
Version string
Release string
// NOTE: The time.Time value is getting chopped off while dumping output
// in json at ansible layer causing json unmarshal failure at consumer.
// so commenting for now.
// BuildDate time.Time
matchedVersion string
v2productVersion `yaml:",inline"`
}
// GetRPMName returns the name of the RPM.
func (v2 v2RPMInfo) GetRPMName() string {
return v2.Name
}
// GetRPMRelease returns the release number of the RPM.
func (v2 v2RPMInfo) GetRPMRelease() string {
return v2.Release
}
// GetRPMType returns the type of the RPM.
func (v2 v2RPMInfo) GetRPMType() string {
return v2.Type
}
// GetRPMVersion returns the release number of the RPM.
func (v2 v2RPMInfo) GetRPMVersion() string {
return v2.Version
}
// GetMatchedVersion retrives the supported product-version from
// version compatibility matrix.
func (v2 v2RPMInfo) GetMatchedVersion() string {
return v2.matchedVersion
}
// Version 1 RPM Information related fields & helper functions below:
// v1RPMInfo is the list of RPM package info
type v1RPMInfo struct {
Description string `yaml:"description"`
Estimate struct {
Hours string `yaml:"hours"`
Minutes string `yaml:"minutes"`
Seconds string `yaml:"seconds"`
} `yaml:"estimate"`
// RPM file name
FileName string `yaml:"filename"`
Name string `yaml:"name"`
matchedVersion string
Reboot string `yaml:"reboot"`
Summary string `yaml:"summary"`
Type string `yaml:"type"`
URL string `yaml:"url"`
Version string `yaml:"version"`
}
// GetRPMName returns the name of the RPM.
func (v1 v1RPMInfo) GetRPMName() string {
return v1.Name
}
// GetRPMRelease returns the release number of the RPM.
func (v1 v1RPMInfo) GetRPMRelease() string {
// v1 doesn't support displaying release number, so return empty.
return ""
}
// GetRPMType returns the type of the RPM.
func (v1 v1RPMInfo) GetRPMType() string {
return v1.Type
}
// GetRPMVersion returns the release number of the RPM.
func (v1 v1RPMInfo) GetRPMVersion() string {
return v1.Version
}
// GetMatchedVersion retrives the supported product-version from
// version compatibility matrix.
func (v1 v1RPMInfo) GetMatchedVersion() string {
return v1.matchedVersion
}
func parseDate(rawDate string) (time.Time, error) {
const dateLayout = "Mon 02 Jan 2006 03:04:05 PM MST"
t, err := time.Parse(dateLayout, rawDate)
if err != nil {
// INFO: On few systems, the build date appeared in
// ANSIC layout, so, try parsing with that.
t, err = time.Parse(time.ANSIC, rawDate)
}
if err != nil {
log.Printf("Failed to parse date: %s. Error: %s\n", rawDate, err)
}
return t, err
}
// List the packages present in the software repo along with their details.
func List(params map[string]string) ([]RPMInfo, error) {
log.Printf("Entering repo::List(%v)", params)
defer log.Println("Exiting repo::List")
productVersion := params["productVersion"]
var info []RPMInfo
files, err := listRepo(params)
if err != nil {
return info, err
}
info, err = ListRPMFilesInfo(files, productVersion)
if err != nil {
return info, err
}
output.Write(info)
return info, nil
}
func listRepo(params map[string]string) ([]string, error) {
log.Printf("Entering repo::listRepo(%v)", params)
defer log.Println("Exiting repo::listRepo")
swName := params["softwareName"]
swRepo := params["softwareRepo"]
swType := strings.ToLower(params["softwareType"])
var files []string
swTypes := []string{}
// If software type is not specified, then get the list of packages for
// all types.
if "" != swType {
swTypes = append(swTypes, swType)
} else {
if _, err := os.Stat(swRepo); os.IsNotExist(err) {
logutil.PrintNLogWarning("Software repository '%s' does not exist.",
swRepo)
return files, nil
}
dirs, err := ioutil.ReadDir(swRepo)
if err != nil {
log.Printf("ioutil.ReadDir(%s); Error: %s", swRepo, err.Error())
return files, logutil.PrintNLogError("Failed to get contents of software repository.")
}
for _, dir := range dirs {
curDir := filepath.FromSlash(swRepo + "/" + dir.Name())
fi, err := os.Stat(curDir)
if err != nil {
log.Printf("Unable to stat on %s directory. Error: %s\n",
dir, err.Error())
continue
}
if !fi.IsDir() {
log.Printf("%s is not a directory.\n", curDir)
continue
}
swTypes = append(swTypes, dir.Name())
}
}
for _, dir := range swTypes {
curDir := filepath.Clean(filepath.FromSlash(swRepo +
string(os.PathSeparator) + dir))
tfiles, err := ioutil.ReadDir(curDir)
if err != nil {
log.Printf("Unable to read contents of %s directory. Error: %s\n",
curDir, err.Error())
}
log.Printf("%s files: %v", dir, tfiles)
for _, tf := range tfiles {
log.Printf("Package: %v", tf)
matched, err := regexp.MatchString("[.]rpm$", tf.Name())
if err != nil {
log.Printf("regexp.MatchString(%s, %s); Error: %s",
"[.]rpm", tf.Name(), err.Error())
continue
}
// If not an RPM file, skip
if !matched {
continue
}
if "" == swName || tf.Name() == swName {
files = append(files, filepath.FromSlash(curDir+
string(os.PathSeparator)+tf.Name()))
}
}
}
return files, nil
}
// ListRPMFilesInfo lists the info of the RPM files.
func ListRPMFilesInfo(files []string, productVersion string) ([]RPMInfo, error) {
log.Printf("Entering repo::ListRPMFilesInfo(%v, %v)", files, productVersion)
defer log.Println("Exiting repo::ListRPMFilesInfo")
var info []RPMInfo
for _, file := range files {
metaData, err := rpm.GetRPMPackageInfo(filepath.FromSlash(file))
if err != nil {
return info, logutil.PrintNLogError("Failed to get software details.")
}
parsedData := rpm.ParseMetaData(string(metaData))
if _, ok := parsedData[FormatVersionName]; !ok {
listData := v1RPMInfo{
Description: parsedData["Description"],
FileName: filepath.Base(file),
Name: filepath.Base(file),
Summary: parsedData["Summary"],
Type: parsedData["Type"],
URL: parsedData["URL"],
Version: parsedData["Version"],
Reboot: "n/a",
}
listData.Estimate.Hours = "0"
listData.Estimate.Minutes = "0"
listData.Estimate.Seconds = "0"
if "" != productVersion {
versionInfo, err := version.GetCompatibileVersionInfo(productVersion, parsedData["VersionInfo"])
//In case of error, i.e the version of rpm and product version is not compatible we ignore error and contiune execution
//This error is expected when the rpm is already applied. List API should still return rpm details
if err != nil {
log.Printf("Error in GetCompatibileVersionInfo::(%s)...",
err)
}
listData.matchedVersion = versionInfo.Version
listData.Reboot = versionInfo.Reboot
listData.Estimate.Hours = versionInfo.Estimate.Hours
listData.Estimate.Minutes = versionInfo.Estimate.Minutes
listData.Estimate.Seconds = versionInfo.Estimate.Seconds
}
info = append(info, listData)
} else {
log.Printf("%s: %v", FormatVersionName, parsedData[FormatVersionName])
listData := v2RPMInfo{
FileName: filepath.Base(file),
Name: parsedData["Name"],
Version: parsedData["Version"],
Release: parsedData["Release"],
URL: parsedData["URL"],
}
rpmInfo := parsedData["RPM Info"]
err := yaml.Unmarshal([]byte(rpmInfo), &listData)
if err != nil {
log.Printf("yaml.Unmarshal(%s, %+v); Error: %s",
rpmInfo, &listData, err.Error())
}
t, err := parseDate(parsedData["Build Date"])
if err != nil {
log.Printf("Build date: %+v\n", t)
// listData.BuildDate = t
}
if "" != productVersion {
allVersionsInfo := struct {
VersionInfo []struct {
Version string `yaml:"product-version"`
v2productVersion `yaml:",inline"`
} `yaml:"compatibility-info"`
}{}
err := yaml.Unmarshal([]byte(rpmInfo), &allVersionsInfo)
if err != nil {
log.Printf("yaml.Unmarshal(%s, %+v); Error: %s",
rpmInfo, &allVersionsInfo, err.Error())
}
// INFO: First check Version as-is,
// if there is no match, then do pattern comparison.
for _, vInfo := range allVersionsInfo.VersionInfo {
if productVersion == vInfo.Version {
listData.v2productVersion = vInfo.v2productVersion
listData.matchedVersion = vInfo.Version
}
}
if listData.matchedVersion == "" {
for _, vInfo := range allVersionsInfo.VersionInfo {
if version.Compare(productVersion, vInfo.Version) {
listData.v2productVersion = vInfo.v2productVersion
listData.matchedVersion = vInfo.Version
}
}
}
}
info = append(info, listData)
}
}
return info, nil
}
// registerCommandList registers the list command that enables one to
// view the RPMs present in the software update repository.
func registerCommandList(progname string) {
log.Printf("Entering repo::registerCommandList(%s)", progname)
defer log.Println("Exiting repo::registerCommandList")
cmdOptions.listCmd = flag.NewFlagSet(progname+" list", flag.PanicOnError)
cmdOptions.listCmd.StringVar(
&cmdOptions.productVersion,
"product-version",
"",
"Version that a software should be compatibile with."+
" (I.e., product-version)",
)
cmdOptions.listCmd.StringVar(
&cmdOptions.softwareName,
"filename",
"",
"File name of the software.",
)
cmdOptions.listCmd.StringVar(
&cmdOptions.softwareRepo,
"repo",
SoftwareRepoPath,
"Path of the software repository.",
)
cmdOptions.listCmd.StringVar(
&cmdOptions.softwareType,
"type",
"",
"Type of the software.",
)
output.RegisterCommandOptions(cmdOptions.listCmd,
map[string]string{"output-format": "yaml"})
}