Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preflight): check APT package installation by status flag #132

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading