Skip to content

Commit 328e478

Browse files
pbuskonicolasbender
authored andcommitted
WIP: Fix passing lifecycle
Co-authored-by: Nicolas Bender <[email protected]>
1 parent 1d6ff88 commit 328e478

File tree

12 files changed

+135
-97
lines changed

12 files changed

+135
-97
lines changed

app/actions/build_create.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def create_and_stage(package:, lifecycle:, metadata: nil, start_after_staging: f
9595

9696
def requested_buildpacks_disabled!(lifecycle)
9797
return if lifecycle.type == Lifecycles::DOCKER
98-
return if lifecycle.type == Lifecycles::CNB
9998

10099
admin_buildpack_records = lifecycle.buildpack_infos.map(&:buildpack_record).compact
101100
disabled_buildpacks = admin_buildpack_records.reject(&:enabled)

app/fetchers/buildpack_lifecycle_fetcher.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
require 'cloud_controller/diego/lifecycles/lifecycles'
2+
13
module VCAP::CloudController
24
class BuildpackLifecycleFetcher
35
class << self
4-
def fetch(buildpack_names, stack_name)
6+
def fetch(buildpack_names, stack_name, lifecycle = VCAP::CloudController::Lifecycles::BUILDPACK)
57
{
68
stack: Stack.find(name: stack_name),
7-
buildpack_infos: ordered_buildpacks(buildpack_names, stack_name)
9+
buildpack_infos: ordered_buildpacks(buildpack_names, stack_name, lifecycle)
810
}
911
end
1012

1113
private
1214

13-
def ordered_buildpacks(buildpack_names, stack_name)
14-
buildpacks_with_stacks, buildpacks_without_stacks = Buildpack.list_admin_buildpacks(stack_name).partition(&:stack)
15+
def ordered_buildpacks(buildpack_names, stack_name, lifecycle)
16+
buildpacks_with_stacks, buildpacks_without_stacks = Buildpack.list_admin_buildpacks(stack_name, lifecycle).partition(&:stack)
1517

1618
buildpack_names.map do |buildpack_name|
1719
buildpack_record = buildpacks_with_stacks.find { |b| b.name == buildpack_name } || buildpacks_without_stacks.find { |b| b.name == buildpack_name }

app/messages/app_manifest_message.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def self.underscore_keys(hash)
7272
validate :validate_buildpack_and_buildpacks_combination!
7373
validate :validate_docker_enabled!
7474
validate :validate_cnb_enabled!
75-
validate :validate_cnb_buildpacks!
7675
validate :validate_docker_buildpacks_combination!
7776
validate :validate_service_bindings_message!, if: ->(record) { record.requested?(:services) }
7877
validate :validate_env_update_message!, if: ->(record) { record.requested?(:env) }
@@ -291,7 +290,7 @@ def cnb_lifecycle_data
291290
requested_buildpacks = @buildpacks
292291
elsif requested?(:buildpack)
293292
requested_buildpacks = []
294-
requested_buildpacks.push(@buildpack)
293+
requested_buildpacks.push(@buildpack) unless should_autodetect?(@buildpack)
295294
end
296295

297296
{
@@ -485,13 +484,6 @@ def validate_cnb_enabled!
485484
errors.add(:base, e.message)
486485
end
487486

488-
def validate_cnb_buildpacks!
489-
return unless @lifecycle == 'cnb'
490-
return if requested?(:lifecycle) && (requested?(:buildpack) || requested?(:buildpacks))
491-
492-
errors.add(:base, 'Buildpack(s) must be specified when using Cloud Native Buildpacks')
493-
end
494-
495487
def validate_docker_buildpacks_combination!
496488
return unless requested?(:docker) && (requested?(:buildpack) || requested?(:buildpacks))
497489

app/messages/buildpack_upload_message.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
require 'messages/base_message'
22

33
module VCAP::CloudController
4+
GZIP_MIME = Regexp.new("\x1F\x8B\x08".force_encoding("binary"))
5+
ZIP_MIME = Regexp.new("PK\x03\x04".force_encoding("binary"))
6+
47
class BuildpackUploadMessage < BaseMessage
58
class MissingFilePathError < StandardError; end
6-
79
register_allowed_keys %i[bits_path bits_name upload_start_time]
810

911
validates_with NoAdditionalKeysValidator
1012

1113
validate :nginx_fields
1214
validate :bits_path_in_tmpdir
13-
validate :is_zip
15+
validate :is_archive
1416
validate :is_not_empty
1517
validate :missing_file_path
1618

@@ -43,10 +45,16 @@ def tmpdir
4345
VCAP::CloudController::Config.config.get(:directories, :tmpdir)
4446
end
4547

46-
def is_zip
48+
def is_archive
4749
return unless bits_name
50+
return unless bits_path
51+
52+
case IO.read(bits_path, 4)
53+
when /^#{VCAP::CloudController::GZIP_MIME}/, /^#{VCAP::CloudController::ZIP_MIME}/
54+
else
55+
errors.add(:base, "#{bits_name} is not a zip or gzip archive")
56+
end
4857

49-
errors.add(:base, "#{bits_name} is not a zip") unless File.extname(bits_name) == '.zip'
5058
end
5159

5260
def missing_file_path

app/models/runtime/buildpack.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'cloud_controller/diego/lifecycles/lifecycles'
2+
13
module VCAP::CloudController
24
class Buildpack < Sequel::Model
35
plugin :list, scope: :lifecycle
@@ -20,7 +22,7 @@ def self.user_visibility_filter(_user)
2022
full_dataset_filter
2123
end
2224

23-
def self.list_admin_buildpacks(stack_name=nil, lifecycle='buildpack')
25+
def self.list_admin_buildpacks(stack_name=nil, lifecycle=VCAP::CloudController::Lifecycles::BUILDPACK)
2426
scoped = exclude(key: nil).exclude(key: '')
2527
scoped = scoped.filter(:lifecycle => lifecycle)
2628
if stack_name.present?

app/models/runtime/cnb_lifecycle_data_model.rb

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def buildpacks
4444
def buildpack_models
4545
if buildpack_lifecycle_buildpacks.present?
4646
buildpack_lifecycle_buildpacks.map do |buildpack|
47-
CustomBuildpack.new(buildpack.name)
47+
Buildpack.find(name: buildpack.name) || CustomBuildpack.new(buildpack.name)
4848
end
4949
else
5050
[]
@@ -64,11 +64,7 @@ def first_custom_buildpack_url
6464
end
6565

6666
def using_custom_buildpack?
67-
true
68-
end
69-
70-
def attributes_from_buildpack(buildpack_name)
71-
{ buildpack_url: buildpack_name, admin_buildpack_name: nil }
67+
buildpack_lifecycle_buildpacks.any?(&:custom?)
7268
end
7369

7470
def to_hash
@@ -96,5 +92,44 @@ def credentials
9692
def credentials=(creds)
9793
self.registry_credentials_json = Oj.dump(creds)
9894
end
95+
96+
private
97+
98+
def attributes_from_buildpack_name(buildpack_name)
99+
if UriUtils.is_buildpack_uri?(buildpack_name)
100+
{ buildpack_url: buildpack_name, admin_buildpack_name: nil }
101+
else
102+
{ buildpack_url: nil, admin_buildpack_name: buildpack_name }
103+
end
104+
end
105+
106+
def attributes_from_buildpack_key(key)
107+
admin_buildpack = Buildpack.find(key:)
108+
if admin_buildpack
109+
{ buildpack_url: nil, admin_buildpack_name: admin_buildpack.name }
110+
elsif UriUtils.is_buildpack_uri?(key)
111+
{ buildpack_url: key, admin_buildpack_name: nil }
112+
else
113+
{} # Will fail a validity check downstream
114+
end
115+
end
116+
117+
def attributes_from_buildpack_hash(buildpack)
118+
{
119+
buildpack_name: buildpack[:name],
120+
version: buildpack[:version]
121+
}.merge(buildpack[:key] ? attributes_from_buildpack_key(buildpack[:key]) : attributes_from_buildpack_name(buildpack[:name]))
122+
end
123+
124+
def attributes_from_buildpack(buildpack)
125+
if buildpack.is_a?(String)
126+
attributes_from_buildpack_name buildpack
127+
elsif buildpack.is_a?(Hash)
128+
attributes_from_buildpack_hash buildpack
129+
else
130+
# Don't set anything -- this will fail later on a validity check
131+
{}
132+
end
133+
end
99134
end
100135
end

lib/cloud_controller/diego/buildpack/staging_action_builder.rb

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,7 @@ module Diego
88
module Buildpack
99
class StagingActionBuilder < VCAP::CloudController::Diego::StagingActionBuilder
1010
def initialize(config, staging_details, lifecycle_data)
11-
super(config, staging_details, lifecycle_data, 'buildpack', '/tmp/app', '/tmp/output-cache')
12-
end
13-
14-
def additional_image_layers
15-
lifecycle_data[:buildpacks].
16-
reject { |buildpack| buildpack[:name] == 'custom' }.
17-
map do |buildpack|
18-
layer = {
19-
name: buildpack[:name],
20-
url: buildpack[:url],
21-
destination_path: buildpack_path(buildpack[:key]),
22-
layer_type: ::Diego::Bbs::Models::ImageLayer::Type::SHARED,
23-
media_type: ::Diego::Bbs::Models::ImageLayer::MediaType::ZIP
24-
}
25-
if buildpack[:sha256]
26-
layer[:digest_algorithm] = ::Diego::Bbs::Models::ImageLayer::DigestAlgorithm::SHA256
27-
layer[:digest_value] = buildpack[:sha256]
28-
end
29-
30-
::Diego::Bbs::Models::ImageLayer.new(layer.compact)
31-
end
32-
end
33-
34-
def cached_dependencies
35-
return nil if @config.get(:diego, :enable_declarative_asset_downloads)
36-
37-
dependencies = [
38-
::Diego::Bbs::Models::CachedDependency.new(
39-
from: LifecycleBundleUriGenerator.uri(config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]),
40-
to: '/tmp/lifecycle',
41-
cache_key: "buildpack-#{lifecycle_stack}-lifecycle"
42-
)
43-
]
44-
45-
others = lifecycle_data[:buildpacks].map do |buildpack|
46-
next if buildpack[:name] == 'custom'
47-
48-
buildpack_dependency = {
49-
name: buildpack[:name],
50-
from: buildpack[:url],
51-
to: buildpack_path(buildpack[:key]),
52-
cache_key: buildpack[:key]
53-
}
54-
if buildpack[:sha256]
55-
buildpack_dependency[:checksum_algorithm] = 'sha256'
56-
buildpack_dependency[:checksum_value] = buildpack[:sha256]
57-
end
58-
59-
::Diego::Bbs::Models::CachedDependency.new(buildpack_dependency.compact)
60-
end.compact
61-
62-
dependencies.concat(others)
11+
super(config, staging_details, lifecycle_data, 'buildpack', '/tmp/app', '/tmp/output-cache', ::Diego::Bbs::Models::ImageLayer::MediaType::ZIP)
6312
end
6413

6514
def task_environment_variables
@@ -96,14 +45,6 @@ def platform_options_env
9645

9746
arr
9847
end
99-
100-
def buildpack_path(buildpack_key)
101-
if config.get(:staging, :legacy_md5_buildpack_paths_enabled)
102-
"/tmp/buildpacks/#{OpenSSL::Digest::MD5.hexdigest(buildpack_key)}"
103-
else
104-
"/tmp/buildpacks/#{Digest::XXH64.hexdigest(buildpack_key)}"
105-
end
106-
end
10748
end
10849
end
10950
end

lib/cloud_controller/diego/cnb/staging_action_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Diego
88
module CNB
99
class StagingActionBuilder < VCAP::CloudController::Diego::StagingActionBuilder
1010
def initialize(config, staging_details, lifecycle_data)
11-
super(config, staging_details, lifecycle_data, 'cnb', '/home/vcap/workspace', '/tmp/cache-output.tgz')
11+
super(config, staging_details, lifecycle_data, 'cnb', '/home/vcap/workspace', '/tmp/cache-output.tgz', ::Diego::Bbs::Models::ImageLayer::MediaType::TGZ)
1212
end
1313

1414
def cached_dependencies

lib/cloud_controller/diego/docker/staging_action_builder.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def cached_dependencies
6767
]
6868
end
6969

70+
def additional_image_layers
71+
[]
72+
end
73+
7074
def stack
7175
"preloaded:#{config.get(:diego, :docker_staging_stack)}"
7276
end

lib/cloud_controller/diego/lifecycles/app_buildpack_lifecycle.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AppBuildpackLifecycle < AppBaseLifecycle
88
def initialize(message)
99
@message = message
1010

11-
db_result = BuildpackLifecycleFetcher.fetch(buildpacks, stack)
11+
db_result = BuildpackLifecycleFetcher.fetch(buildpacks, stack, type)
1212
@validator = BuildpackLifecycleDataValidator.new({
1313
buildpack_infos: db_result[:buildpack_infos],
1414
stack: db_result[:stack]

0 commit comments

Comments
 (0)