This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
dnf.go
102 lines (88 loc) · 2.63 KB
/
dnf.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
package phases
import (
"fmt"
"strings"
"github.com/flanksource/konfigadm/pkg/types"
"github.com/flanksource/konfigadm/pkg/utils"
log "github.com/sirupsen/logrus"
)
type DnfPackageManager struct{}
func (p DnfPackageManager) Install(pkg ...string) types.Commands {
return types.NewCommand(fmt.Sprintf("dnf install -y %s", strings.Join(pkg, " ")))
}
func (p DnfPackageManager) Update() types.Commands {
return types.NewCommand("dnf makecache")
}
func (p DnfPackageManager) Uninstall(pkg ...string) types.Commands {
return types.NewCommand(fmt.Sprintf("dnf remove -y %s", strings.Join(pkg, " ")))
}
func (p DnfPackageManager) Mark(pkg ...string) types.Commands {
return types.Commands{}
}
func (p DnfPackageManager) CleanupCaches() types.Commands {
return types.Commands{}
}
func (p DnfPackageManager) GetInstalledVersion(pkg string) string {
pkg = strings.Split(pkg, "=")[0]
var version, release, arch string
stdout, ok := utils.SafeExec("dnf info " + pkg)
if !ok {
log.Debugf("Failed installation check for %s -> %s", pkg, stdout)
return ""
}
if !strings.Contains(stdout, "Installed Packages") {
log.Debugf("%s is in db, but is not installed", pkg)
return ""
}
for _, line := range strings.Split(stdout, "\n") {
if strings.HasPrefix(line, "Version") {
version = strings.Split(line, ": ")[1]
}
if strings.HasPrefix(line, "Release") {
release = fmt.Sprintf("-%s", strings.Split(line, ": ")[1])
}
if strings.HasPrefix(line, "Arch") {
arch = fmt.Sprintf(".%s", strings.Split(line, ": ")[1])
}
}
retval := fmt.Sprintf("%s%s%s", version, release, arch)
if retval == "" {
log.Debugf("Unable to find version info in " + stdout)
}
return retval
}
func (p DnfPackageManager) GetKernelPackageNames(version string) (string, string) {
read, ok := utils.SafeExec("rpm --eval %%{dist}")
if !ok {
return "", ""
}
dist := strings.TrimSuffix(read, "\n")
read, ok = utils.SafeExec("uname -p")
if !ok {
return "", ""
}
arch := strings.TrimSuffix(read, "\n")
kernelname := fmt.Sprintf("kernel-%s%s.%s", version, dist, arch)
headername := fmt.Sprintf("kernel-devel-%s%s.%s", version, dist, arch)
return kernelname, headername
}
func (p DnfPackageManager) AddRepo(url string, channel string, versionCodeName string, name string, gpgKey string, extraArgs map[string]string) types.Commands {
repo := fmt.Sprintf(
`[%s]
name=%s
baseurl=%s
enabled=1
`, name, name, url)
if gpgKey != "" {
repo += fmt.Sprintf(`gpgcheck=1
repo_gpgcheck=1
gpgkey=%s
`, gpgKey)
}
for k, v := range extraArgs {
repo += fmt.Sprintf("%s = %s\n", k, v)
}
return types.NewCommand(fmt.Sprintf(`cat <<EOF >/etc/yum.repos.d/%s.repo
%s
EOF`, name, repo))
}