@@ -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
135198end
0 commit comments