Skip to content

Commit

Permalink
Allow usage of pre-compiled CPI for create-env command
Browse files Browse the repository at this point in the history
The machinery to allow the use of pre-compiled releases for things
_other_ than the CPI has existed for some years.

Note that the CPI must be compiled for the architecture and OS of the
machine you are running the create-env command from.  At the time of
writing, we're not sure if that architecture and OS must match that of
the BOSH director you're creating.

[#187692170]

Co-authored-by: Chris Selzo <chris.selzo@broadcom.com>
  • Loading branch information
2 people authored and lnguyen committed May 30, 2024
1 parent c6c7f6a commit 7f10ef5
Show file tree
Hide file tree
Showing 2 changed files with 420 additions and 206 deletions.
96 changes: 53 additions & 43 deletions installation/pkg/compiler.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package pkg

import (
"fmt"
"os"
"path/filepath"

"github.com/cloudfoundry/bosh-cli/v7/installation/blobextract"
birelpkg "github.com/cloudfoundry/bosh-cli/v7/release/pkg"
bistatepkg "github.com/cloudfoundry/bosh-cli/v7/state/pkg"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"

"github.com/cloudfoundry/bosh-cli/v7/installation/blobextract"
birelpkg "github.com/cloudfoundry/bosh-cli/v7/release/pkg"
bistatepkg "github.com/cloudfoundry/bosh-cli/v7/state/pkg"
)

type compiler struct {
Expand Down Expand Up @@ -50,9 +52,6 @@ func NewPackageCompiler(
}

func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageRecord, bool, error) {
// This is a variable being used now to fulfill the requirement of the compiler_interface compile method
// to indicate whether the package is already compiled. Compiled CPI releases are not currently allowed.
// No other packages, but CPI ones, are currently being compiled locally.
isCompiledPackage := false

c.logger.Debug(c.logTag, "Checking for compiled package '%s/%s'", pkg.Name(), pkg.Fingerprint())
Expand All @@ -77,51 +76,62 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR
}
}()

c.logger.Debug(c.logTag, "Compiling package '%s/%s'", pkg.Name(), pkg.Fingerprint())
var tarball string
switch v := pkg.(type) {
case *birelpkg.Package:
c.logger.Debug(c.logTag, "Compiling package '%s/%s'", pkg.Name(), pkg.Fingerprint())

installDir := filepath.Join(c.packagesDir, pkg.Name())
installDir := filepath.Join(c.packagesDir, pkg.Name())

err = c.fileSystem.MkdirAll(installDir, os.ModePerm)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Creating package install dir")
}

packageSrcDir := pkg.(*birelpkg.Package).ExtractedPath()
err = c.fileSystem.MkdirAll(installDir, os.ModePerm)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Creating package install dir")
}

if !c.fileSystem.FileExists(filepath.Join(packageSrcDir, "packaging")) {
return record, isCompiledPackage, bosherr.Errorf("Packaging script for package '%s' not found", pkg.Name())
}
packageSrcDir := pkg.(*birelpkg.Package).ExtractedPath()

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: true,
WorkingDir: packageSrcDir,
}
if !c.fileSystem.FileExists(filepath.Join(packageSrcDir, "packaging")) {
return record, isCompiledPackage, bosherr.Errorf("Packaging script for package '%s' not found", pkg.Name())
}

_, _, _, err = c.runner.RunComplexCommand(cmd)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Compiling package")
}
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: true,
WorkingDir: packageSrcDir,
}

tarball, err := c.compressor.CompressFilesInDir(installDir)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Compressing compiled package")
}
_, _, _, err = c.runner.RunComplexCommand(cmd)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Compiling package")
}

defer func() {
if err = c.compressor.CleanUp(tarball); err != nil {
c.logger.Warn(c.logTag, "Failed to clean up tarball: %s", err.Error())
tarball, err = c.compressor.CompressFilesInDir(installDir)
if err != nil {
return record, isCompiledPackage, bosherr.WrapError(err, "Compressing compiled package")
}
}()

defer func() {
if err = c.compressor.CleanUp(tarball); err != nil {
c.logger.Warn(c.logTag, "Failed to clean up tarball: %s", err.Error())
}
}()
case *birelpkg.CompiledPackage:
c.logger.Debug(c.logTag, "Using compiled package '%s/%s'", pkg.Name(), pkg.Fingerprint())

isCompiledPackage = true
tarball = pkg.ArchivePath()
default:
panic(fmt.Sprintf("Unknown package object type %T!\n", v))
}

blobID, digest, err := c.blobstore.Create(tarball)
if err != nil {
Expand Down
Loading

0 comments on commit 7f10ef5

Please sign in to comment.