diff --git a/pkg/local/preflight/packagemanager/apt.go b/pkg/local/preflight/packagemanager/apt.go index bdcbd305..b537e0f9 100644 --- a/pkg/local/preflight/packagemanager/apt.go +++ b/pkg/local/preflight/packagemanager/apt.go @@ -1,6 +1,8 @@ package packagemanager import ( + "bufio" + "strings" "time" commonns "github.com/longhorn/go-common-libs/ns" @@ -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 } diff --git a/pkg/local/preflight/packagemanager/packagemanager.go b/pkg/local/preflight/packagemanager/packagemanager.go index 5c21c9c1..6df4f54d 100644 --- a/pkg/local/preflight/packagemanager/packagemanager.go +++ b/pkg/local/preflight/packagemanager/packagemanager.go @@ -1,6 +1,7 @@ package packagemanager import ( + "errors" "fmt" "time" @@ -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)