|
| 1 | +# Cloud Foundry Java Buildpack |
| 2 | +# |
| 3 | +# Copyright 2013-2017 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 'fileutils' |
| 18 | +require 'java_buildpack/component/versioned_dependency_component' |
| 19 | +require 'java_buildpack/framework' |
| 20 | +require 'java_buildpack/util/qualify_path' |
| 21 | + |
| 22 | +module JavaBuildpack |
| 23 | + module Framework |
| 24 | + |
| 25 | + # Encapsulates the functionality for enabling zero-touch OverOps (fka Takipi) support. |
| 26 | + class TakipiAgent < JavaBuildpack::Component::VersionedDependencyComponent |
| 27 | + include JavaBuildpack::Util |
| 28 | + |
| 29 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 30 | + def compile |
| 31 | + download_tar |
| 32 | + @droplet.copy_resources |
| 33 | + end |
| 34 | + |
| 35 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 36 | + def release |
| 37 | + java_opts = @droplet.java_opts |
| 38 | + java_opts.add_agentpath(@droplet.sandbox + 'lib/libTakipiAgent.so') |
| 39 | + application_name java_opts |
| 40 | + default_env_vars |
| 41 | + credentials = @application.services.find_service(FILTER)['credentials'] |
| 42 | + config_env_vars credentials |
| 43 | + end |
| 44 | + |
| 45 | + protected |
| 46 | + |
| 47 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) |
| 48 | + def supports? |
| 49 | + @application.services.one_service? FILTER, [SECRET_KEY, COLLECTOR_HOST] |
| 50 | + end |
| 51 | + |
| 52 | + private |
| 53 | + |
| 54 | + FILTER = /takipi/ |
| 55 | + |
| 56 | + SECRET_KEY = 'secret_key'.freeze |
| 57 | + |
| 58 | + COLLECTOR_HOST = 'collector_host'.freeze |
| 59 | + |
| 60 | + private_constant :FILTER |
| 61 | + |
| 62 | + def jvm_lib_file |
| 63 | + @droplet.java_home.root + 'lib/amd64/server/libjvm.so' |
| 64 | + end |
| 65 | + |
| 66 | + def default_env_vars |
| 67 | + env = @droplet.environment_variables |
| 68 | + sandbox = @droplet.sandbox |
| 69 | + |
| 70 | + env.add_environment_variable( |
| 71 | + 'LD_LIBRARY_PATH', |
| 72 | + "$LD_LIBRARY_PATH:#{qualify_path(sandbox + 'lib', @droplet.root)}" |
| 73 | + ) |
| 74 | + env.add_environment_variable('JVM_LIB_FILE', jvm_lib_file) |
| 75 | + env.add_environment_variable('TAKIPI_HOME', sandbox) |
| 76 | + env.add_environment_variable('TAKIPI_MACHINE_NAME', node_name) |
| 77 | + end |
| 78 | + |
| 79 | + def config_env_vars(credentials) |
| 80 | + env = @droplet.environment_variables |
| 81 | + |
| 82 | + secret_key = credentials['secret_key'] |
| 83 | + env.add_environment_variable 'TAKIPI_SECRET_KEY', secret_key if secret_key |
| 84 | + |
| 85 | + collector_host = credentials['collector_host'] |
| 86 | + env.add_environment_variable 'TAKIPI_MASTER_HOST', collector_host if collector_host |
| 87 | + |
| 88 | + collector_port = credentials['collector_port'] |
| 89 | + env.add_environment_variable 'TAKIPI_MASTER_PORT', collector_port if collector_port |
| 90 | + end |
| 91 | + |
| 92 | + def application_name(java_opts) |
| 93 | + app_name = @configuration['application_name'] || @application.details['application_name'] |
| 94 | + java_opts.add_system_property('takipi.name', app_name) |
| 95 | + end |
| 96 | + |
| 97 | + def node_name |
| 98 | + "#{@configuration['node_name_prefix']}-$CF_INSTANCE_INDEX" |
| 99 | + end |
| 100 | + |
| 101 | + end |
| 102 | + |
| 103 | + end |
| 104 | +end |
0 commit comments