-
Notifications
You must be signed in to change notification settings - Fork 114
/
concrete_applier.go
125 lines (105 loc) · 3.28 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
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"
)
type concreteApplier struct {
jobApplier jobs.Applier
packageApplier packages.Applier
logrotateDelegate LogrotateDelegate
jobSupervisor boshjobsuper.JobSupervisor
dirProvider boshdirs.Provider
}
func NewConcreteApplier(
jobApplier jobs.Applier,
packageApplier packages.Applier,
logrotateDelegate LogrotateDelegate,
jobSupervisor boshjobsuper.JobSupervisor,
dirProvider boshdirs.Provider,
) Applier {
return &concreteApplier{
jobApplier: jobApplier,
packageApplier: packageApplier,
logrotateDelegate: logrotateDelegate,
jobSupervisor: jobSupervisor,
dirProvider: dirProvider,
}
}
func (a *concreteApplier) Prepare(desiredApplySpec as.ApplySpec) error {
for _, job := range desiredApplySpec.Jobs() {
err := a.jobApplier.Prepare(job)
if err != nil {
return bosherr.WrapErrorf(err, "Preparing job %s", job.Name)
}
}
for _, pkg := range desiredApplySpec.Packages() {
err := a.packageApplier.Prepare(pkg)
if err != nil {
return bosherr.WrapErrorf(err, "Preparing package %s", pkg.Name)
}
}
return nil
}
func (a *concreteApplier) Apply(currentApplySpec, 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.KeepOnly(append(currentApplySpec.Jobs(), 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(append(currentApplySpec.Packages(), 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
}