-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathdesired_lrp_builder.rb
More file actions
165 lines (137 loc) · 6.39 KB
/
desired_lrp_builder.rb
File metadata and controls
165 lines (137 loc) · 6.39 KB
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
module VCAP::CloudController
module Diego
module Buildpack
class DesiredLrpBuilder
include ::Diego::ActionBuilder
class InvalidStack < StandardError; end
attr_reader :start_command, :action_user
def initialize(config, opts)
@config = config
@stack = opts[:stack]
@droplet_uri = opts[:droplet_uri]
@process_guid = opts[:process_guid]
@droplet_hash = opts[:droplet_hash]
@ports = opts[:ports]
@checksum_algorithm = opts[:checksum_algorithm]
@checksum_value = opts[:checksum_value]
@start_command = opts[:start_command]
@action_user = opts[:action_user]
@additional_container_env_vars = opts[:additional_container_env_vars]
end
def cached_dependencies
return nil if @config.get(:diego, :enable_declarative_asset_downloads)
lifecycle_bundle_key = :"buildpack/#{@stack}"
lifecycle_bundle = @config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]
# If custom stack doesn't have a bundle, use the default stack's bundle
if !lifecycle_bundle && @stack.is_a?(String) && is_custom_stack?(@stack)
default_stack = Stack.default.name
lifecycle_bundle_key = :"buildpack/#{default_stack}"
lifecycle_bundle = @config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]
end
raise InvalidStack.new("no compiler defined for requested stack '#{@stack}'") unless lifecycle_bundle
[
::Diego::Bbs::Models::CachedDependency.new(
from: LifecycleBundleUriGenerator.uri(lifecycle_bundle),
to: '/tmp/lifecycle',
cache_key: "buildpack-#{@stack}-lifecycle"
)
]
end
def root_fs
# Handle custom stacks (docker:// URLs)
if @stack.is_a?(String) && is_custom_stack?(@stack)
return normalize_stack_url(@stack)
end
@stack_obj ||= Stack.find(name: @stack)
raise CloudController::Errors::ApiError.new_from_details('StackNotFound', @stack) unless @stack_obj
"preloaded:#{@stack_obj.run_rootfs_image}"
end
def setup
return nil if @config.get(:diego, :enable_declarative_asset_downloads) && @checksum_algorithm == 'sha256'
serial([
::Diego::Bbs::Models::DownloadAction.new({
artifact: 'droplet',
from: @droplet_uri,
to: '.',
cache_key: "droplets-#{@process_guid}",
user: action_user,
checksum_algorithm: @checksum_algorithm,
checksum_value: @checksum_value
}.compact)
])
end
def image_layers
return [] unless @config.get(:diego, :enable_declarative_asset_downloads)
lifecycle_bundle_key = :"buildpack/#{@stack}"
lifecycle_bundle = @config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]
# If custom stack doesn't have a bundle, use the default stack's bundle
if !lifecycle_bundle && @stack.is_a?(String) && is_custom_stack?(@stack)
default_stack = Stack.default.name
lifecycle_bundle_key = :"buildpack/#{default_stack}"
lifecycle_bundle = @config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]
end
raise InvalidStack.new("no compiler defined for requested stack '#{@stack}'") unless lifecycle_bundle
destination = @config.get(:diego, :droplet_destinations)[@stack.to_sym]
# For custom stacks, use a default destination if not configured
if !destination && @stack.is_a?(String) && is_custom_stack?(@stack)
default_stack = Stack.default.name
destination = @config.get(:diego, :droplet_destinations)[default_stack.to_sym]
end
raise InvalidStack.new("no droplet destination defined for requested stack '#{@stack}'") unless destination
layers = [
::Diego::Bbs::Models::ImageLayer.new(
name: "buildpack-#{@stack}-lifecycle",
url: LifecycleBundleUriGenerator.uri(lifecycle_bundle),
destination_path: '/tmp/lifecycle',
layer_type: ::Diego::Bbs::Models::ImageLayer::Type::SHARED,
media_type: ::Diego::Bbs::Models::ImageLayer::MediaType::TGZ
)
]
if @checksum_algorithm == 'sha256'
layers << ::Diego::Bbs::Models::ImageLayer.new({
name: 'droplet',
url: UriUtils.uri_escape(@droplet_uri),
destination_path: destination,
layer_type: ::Diego::Bbs::Models::ImageLayer::Type::EXCLUSIVE,
media_type: ::Diego::Bbs::Models::ImageLayer::MediaType::TGZ,
digest_value: @checksum_value,
digest_algorithm: ::Diego::Bbs::Models::ImageLayer::DigestAlgorithm::SHA256
}.compact)
end
layers
end
def global_environment_variables
[::Diego::Bbs::Models::EnvironmentVariable.new(name: 'LANG', value: DEFAULT_LANG)] + @additional_container_env_vars
end
def ports
return @ports if @ports.present?
[DEFAULT_APP_PORT]
end
def port_environment_variables
[
::Diego::Bbs::Models::EnvironmentVariable.new(name: 'PORT', value: ports.first.to_s),
::Diego::Bbs::Models::EnvironmentVariable.new(name: 'VCAP_APP_PORT', value: ports.first.to_s),
::Diego::Bbs::Models::EnvironmentVariable.new(name: 'VCAP_APP_HOST', value: '0.0.0.0')
]
end
def privileged?
@config.get(:diego, :use_privileged_containers_for_running)
end
private
def is_custom_stack?(stack_name)
# Check for various container registry URL formats
return true if stack_name.include?('docker://')
return true if stack_name.match?(%r{^https?://}) # Any https/http URL
return true if stack_name.include?('.') # Any string with a dot (likely a registry)
false
end
def normalize_stack_url(stack_url)
return stack_url if stack_url.start_with?('docker://')
return stack_url.sub(/^https?:\/\//, 'docker://') if stack_url.match?(%r{^https?://})
return "docker://#{stack_url}" if stack_url.include?('.')
stack_url
end
end
end
end
end