This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cppforlife/bosh-provisioner
/
dir_reader.go
85 lines (64 loc) · 1.71 KB
/
dir_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
package job
import (
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
bpjobrelman "github.com/cppforlife/bosh-provisioner/release/job/manifest"
)
const dirReaderLogTag = "DirReader"
type DirReader struct {
dir string
fs boshsys.FileSystem
logger boshlog.Logger
}
func NewDirReader(
dir string,
fs boshsys.FileSystem,
logger boshlog.Logger,
) DirReader {
return DirReader{
dir: dir,
fs: fs,
logger: logger,
}
}
func (r DirReader) Read() (Job, error) {
var job Job
manifestPath := filepath.Join(r.dir, "spec")
manifest, err := bpjobrelman.NewManifestFromPath(manifestPath, r.fs)
if err != nil {
closeErr := r.Close()
if closeErr != nil {
r.logger.Debug(dirReaderLogTag,
"Failed to close job %v", closeErr)
}
return job, bosherr.WrapError(err, "Building manifest")
}
r.logger.Debug(dirReaderLogTag, "Done building manifest %#v", manifest)
job.populateFromManifest(manifest)
r.populateJobPaths(&job)
return job, nil
}
func (r DirReader) Close() error {
// Caller owns release directory; hence, nothing to clean up
return nil
}
// populateJobPaths sets Path for each template and monit in the job.
func (r DirReader) populateJobPaths(job *Job) {
// monit file is outside of templates/ directory
job.MonitTemplate.Path = filepath.Join(r.dir, "monit")
for i, template := range job.Templates {
job.Templates[i].Path = filepath.Join(
r.dir, "templates", template.SrcPathEnd)
}
}
/*
Example layout of a job directory:
$ tree ~/Downloads/dummy-job
~/Downloads/dummy-job
├── spec
├── monit
└── templates
└── dummy_ctl.erb
*/