Skip to content

Commit

Permalink
fix(preflight): check package install by status flag
Browse files Browse the repository at this point in the history
Signed-off-by: Raphanus Lo <yunchang.lo@suse.com>
  • Loading branch information
COLDTURNIP committed Dec 3, 2024
1 parent cd47aee commit 5b3b5c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 26 additions & 2 deletions pkg/local/preflight/packagemanager/apt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package packagemanager

import (
"bufio"
"strings"
"time"

commonns "github.com/longhorn/go-common-libs/ns"
Expand Down Expand Up @@ -64,6 +66,28 @@ func (c *AptPackageManager) GetServiceStatus(name string) (string, error) {
}

// CheckPackageInstalled checks if a package is installed
func (c *AptPackageManager) CheckPackageInstalled(name string) (string, error) {
return c.executor.Execute([]string{}, "dpkg-query", []string{"-l", name}, commontypes.ExecuteNoTimeout)
func (c *AptPackageManager) CheckPackageInstalled(name string) (output string, err error) {
// example for an installed package:
// $ dpkg-query -l nfs-common
// Desired=Unknown/Install/Remove/Purge/Hold
// | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
// |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
// ||/ Name Version Architecture Description
// +++-==============-================-============-=============================================
// ii nfs-common 1:2.6.4-4ubuntu1 amd64 NFS support files common to client and server
output, err = c.executor.Execute([]string{}, "dpkg-query", []string{"-l", name}, commontypes.ExecuteNoTimeout)
if err != nil {
return
}
scanner := bufio.NewScanner(strings.NewReader(output))
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 2 {
continue
}
if fields[1] == name && fields[0] == "ii" {
return output, nil
}
}
return output, packageNotInstalledError
}
3 changes: 3 additions & 0 deletions pkg/local/preflight/packagemanager/packagemanager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package packagemanager

import (
"errors"
"fmt"
"time"

Expand All @@ -19,6 +20,8 @@ const (
// PackageManagerQlist = PackageManagerType("qlist")
)

var packageNotInstalledError = errors.New("package not installed")

type PackageManager interface {
UpdatePackageList() (string, error)
InstallPackage(name string) (string, error)
Expand Down

0 comments on commit 5b3b5c2

Please sign in to comment.