forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_writer.go
250 lines (190 loc) · 7.08 KB
/
archive_writer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package release
import (
"os"
gopath "path"
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"
"gopkg.in/yaml.v2"
boshjob "github.com/cloudfoundry/bosh-cli/release/job"
boshlic "github.com/cloudfoundry/bosh-cli/release/license"
boshpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
"path/filepath"
)
type ArchiveWriter struct {
compressor boshcmd.Compressor
fs boshsys.FileSystem
filesToInclude []string
logTag string
logger boshlog.Logger
}
func NewArchiveWriter(compressor boshcmd.Compressor, fs boshsys.FileSystem, logger boshlog.Logger) ArchiveWriter {
return ArchiveWriter{compressor: compressor, fs: fs, logTag: "release.ArchiveWriter", logger: logger}
}
func (w ArchiveWriter) Write(release Release, pkgFpsToSkip []string) (string, error) {
stagingDir, err := w.fs.TempDir("bosh-release")
if err != nil {
return "", bosherr.WrapErrorf(err, "Creating staging release dir")
}
defer w.cleanUp(stagingDir)
w.logger.Info(w.logTag, "Writing release tarball into '%s'", stagingDir)
manifestBytes, err := yaml.Marshal(release.Manifest())
if err != nil {
return "", bosherr.WrapError(err, "Marshalling release manifest")
}
manifestPath := gopath.Join(stagingDir, "release.MF")
err = w.fs.WriteFile(manifestPath, manifestBytes)
if err != nil {
return "", bosherr.WrapErrorf(err, "Writing release manifest '%s'", manifestPath)
}
w.filesToInclude = w.appendFile("release.MF")
jobsFiles, err := w.writeJobs(release.Jobs(), stagingDir)
if err != nil {
return "", bosherr.WrapError(err, "Writing jobs")
}
w.filesToInclude = w.appendFiles(jobsFiles)
packagesFiles, err := w.writePackages(release.Packages(), pkgFpsToSkip, stagingDir)
if err != nil {
return "", bosherr.WrapError(err, "Writing packages")
}
w.filesToInclude = w.appendFiles(packagesFiles)
compiledPackagesFiles, err := w.writeCompiledPackages(release.CompiledPackages(), pkgFpsToSkip, stagingDir)
if err != nil {
return "", bosherr.WrapError(err, "Writing compiled packages")
}
w.filesToInclude = w.appendFiles(compiledPackagesFiles)
licenseFiles, err := w.writeLicense(release.License(), stagingDir)
if err != nil {
return "", bosherr.WrapError(err, "Writing license")
}
w.filesToInclude = w.appendFiles(licenseFiles)
files := w.filesToInclude
path, err := w.compressor.CompressSpecificFilesInDir(stagingDir, files)
if err != nil {
return "", bosherr.WrapError(err, "Compressing release")
}
return path, nil
}
func (w ArchiveWriter) cleanUp(stagingDir string) {
removeErr := w.fs.RemoveAll(stagingDir)
if removeErr != nil {
w.logger.Error(w.logTag, "Failed to remove staging dir for release: %s", removeErr.Error())
}
}
func (w ArchiveWriter) writeJobs(jobs []*boshjob.Job, stagingDir string) ([]string, error) {
var jobsFiles []string
if len(jobs) == 0 {
return jobsFiles, nil
}
jobsPath := gopath.Join(stagingDir, "jobs")
err := w.fs.MkdirAll(jobsPath, os.ModePerm)
if err != nil {
return jobsFiles, bosherr.WrapError(err, "Creating jobs/")
}
jobsFiles = append(jobsFiles, "jobs")
for _, job := range jobs {
err := w.fs.CopyFile(job.ArchivePath(), gopath.Join(jobsPath, job.Name()+".tgz"))
if err != nil {
return jobsFiles, bosherr.WrapErrorf(err, "Copying job '%s' archive into staging dir", job.Name())
}
}
return jobsFiles, nil
}
func (w ArchiveWriter) writePackages(packages []*boshpkg.Package, pkgFpsToSkip []string, stagingDir string) ([]string, error) {
var packagesFiles []string
if len(packages) == 0 {
return packagesFiles, nil
}
pkgsPath := gopath.Join(stagingDir, "packages")
err := w.fs.MkdirAll(pkgsPath, os.ModePerm)
if err != nil {
return packagesFiles, bosherr.WrapError(err, "Creating packages/")
}
packagesFiles = append(packagesFiles, "packages")
for _, pkg := range packages {
if w.shouldSkip(pkg.Fingerprint(), pkgFpsToSkip) {
w.logger.Debug(w.logTag, "Package '%s' was filtered out", pkg.Name())
} else {
err := w.fs.CopyFile(pkg.ArchivePath(), gopath.Join(pkgsPath, pkg.Name()+".tgz"))
if err != nil {
return packagesFiles, bosherr.WrapErrorf(err, "Copying package '%s' archive into staging dir", pkg.Name())
}
}
}
return packagesFiles, nil
}
func (w ArchiveWriter) writeCompiledPackages(compiledPkgs []*boshpkg.CompiledPackage, pkgFpsToSkip []string, stagingDir string) ([]string, error) {
var compiledPackagesFiles []string
if len(compiledPkgs) == 0 {
return compiledPackagesFiles, nil
}
pkgsPath := gopath.Join(stagingDir, "compiled_packages")
err := w.fs.MkdirAll(pkgsPath, os.ModePerm)
if err != nil {
return compiledPackagesFiles, bosherr.WrapError(err, "Creating compiled_packages/")
}
compiledPackagesFiles = append(compiledPackagesFiles, "compiled_packages")
for _, compiledPkg := range compiledPkgs {
if w.shouldSkip(compiledPkg.Fingerprint(), pkgFpsToSkip) {
w.logger.Debug(w.logTag, "Compiled package '%s' was filtered out", compiledPkg.Name())
} else {
err := w.fs.CopyFile(compiledPkg.ArchivePath(), gopath.Join(pkgsPath, compiledPkg.Name()+".tgz"))
if err != nil {
return compiledPackagesFiles, bosherr.WrapErrorf(err, "Copying compiled package '%s' archive into staging dir", compiledPkg.Name())
}
}
}
return compiledPackagesFiles, nil
}
func (w ArchiveWriter) writeLicense(license *boshlic.License, stagingDir string) ([]string, error) {
var licenseFiles []string
if license == nil {
return licenseFiles, nil
}
err := w.fs.CopyFile(license.ArchivePath(), gopath.Join(stagingDir, "license.tgz"))
if err != nil {
return licenseFiles, bosherr.WrapError(err, "Copying license archive into staging dir")
}
licenseFiles = append(licenseFiles, "license.tgz")
err = w.compressor.DecompressFileToDir(license.ArchivePath(), stagingDir, boshcmd.CompressorOptions{})
if err != nil {
return licenseFiles, bosherr.WrapErrorf(err, "Decompressing license archive into staging dir")
}
licenseFiles, err = w.appendMatchedFiles(licenseFiles, stagingDir, "LICENSE*")
if err != nil {
return licenseFiles, bosherr.WrapErrorf(err, "Reading LICENSE files")
}
licenseFiles, err = w.appendMatchedFiles(licenseFiles, stagingDir, "NOTICE*")
if err != nil {
return licenseFiles, bosherr.WrapErrorf(err, "Reading NOTICE files")
}
return licenseFiles, nil
}
func (w ArchiveWriter) shouldSkip(fp string, pkgFpsToSkip []string) bool {
for _, pkgFp := range pkgFpsToSkip {
if fp == pkgFp {
return true
}
}
return false
}
func (w ArchiveWriter) appendFile(filename string) []string {
return append(w.filesToInclude, filename)
}
func (w ArchiveWriter) appendFiles(filenames []string) []string {
for _, filename := range filenames {
w.filesToInclude = w.appendFile(filename)
}
return w.filesToInclude
}
func (w ArchiveWriter) appendMatchedFiles(files []string, stagingDir string, filePattern string) ([]string, error) {
noticeMatches, err := w.fs.Glob(gopath.Join(stagingDir, filePattern))
if err != nil {
return files, err
}
for _, file := range noticeMatches {
files = append(files, filepath.Base(file))
}
return files, nil
}