-
Notifications
You must be signed in to change notification settings - Fork 115
/
job.go
77 lines (62 loc) · 1.81 KB
/
job.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
package models
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"os"
)
type Job struct {
Name string
Version string
Source Source
// Packages that this job depends on; however,
// currently it will contain packages from all jobs
Packages []Package
}
func (s Job) BundleName() string {
return s.Name
}
func (s Job) BundleVersion() string {
if len(s.Version) == 0 {
panic("Internal inconsistency: Expected job.Version to be non-empty")
}
// Job template is not unique per version because
// Source contains files with interpolated values
// which might be different across job versions.
return s.Version + "-" + s.Source.Sha1.String()
}
type JobDirectoryCreator interface {
MkdirAll(path string, perm os.FileMode) error
Chown(path, username string) error
Chmod(path string, perm os.FileMode) error
FileExists(path string) bool
}
type JobDirectoryProvider interface {
JobLogDir(jobName string) string
JobRunDir(jobName string) string
JobDir(jobName string) string
}
func (s Job) CreateDirectories(jobDirectoryCreator JobDirectoryCreator, jobDirProvider JobDirectoryProvider) error {
if len(s.Name) < 1 {
return bosherr.Error("Job name cannot be emtpy")
}
dirs := []string{
jobDirProvider.JobLogDir(s.Name),
jobDirProvider.JobRunDir(s.Name),
jobDirProvider.JobDir(s.Name),
}
for _, dir := range dirs {
if jobDirectoryCreator.FileExists(dir) {
continue
}
mode := os.FileMode(0770)
if err := jobDirectoryCreator.MkdirAll(dir, mode); err != nil {
return bosherr.WrapError(err, "Failed to create dir")
}
if err := jobDirectoryCreator.Chmod(dir, mode); err != nil {
return bosherr.WrapError(err, "Failed to chmod dir")
}
if err := jobDirectoryCreator.Chown(dir, "root:vcap"); err != nil {
return bosherr.WrapError(err, "Failed to chown dir")
}
}
return nil
}