-
Notifications
You must be signed in to change notification settings - Fork 3
/
yum.go
176 lines (157 loc) · 4.18 KB
/
yum.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
package main
import (
"bytes"
"encoding/base64"
"errors"
"github.com/sysward/sysward-agent/logging"
"os"
"os/exec"
"strings"
)
type CentosPackageManager struct {
ForceYum bool
}
func (pm CentosPackageManager) UpdatePackage(pkg string) error {
out, err := runner.Run("yum",
"update",
"-y", pkg)
if os.Getenv("DEBUG") == "true" {
debugMsg := strings.Join([]string{"yum",
"update",
"-y", pkg}, " ")
logging.LogMsg("Command: " + debugMsg)
}
logging.LogMsg(string(out))
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm CentosPackageManager) HoldPackage(pkg string) error {
out, err := runner.Run("yum", "versionlock", pkg)
logging.LogMsg(string(out))
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm CentosPackageManager) UnholdPackage(pkg string) error {
out, err := runner.Run("yum", "versionlock", "delete", pkg)
logging.LogMsg(string(out))
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm CentosPackageManager) GetSourcesList() []Source {
out, _ := runner.Run("grep", "-h", "^deb", "/etc/apt/sources.list", "/etc/apt/sources.list.d/*")
out_arr := strings.Split(strings.TrimSpace(string(out)), "\n")
sources := make([]Source, len(out_arr))
for index, o := range out_arr {
x := strings.Split(o, " ")
src := false
if x[0] == "deb-src" {
src = true
}
sources[index] = Source{x[1], src, x[2:]}
}
return sources
}
func (pm CentosPackageManager) GetChangelog(package_name string) string {
changelog, _ := runner.Run("yum", "changelog", package_name)
changelog_encoded := base64.StdEncoding.EncodeToString([]byte(changelog))
return changelog_encoded
}
func (pm CentosPackageManager) BuildInstalledPackageList() []string {
installed, _ := runner.Run("rpm", "-qa", "--queryformat", "%{name}\t%{version}\n")
installed_arr := strings.Split(string(installed), "\n")[1:]
packages := []string{}
for _, line := range installed_arr {
x := strings.Fields(line)
if len(x) == 0 {
continue
}
if x[0] == "" {
continue
}
pkg_name := x[0]
packages = append(packages, strings.TrimSpace(pkg_name))
}
return packages
}
func (pm CentosPackageManager) BuildPackageList() []OsPackage {
// build list of security updates first
pkgManager := "dnf"
if pm.ForceYum {
pkgManager = "yum"
}
cmd := exec.Command(pkgManager, "list", "updates", "--security")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
panic(err)
}
// Parse the command output to extract update information
security := map[string]struct{}{}
lines := strings.Split(out.String(), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
security[fields[0]] = struct{}{}
}
cmd = exec.Command(pkgManager, "list", "updates")
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
panic(err)
}
// Parse the command output to extract update information
var updates []OsPackage
lines = strings.Split(out.String(), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
// Use rpm command to get the installed version
rpmCmd := exec.Command("rpm", "-q", fields[0])
rpmOut, err := rpmCmd.Output()
if err != nil {
continue // Skip if we can't get the installed version
}
installedVersion := strings.TrimSpace(string(rpmOut))
_, isSecurity := security[fields[0]]
// Create an Update struct and append it to the list
updates = append(updates, OsPackage{
Name: fields[0],
Current_version: installedVersion,
Candidate_version: fields[1],
// The Priority and Section information are not available directly from dnf list updates command
// If they are available from another source, you will need to adjust this part
Priority: "N/A",
Section: fields[2],
Security: isSecurity,
})
}
return updates
}
func (pm CentosPackageManager) UpdatePackageLists() error {
// NOOP
return nil
}
func (pm CentosPackageManager) UpdateCounts() Updates {
packages := pm.BuildPackageList()
security := 0
regular := 0
for _, p := range packages {
if p.Security {
security++
} else {
regular++
}
}
return Updates{regular, security}
}