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

Remove isolated environment from cpi and compiler cmd #673

Merged
merged 17 commits into from
Jan 31, 2025
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
15 changes: 2 additions & 13 deletions cloud/cpi_cmd_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"

bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
Expand Down Expand Up @@ -100,26 +98,17 @@ func (r *cpiCmdRunner) Run(context CmdContext, method string, apiVersion int, ar
if err != nil {
return CmdOutput{}, bosherr.WrapErrorf(err, "Marshalling external CPI command input %#v", cmdInput)
}
useIsolatedEnv := true
value, present := os.LookupEnv("BOSH_CPI_USE_ISOLATED_ENV")
if present {
useIsolatedEnv, err = strconv.ParseBool(value)
if err != nil {
return CmdOutput{}, bosherr.WrapErrorf(err, "Parsing $BOSH_CPI_USE_ISOLATED_ENV error, could not parse value: %v", value)
}
}

cmdPath := r.cpi.ExecutablePath()
cmd := boshsys.Command{
Name: cmdPath,
Env: map[string]string{
"BOSH_PACKAGES_DIR": r.cpi.PackagesDir,
"BOSH_JOBS_DIR": r.cpi.JobsDir,
"PATH": "/usr/local/bin:/usr/bin:/bin:/sbin",
},
UseIsolatedEnv: useIsolatedEnv,
Stdin: bytes.NewReader(inputBytes),
Stdin: bytes.NewReader(inputBytes),
}

stdout, stderr, exitCode, err := r.cmdRunner.RunComplexCommand(cmd)
r.logger.Debug(r.logTag, "Exit Code %d when executing external CPI command '%s'\nSTDIN: '%s'\nSTDOUT: '%s'\nSTDERR: '%s'", exitCode, cmdPath, string(inputBytes), stdout, stderr)
if err != nil {
Expand Down
40 changes: 0 additions & 40 deletions cloud/cpi_cmd_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"io"
"os"

boshlog "github.com/cloudfoundry/bosh-utils/logger"
fakesys "github.com/cloudfoundry/bosh-utils/system/fakes"
Expand Down Expand Up @@ -63,9 +62,7 @@ var _ = Describe("CpiCmdRunner", func() {
Expect(actualCmd.Env).To(Equal(map[string]string{
"BOSH_PACKAGES_DIR": cpi.PackagesDir,
"BOSH_JOBS_DIR": cpi.JobsDir,
"PATH": "/usr/local/bin:/usr/bin:/bin:/sbin",
}))
Expect(actualCmd.UseIsolatedEnv).To(BeTrue())
bytes, err := io.ReadAll(actualCmd.Stdin)
Expect(err).NotTo(HaveOccurred())
Expect(string(bytes)).To(Equal(
Expand All @@ -89,41 +86,6 @@ var _ = Describe("CpiCmdRunner", func() {
apiVersion = 2
})

AfterEach(func() {
os.Unsetenv("BOSH_CPI_USE_ISOLATED_ENV")
})
It("creates correct command with UseIsolatedEnv false if BOSH_CPI_USE_ISOLATED_ENV is set", func() {
os.Setenv("BOSH_CPI_USE_ISOLATED_ENV", "false")
cmdOutput := CmdOutput{}
outputBytes, err := json.Marshal(cmdOutput)
Expect(err).NotTo(HaveOccurred())

result := fakesys.FakeCmdResult{
Stdout: string(outputBytes),
ExitStatus: 0,
}
cmdRunner.AddCmdResult("/jobs/cpi/bin/cpi", result)
_, err = cpiCmdRunner.Run(context, "fake-method", apiVersion, "fake-argument-1", "fake-argument-2")
Expect(err).NotTo(HaveOccurred())
actualCmd := cmdRunner.RunComplexCommands[0]
Expect(actualCmd.UseIsolatedEnv).To(BeFalse())

})
It("throws helpful error if the value of BOSH_CPI_USE_ISOLATED_ENV cannot be parsed into a bool", func() {
os.Setenv("BOSH_CPI_USE_ISOLATED_ENV", "falasdse")
cmdOutput := CmdOutput{}
outputBytes, err := json.Marshal(cmdOutput)
Expect(err).NotTo(HaveOccurred())

result := fakesys.FakeCmdResult{
Stdout: string(outputBytes),
ExitStatus: 0,
}
cmdRunner.AddCmdResult("/jobs/cpi/bin/cpi", result)
_, err = cpiCmdRunner.Run(context, "fake-method", apiVersion, "fake-argument-1", "fake-argument-2")
Expect(err).To(HaveOccurred())
Expect(MatchRegexp("BOSH_CPI_USE_ISOLATED_ENV cannot be parsed", err))
})
It("creates correct command with stemcell api_version in context", func() {
cmdOutput := CmdOutput{}
outputBytes, err := json.Marshal(cmdOutput)
Expand All @@ -145,9 +107,7 @@ var _ = Describe("CpiCmdRunner", func() {
Expect(actualCmd.Env).To(Equal(map[string]string{
"BOSH_PACKAGES_DIR": cpi.PackagesDir,
"BOSH_JOBS_DIR": cpi.JobsDir,
"PATH": "/usr/local/bin:/usr/bin:/bin:/sbin",
}))
Expect(actualCmd.UseIsolatedEnv).To(BeTrue())
bytes, err := io.ReadAll(actualCmd.Stdin)
Expect(err).NotTo(HaveOccurred())
Expect(string(bytes)).To(Equal(
Expand Down
5 changes: 1 addition & 4 deletions installation/pkg/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,8 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR
"BOSH_INSTALL_TARGET": installDir,
"BOSH_PACKAGE_NAME": pkg.Name(),
"BOSH_PACKAGES_DIR": c.packagesDir,
"PATH": os.Getenv("PATH"),
"LD_LIBRARY_PATH": os.Getenv("LD_LIBRARY_PATH"),
},
UseIsolatedEnv: true,
WorkingDir: packageSrcDir,
WorkingDir: packageSrcDir,
}

_, _, _, err = c.runner.RunComplexCommand(cmd)
Expand Down
6 changes: 1 addition & 5 deletions installation/pkg/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pkg_test

import (
"errors"
"os"
"path/filepath"

fakeblobstore "github.com/cloudfoundry/bosh-utils/blobstore/fakes"
Expand Down Expand Up @@ -175,11 +174,8 @@ var _ = Describe("PackageCompiler", func() {
"BOSH_INSTALL_TARGET": installPath,
"BOSH_PACKAGE_NAME": "pkg1-name",
"BOSH_PACKAGES_DIR": packagesDir,
"PATH": os.Getenv("PATH"),
"LD_LIBRARY_PATH": os.Getenv("LD_LIBRARY_PATH"),
},
UseIsolatedEnv: true,
WorkingDir: "/pkg-dir",
WorkingDir: "/pkg-dir",
}

Expect(runner.RunComplexCommands).To(HaveLen(1))
Expand Down