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 <[email protected]>
  • Loading branch information
COLDTURNIP authored and innobead committed Dec 3, 2024
1 parent 97980fe commit dcf886e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/local/preflight/packagemanager/apt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package packagemanager

import (
"strings"
"time"

commonns "github.com/longhorn/go-common-libs/ns"
Expand Down Expand Up @@ -64,6 +65,20 @@ 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) {
// Check man 1 dpkg-query for status flags.
// example for an installed package:
// $ dpkg-query -f='${binary:Package} ${db:Status-Abbrev}' -W nfs-common
// nfs-common ii
output, err = c.executor.Execute([]string{}, "dpkg-query", []string{"-f=${binary:Package} ${db:Status-Abbrev}", "-W", name}, commontypes.ExecuteNoTimeout)
if err != nil {
return
}
fields := strings.Fields(strings.TrimSpace(output))
if len(fields) == 2 {
if fields[0] == name && fields[1] == "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 dcf886e

Please sign in to comment.