-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathdynatrace_one_agent.rb
219 lines (165 loc) · 6.84 KB
/
dynatrace_one_agent.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# frozen_string_literal: true
# Cloud Foundry Java Buildpack
# Copyright 2013-2020 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fileutils'
require 'java_buildpack/component/versioned_dependency_component'
require 'java_buildpack/framework'
require 'java_buildpack/util/cache/internet_availability'
require 'java_buildpack/util/to_b'
require 'json'
module JavaBuildpack
module Framework
# Encapsulates the functionality for enabling zero-touch Dynatrace SaaS/Managed support.
class DynatraceOneAgent < JavaBuildpack::Component::VersionedDependencyComponent
# Creates an instance
#
# @param [Hash] context a collection of utilities used the component
def initialize(context)
@application = context[:application]
@component_name = self.class.to_s.space_case
@configuration = context[:configuration]
@droplet = context[:droplet]
@version, @uri = agent_download_url if supports?
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger DynatraceOneAgent
end
# (see JavaBuildpack::Component::BaseComponent#compile)
def compile
JavaBuildpack::Util::Cache::InternetAvailability.instance.available(
true, 'The Dynatrace One Agent download location is always accessible'
) do
download(@version, @uri) { |file| expand file }
end
@droplet.copy_resources
rescue StandardError => e
raise unless skip_errors?
@logger.error { "Dynatrace OneAgent download failed: #{e}" }
@logger.warn { "Agent injection disabled because of #{SKIP_ERRORS} credential is set to true!" }
FileUtils.mkdir_p(error_file.parent)
File.write(error_file, e.to_s)
end
# (see JavaBuildpack::Component::BaseComponent#release)
def release
if error_file.exist?
@logger.warn { "Dynatrace OneAgent injection disabled due to download error: #{File.read(error_file)}" }
return
end
manifest = agent_manifest
environment_variables = @droplet.environment_variables
environment_variables.add_environment_variable(LD_PRELOAD, agent_path(manifest))
File.delete(@droplet.sandbox + 'agent/dt_fips_disabled.flag') if enable_fips?
dynatrace_environment_variables(manifest)
end
protected
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
def supports?
@application.services.one_service? FILTER, APITOKEN, ENVIRONMENTID
end
private
APIURL = 'apiurl'
APITOKEN = 'apitoken'
ENABLE_FIPS = 'enablefips'
DT_APPLICATION_ID = 'DT_APPLICATIONID'
DT_CONNECTION_POINT = 'DT_CONNECTION_POINT'
DT_TENANT = 'DT_TENANT'
DT_TENANTTOKEN = 'DT_TENANTTOKEN'
DT_LOGSTREAM = 'DT_LOGSTREAM'
DT_NETWORK_ZONE = 'DT_NETWORK_ZONE'
LD_PRELOAD = 'LD_PRELOAD'
ENVIRONMENTID = 'environmentid'
FILTER = /dynatrace/.freeze
NETWORKZONE = 'networkzone'
SKIP_ERRORS = 'skiperrors'
private_constant :APIURL, :APITOKEN, :ENABLE_FIPS, :DT_APPLICATION_ID, :DT_CONNECTION_POINT, :DT_NETWORK_ZONE,
:DT_LOGSTREAM, :DT_TENANT, :DT_TENANTTOKEN, :LD_PRELOAD, :ENVIRONMENTID, :FILTER, :NETWORKZONE,
:SKIP_ERRORS
def agent_download_url
download_uri = "#{api_base_url(credentials)}/v1/deployment/installer/agent/unix/paas/latest?include=java" \
'&bitness=64' \
"&Api-Token=#{credentials[APITOKEN]}"
download_uri += "&networkZone=#{networkzone}" if networkzone?
['latest', download_uri]
end
def agent_manifest
JSON.parse(File.read(@droplet.sandbox + 'manifest.json'))
end
def agent_path(manifest)
technologies = manifest['technologies']
java_binaries = technologies['process']['linux-x86-64']
loader = java_binaries.find { |bin| bin['binarytype'] == 'primary' }
@droplet.sandbox + loader['path']
end
def api_base_url(credentials)
credentials[APIURL] || "https://#{credentials[ENVIRONMENTID]}.live.dynatrace.com/api"
end
def application_id
@application.details['application_name']
end
def application_id?
@application.environment.key?(DT_APPLICATION_ID)
end
def credentials
@application.services.find_service(FILTER, APITOKEN, ENVIRONMENTID)['credentials']
end
def dynatrace_environment_variables(manifest)
environment_variables = @droplet.environment_variables
environment_variables
.add_environment_variable(DT_TENANT, credentials[ENVIRONMENTID])
.add_environment_variable(DT_TENANTTOKEN, tenanttoken(manifest))
.add_environment_variable(DT_CONNECTION_POINT, endpoints(manifest))
environment_variables.add_environment_variable(DT_APPLICATION_ID, application_id) unless application_id?
environment_variables.add_environment_variable(DT_NETWORK_ZONE, credentials[NETWORKZONE]) if networkzone?
environment_variables.add_environment_variable(DT_LOGSTREAM, 'stdout') unless logstream?
end
def endpoints(manifest)
"\"#{manifest['communicationEndpoints'].join(';')}\""
end
def error_file
@droplet.sandbox + 'dynatrace_download_error'
end
def expand(file)
with_timing "Expanding Dynatrace OneAgent to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do
Dir.mktmpdir do |root|
root_path = Pathname.new(root)
shell "unzip -qq #{file.path} -d #{root_path} 2>&1"
unpack_agent root_path
end
end
end
def networkzone
credentials[NETWORKZONE]
end
def networkzone?
credentials.key?(NETWORKZONE)
end
def logstream?
@application.environment.key?(DT_LOGSTREAM)
end
def skip_errors?
credentials[SKIP_ERRORS] == 'true'
end
def enable_fips?
credentials[ENABLE_FIPS] == 'true'
end
def tenanttoken(manifest)
manifest['tenantToken']
end
def unpack_agent(root)
FileUtils.mkdir_p(@droplet.sandbox)
FileUtils.mv(root + 'agent', @droplet.sandbox)
FileUtils.mv(root + 'manifest.json', @droplet.sandbox)
end
end
end
end