forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
139 lines (110 loc) · 3.09 KB
/
validator.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
package release
import (
"errors"
"fmt"
"path"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshsys "github.com/cloudfoundry/bosh-agent/system"
)
type Validator interface {
Validate(release Release) error
}
type validator struct {
fs boshsys.FileSystem
}
func NewValidator(fs boshsys.FileSystem) Validator {
return &validator{fs: fs}
}
func (v *validator) Validate(release Release) error {
errs := []error{}
err := v.validateReleaseName(release)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Validating release name"))
}
err = v.validateReleaseVersion(release)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Validating release version"))
}
err = v.validateReleaseJobs(release)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Validating release jobs"))
}
err = v.validateReleasePackages(release)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Validating release packages"))
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}
func (v *validator) validateReleaseName(release Release) error {
if release.Name() == "" {
return errors.New("Release name is missing")
}
return nil
}
func (v *validator) validateReleaseVersion(release Release) error {
if release.Version() == "" {
return errors.New("Release version is missing")
}
return nil
}
func (v *validator) validateReleaseJobs(release Release) error {
errs := []error{}
for _, job := range release.Jobs() {
if job.Name == "" {
errs = append(errs, errors.New("Job name is missing"))
}
if job.Fingerprint == "" {
errs = append(errs, fmt.Errorf("Job '%s' fingerprint is missing", job.Name))
}
if job.SHA1 == "" {
errs = append(errs, fmt.Errorf("Job '%s' sha1 is missing", job.Name))
}
monitPath := path.Join(job.ExtractedPath, "monit")
if !v.fs.FileExists(monitPath) {
errs = append(errs, fmt.Errorf("Job '%s' is missing monit file", job.Name))
}
for template := range job.Templates {
templatePath := path.Join(job.ExtractedPath, "templates", template)
if !v.fs.FileExists(templatePath) {
errs = append(errs, fmt.Errorf("Job '%s' is missing template '%s'", job.Name, templatePath))
}
}
for _, pkgName := range job.PackageNames {
found := false
for _, releasePackage := range release.Packages() {
if releasePackage.Name == pkgName {
found = true
break
}
}
if !found {
errs = append(errs, fmt.Errorf("Job '%s' requires '%s' which is not in the release", job.Name, pkgName))
}
}
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}
func (v *validator) validateReleasePackages(release Release) error {
errs := []error{}
for _, pkg := range release.Packages() {
if pkg.Name == "" {
errs = append(errs, errors.New("Package name is missing"))
}
if pkg.Fingerprint == "" {
errs = append(errs, fmt.Errorf("Package '%s' fingerprint is missing", pkg.Name))
}
if pkg.SHA1 == "" {
errs = append(errs, fmt.Errorf("Package '%s' sha1 is missing", pkg.Name))
}
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}