forked from cloudfoundry/cloud_controller_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpack_installer.rb
81 lines (69 loc) · 2.45 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
76
77
78
79
80
81
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}"
buildpacks = find_existing_buildpacks
if buildpacks.count > 1
logger.error "Update failed: Unable to determine buildpack to update as there are multiple buildpacks named #{name} for different stacks."
return
end
buildpack = buildpacks.first
if buildpack.nil?
buildpacks_lock = Locking[name: 'buildpacks']
buildpacks_lock.db.transaction do
buildpacks_lock.lock!
buildpack = Buildpack.create(name: name)
end
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 => 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_buildpacks
stack = VCAP::CloudController::Buildpacks::StackNameExtractor.extract_from_file(file)
if stack.present?
buildpacks_by_stack = Buildpack.where(name: name, stack: stack)
return buildpacks_by_stack if buildpacks_by_stack.any?
return Buildpack.where(name: name, stack: nil)
# XTEAM: We were reconsidering whether or not we should overwrite buildpacks of unknown stack during install
end
Buildpack.where(name: name)
end
end
end
end
end