-
Notifications
You must be signed in to change notification settings - Fork 355
/
buildpack_installer.rb
75 lines (63 loc) · 2.1 KB
/
buildpack_installer.rb
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
module VCAP::CloudController
module Jobs
module Runtime
class BuildpackInstaller < VCAP::CloudController::Jobs::CCJob
attr_accessor :name, :file, :opts
def initialize(name, file, opts)
@name = name
@file = file
@opts = opts
end
def perform
logger = Steno.logger('cc.background')
logger.info "Installing buildpack #{name}"
buildpack = find_existing_buildpack
if buildpack.nil?
buildpack = Buildpack.create(name: name, stack: 'unknown')
created = true
elsif buildpack.locked
logger.info "Buildpack #{name} locked, not updated"
return
end
begin
buildpack_uploader.upload_buildpack(buildpack, file, File.basename(file))
rescue => e
if created
buildpack.destroy
end
raise e
end
buildpack.update(opts)
logger.info "Buildpack #{name} installed or updated"
rescue AmbiguousBuildpackException => abe
logger.error abe.message
rescue => e
logger.error("Buildpack #{name} failed to install or update. Error: #{e.inspect}")
raise e
end
def max_attempts
1
end
def job_name_in_configuration
:buildpack_installer
end
def buildpack_uploader
buildpack_blobstore = CloudController::DependencyLocator.instance.buildpack_blobstore
UploadBuildpack.new(buildpack_blobstore)
end
private
def find_existing_buildpack
stack = buildpack_uploader.extract_stack_from_buildpack(file)
return Buildpack.find(name: name, stack: stack) if stack.to_s != ''
buildpacks = Buildpack.where(name: name)
if buildpacks.count > 1
raise AmbiguousBuildpackException.new("Buildpack #{name} has #{buildpacks.count} stacks, not updated")
end
buildpacks.first
end
class AmbiguousBuildpackException < RuntimeError
end
end
end
end
end