-
Notifications
You must be signed in to change notification settings - Fork 114
/
concrete_applier.go
156 lines (133 loc) · 3.95 KB
/
concrete_applier.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
package applier
import (
as "github.com/cloudfoundry/bosh-agent/agent/applier/applyspec"
"github.com/cloudfoundry/bosh-agent/agent/applier/jobs"
"github.com/cloudfoundry/bosh-agent/agent/applier/packages"
boshjobsuper "github.com/cloudfoundry/bosh-agent/jobsupervisor"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdirs "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/cloudfoundry/bosh-utils/work"
)
type concreteApplier struct {
jobApplier jobs.Applier
packageApplier packages.Applier
logrotateDelegate LogrotateDelegate
jobSupervisor boshjobsuper.JobSupervisor
dirProvider boshdirs.Provider
settings boshsettings.Settings
}
func NewConcreteApplier(
jobApplier jobs.Applier,
packageApplier packages.Applier,
logrotateDelegate LogrotateDelegate,
jobSupervisor boshjobsuper.JobSupervisor,
dirProvider boshdirs.Provider,
settings boshsettings.Settings,
) Applier {
return &concreteApplier{
jobApplier: jobApplier,
packageApplier: packageApplier,
logrotateDelegate: logrotateDelegate,
jobSupervisor: jobSupervisor,
dirProvider: dirProvider,
settings: settings,
}
}
func (a *concreteApplier) Prepare(desiredApplySpec as.ApplySpec) error {
var tasks []func() error
pool := work.Pool{
Count: *a.settings.Env.GetParallel(),
}
for _, job := range desiredApplySpec.Jobs() {
job := job
tasks = append(tasks, func() error {
jobErr := a.jobApplier.Prepare(job)
if jobErr != nil {
return bosherr.WrapErrorf(jobErr, "Preparing job %s", job.Name)
}
return nil
})
}
for _, pkg := range desiredApplySpec.Packages() {
pkg := pkg
tasks = append(tasks, func() error {
pkgErr := a.packageApplier.Prepare(pkg)
if pkgErr != nil {
return bosherr.WrapErrorf(pkgErr, "Preparing package %s", pkg.Name)
}
return nil
})
}
err := pool.ParallelDo(tasks...)
if err != nil {
return err
}
err = a.jobApplier.DeleteSourceBlobs(desiredApplySpec.Jobs())
if err != nil {
return bosherr.WrapError(err, "Failed removing job source blobs")
}
return nil
}
func (a *concreteApplier) Apply(desiredApplySpec as.ApplySpec) error {
err := a.jobSupervisor.RemoveAllJobs()
if err != nil {
return bosherr.WrapError(err, "Removing all jobs")
}
jobs := desiredApplySpec.Jobs()
for _, job := range jobs {
err = a.jobApplier.Apply(job)
if err != nil {
return bosherr.WrapErrorf(err, "Applying job %s", job.Name)
}
}
err = a.jobApplier.DeleteSourceBlobs(desiredApplySpec.Jobs())
if err != nil {
return bosherr.WrapError(err, "Failed removing job source blobs")
}
err = a.jobApplier.KeepOnly(desiredApplySpec.Jobs())
if err != nil {
return bosherr.WrapError(err, "Keeping only needed jobs")
}
for _, pkg := range desiredApplySpec.Packages() {
err = a.packageApplier.Apply(pkg)
if err != nil {
return bosherr.WrapErrorf(err, "Applying package %s", pkg.Name)
}
}
err = a.packageApplier.KeepOnly(desiredApplySpec.Packages())
if err != nil {
return bosherr.WrapError(err, "Keeping only needed packages")
}
err = a.jobSupervisor.Reload()
if err != nil {
return bosherr.WrapError(err, "Reloading jobSupervisor")
}
return a.setUpLogrotate(desiredApplySpec)
}
func (a *concreteApplier) ConfigureJobs(desiredApplySpec as.ApplySpec) error {
jobs := desiredApplySpec.Jobs()
for i := 0; i < len(jobs); i++ {
job := jobs[len(jobs)-1-i]
err := a.jobApplier.Configure(job, i)
if err != nil {
return bosherr.WrapErrorf(err, "Configuring job %s", job.Name)
}
}
err := a.jobSupervisor.Reload()
if err != nil {
return bosherr.WrapError(err, "Reloading jobSupervisor")
}
return nil
}
func (a *concreteApplier) setUpLogrotate(applySpec as.ApplySpec) error {
err := a.logrotateDelegate.SetupLogrotate(
boshsettings.VCAPUsername,
a.dirProvider.BaseDir(),
applySpec.MaxLogFileSize(),
)
if err != nil {
return bosherr.WrapError(err, "Logrotate setup failed")
}
return nil
}