|
| 1 | +# Encoding: utf-8 |
| 2 | +# Cloud Foundry Java Buildpack |
| 3 | +# Copyright 2013 the original author or authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require 'java_buildpack/base_component' |
| 18 | +require 'java_buildpack/framework' |
| 19 | +require 'java_buildpack/util/service_utils' |
| 20 | +require 'tmpdir' |
| 21 | +require 'fileutils' |
| 22 | +require 'uri' |
| 23 | + |
| 24 | +module JavaBuildpack::Framework |
| 25 | + |
| 26 | + # Encapsulates the detect, compile, and release functionality for enabling Insight auto configuration. |
| 27 | + class SpringInsight < JavaBuildpack::BaseComponent |
| 28 | + |
| 29 | + def initialize(context) |
| 30 | + super('Spring Insight', context) |
| 31 | + @version, @uri = supports? ? find_insight_agent : [nil, nil] |
| 32 | + end |
| 33 | + |
| 34 | + def detect |
| 35 | + @version ? id(@version) : nil |
| 36 | + end |
| 37 | + |
| 38 | + def compile |
| 39 | + download(@version, @uri + AGENT_DOWNLOAD_URI_SUFFIX) { |file| expand file } # TODO: AGENT_DOWNLOAD_URI_SUFFIX To be removed once the full path is included in VCAP_SERVICES see issue 58873498 |
| 40 | + end |
| 41 | + |
| 42 | + def release |
| 43 | + weaver_jar = @application.relative_path_to(Pathname.new Dir[File.join(insight_home, 'weaver', 'insight-weaver-*.jar')][0]) |
| 44 | + |
| 45 | + @java_opts << "-javaagent:#{weaver_jar}" |
| 46 | + @java_opts << "-Dinsight.base=#{File.join insight_home, 'insight'}" |
| 47 | + @java_opts << "-Dinsight.logs=#{File.join insight_home, 'insight' , 'logs'}" |
| 48 | + @java_opts << '-Daspectj.overweaving=true' |
| 49 | + @java_opts << '-Dorg.aspectj.tracing.factory=default' |
| 50 | + @java_opts << '-Dagent.http.protocol=http' |
| 51 | + @java_opts << "-Dagent.http.host=#{URI(@uri).host}" |
| 52 | + @java_opts << '-Dagent.http.port=80' |
| 53 | + @java_opts << '-Dagent.http.context.path=insight' |
| 54 | + @java_opts << '-Dagent.http.username=spring' |
| 55 | + @java_opts << '-Dagent.http.password=insight' |
| 56 | + @java_opts << '-Dagent.http.send.json=false' |
| 57 | + @java_opts << '-Dagent.http.use.proxy=false' |
| 58 | + @java_opts << '-Dinsight.transport.type=HTTP' |
| 59 | + @java_opts << "-Dagent.name.override=#{@vcap_application[NAME_KEY]}" |
| 60 | + end |
| 61 | + |
| 62 | + protected |
| 63 | + |
| 64 | + # The unique identifier of the component, incorporating the version of the dependency (e.g. +spring-insight=1.9.3+) |
| 65 | + # |
| 66 | + # @param [String] version the version of the dependency |
| 67 | + # @return [String] the unique identifier of the component |
| 68 | + def id(version) |
| 69 | + "spring-insight=#{version}" |
| 70 | + end |
| 71 | + |
| 72 | + def supports? |
| 73 | + JavaBuildpack::Util::ServiceUtils.find_service(@vcap_services, SERVICE_NAME) |
| 74 | + end |
| 75 | + |
| 76 | + private |
| 77 | + |
| 78 | + AGENT_DOWNLOAD_URI_SUFFIX = 'services/config/agent-download'.freeze # TODO: To be removed once the full path is included in VCAP_SERVICES see issue 58873498 |
| 79 | + |
| 80 | + EXTRA_APPLICATIONS_DIRECTORY = '.extra-applications'.freeze |
| 81 | + |
| 82 | + CONTAINER_LIBS_DIRECTORY = '.container-libs'.freeze |
| 83 | + |
| 84 | + INSIGHT_HOME = '.insight'.freeze |
| 85 | + |
| 86 | + NAME_KEY = 'application_name'.freeze |
| 87 | + |
| 88 | + SERVICE_NAME = /insight/.freeze |
| 89 | + |
| 90 | + def expand(file) |
| 91 | + expand_start_time = Time.now |
| 92 | + print " Expanding Spring Insight to #{INSIGHT_HOME} " |
| 93 | + |
| 94 | + Dir.mktmpdir do |root| |
| 95 | + agent_dir = unpack_agent_installer(root, file) |
| 96 | + install_insight(agent_dir) |
| 97 | + end |
| 98 | + |
| 99 | + puts "(#{(Time.now - expand_start_time).duration})" |
| 100 | + end |
| 101 | + |
| 102 | + def unpack_agent_installer(root, file) |
| 103 | + installer_dir = File.join root, 'installer' |
| 104 | + agent_dir = File.join root, 'agent' |
| 105 | + |
| 106 | + FileUtils.mkdir_p(installer_dir) |
| 107 | + FileUtils.mkdir_p(agent_dir) |
| 108 | + shell "unzip -qq #{file.path} -d #{installer_dir} 2>&1" |
| 109 | + shell "unzip -qq #{uber_agent_zip(installer_dir)} -d #{agent_dir} 2>&1" |
| 110 | + |
| 111 | + agent_dir |
| 112 | + end |
| 113 | + |
| 114 | + def install_insight(agent_dir) |
| 115 | + weaver_directory = File.join insight_home, 'weaver' |
| 116 | + insight_directory = File.join insight_home, 'insight' |
| 117 | + insight_analyser_directory = File.join extra_applications_directory, 'insight-agent' |
| 118 | + uber_agent_directory = File.join agent_dir, 'springsource-insight-uber-agent-*' |
| 119 | + |
| 120 | + shell "rm -rf #{insight_home}" |
| 121 | + shell "rm -rf #{insight_analyser_directory}" |
| 122 | + FileUtils.mkdir_p(container_libs_directory) |
| 123 | + FileUtils.mkdir_p(extra_applications_directory) |
| 124 | + FileUtils.mkdir_p(weaver_directory) |
| 125 | + FileUtils.mkdir_p(insight_directory) |
| 126 | + |
| 127 | + shell "mv #{File.join uber_agent_directory, 'agents', 'common', 'insight-weaver-*.jar'} #{weaver_directory}" |
| 128 | + shell "mv #{File.join uber_agent_directory, 'agents', 'common', 'insight-bootstrap-generic-*.jar'} #{container_libs_directory}" |
| 129 | + shell "mv #{File.join uber_agent_directory, 'agents', 'tomcat', '7', 'lib', 'insight-bootstrap-tomcat-common-*.jar'} #{container_libs_directory}" |
| 130 | + shell "mv #{File.join uber_agent_directory, 'insight', 'collection-plugins'} #{insight_directory}" |
| 131 | + shell "mv #{File.join uber_agent_directory, 'insight', 'conf'} #{insight_directory}" |
| 132 | + shell "mv #{File.join uber_agent_directory, 'insight-agent'} #{insight_analyser_directory}" |
| 133 | + shell "mv #{File.join uber_agent_directory, 'transport', 'http', 'insight-agent-http-*.jar'} #{File.join insight_analyser_directory, 'WEB-INF', 'lib'} " |
| 134 | + end |
| 135 | + |
| 136 | + def container_libs_directory |
| 137 | + File.join @app_dir, CONTAINER_LIBS_DIRECTORY |
| 138 | + end |
| 139 | + |
| 140 | + def extra_applications_directory |
| 141 | + File.join @app_dir, EXTRA_APPLICATIONS_DIRECTORY |
| 142 | + end |
| 143 | + |
| 144 | + def uber_agent_zip(location) |
| 145 | + candidates = Dir[File.join location, 'springsource-insight-uber-agent-*.zip'] |
| 146 | + fail 'There was not exactly one Uber Agent zip' if candidates.size != 1 |
| 147 | + candidates[0] |
| 148 | + end |
| 149 | + |
| 150 | + def find_insight_agent |
| 151 | + service = JavaBuildpack::Util::ServiceUtils.find_service(@vcap_services, SERVICE_NAME) |
| 152 | + version = service['label'].match(/(.*)-(.*)/)[2] |
| 153 | + uri = service['credentials']['dashboard_url'] |
| 154 | + |
| 155 | + return version, uri # rubocop:disable RedundantReturn |
| 156 | + end |
| 157 | + |
| 158 | + def insight_home |
| 159 | + File.join @app_dir, INSIGHT_HOME |
| 160 | + end |
| 161 | + |
| 162 | + end |
| 163 | + |
| 164 | +end |
0 commit comments