forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
184 lines (152 loc) · 5.43 KB
/
reader.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
package release
import (
"gopkg.in/yaml.v2"
"os"
"path"
bireljob "github.com/cloudfoundry/bosh-init/release/job"
birelmanifest "github.com/cloudfoundry/bosh-init/release/manifest"
birelpkg "github.com/cloudfoundry/bosh-init/release/pkg"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type reader struct {
tarFilePath string
extractedReleasePath string
fs boshsys.FileSystem
extractor boshcmd.Compressor
}
type Reader interface {
Read() (Release, error)
}
func NewReader(
tarFilePath string,
extractedReleasePath string,
fs boshsys.FileSystem,
extractor boshcmd.Compressor,
) Reader {
return &reader{
tarFilePath: tarFilePath,
extractedReleasePath: extractedReleasePath,
fs: fs,
extractor: extractor,
}
}
func (r *reader) Read() (Release, error) {
err := r.extractor.DecompressFileToDir(r.tarFilePath, r.extractedReleasePath, boshcmd.CompressorOptions{})
if err != nil {
return nil, bosherr.WrapError(err, "Extracting release")
}
releaseManifestPath := path.Join(r.extractedReleasePath, "release.MF")
releaseManifestBytes, err := r.fs.ReadFile(releaseManifestPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading release manifest '%s'", releaseManifestPath)
}
var manifest birelmanifest.Manifest
err = yaml.Unmarshal(releaseManifestBytes, &manifest)
if err != nil {
return nil, bosherr.WrapError(err, "Parsing release manifest")
}
release, err := r.newReleaseFromManifest(manifest)
if err != nil {
return nil, bosherr.WrapError(err, "Constructing release from manifest")
}
return release, nil
}
func (r *reader) newReleaseFromManifest(releaseManifest birelmanifest.Manifest) (Release, error) {
errors := []error{}
packages, err := r.newPackagesFromManifestPackages(releaseManifest.Packages)
if err != nil {
errors = append(errors, bosherr.WrapError(err, "Constructing packages from manifest"))
}
jobs, err := r.newJobsFromManifestJobs(packages, releaseManifest.Jobs)
if err != nil {
errors = append(errors, bosherr.WrapError(err, "Constructing jobs from manifest"))
}
if len(errors) > 0 {
return nil, bosherr.NewMultiError(errors...)
}
release := &release{
name: releaseManifest.Name,
version: releaseManifest.Version,
jobs: jobs,
packages: packages,
extractedPath: r.extractedReleasePath,
fs: r.fs,
}
return release, nil
}
func (r *reader) newJobsFromManifestJobs(packages []*birelpkg.Package, manifestJobs []birelmanifest.JobRef) ([]bireljob.Job, error) {
jobs := []bireljob.Job{}
errors := []error{}
for _, manifestJob := range manifestJobs {
extractedJobPath := path.Join(r.extractedReleasePath, "extracted_jobs", manifestJob.Name)
err := r.fs.MkdirAll(extractedJobPath, os.ModeDir|0700)
if err != nil {
errors = append(errors, bosherr.WrapError(err, "Creating extracted job path"))
continue
}
jobArchivePath := path.Join(r.extractedReleasePath, "jobs", manifestJob.Name+".tgz")
jobReader := bireljob.NewReader(jobArchivePath, extractedJobPath, r.extractor, r.fs)
job, err := jobReader.Read()
if err != nil {
errors = append(errors, bosherr.WrapErrorf(err, "Reading job '%s' from archive", manifestJob.Name))
continue
}
job.Fingerprint = manifestJob.Fingerprint
job.SHA1 = manifestJob.SHA1
for _, pkgName := range job.PackageNames {
pkg, found := r.findPackageByName(packages, pkgName)
if !found {
return []bireljob.Job{}, bosherr.Errorf("Package not found: '%s'", pkgName)
}
job.Packages = append(job.Packages, pkg)
}
jobs = append(jobs, job)
}
if len(errors) > 0 {
return []bireljob.Job{}, bosherr.NewMultiError(errors...)
}
return jobs, nil
}
func (r *reader) findPackageByName(packages []*birelpkg.Package, pkgName string) (*birelpkg.Package, bool) {
for _, pkg := range packages {
if pkg.Name == pkgName {
return pkg, true
}
}
return nil, false
}
func (r *reader) newPackagesFromManifestPackages(manifestPackages []birelmanifest.PackageRef) ([]*birelpkg.Package, error) {
packages := []*birelpkg.Package{}
errors := []error{}
packageRepo := &birelpkg.PackageRepo{}
for _, manifestPackage := range manifestPackages {
pkg := packageRepo.FindOrCreatePackage(manifestPackage.Name)
extractedPackagePath := path.Join(r.extractedReleasePath, "extracted_packages", manifestPackage.Name)
err := r.fs.MkdirAll(extractedPackagePath, os.ModeDir|0700)
if err != nil {
errors = append(errors, bosherr.WrapError(err, "Creating extracted package path"))
continue
}
packageArchivePath := path.Join(r.extractedReleasePath, "packages", manifestPackage.Name+".tgz")
err = r.extractor.DecompressFileToDir(packageArchivePath, extractedPackagePath, boshcmd.CompressorOptions{})
if err != nil {
errors = append(errors, bosherr.WrapErrorf(err, "Extracting package '%s'", manifestPackage.Name))
continue
}
pkg.Fingerprint = manifestPackage.Fingerprint
pkg.SHA1 = manifestPackage.SHA1
pkg.ExtractedPath = extractedPackagePath
pkg.ArchivePath = packageArchivePath
pkg.Dependencies = []*birelpkg.Package{}
for _, manifestPackageName := range manifestPackage.Dependencies {
pkg.Dependencies = append(pkg.Dependencies, packageRepo.FindOrCreatePackage(manifestPackageName))
}
packages = append(packages, pkg)
}
if len(errors) > 0 {
return []*birelpkg.Package{}, bosherr.NewMultiError(errors...)
}
return packages, nil
}