From 8ece8d106799f67e180906df4b168ea6b39af0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Wed, 25 Sep 2024 13:46:19 +0200 Subject: [PATCH 01/12] =?UTF-8?q?Add=20flake=20to=20provide=20a=20developm?= =?UTF-8?q?ent-environment=20(a.k.a.=20=E2=80=9Cdev-shell=E2=80=9D);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .envrc | 6 ++++++ flake.lock | 27 +++++++++++++++++++++++++++ flake.nix | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..3c0fb5553 --- /dev/null +++ b/.envrc @@ -0,0 +1,6 @@ +strict_env + +if has nix +then + use flake +fi diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..ce639c580 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgsRepo": { + "locked": { + "lastModified": 1727129439, + "narHash": "sha256-nPyrcFm6FSk7CxzVW4x2hu62aLDghNcv9dX6DF3dXw8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "babc25a577c3310cce57c72d5bed70f4c3c3843a", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgsRepo": "nixpkgsRepo" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..daef0ac8c --- /dev/null +++ b/flake.nix @@ -0,0 +1,32 @@ +{ + description = "Flake for development on the bosh-cli featuring a nix-shell"; + + inputs = { + nixpkgsRepo.url = github:NixOS/nixpkgs/nixos-24.05; + }; + + outputs = { self, nixpkgsRepo }: + let + nixpkgsLib = nixpkgsRepo.lib; + supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. + forAllSystems = nixpkgsLib.genAttrs supportedSystems; + + # Nixpkgs instantiated for supported system types. + nixpkgsFor = forAllSystems (system: import nixpkgsRepo { inherit system; }); + in { + devShells = forAllSystems (system: + let + nixpkgs = nixpkgsFor.${system}; + in { + default = nixpkgs.mkShell { + buildInputs = with nixpkgs; [ + delve + go + gopls + ]; + }; + }); + }; +} From 405519b982451f0b8e44ccb5dd9650b7af6e7e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Wed, 25 Sep 2024 17:09:39 +0200 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Make=20the=20bina?= =?UTF-8?q?ry=20roughly=20work.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/cpi_cmd_runner.go | 4 +++- installation/pkg/compiler.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cloud/cpi_cmd_runner.go b/cloud/cpi_cmd_runner.go index a3fc0d074..18d5f69da 100644 --- a/cloud/cpi_cmd_runner.go +++ b/cloud/cpi_cmd_runner.go @@ -115,8 +115,10 @@ func (r *cpiCmdRunner) Run(context CmdContext, method string, apiVersion int, ar Env: map[string]string{ "BOSH_PACKAGES_DIR": r.cpi.PackagesDir, "BOSH_JOBS_DIR": r.cpi.JobsDir, - "PATH": "/usr/local/bin:/usr/bin:/bin:/sbin", + "PATH": os.Getenv("PATH"), }, + // 🚧 To-do: Make this configurable via cli-flag or use everywhere the environment-variable + // “BOSH_CPI_USE_ISOLATED_ENV” as in cpi_cmd_runner.go, see . UseIsolatedEnv: useIsolatedEnv, Stdin: bytes.NewReader(inputBytes), } diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index 4867ffcbf..f4df44139 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -103,7 +103,9 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR "PATH": os.Getenv("PATH"), "LD_LIBRARY_PATH": os.Getenv("LD_LIBRARY_PATH"), }, - UseIsolatedEnv: true, + // 🚧 To-do: Make this configurable via cli-flag or use everywhere the environment-variable + // “BOSH_CPI_USE_ISOLATED_ENV” as in cpi_cmd_runner.go, see . + UseIsolatedEnv: false, WorkingDir: packageSrcDir, } From f2cc986425dba1dd42a1919303fb0c34d8fb310d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Wed, 25 Sep 2024 18:02:36 +0200 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=9A=A7=20Work=20in=20progress:=20Ad?= =?UTF-8?q?d=20and=20propagate=20new=20cli-option=20to=20use=20environment?= =?UTF-8?q?=20of=20host-system;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/opts/opts.go | 1 + installation/pkg/compiler.go | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/opts/opts.go b/cmd/opts/opts.go index b4eed1f4a..9ad835417 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -197,6 +197,7 @@ type CreateEnvOpts struct { StatePath string `long:"state" value-name:"PATH" description:"State file path"` Recreate bool `long:"recreate" description:"Recreate VM in deployment"` RecreatePersistentDisks bool `long:"recreate-persistent-disks" description:"Recreate persistent disks in the deployment"` + UseIsolatedEnv bool `long:"use-isolated-environment" description:"Compile and run cpi-commands in a clean environment"` cmd } diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index f4df44139..7eda129d0 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -24,6 +24,7 @@ type compiler struct { blobExtractor blobextract.Extractor logger boshlog.Logger logTag string + useIsolatedEnv bool } func NewPackageCompiler( @@ -35,6 +36,7 @@ func NewPackageCompiler( compiledPackageRepo bistatepkg.CompiledPackageRepo, blobExtractor blobextract.Extractor, logger boshlog.Logger, + useIsolatedEnv bool, ) bistatepkg.Compiler { return &compiler{ runner: runner, @@ -46,6 +48,7 @@ func NewPackageCompiler( blobExtractor: blobExtractor, logger: logger, logTag: "packageCompiler", + useIsolatedEnv: useIsolatedEnv, } } @@ -103,9 +106,7 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR "PATH": os.Getenv("PATH"), "LD_LIBRARY_PATH": os.Getenv("LD_LIBRARY_PATH"), }, - // 🚧 To-do: Make this configurable via cli-flag or use everywhere the environment-variable - // “BOSH_CPI_USE_ISOLATED_ENV” as in cpi_cmd_runner.go, see . - UseIsolatedEnv: false, + UseIsolatedEnv: c.useIsolatedEnv, WorkingDir: packageSrcDir, } From a0b97ee01f59d561e09bdf68b6ef7cb5d2a3b5ca Mon Sep 17 00:00:00 2001 From: Shilpa Chandrashekara Date: Thu, 26 Sep 2024 15:05:01 +0200 Subject: [PATCH 04/12] add golang to toolversion --- .tool-versions | 1 + 1 file changed, 1 insertion(+) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 000000000..4e7d6aac0 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.21.13 From 4aa40dc37ccf3da22aca2ee60a5ba0d6ebbbe84d Mon Sep 17 00:00:00 2001 From: Shilpa Chandrashekara Date: Thu, 26 Sep 2024 15:06:05 +0200 Subject: [PATCH 05/12] add UseIsolatedEnv cli option --- cmd/cmd.go | 8 ++++---- cmd/env_factory.go | 3 ++- installation/installer_factory.go | 6 ++++++ installation/pkg/compiler_test.go | 4 ++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 978391f71..c1f2e5b42 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -72,7 +72,7 @@ func (c Cmd) Execute() (cmdErr error) { case *CreateEnvOpts: envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentPreparer { - return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks).Preparer() + return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks, opts.UseIsolatedEnv).Preparer() } stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger) @@ -80,7 +80,7 @@ func (c Cmd) Execute() (cmdErr error) { case *DeleteEnvOpts: envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentDeleter { - return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).Deleter() + return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, true).Deleter() } stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger) @@ -88,7 +88,7 @@ func (c Cmd) Execute() (cmdErr error) { case *StopEnvOpts: envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentStateManager { - return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).StateManager() + return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, true).StateManager() } stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger) @@ -96,7 +96,7 @@ func (c Cmd) Execute() (cmdErr error) { case *StartEnvOpts: envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentStateManager { - return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).StateManager() + return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, true).StateManager() } stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger) diff --git a/cmd/env_factory.go b/cmd/env_factory.go index 105d2a83c..8a031eafd 100644 --- a/cmd/env_factory.go +++ b/cmd/env_factory.go @@ -71,6 +71,7 @@ func NewEnvFactory( manifestVars boshtpl.Variables, manifestOp patch.Op, recreatePersistentDisks bool, + useIsolatedEnv bool, ) *envFactory { f := envFactory{ deps: deps, @@ -116,7 +117,7 @@ func NewEnvFactory( { installerFactory := boshinst.NewInstallerFactory( deps.UI, deps.CmdRunner, deps.Compressor, releaseJobResolver, - deps.UUIDGen, deps.Logger, deps.FS, deps.DigestCreationAlgorithms) + deps.UUIDGen, deps.Logger, useIsolatedEnv, deps.FS, deps.DigestCreationAlgorithms) f.cpiInstaller = bicpirel.CpiInstaller{ ReleaseManager: f.releaseManager, diff --git a/installation/installer_factory.go b/installation/installer_factory.go index 50513e3bd..bc1482a96 100644 --- a/installation/installer_factory.go +++ b/installation/installer_factory.go @@ -29,6 +29,7 @@ type installerFactory struct { releaseJobResolver bideplrel.JobResolver uuidGenerator boshuuid.Generator logger boshlog.Logger + useIsolatedEnv bool logTag string fs boshsys.FileSystem digestCreateAlgorithms []boshcrypto.Algorithm @@ -41,6 +42,7 @@ func NewInstallerFactory( releaseJobResolver bideplrel.JobResolver, uuidGenerator boshuuid.Generator, logger boshlog.Logger, + useIsolatedEnv bool, fs boshsys.FileSystem, digestCreateAlgorithms []boshcrypto.Algorithm, ) InstallerFactory { @@ -51,6 +53,7 @@ func NewInstallerFactory( releaseJobResolver: releaseJobResolver, uuidGenerator: uuidGenerator, logger: logger, + useIsolatedEnv: useIsolatedEnv, logTag: "installer", fs: fs, digestCreateAlgorithms: digestCreateAlgorithms, @@ -62,6 +65,7 @@ func (f *installerFactory) NewInstaller(target Target) Installer { target: target, runner: f.runner, logger: f.logger, + useIsolatedEnv: f.useIsolatedEnv, extractor: f.extractor, uuidGenerator: f.uuidGenerator, releaseJobResolver: f.releaseJobResolver, @@ -84,6 +88,7 @@ type installerFactoryContext struct { fs boshsys.FileSystem runner boshsys.CmdRunner logger boshlog.Logger + useIsolatedEnv bool extractor boshcmd.Compressor uuidGenerator boshuuid.Generator releaseJobResolver bideplrel.JobResolver @@ -147,6 +152,7 @@ func (c *installerFactoryContext) InstallationStatePackageCompiler() bistatepkg. c.CompiledPackageRepo(), c.BlobExtractor(), c.logger, + c.useIsolatedEnv, ) return c.packageCompiler diff --git a/installation/pkg/compiler_test.go b/installation/pkg/compiler_test.go index feeb7addc..6d4b1cae7 100644 --- a/installation/pkg/compiler_test.go +++ b/installation/pkg/compiler_test.go @@ -36,6 +36,7 @@ var _ = Describe("PackageCompiler", func() { var ( logger boshlog.Logger + useIsolatedEnv bool compiler bistatepkg.Compiler runner *fakesys.FakeCmdRunner pkg *birelpkg.Package @@ -53,6 +54,8 @@ var _ = Describe("PackageCompiler", func() { BeforeEach(func() { logger = boshlog.NewLogger(boshlog.LevelNone) + useIsolatedEnv = true + packagesDir = "fake-packages-dir" runner = fakesys.NewFakeCmdRunner() fs = fakesys.NewFakeFileSystem() @@ -82,6 +85,7 @@ var _ = Describe("PackageCompiler", func() { mockCompiledPackageRepo, fakeExtractor, logger, + useIsolatedEnv, ) }) From 035c1f87aa8136d6f9e13584e6ff72ac6037aec5 Mon Sep 17 00:00:00 2001 From: Shilpa Chandrashekara Date: Thu, 26 Sep 2024 16:20:47 +0200 Subject: [PATCH 06/12] rename to avoidIsolatedEnv cli --- cmd/cmd.go | 2 +- cmd/opts/opts.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index c1f2e5b42..1d17a5a51 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -72,7 +72,7 @@ func (c Cmd) Execute() (cmdErr error) { case *CreateEnvOpts: envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentPreparer { - return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks, opts.UseIsolatedEnv).Preparer() + return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks, !opts.AvoidIsolatedEnv).Preparer() } stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger) diff --git a/cmd/opts/opts.go b/cmd/opts/opts.go index 9ad835417..011dfa872 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -197,7 +197,7 @@ type CreateEnvOpts struct { StatePath string `long:"state" value-name:"PATH" description:"State file path"` Recreate bool `long:"recreate" description:"Recreate VM in deployment"` RecreatePersistentDisks bool `long:"recreate-persistent-disks" description:"Recreate persistent disks in the deployment"` - UseIsolatedEnv bool `long:"use-isolated-environment" description:"Compile and run cpi-commands in a clean environment"` + AvoidIsolatedEnv bool `long:"avoid-isolated-environment" short:"I" description:"Compile and run cpi-commands in a clean environment or not"` cmd } From 93d81cb27006c5020037657fb61babdbc24b086d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Thu, 26 Sep 2024 17:23:11 +0200 Subject: [PATCH 07/12] Propagate useIsolatedEnv through to cpi_cmd_runner.go; --- cloud/cpi_cmd_runner.go | 24 +++++++++--------------- cloud/factory.go | 17 ++++++++++------- cmd/env_factory.go | 2 +- cmd/opts/opts.go | 2 +- installation/pkg/compiler.go | 3 +++ 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cloud/cpi_cmd_runner.go b/cloud/cpi_cmd_runner.go index 18d5f69da..1b1c70010 100644 --- a/cloud/cpi_cmd_runner.go +++ b/cloud/cpi_cmd_runner.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "os" - "strconv" bosherr "github.com/cloudfoundry/bosh-utils/errors" boshlog "github.com/cloudfoundry/bosh-utils/logger" @@ -68,21 +67,24 @@ type CPICmdRunner interface { type cpiCmdRunner struct { cmdRunner boshsys.CmdRunner - cpi CPI - logger boshlog.Logger - logTag string + cpi CPI + logger boshlog.Logger + logTag string + useIsolatedEnv bool } func NewCPICmdRunner( cmdRunner boshsys.CmdRunner, cpi CPI, logger boshlog.Logger, + useIsolatedEnv bool, ) CPICmdRunner { return &cpiCmdRunner{ cmdRunner: cmdRunner, cpi: cpi, logger: logger, logTag: "cpiCmdRunner", + useIsolatedEnv: useIsolatedEnv, } } @@ -100,14 +102,6 @@ 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{ @@ -117,11 +111,11 @@ func (r *cpiCmdRunner) Run(context CmdContext, method string, apiVersion int, ar "BOSH_JOBS_DIR": r.cpi.JobsDir, "PATH": os.Getenv("PATH"), }, - // 🚧 To-do: Make this configurable via cli-flag or use everywhere the environment-variable - // “BOSH_CPI_USE_ISOLATED_ENV” as in cpi_cmd_runner.go, see . - UseIsolatedEnv: useIsolatedEnv, + UseIsolatedEnv: r.useIsolatedEnv, Stdin: bytes.NewReader(inputBytes), } + fmt.Printf("cpi_cmd_runner.go: UseIsolatedEnv = %t\n", cmd.UseIsolatedEnv) // 🚧 To-do: Debug-code + 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 { diff --git a/cloud/factory.go b/cloud/factory.go index c01bf4e19..dbff36eb8 100644 --- a/cloud/factory.go +++ b/cloud/factory.go @@ -12,20 +12,23 @@ type Factory interface { } type factory struct { - fs boshsys.FileSystem - cmdRunner boshsys.CmdRunner - logger boshlog.Logger + fs boshsys.FileSystem + cmdRunner boshsys.CmdRunner + logger boshlog.Logger + useIsolatedEnv bool } func NewFactory( fs boshsys.FileSystem, cmdRunner boshsys.CmdRunner, logger boshlog.Logger, + useIsolatedEnv bool, ) Factory { return &factory{ - fs: fs, - cmdRunner: cmdRunner, - logger: logger, + fs: fs, + cmdRunner: cmdRunner, + logger: logger, + useIsolatedEnv: useIsolatedEnv, } } @@ -43,6 +46,6 @@ func (f *factory) NewCloud(installation biinstall.Installation, directorID strin return nil, bosherr.Errorf("Installed CPI job '%s' does not contain the required executable '%s'", cpiJob.Name, cmdPath) } - cpiCmdRunner := NewCPICmdRunner(f.cmdRunner, cpi, f.logger) + cpiCmdRunner := NewCPICmdRunner(f.cmdRunner, cpi, f.logger, f.useIsolatedEnv) return NewCloud(cpiCmdRunner, directorID, stemcellApiVersion, f.logger), nil } diff --git a/cmd/env_factory.go b/cmd/env_factory.go index 8a031eafd..8f93b2309 100644 --- a/cmd/env_factory.go +++ b/cmd/env_factory.go @@ -150,7 +150,7 @@ func NewEnvFactory( f.blobstoreFactory = biblobstore.NewBlobstoreFactory(deps.UUIDGen, deps.FS, deps.Logger) f.deploymentFactory = bidepl.NewFactory(10*time.Second, 500*time.Millisecond) f.agentClientFactory = bihttpagent.NewAgentClientFactory(1*time.Second, deps.Logger) - f.cloudFactory = bicloud.NewFactory(deps.FS, deps.CmdRunner, deps.Logger) + f.cloudFactory = bicloud.NewFactory(deps.FS, deps.CmdRunner, deps.Logger, useIsolatedEnv) } { diff --git a/cmd/opts/opts.go b/cmd/opts/opts.go index 011dfa872..27d2e028f 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -197,7 +197,7 @@ type CreateEnvOpts struct { StatePath string `long:"state" value-name:"PATH" description:"State file path"` Recreate bool `long:"recreate" description:"Recreate VM in deployment"` RecreatePersistentDisks bool `long:"recreate-persistent-disks" description:"Recreate persistent disks in the deployment"` - AvoidIsolatedEnv bool `long:"avoid-isolated-environment" short:"I" description:"Compile and run cpi-commands in a clean environment or not"` + AvoidIsolatedEnv bool `long:"avoid-isolated-environment" short:"I" description:"Compile and run cpi-commands in a clean environment or not"` cmd } diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index 7eda129d0..fce20ff19 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -1,6 +1,7 @@ package pkg import ( + "fmt" "os" "path/filepath" @@ -110,6 +111,8 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR WorkingDir: packageSrcDir, } + fmt.Printf("compiler.go: UseIsolatedEnv = %t\n", cmd.UseIsolatedEnv) // 🚧 To-do: Debug-code + _, _, _, err = c.runner.RunComplexCommand(cmd) if err != nil { return record, isCompiledPackage, bosherr.WrapError(err, "Compiling package") From aa9fc86d25e82ff2165c5c2f11dceffbb0bbc8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Thu, 26 Sep 2024 18:02:46 +0200 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=94=A7=F0=9F=A7=B9=20Fix=20tests=20?= =?UTF-8?q?and=20clean-up;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/cpi_cmd_runner.go | 11 +++++------ cloud/cpi_cmd_runner_test.go | 2 +- flake.nix | 1 + installation/pkg/compiler.go | 3 --- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/cloud/cpi_cmd_runner.go b/cloud/cpi_cmd_runner.go index 1b1c70010..8ad0680f5 100644 --- a/cloud/cpi_cmd_runner.go +++ b/cloud/cpi_cmd_runner.go @@ -66,7 +66,7 @@ type CPICmdRunner interface { } type cpiCmdRunner struct { - cmdRunner boshsys.CmdRunner + cmdRunner boshsys.CmdRunner cpi CPI logger boshlog.Logger logTag string @@ -80,10 +80,10 @@ func NewCPICmdRunner( useIsolatedEnv bool, ) CPICmdRunner { return &cpiCmdRunner{ - cmdRunner: cmdRunner, - cpi: cpi, - logger: logger, - logTag: "cpiCmdRunner", + cmdRunner: cmdRunner, + cpi: cpi, + logger: logger, + logTag: "cpiCmdRunner", useIsolatedEnv: useIsolatedEnv, } } @@ -114,7 +114,6 @@ func (r *cpiCmdRunner) Run(context CmdContext, method string, apiVersion int, ar UseIsolatedEnv: r.useIsolatedEnv, Stdin: bytes.NewReader(inputBytes), } - fmt.Printf("cpi_cmd_runner.go: UseIsolatedEnv = %t\n", cmd.UseIsolatedEnv) // 🚧 To-do: Debug-code 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) diff --git a/cloud/cpi_cmd_runner_test.go b/cloud/cpi_cmd_runner_test.go index 73a86e123..1d6f4df32 100644 --- a/cloud/cpi_cmd_runner_test.go +++ b/cloud/cpi_cmd_runner_test.go @@ -35,7 +35,7 @@ var _ = Describe("CpiCmdRunner", func() { cmdRunner = fakesys.NewFakeCmdRunner() logger := boshlog.NewLogger(boshlog.LevelNone) - cpiCmdRunner = NewCPICmdRunner(cmdRunner, cpi, logger) + cpiCmdRunner = NewCPICmdRunner(cmdRunner, cpi, logger, true) apiVersion = 1 }) diff --git a/flake.nix b/flake.nix index daef0ac8c..afadd8ccd 100644 --- a/flake.nix +++ b/flake.nix @@ -25,6 +25,7 @@ delve go gopls + gotools ]; }; }); diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index fce20ff19..7eda129d0 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -1,7 +1,6 @@ package pkg import ( - "fmt" "os" "path/filepath" @@ -111,8 +110,6 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR WorkingDir: packageSrcDir, } - fmt.Printf("compiler.go: UseIsolatedEnv = %t\n", cmd.UseIsolatedEnv) // 🚧 To-do: Debug-code - _, _, _, err = c.runner.RunComplexCommand(cmd) if err != nil { return record, isCompiledPackage, bosherr.WrapError(err, "Compiling package") From 2e81740f972093c2e59ed1fa4da5c11272a65aa7 Mon Sep 17 00:00:00 2001 From: Felix Moehler <101196374+fmoehler@users.noreply.github.com> Date: Tue, 19 Nov 2024 16:39:06 +0100 Subject: [PATCH 09/12] Update compiler.go --- installation/pkg/compiler.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index ee04a57fb..9c4d2bdfa 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -97,20 +97,20 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR return record, isCompiledPackage, bosherr.Errorf("Packaging script for package '%s' not found", pkg.Name()) } - cmd := boshsys.Command{ - Name: "bash", - Args: []string{"-x", "packaging"}, - Env: map[string]string{ - "BOSH_COMPILE_TARGET": packageSrcDir, - "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: c.useIsolatedEnv, - WorkingDir: packageSrcDir, - } + cmd := boshsys.Command{ + Name: "bash", + Args: []string{"-x", "packaging"}, + Env: map[string]string{ + "BOSH_COMPILE_TARGET": packageSrcDir, + "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: c.useIsolatedEnv, + WorkingDir: packageSrcDir, + } _, _, _, err = c.runner.RunComplexCommand(cmd) if err != nil { From 88671688b0e1b8c8487094c833de6a585c48b3b6 Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Tue, 19 Nov 2024 20:35:33 +0100 Subject: [PATCH 10/12] fix env_factory --- cmd/env_factory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/env_factory.go b/cmd/env_factory.go index 795814bbd..6a76973dd 100644 --- a/cmd/env_factory.go +++ b/cmd/env_factory.go @@ -71,8 +71,8 @@ func NewEnvFactory( manifestVars boshtpl.Variables, manifestOp patch.Op, recreatePersistentDisks bool, - useIsolatedEnv bool, packageDir string, + useIsolatedEnv bool, ) *envFactory { f := envFactory{ deps: deps, From eacc7372489dfdada438c93517496a77d02e8e32 Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Tue, 19 Nov 2024 20:44:10 +0100 Subject: [PATCH 11/12] fix compiler.go --- cloud/cpi_cmd_runner_test.go | 29 ++++++----------------------- installation/pkg/compiler.go | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/cloud/cpi_cmd_runner_test.go b/cloud/cpi_cmd_runner_test.go index 116e95e18..4b9d512d0 100644 --- a/cloud/cpi_cmd_runner_test.go +++ b/cloud/cpi_cmd_runner_test.go @@ -21,6 +21,7 @@ var _ = Describe("CpiCmdRunner", func() { cmdRunner *fakesys.FakeCmdRunner cpi CPI apiVersion int + logger boshlog.Logger ) BeforeEach(func() { @@ -35,7 +36,7 @@ var _ = Describe("CpiCmdRunner", func() { } cmdRunner = fakesys.NewFakeCmdRunner() - logger := boshlog.NewLogger(boshlog.LevelNone) + logger = boshlog.NewLogger(boshlog.LevelNone) cpiCmdRunner = NewCPICmdRunner(cmdRunner, cpi, logger, true) apiVersion = 1 @@ -63,7 +64,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", + "PATH": os.Getenv("PATH"), })) Expect(actualCmd.UseIsolatedEnv).To(BeTrue()) bytes, err := io.ReadAll(actualCmd.Stdin) @@ -89,11 +90,8 @@ 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") + It("creates correct command with UseIsolatedEnv false if NewCPICmdRunner is initialized with false", func() { + cpiCmdRunner = NewCPICmdRunner(cmdRunner, cpi, logger, false) cmdOutput := CmdOutput{} outputBytes, err := json.Marshal(cmdOutput) Expect(err).NotTo(HaveOccurred()) @@ -109,21 +107,6 @@ var _ = Describe("CpiCmdRunner", func() { 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) @@ -145,7 +128,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", + "PATH": os.Getenv("PATH"), })) Expect(actualCmd.UseIsolatedEnv).To(BeTrue()) bytes, err := io.ReadAll(actualCmd.Stdin) diff --git a/installation/pkg/compiler.go b/installation/pkg/compiler.go index 9c4d2bdfa..2ed653263 100644 --- a/installation/pkg/compiler.go +++ b/installation/pkg/compiler.go @@ -97,20 +97,20 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR return record, isCompiledPackage, bosherr.Errorf("Packaging script for package '%s' not found", pkg.Name()) } - cmd := boshsys.Command{ - Name: "bash", - Args: []string{"-x", "packaging"}, - Env: map[string]string{ - "BOSH_COMPILE_TARGET": packageSrcDir, - "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: c.useIsolatedEnv, - WorkingDir: packageSrcDir, - } + cmd := boshsys.Command{ + Name: "bash", + Args: []string{"-x", "packaging"}, + Env: map[string]string{ + "BOSH_COMPILE_TARGET": packageSrcDir, + "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: c.useIsolatedEnv, + WorkingDir: packageSrcDir, + } _, _, _, err = c.runner.RunComplexCommand(cmd) if err != nil { From 8f84216b12dc79c223cd4b13be6492e7818c8838 Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Thu, 21 Nov 2024 17:09:16 +0100 Subject: [PATCH 12/12] add test for compiler.go --- installation/pkg/compiler_test.go | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/installation/pkg/compiler_test.go b/installation/pkg/compiler_test.go index cce9c5d4e..35a55688c 100644 --- a/installation/pkg/compiler_test.go +++ b/installation/pkg/compiler_test.go @@ -190,6 +190,46 @@ var _ = Describe("PackageCompiler", func() { Expect(runner.RunComplexCommands[0]).To(Equal(expectedCmd)) }) + Context("when useIsolatedEnv is set to false", func() { + BeforeEach(func() { + useIsolatedEnv = false + compiler = NewPackageCompiler( + runner, + packagesDir, + fs, + compressor, + blobstore, + mockCompiledPackageRepo, + fakeExtractor, + logger, + useIsolatedEnv, + ) + }) + + It("runs the packaging script with UseIsolatedEnv set to false", func() { + _, _, err := compiler.Compile(pkg) + Expect(err).ToNot(HaveOccurred()) + + expectedCmd := boshsys.Command{ + Name: "bash", + Args: []string{"-x", "packaging"}, + Env: map[string]string{ + "BOSH_COMPILE_TARGET": "/pkg-dir", + "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: false, + WorkingDir: "/pkg-dir", + } + + Expect(runner.RunComplexCommands).To(HaveLen(1)) + Expect(runner.RunComplexCommands[0]).To(Equal(expectedCmd)) + }) + }) + It("compresses the compiled package", func() { _, _, err := compiler.Compile(pkg) Expect(err).ToNot(HaveOccurred())