Skip to content

Commit a43eaa7

Browse files
Harsh Patelnebhale
authored andcommitted
Downloadable Configuration
If APPD_CONF_HTTP_URL is specified, download configuration files from that endpoint and map them to the corresponding agent directory. [#753] Signed-off-by: Ben Hale <[email protected]>
1 parent 349e93b commit a43eaa7

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

lib/java_buildpack/framework/app_dynamics_agent.rb

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ module Framework
2424

2525
# Encapsulates the functionality for enabling zero-touch AppDynamics support.
2626
class AppDynamicsAgent < JavaBuildpack::Component::VersionedDependencyComponent
27+
@conf_files = [
28+
'logging/log4j2.xml',
29+
'logging/log4j.xml',
30+
'app-agent-config.xml',
31+
'controller-info.xml',
32+
'service-endpoint.xml',
33+
'transactions.xml'
34+
]
35+
36+
def initialize(context)
37+
super(context)
38+
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger AppDynamicsAgent
39+
end
2740

2841
# (see JavaBuildpack::Component::BaseComponent#compile)
2942
def compile
@@ -34,7 +47,7 @@ def compile
3447
default_conf_dir = resources_dir + @droplet.component_id + 'defaults'
3548

3649
copy_appd_default_configuration(default_conf_dir)
37-
50+
override_default_config_if_applicable
3851
@droplet.copy_resources
3952
end
4053

@@ -129,7 +142,57 @@ def copy_appd_default_configuration(default_conf_dir)
129142
end
130143
end
131144

132-
end
145+
# Check if configuration file exists on the server before download
146+
# @param [ResourceURI] uri URI of the remote configuration server
147+
# @param [ConfigFileName] conf_file Name of the configuration file
148+
# @return [Boolean] returns true if files exists on path specified by APPD_CONF_HTTP_URL, false otherwise
149+
def check_if_resource_exists(resource_uri, conf_file)
150+
# check if resource exists on remote server
151+
begin
152+
response = Net::HTTP.start(resource_uri.host, resource_uri.port) do |http|
153+
http.request_head(resource_uri)
154+
end
155+
rescue StandardError => e
156+
@logger.error { "Request failure: #{e.message}" }
157+
return false
158+
end
133159

160+
case response
161+
when Net::HTTPSuccess
162+
return true
163+
when Net::HTTPRedirection
164+
location = response['location']
165+
@logger.info { "redirected to #{location}" }
166+
return check_if_resource_exists(location, conf_file)
167+
else
168+
@logger.info { "Could not retrieve #{resource_uri}. Code: #{response.code} Message: #{response.message}" }
169+
return false
170+
end
171+
end
172+
173+
# Check for configuration files on a remote server. If found, copy to conf dir under each ver* dir
174+
# @return [Void]
175+
def override_default_config_if_applicable
176+
return unless @application.environment['APPD_CONF_HTTP_URL']
177+
178+
agent_root = @application.environment['APPD_CONF_HTTP_URL'].chomp('/') + '/java/'
179+
@logger.info { "Downloading override configuration files from #{agent_root}" }
180+
JavaBuildpack::Framework::AppDynamicsAgent.instance_variable_get(:@conf_files).each do |conf_file|
181+
uri = URI(agent_root + conf_file)
182+
183+
# `download()` uses retries with exponential backoff which is expensive
184+
# for situations like 404 File not Found. Also, `download()` doesn't expose
185+
# an api to disable retries, which makes this check necessary to prevent
186+
# long install times.
187+
next unless check_if_resource_exists(uri, conf_file)
188+
189+
download(false, uri.to_s) do |file|
190+
Dir.glob(@droplet.sandbox + 'ver*') do |target_directory|
191+
FileUtils.cp_r file, target_directory + '/conf/' + conf_file
192+
end
193+
end
194+
end
195+
end
196+
end
134197
end
135198
end

spec/java_buildpack/framework/app_dynamics_agent_spec.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,25 @@
141141
expect(java_opts).to include('-Dappdynamics.agent.accountAccessKey=test-account-access-key')
142142
end
143143
end
144-
end
145144

146-
end
145+
context do
147146

147+
let(:environment) { { 'APPD_CONF_HTTP_URL' => 'http://foo.com' } }
148+
let(:conf_files) { described_class.instance_variable_get(:@conf_files) }
149+
150+
it 'sets APPD_CONF_HTTP_URL env var to download config files from',
151+
cache_fixture: 'stub-app-dynamics-agent.zip' do
152+
conf_files.each do |file|
153+
uri = "http://foo.com/java/#{file}"
154+
allow(application_cache).to receive(:get)
155+
.with(uri)
156+
stub_request(:head, uri)
157+
.with(headers: { 'Accept' => '*/*', 'Host' => 'foo.com', 'User-Agent' => 'Ruby' })
158+
.to_return(status: 200, body: '', headers: {})
159+
end
160+
component.compile
161+
end
162+
end
163+
end
164+
end
148165
end

0 commit comments

Comments
 (0)