From d959a9472bd73172e800f6c54a14a71abea65f48 Mon Sep 17 00:00:00 2001 From: Akash Singh Date: Wed, 19 Jun 2024 19:02:24 +0530 Subject: [PATCH] Fixes #652 Updated probeRPMStatus to check if binaries are present Signed-off-by: Akash Singh --- pkg/pkgmgr/rpm.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/pkgmgr/rpm.go b/pkg/pkgmgr/rpm.go index 7a18cab88..9460ae4bb 100644 --- a/pkg/pkgmgr/rpm.go +++ b/pkg/pkgmgr/rpm.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "os/exec" "path/filepath" "sort" "strings" @@ -35,6 +36,7 @@ const ( rpmManifestWildcard = "container-manifest-*" installToolsCmd = "tdnf install busybox cpio dnf-utils -y" + isBinariesCmd = "sh -c 'ls /usr/sbin/busybox /usr/bin/rpm /usr/bin/yum /usr/bin/dnf /usr/bin/microdnf'" resultQueryFormat = "%{NAME}\t%{VERSION}-%{RELEASE}\t%{ARCH}\n" ) @@ -247,14 +249,31 @@ func (rm *rpmManager) probeRPMStatus(ctx context.Context, toolImage string) erro llb.ResolveModeDefault, ) - toolsInstalled := toolingBase.Run(llb.Shlex(installToolsCmd), llb.WithProxy(utils.GetProxy())).Root() - toolsApplied := rm.config.ImageState.File(llb.Copy(toolsInstalled, "/usr/sbin/busybox", "/usr/sbin/busybox")) + toolList := []string{"dnf", "microdnf", "rpm", "yum"} + missingTools := make([]string, 0) + + // Check if required tools are already present in the image + for _, tool := range toolList { + toolPath, err := exec.LookPath(tool) + if err != nil || toolPath == "" { + missingTools = append(missingTools, tool) + } + } + + var toolsApplied llb.State + if len(missingTools) > 0 { + log.Info("Required tools are missing, proceeding with tool installation...") + toolsInstalled := toolingBase.Run(llb.Shlex(installToolsCmd), llb.WithProxy(utils.GetProxy())).Root() + toolsApplied = rm.config.ImageState.File(llb.Copy(toolsInstalled, "/usr/sbin/busybox", "/usr/sbin/busybox")) + } else { + log.Info("All required tools are present, skipping tool installation.") + toolsApplied = rm.config.ImageState + } + mkFolders := toolsApplied. File(llb.Mkdir(resultsPath, 0o744, llb.WithParents(true))). File(llb.Mkdir(inputPath, 0o744, llb.WithParents(true))) - toolList := []string{"dnf", "microdnf", "rpm", "yum"} - rpmDBList := []string{ filepath.Join(rpmLibPath, rpmBDB), filepath.Join(rpmLibPath, rpmNDB), @@ -353,6 +372,7 @@ func (rm *rpmManager) probeRPMStatus(ctx context.Context, toolImage string) erro return nil } + func parseManifestFile(file string) (map[string]string, error) { // split into lines file = strings.TrimSuffix(file, "\n")