github.com/flanksource/konfigadm@v0.12.0/pkg/phases/apt.go (about) 1 package phases 2 3 import ( 4 "fmt" 5 "net/url" 6 "path/filepath" 7 "strings" 8 9 "github.com/flanksource/konfigadm/pkg/types" 10 "github.com/flanksource/konfigadm/pkg/utils" 11 log "github.com/sirupsen/logrus" 12 ) 13 14 type AptPackageManager struct { 15 } 16 17 func (p AptPackageManager) Install(pkg ...string) types.Commands { 18 return types.NewCommand("DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-downgrades --allow-change-held-packages --no-install-recommends " + strings.Join(utils.ReplaceAllInSlice(pkg, "==", "="), " ")) 19 } 20 21 func (p AptPackageManager) Uninstall(pkg ...string) types.Commands { 22 return types.NewCommand("apt-get purge -y " + strings.Join(utils.SplitAllInSlice(pkg, "=", 0), " ")) 23 } 24 25 func (p AptPackageManager) Mark(pkg ...string) types.Commands { 26 return types.NewCommand("apt-mark hold " + strings.Join(utils.SplitAllInSlice(pkg, "=", 0), " ")) 27 } 28 29 func (p AptPackageManager) ListInstalled() string { 30 return "dpkg --get-selections" 31 } 32 33 func (p AptPackageManager) Update() types.Commands { 34 return types.NewCommand("apt-get update") 35 } 36 37 func (p AptPackageManager) GetInstalledVersion(pkg string) string { 38 pkg = strings.Split(pkg, "=")[0] 39 stdout, ok := utils.SafeExec("dpkg-query -W -f='${db:Status-Status}\t${Version}' " + pkg) 40 if !ok { 41 log.Debugf("Failed installation check for %s -> %s", pkg, stdout) 42 return "" 43 } 44 status := strings.Split(stdout, "\t")[0] 45 version := strings.Split(stdout, "\t")[1] 46 47 log.Tracef("package: %s, status: %s, version: %s", pkg, status, version) 48 if status != "installed" { 49 log.Debugf("%s is in db, but is not installed: %s", pkg, status) 50 return "" 51 } 52 return version 53 } 54 55 func (p AptPackageManager) GetKernelPackageNames(version string) (string, string) { 56 kernelname := fmt.Sprintf("linux-image-%s", version) 57 headername := fmt.Sprintf("linux-headers-%s", version) 58 return kernelname, headername 59 } 60 61 func (p AptPackageManager) AddRepo(uri string, channel string, versionCodeName string, name string, gpgKey string, extraArgs map[string]string) types.Commands { 62 cmds := &types.Commands{} 63 if channel == "" { 64 channel = "main" 65 } 66 67 cmds = cmds.AddDependency("(test \"$(ls /var/lib/apt/lists)\" = \"\" && apt-get update) || true") 68 69 if versionCodeName == "" { 70 cmds = cmds.AddDependency("which lsb_release 2>&1 > /dev/null || apt-get install -y lsb-release") 71 versionCodeName = "$(lsb_release -cs)" 72 } 73 if name == "" { 74 _uri, _ := url.Parse(uri) 75 name = filepath.Base(_uri.Path) 76 } 77 78 if strings.HasPrefix(uri, "https://") { 79 cmds = cmds.AddDependency("test -e /usr/share/doc/apt-transport-https || apt-get install -y apt-transport-https") 80 } 81 cmds = cmds. 82 AddDependency("which curl 2>&1 > /dev/null || apt-get install -y curl") 83 84 if gpgKey != "" { 85 cmds = cmds. 86 AddDependency("which gpg2 2>&1 > /dev/null || apt-get install -y gnupg") 87 cmds = cmds.Add(fmt.Sprintf("curl -skL \"%s\" | apt-key add -", gpgKey)) 88 } 89 90 return *cmds.Add(fmt.Sprintf("echo deb [arch=amd64] %s %s %s > /etc/apt/sources.list.d/%s.list", uri, versionCodeName, channel, name)) 91 } 92 93 func (p AptPackageManager) CleanupCaches() types.Commands { 94 set := types.Commands{} 95 return *set.Add("apt-get -y autoremove --purge", "apt-get -y clean", "apt-get -y autoclean") 96 }