Skip to content

Commit

Permalink
Replace shell env with calls to AddEnv
Browse files Browse the repository at this point in the history
Since these containers are discarded after the results are determined,
we can add to the environment without concern for the environment
variables persisting into the final container.

Signed-off-by: Peter Engelbert <[email protected]>
  • Loading branch information
pmengelbert committed Oct 18, 2023
1 parent 60ad47a commit f706029
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
5 changes: 0 additions & 5 deletions pkg/buildkit/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package buildkit
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -149,10 +148,6 @@ func WithFileBytes(s *llb.State, path string, contents []byte) llb.State {
return s.File(llb.Mkfile(path, 0o600, contents))

Check warning on line 148 in pkg/buildkit/buildkit.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildkit/buildkit.go#L147-L148

Added lines #L147 - L148 were not covered by tests
}

func Env(k, v string) string {
return fmt.Sprintf("%s=%s", k, v)
}

func SolveToLocal(ctx context.Context, c *client.Client, st *llb.State, outPath string) error {
def, err := st.Marshal(ctx)
if err != nil {
Expand Down
23 changes: 11 additions & 12 deletions pkg/pkgmgr/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,17 @@ func (dm *dpkgManager) probeDPKGStatus(ctx context.Context, toolImage string) er
busyBoxApplied := dm.config.ImageState.File(llb.Copy(busyBoxInstalled, "/bin/busybox", "/bin/busybox"))
mkFolders := busyBoxApplied.File(llb.Mkdir(resultsPath, 0o744, llb.WithParents(true)))

probed := mkFolders.
Run(llb.Args([]string{
`/bin/busybox`, `env`,
buildkit.Env("DPKG_STATUS_PATH", dpkgStatusPath),
buildkit.Env("RESULTS_PATH", resultsPath),
buildkit.Env("DPKG_STATUS_FOLDER", dpkgStatusFolder),
buildkit.Env("RESULT_STATUSD_PATH", filepath.Join(resultsPath, "status.d")),
buildkit.Env("DPKG_STATUS_IS_DIRECTORY", fmt.Sprintf("%d", DPKGStatusDirectory)),
buildkit.Env("DPKG_STATUS_IS_FILE", fmt.Sprintf("%d", DPKGStatusFile)),
buildkit.Env("DPKG_STATUS_IS_UNKNOWN", fmt.Sprintf("%d", DPKGStatusNone)),
buildkit.Env("STATUSD_OUTPUT_FILENAME", statusdOutputFilename),
`sh`, `-c`, `
probed := mkFolders.Run(
llb.AddEnv("DPKG_STATUS_PATH", dpkgStatusPath),
llb.AddEnv("RESULTS_PATH", resultsPath),
llb.AddEnv("DPKG_STATUS_FOLDER", dpkgStatusFolder),
llb.AddEnv("RESULT_STATUSD_PATH", filepath.Join(resultsPath, "status.d")),
llb.AddEnv("DPKG_STATUS_IS_DIRECTORY", fmt.Sprintf("%d", DPKGStatusDirectory)),
llb.AddEnv("DPKG_STATUS_IS_FILE", fmt.Sprintf("%d", DPKGStatusFile)),
llb.AddEnv("DPKG_STATUS_IS_UNKNOWN", fmt.Sprintf("%d", DPKGStatusNone)),
llb.AddEnv("STATUSD_OUTPUT_FILENAME", statusdOutputFilename),
llb.Args([]string{
`/bin/busybox`, `sh`, `-c`, `
status="$DPKG_STATUS_IS_UNKNOWN"
if [ -f "$DPKG_STATUS_PATH" ]; then
status="$DPKG_STATUS_IS_FILE"
Expand Down
46 changes: 23 additions & 23 deletions pkg/pkgmgr/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,29 @@ func (rm *rpmManager) probeRPMStatus(ctx context.Context, toolImage string) erro

probed := buildkit.WithArrayFile(&mkFolders, toolListPath, toolList)
probed = buildkit.WithArrayFile(&probed, dbListPath, rpmDBList)
probed = probed.Run(llb.Args([]string{
`/usr/sbin/busybox`, `env`,
buildkit.Env("TOOL_LIST_PATH", toolListPath),
buildkit.Env("DB_LIST_PATH", dbListPath),
buildkit.Env("RESULTS_PATH", resultsPath),
buildkit.Env("RPM_TOOLS_OUTPUT_FILENAME", rpmToolsFile),
buildkit.Env("RPM_DB_LIST_OUTPUT_FILENAME", rpmDBFile),
buildkit.Env("BUSYBOX", "/usr/sbin/busybox"),
`/usr/sbin/busybox`, `sh`, `-c`, `
while IFS= read -r tool; do
tool_path="$($BUSYBOX which "$tool")"
echo "${tool}:${tool_path:-notfound}" >> "${RESULTS_PATH}/${RPM_TOOLS_OUTPUT_FILENAME}"
done < "$TOOL_LIST_PATH"
while IFS= read -r db; do
echo "$db"
if [ -f "$db" ]; then
$BUSYBOX cp "$db" "$RESULTS_PATH"
echo "$db" >> "${RESULTS_PATH}/${RPM_DB_LIST_OUTPUT_FILENAME}"
fi
done < "$DB_LIST_PATH"
`,
})).Root()
probed = probed.Run(
llb.AddEnv("TOOL_LIST_PATH", toolListPath),
llb.AddEnv("DB_LIST_PATH", dbListPath),
llb.AddEnv("RESULTS_PATH", resultsPath),
llb.AddEnv("RPM_TOOLS_OUTPUT_FILENAME", rpmToolsFile),
llb.AddEnv("RPM_DB_LIST_OUTPUT_FILENAME", rpmDBFile),
llb.AddEnv("BUSYBOX", "/usr/sbin/busybox"),
llb.Args([]string{
`/usr/sbin/busybox`, `sh`, `-c`, `
while IFS= read -r tool; do
tool_path="$($BUSYBOX which "$tool")"
echo "${tool}:${tool_path:-notfound}" >> "${RESULTS_PATH}/${RPM_TOOLS_OUTPUT_FILENAME}"
done < "$TOOL_LIST_PATH"
while IFS= read -r db; do
echo "$db"
if [ -f "$db" ]; then
$BUSYBOX cp "$db" "$RESULTS_PATH"
echo "$db" >> "${RESULTS_PATH}/${RPM_DB_LIST_OUTPUT_FILENAME}"
fi
done < "$DB_LIST_PATH"
`,
})).Root()

Check warning on line 267 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L239-L267

Added lines #L239 - L267 were not covered by tests
outState := llb.Diff(toolsApplied, probed)

rpmDBListOutputBytes, err := buildkit.ExtractFileFromState(ctx, rm.config.Client, &outState, filepath.Join(resultsPath, rpmDBFile))
Expand Down

0 comments on commit f706029

Please sign in to comment.