11package pkg
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
67
7- "github.com/cloudfoundry/bosh-cli/v7/installation/blobextract"
8- birelpkg "github.com/cloudfoundry/bosh-cli/v7/release/pkg"
9- bistatepkg "github.com/cloudfoundry/bosh-cli/v7/state/pkg"
108 boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
119 bosherr "github.com/cloudfoundry/bosh-utils/errors"
1210 boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
1311 boshlog "github.com/cloudfoundry/bosh-utils/logger"
1412 boshsys "github.com/cloudfoundry/bosh-utils/system"
13+
14+ "github.com/cloudfoundry/bosh-cli/v7/installation/blobextract"
15+ birelpkg "github.com/cloudfoundry/bosh-cli/v7/release/pkg"
16+ bistatepkg "github.com/cloudfoundry/bosh-cli/v7/state/pkg"
1517)
1618
1719type compiler struct {
@@ -50,9 +52,6 @@ func NewPackageCompiler(
5052}
5153
5254func (c * compiler ) Compile (pkg birelpkg.Compilable ) (bistatepkg.CompiledPackageRecord , bool , error ) {
53- // This is a variable being used now to fulfill the requirement of the compiler_interface compile method
54- // to indicate whether the package is already compiled. Compiled CPI releases are not currently allowed.
55- // No other packages, but CPI ones, are currently being compiled locally.
5655 isCompiledPackage := false
5756
5857 c .logger .Debug (c .logTag , "Checking for compiled package '%s/%s'" , pkg .Name (), pkg .Fingerprint ())
@@ -77,51 +76,62 @@ func (c *compiler) Compile(pkg birelpkg.Compilable) (bistatepkg.CompiledPackageR
7776 }
7877 }()
7978
80- c .logger .Debug (c .logTag , "Compiling package '%s/%s'" , pkg .Name (), pkg .Fingerprint ())
79+ var tarball string
80+ switch v := pkg .(type ) {
81+ case * birelpkg.Package :
82+ c .logger .Debug (c .logTag , "Compiling package '%s/%s'" , pkg .Name (), pkg .Fingerprint ())
8183
82- installDir := filepath .Join (c .packagesDir , pkg .Name ())
84+ installDir := filepath .Join (c .packagesDir , pkg .Name ())
8385
84- err = c .fileSystem .MkdirAll (installDir , os .ModePerm )
85- if err != nil {
86- return record , isCompiledPackage , bosherr .WrapError (err , "Creating package install dir" )
87- }
88-
89- packageSrcDir := pkg .(* birelpkg.Package ).ExtractedPath ()
86+ err = c .fileSystem .MkdirAll (installDir , os .ModePerm )
87+ if err != nil {
88+ return record , isCompiledPackage , bosherr .WrapError (err , "Creating package install dir" )
89+ }
9090
91- if ! c .fileSystem .FileExists (filepath .Join (packageSrcDir , "packaging" )) {
92- return record , isCompiledPackage , bosherr .Errorf ("Packaging script for package '%s' not found" , pkg .Name ())
93- }
91+ packageSrcDir := pkg .(* birelpkg.Package ).ExtractedPath ()
9492
95- cmd := boshsys.Command {
96- Name : "bash" ,
97- Args : []string {"-x" , "packaging" },
98- Env : map [string ]string {
99- "BOSH_COMPILE_TARGET" : packageSrcDir ,
100- "BOSH_INSTALL_TARGET" : installDir ,
101- "BOSH_PACKAGE_NAME" : pkg .Name (),
102- "BOSH_PACKAGES_DIR" : c .packagesDir ,
103- "PATH" : os .Getenv ("PATH" ),
104- "LD_LIBRARY_PATH" : os .Getenv ("LD_LIBRARY_PATH" ),
105- },
106- UseIsolatedEnv : true ,
107- WorkingDir : packageSrcDir ,
108- }
93+ if ! c .fileSystem .FileExists (filepath .Join (packageSrcDir , "packaging" )) {
94+ return record , isCompiledPackage , bosherr .Errorf ("Packaging script for package '%s' not found" , pkg .Name ())
95+ }
10996
110- _ , _ , _ , err = c .runner .RunComplexCommand (cmd )
111- if err != nil {
112- return record , isCompiledPackage , bosherr .WrapError (err , "Compiling package" )
113- }
97+ cmd := boshsys.Command {
98+ Name : "bash" ,
99+ Args : []string {"-x" , "packaging" },
100+ Env : map [string ]string {
101+ "BOSH_COMPILE_TARGET" : packageSrcDir ,
102+ "BOSH_INSTALL_TARGET" : installDir ,
103+ "BOSH_PACKAGE_NAME" : pkg .Name (),
104+ "BOSH_PACKAGES_DIR" : c .packagesDir ,
105+ "PATH" : os .Getenv ("PATH" ),
106+ "LD_LIBRARY_PATH" : os .Getenv ("LD_LIBRARY_PATH" ),
107+ },
108+ UseIsolatedEnv : true ,
109+ WorkingDir : packageSrcDir ,
110+ }
114111
115- tarball , err : = c .compressor . CompressFilesInDir ( installDir )
116- if err != nil {
117- return record , isCompiledPackage , bosherr .WrapError (err , "Compressing compiled package" )
118- }
112+ _ , _ , _ , err = c .runner . RunComplexCommand ( cmd )
113+ if err != nil {
114+ return record , isCompiledPackage , bosherr .WrapError (err , "Compiling package" )
115+ }
119116
120- defer func () {
121- if err = c . compressor . CleanUp ( tarball ); err != nil {
122- c . logger . Warn ( c . logTag , "Failed to clean up tarball: %s" , err . Error () )
117+ tarball , err = c . compressor . CompressFilesInDir ( installDir )
118+ if err != nil {
119+ return record , isCompiledPackage , bosherr . WrapError ( err , "Compressing compiled package" )
123120 }
124- }()
121+
122+ defer func () {
123+ if err = c .compressor .CleanUp (tarball ); err != nil {
124+ c .logger .Warn (c .logTag , "Failed to clean up tarball: %s" , err .Error ())
125+ }
126+ }()
127+ case * birelpkg.CompiledPackage :
128+ c .logger .Debug (c .logTag , "Using compiled package '%s/%s'" , pkg .Name (), pkg .Fingerprint ())
129+
130+ isCompiledPackage = true
131+ tarball = pkg .ArchivePath ()
132+ default :
133+ panic (fmt .Sprintf ("Unknown package object type %T!\n " , v ))
134+ }
125135
126136 blobID , digest , err := c .blobstore .Create (tarball )
127137 if err != nil {
0 commit comments