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
/
apt.go
90 lines (73 loc) · 2.9 KB
/
apt.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
package phases
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/flanksource/konfigadm/pkg/types"
"github.com/flanksource/konfigadm/pkg/utils"
log "github.com/sirupsen/logrus"
)
type AptPackageManager struct {
}
func (p AptPackageManager) Install(pkg ...string) types.Commands {
return types.NewCommand("DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-downgrades --allow-change-held-packages --no-install-recommends " + strings.Join(utils.ReplaceAllInSlice(pkg, "==", "="), " "))
}
func (p AptPackageManager) Uninstall(pkg ...string) types.Commands {
return types.NewCommand("apt-get purge -y " + strings.Join(utils.SplitAllInSlice(pkg, "=", 0), " "))
}
func (p AptPackageManager) Mark(pkg ...string) types.Commands {
return types.NewCommand("apt-mark hold " + strings.Join(utils.SplitAllInSlice(pkg, "=", 0), " "))
}
func (p AptPackageManager) ListInstalled() string {
return "dpkg --get-selections"
}
func (p AptPackageManager) Update() types.Commands {
return types.NewCommand("apt-get update")
}
func (p AptPackageManager) GetInstalledVersion(pkg string) string {
pkg = strings.Split(pkg, "=")[0]
stdout, ok := utils.SafeExec("dpkg-query -W -f='${db:Status-Status}\t${Version}' " + pkg)
if !ok {
log.Debugf("Failed installation check for %s -> %s", pkg, stdout)
return ""
}
status := strings.Split(stdout, "\t")[0]
version := strings.Split(stdout, "\t")[1]
log.Tracef("package: %s, status: %s, version: %s", pkg, status, version)
if status != "installed" {
log.Debugf("%s is in db, but is not installed: %s", pkg, status)
return ""
}
return version
}
func (p AptPackageManager) AddRepo(uri string, channel string, versionCodeName string, name string, gpgKey string, extraArgs map[string]string) types.Commands {
cmds := &types.Commands{}
if channel == "" {
channel = "main"
}
cmds = cmds.AddDependency("(test \"$(ls /var/lib/apt/lists)\" = \"\" && apt-get update) || true")
if versionCodeName == "" {
cmds = cmds.AddDependency("which lsb_release 2>&1 > /dev/null || apt-get install -y lsb-release")
versionCodeName = "$(lsb_release -cs)"
}
if name == "" {
_uri, _ := url.Parse(uri)
name = filepath.Base(_uri.Path)
}
if strings.HasPrefix(uri, "https://") {
cmds = cmds.AddDependency("test -e /usr/share/doc/apt-transport-https || apt-get install -y apt-transport-https")
}
cmds = cmds.
AddDependency("which curl 2>&1 > /dev/null || apt-get install -y curl")
if gpgKey != "" {
cmds = cmds.
AddDependency("which gpg2 2>&1 > /dev/null || apt-get install -y gnupg")
cmds = cmds.Add(fmt.Sprintf("curl -skL \"%s\" | apt-key add -", gpgKey))
}
return *cmds.Add(fmt.Sprintf("echo deb [arch=amd64] %s %s %s > /etc/apt/sources.list.d/%s.list", uri, versionCodeName, channel, name))
}
func (p AptPackageManager) CleanupCaches() types.Commands {
set := types.Commands{}
return *set.Add("apt-get -y autoremove --purge", "apt-get -y clean", "apt-get -y autoclean")
}