forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concrete_compiler.go
211 lines (172 loc) · 5.91 KB
/
concrete_compiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package compiler
import (
"fmt"
"os"
"path"
boshbc "github.com/cloudfoundry/bosh-agent/agent/applier/bundlecollection"
boshmodels "github.com/cloudfoundry/bosh-agent/agent/applier/models"
"github.com/cloudfoundry/bosh-agent/agent/applier/packages"
boshcmdrunner "github.com/cloudfoundry/bosh-agent/agent/cmdrunner"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const PackagingScriptName = "packaging"
type CompileDirProvider interface {
CompileDir() string
}
type concreteCompiler struct {
compressor boshcmd.Compressor
blobstore boshblob.DigestBlobstore
fs boshsys.FileSystem
runner boshcmdrunner.CmdRunner
compileDirProvider CompileDirProvider
packageApplier packages.Applier
packagesBc boshbc.BundleCollection
}
func NewConcreteCompiler(
compressor boshcmd.Compressor,
blobstore boshblob.DigestBlobstore,
fs boshsys.FileSystem,
runner boshcmdrunner.CmdRunner,
compileDirProvider CompileDirProvider,
packageApplier packages.Applier,
packagesBc boshbc.BundleCollection,
) Compiler {
return concreteCompiler{
compressor: compressor,
blobstore: blobstore,
fs: fs,
runner: runner,
compileDirProvider: compileDirProvider,
packageApplier: packageApplier,
packagesBc: packagesBc,
}
}
func (c concreteCompiler) Compile(pkg Package, deps []boshmodels.Package) (blobID string, digest boshcrypto.Digest, err error) {
err = c.packageApplier.KeepOnly([]boshmodels.Package{})
if err != nil {
return "", nil, bosherr.WrapError(err, "Removing packages")
}
for _, dep := range deps {
err := c.packageApplier.Apply(dep)
if err != nil {
return "", nil, bosherr.WrapErrorf(err, "Installing dependent package: '%s'", dep.Name)
}
}
compilePath := path.Join(c.compileDirProvider.CompileDir(), pkg.Name)
err = c.fetchAndUncompress(pkg, compilePath)
if err != nil {
return "", nil, bosherr.WrapErrorf(err, "Fetching package %s", pkg.Name)
}
defer c.fs.RemoveAll(compilePath)
defer func() {
e := c.fs.RemoveAll(compilePath)
if e != nil && err == nil {
err = e
}
}()
compiledPkg := boshmodels.LocalPackage{
Name: pkg.Name,
Version: pkg.Version,
}
compiledPkgBundle, err := c.packagesBc.Get(compiledPkg)
if err != nil {
return "", nil, bosherr.WrapError(err, "Getting bundle for new package")
}
_, installPath, err := compiledPkgBundle.InstallWithoutContents()
if err != nil {
return "", nil, bosherr.WrapError(err, "Setting up new package bundle")
}
_, enablePath, err := compiledPkgBundle.Enable()
if err != nil {
return "", nil, bosherr.WrapError(err, "Enabling new package bundle")
}
scriptPath := path.Join(compilePath, PackagingScriptName)
if c.fs.FileExists(scriptPath) {
if err := c.runPackagingCommand(compilePath, enablePath, pkg); err != nil {
return "", nil, bosherr.WrapError(err, "Running packaging script")
}
}
tmpPackageTar, err := c.compressor.CompressFilesInDir(installPath)
if err != nil {
return "", nil, bosherr.WrapError(err, "Compressing compiled package")
}
defer func() {
_ = c.compressor.CleanUp(tmpPackageTar)
}()
file, err := c.fs.OpenFile(tmpPackageTar, os.O_RDONLY, os.ModePerm)
if err != nil {
return "", nil, bosherr.WrapError(err, "Opening compiled package")
}
// Use SHA256, not the strongest algo from the source blob
digest, err = pkg.Sha1.Algorithm().CreateDigest(file)
if err != nil {
return "", nil, bosherr.WrapError(err, "Calculating compiled package digest")
}
uploadedBlobID, _, err := c.blobstore.Create(tmpPackageTar)
if err != nil {
return "", nil, bosherr.WrapError(err, "Uploading compiled package")
}
err = compiledPkgBundle.Disable()
if err != nil {
return "", nil, bosherr.WrapError(err, "Disabling compiled package")
}
err = compiledPkgBundle.Uninstall()
if err != nil {
return "", nil, bosherr.WrapError(err, "Uninstalling compiled package")
}
err = c.packageApplier.KeepOnly([]boshmodels.Package{})
if err != nil {
return "", nil, bosherr.WrapError(err, "Removing packages")
}
return uploadedBlobID, digest, nil
}
func (c concreteCompiler) fetchAndUncompress(pkg Package, targetDir string) error {
if pkg.BlobstoreID == "" {
return bosherr.Error(fmt.Sprintf("Blobstore ID for package '%s' is empty", pkg.Name))
}
depFilePath, err := c.blobstore.Get(pkg.BlobstoreID, pkg.Sha1)
if err != nil {
return bosherr.WrapErrorf(err, "Fetching package blob %s", pkg.BlobstoreID)
}
err = c.atomicDecompress(depFilePath, targetDir)
if err != nil {
return bosherr.WrapErrorf(err, "Uncompressing package %s", pkg.Name)
}
return nil
}
func (c concreteCompiler) atomicDecompress(archivePath string, finalDir string) error {
tmpInstallPath := finalDir + "-bosh-agent-unpack"
{
err := c.fs.RemoveAll(finalDir)
if err != nil {
return bosherr.WrapErrorf(err, "Removing install path %s", finalDir)
}
err = c.fs.MkdirAll(finalDir, os.FileMode(0755))
if err != nil {
return bosherr.WrapErrorf(err, "Creating install path %s", finalDir)
}
}
{
err := c.fs.RemoveAll(tmpInstallPath)
if err != nil {
return bosherr.WrapErrorf(err, "Removing temporary compile directory %s", tmpInstallPath)
}
err = c.fs.MkdirAll(tmpInstallPath, os.FileMode(0755))
if err != nil {
return bosherr.WrapErrorf(err, "Creating temporary compile directory %s", tmpInstallPath)
}
}
err := c.compressor.DecompressFileToDir(archivePath, tmpInstallPath, boshcmd.CompressorOptions{})
if err != nil {
return bosherr.WrapErrorf(err, "Decompressing files from %s to %s", archivePath, tmpInstallPath)
}
err = c.fs.Rename(tmpInstallPath, finalDir)
if err != nil {
return bosherr.WrapErrorf(err, "Moving temporary directory %s to final destination %s", tmpInstallPath, finalDir)
}
return nil
}