forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendered_job_applier.go
266 lines (222 loc) · 7.33 KB
/
rendered_job_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package jobs
import (
"fmt"
"os"
"path"
"strings"
boshbc "github.com/cloudfoundry/bosh-agent/agent/applier/bundlecollection"
"github.com/cloudfoundry/bosh-agent/agent/applier/models"
"github.com/cloudfoundry/bosh-agent/agent/applier/packages"
boshjobsuper "github.com/cloudfoundry/bosh-agent/jobsupervisor"
"github.com/cloudfoundry/bosh-agent/settings/directories"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const logTag = "renderedJobApplier"
type renderedJobApplier struct {
dirProvider directories.Provider
jobsBc boshbc.BundleCollection
jobSupervisor boshjobsuper.JobSupervisor
packageApplierProvider packages.ApplierProvider
blobstore boshblob.DigestBlobstore
compressor boshcmd.Compressor
fs boshsys.FileSystem
logger boshlog.Logger
}
func NewRenderedJobApplier(
dirProvider directories.Provider,
jobsBc boshbc.BundleCollection,
jobSupervisor boshjobsuper.JobSupervisor,
packageApplierProvider packages.ApplierProvider,
blobstore boshblob.DigestBlobstore,
compressor boshcmd.Compressor,
fs boshsys.FileSystem,
logger boshlog.Logger,
) Applier {
return &renderedJobApplier{
dirProvider: dirProvider,
jobsBc: jobsBc,
jobSupervisor: jobSupervisor,
packageApplierProvider: packageApplierProvider,
blobstore: blobstore,
compressor: compressor,
fs: fs,
logger: logger,
}
}
func (s renderedJobApplier) Prepare(job models.Job) error {
s.logger.Debug(logTag, "Preparing job %v", job)
jobBundle, err := s.jobsBc.Get(job)
if err != nil {
return bosherr.WrapError(err, "Getting job bundle")
}
jobInstalled, err := jobBundle.IsInstalled()
if err != nil {
return bosherr.WrapError(err, "Checking if job is installed")
}
if !jobInstalled {
err := s.downloadAndInstall(job, jobBundle)
if err != nil {
return err
}
}
return nil
}
func (s *renderedJobApplier) Apply(job models.Job) error {
s.logger.Debug(logTag, "Applying job %v", job)
err := s.Prepare(job)
if err != nil {
return bosherr.WrapError(err, "Preparing job")
}
if err := job.CreateDirectories(s.fs, s.dirProvider); err != nil {
return bosherr.WrapErrorf(err, "Creating directories for job %s", job.Name)
}
jobBundle, err := s.jobsBc.Get(job)
if err != nil {
return bosherr.WrapError(err, "Getting job bundle")
}
_, _, err = jobBundle.Enable()
if err != nil {
return bosherr.WrapError(err, "Enabling job")
}
return s.applyPackages(job)
}
func (s *renderedJobApplier) downloadAndInstall(job models.Job, jobBundle boshbc.Bundle) error {
tmpDir, err := s.fs.TempDir("bosh-agent-applier-jobs-RenderedJobApplier-Apply")
if err != nil {
return bosherr.WrapError(err, "Getting temp dir")
}
defer func() {
if err = s.fs.RemoveAll(tmpDir); err != nil {
s.logger.Warn(logTag, "Failed to clean up temp directory: %s", err.Error())
}
}()
file, err := s.blobstore.Get(job.Source.BlobstoreID, job.Source.Sha1)
if err != nil {
return bosherr.WrapError(err, "Getting job source from blobstore")
}
defer func() {
if err = s.blobstore.CleanUp(file); err != nil {
s.logger.Warn(logTag, "Failed to clean up blobstore blob: %s", err.Error())
}
}()
err = s.compressor.DecompressFileToDir(file, tmpDir, boshcmd.CompressorOptions{})
if err != nil {
return bosherr.WrapError(err, "Decompressing files to temp dir")
}
binPath := path.Join(tmpDir, job.Source.PathInArchive, "bin") + "/"
err = s.fs.Walk(path.Join(tmpDir, job.Source.PathInArchive), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if info.IsDir() || strings.HasPrefix(path, binPath) {
err := s.fs.Chown(path, "root:vcap")
if err != nil {
return bosherr.WrapError(err, "Failed to chown dir")
}
return s.fs.Chmod(path, os.FileMode(0750))
} else {
err := s.fs.Chown(path, "root:vcap")
if err != nil {
return bosherr.WrapError(err, "Failed to chown dir")
}
return s.fs.Chmod(path, os.FileMode(0640))
}
})
if err != nil {
return bosherr.WrapError(err, "Correcting file permissions")
}
_, _, err = jobBundle.Install(path.Join(tmpDir, job.Source.PathInArchive))
if err != nil {
return bosherr.WrapError(err, "Installing job bundle")
}
return nil
}
// applyPackages keeps job specific packages directory up-to-date with installed packages.
// (e.g. /var/vcap/jobs/job-a/packages/pkg-a has symlinks to /var/vcap/packages/pkg-a)
func (s *renderedJobApplier) applyPackages(job models.Job) error {
packageApplier := s.packageApplierProvider.JobSpecific(job.Name)
for _, pkg := range job.Packages {
err := packageApplier.Apply(pkg)
if err != nil {
return bosherr.WrapErrorf(err, "Applying package %s for job %s", pkg.Name, job.Name)
}
}
err := packageApplier.KeepOnly(job.Packages)
if err != nil {
return bosherr.WrapErrorf(err, "Keeping only needed packages for job %s", job.Name)
}
return nil
}
func (s *renderedJobApplier) Configure(job models.Job, jobIndex int) (err error) {
s.logger.Debug(logTag, "Configuring job %v with index %d", job, jobIndex)
jobBundle, err := s.jobsBc.Get(job)
if err != nil {
err = bosherr.WrapError(err, "Getting job bundle")
return
}
fs, jobDir, err := jobBundle.GetInstallPath()
if err != nil {
err = bosherr.WrapError(err, "Looking up job directory")
return
}
monitFilePath := path.Join(jobDir, "monit")
if fs.FileExists(monitFilePath) {
err = s.jobSupervisor.AddJob(job.Name, jobIndex, monitFilePath)
if err != nil {
err = bosherr.WrapError(err, "Adding monit configuration")
return
}
}
monitFilePaths, err := fs.Glob(path.Join(jobDir, "*.monit"))
if err != nil {
err = bosherr.WrapError(err, "Looking for additional monit files")
return
}
for _, monitFilePath := range monitFilePaths {
label := strings.Replace(path.Base(monitFilePath), ".monit", "", 1)
subJobName := fmt.Sprintf("%s_%s", job.Name, label)
err = s.jobSupervisor.AddJob(subJobName, jobIndex, monitFilePath)
if err != nil {
err = bosherr.WrapErrorf(err, "Adding additional monit configuration %s", label)
return
}
}
return nil
}
func (s *renderedJobApplier) KeepOnly(jobs []models.Job) error {
s.logger.Debug(logTag, "Keeping only jobs %v", jobs)
installedBundles, err := s.jobsBc.List()
if err != nil {
return bosherr.WrapError(err, "Retrieving installed bundles")
}
for _, installedBundle := range installedBundles {
var shouldKeep bool
for _, job := range jobs {
jobBundle, err := s.jobsBc.Get(job)
if err != nil {
return bosherr.WrapError(err, "Getting job bundle")
}
if jobBundle == installedBundle {
shouldKeep = true
break
}
}
if !shouldKeep {
err = installedBundle.Disable()
if err != nil {
return bosherr.WrapError(err, "Disabling job bundle")
}
// If we uninstall the bundle first, and the disable failed (leaving the symlink),
// then the next time bundle collection will not include bundle in its list
// which means that symlink will never be deleted.
err = installedBundle.Uninstall()
if err != nil {
return bosherr.WrapError(err, "Uninstalling job bundle")
}
}
}
return nil
}