|
| 1 | +# Encoding: utf-8 |
| 2 | +# Cloud Foundry Java Buildpack |
| 3 | +# Copyright 2013-2016 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 | + |
| 21 | +module JavaBuildpack |
| 22 | + module Framework |
| 23 | + |
| 24 | + # Encapsulates the functionality for enabling zero-touch Ruxit support. |
| 25 | + class RuxitAgent < JavaBuildpack::Component::VersionedDependencyComponent |
| 26 | + |
| 27 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 28 | + def compile |
| 29 | + download(@version, @uri) { |file| expand file } |
| 30 | + @droplet.copy_resources |
| 31 | + end |
| 32 | + |
| 33 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 34 | + def release |
| 35 | + credentials = @application.services.find_service(FILTER)['credentials'] |
| 36 | + |
| 37 | + @droplet.java_opts.add_agentpath_with_props(agent_path, |
| 38 | + SERVER => server(credentials), |
| 39 | + TENANT => tenant(credentials), |
| 40 | + TENANTTOKEN => tenanttoken(credentials)) |
| 41 | + |
| 42 | + environment = @application.environment |
| 43 | + environment_variables = @droplet.environment_variables |
| 44 | + |
| 45 | + unless environment.key?(RUXIT_APPLICATION_ID) |
| 46 | + environment_variables.add_environment_variable(RUXIT_APPLICATION_ID, application_id) |
| 47 | + end |
| 48 | + |
| 49 | + unless environment.key?(RUXIT_CLUSTER_ID) |
| 50 | + environment_variables.add_environment_variable(RUXIT_CLUSTER_ID, cluster_id) |
| 51 | + end |
| 52 | + |
| 53 | + environment_variables.add_environment_variable(RUXIT_HOST_ID, host_id) unless environment.key?(RUXIT_HOST_ID) |
| 54 | + end |
| 55 | + |
| 56 | + protected |
| 57 | + |
| 58 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) |
| 59 | + def supports? |
| 60 | + @application.services.one_service? FILTER, TENANT, TENANTTOKEN |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + |
| 65 | + FILTER = /ruxit/.freeze |
| 66 | + |
| 67 | + RUXIT_APPLICATION_ID = 'RUXIT_APPLICATIONID'.freeze |
| 68 | + |
| 69 | + RUXIT_CLUSTER_ID = 'RUXIT_CLUSTER_ID'.freeze |
| 70 | + |
| 71 | + RUXIT_HOST_ID = 'RUXIT_HOST_ID'.freeze |
| 72 | + |
| 73 | + SERVER = 'server'.freeze |
| 74 | + |
| 75 | + TENANT = 'tenant'.freeze |
| 76 | + |
| 77 | + TENANTTOKEN = 'tenanttoken'.freeze |
| 78 | + |
| 79 | + private_constant :FILTER, :RUXIT_APPLICATION_ID, :RUXIT_CLUSTER_ID, :RUXIT_HOST_ID, :SERVER, :TENANT, :TENANTTOKEN |
| 80 | + |
| 81 | + def agent_dir |
| 82 | + @droplet.sandbox + 'agent' |
| 83 | + end |
| 84 | + |
| 85 | + def agent_path |
| 86 | + agent_dir + 'lib64/libruxitagentloader.so' |
| 87 | + end |
| 88 | + |
| 89 | + def application_id |
| 90 | + @application.details['application_name'] |
| 91 | + end |
| 92 | + |
| 93 | + def cluster_id |
| 94 | + @application.details['application_name'] |
| 95 | + end |
| 96 | + |
| 97 | + def expand(file) |
| 98 | + with_timing "Expanding Ruxit Agent to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do |
| 99 | + Dir.mktmpdir do |root| |
| 100 | + root_path = Pathname.new(root) |
| 101 | + shell "unzip -qq #{file.path} -d #{root_path} 2>&1" |
| 102 | + unpack_agent root_path |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def host_id |
| 108 | + "#{@application.details['application_name']}_${CF_INSTANCE_INDEX}" |
| 109 | + end |
| 110 | + |
| 111 | + def server(credentials) |
| 112 | + credentials[SERVER] || "https://#{tenant(credentials)}.live.ruxit.com:443/communication" |
| 113 | + end |
| 114 | + |
| 115 | + def tenant(credentials) |
| 116 | + credentials[TENANT] |
| 117 | + end |
| 118 | + |
| 119 | + def tenanttoken(credentials) |
| 120 | + credentials[TENANTTOKEN] |
| 121 | + end |
| 122 | + |
| 123 | + def unpack_agent(root) |
| 124 | + FileUtils.mkdir_p(agent_dir) |
| 125 | + FileUtils.mv(root + 'agent/bin', agent_dir) |
| 126 | + FileUtils.mv(root + 'agent/conf', agent_dir) |
| 127 | + FileUtils.mv(root + 'agent/lib64', agent_dir) |
| 128 | + end |
| 129 | + |
| 130 | + end |
| 131 | + |
| 132 | + end |
| 133 | +end |
0 commit comments