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 committed Dec 3, 2024
1 parent cd47aee commit 513edfa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/local/preflight/packagemanager/apt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package packagemanager

import (
"strings"
"time"

commonns "github.com/longhorn/go-common-libs/ns"
commontypes "github.com/longhorn/go-common-libs/types"
)

const (
// Ref: man 1 dpkg-query

dpkgDesiredActionUnknown = "u"
dpkgDesiredActionInstall = "i"
dpkgDesiredActionHold = "h"
dpkgDesiredActionRemove = "r"
dpkgDesiredActionPurge = "p"

dpkgStatusNotInstalled = "n"
dpkgStatusConfigFiles = "c"
dpkgStatusHalfInstalled = "H"
dpkgStatusUnpackaged = "U"
dpkgStatusHalfConfigured = "F"
dpkgStatusTriggersAwaiting = "W"
dpkgStatusTriggersPending = "t"
dpkgStatusInstalled = "i"
)

type AptPackageManager struct {
executor *commonns.Executor
}
Expand Down Expand Up @@ -64,6 +84,19 @@ 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 -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] == dpkgDesiredActionInstall+dpkgStatusInstalled {
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 513edfa

Please sign in to comment.