|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Copyright IBM Corporation 2017 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +## |
| 16 | + |
| 17 | +require 'yaml' |
| 18 | +require 'fileutils' |
| 19 | + |
| 20 | +class EnvProfileApplier |
| 21 | + def initialize(app_dir, log_env = false) |
| 22 | + @app_dir = app_dir |
| 23 | + @log_env = log_env |
| 24 | + @bp_dir = File.expand_path(File.join('..','..'), __FILE__) |
| 25 | + end |
| 26 | + |
| 27 | + def apply_env_profile |
| 28 | + profiled_dir = File.join(@app_dir, '.profile.d') |
| 29 | + |
| 30 | + # load config file |
| 31 | + config = YAML.load_file(File.join(@bp_dir, "config", "env.yml")) |
| 32 | + variables = {} |
| 33 | + # apply default variables |
| 34 | + copy_variables(variables, config) |
| 35 | + |
| 36 | + # apply profiles |
| 37 | + profiles.each do | profile | |
| 38 | + profile = profile.strip |
| 39 | + profile_variables = config[profile] |
| 40 | + copy_variables(variables, profile_variables) unless profile_variables.nil? |
| 41 | + end |
| 42 | + |
| 43 | + # create bluemix_env.sh file in app's '.profile.d' folder |
| 44 | + FileUtils.mkdir_p(profiled_dir) |
| 45 | + env_file_name = File.join(profiled_dir, 'bluemix_env.sh') |
| 46 | + env_file = File.new(env_file_name, 'w') |
| 47 | + variables.each do | key, value | |
| 48 | + env_file.puts("export #{key}=\"#{value}\"") |
| 49 | + end |
| 50 | + env_file.close |
| 51 | + |
| 52 | + log_env(env_file_name) if @log_env |
| 53 | + end |
| 54 | + |
| 55 | + private |
| 56 | + |
| 57 | + def log_env(env_file_name) |
| 58 | + if profiles.empty? |
| 59 | + puts "-----> No configuration profiles applied" |
| 60 | + else |
| 61 | + puts "-----> Applied configuration profiles: #{profiles}" |
| 62 | + if File.exist?(env_file_name) |
| 63 | + env_contents = File.open(env_file_name) { |file| file.read } |
| 64 | + puts "-----> Generated 'bluemix_env.sh' in application's '.profile.d' folder" |
| 65 | + puts "'bluemix_env.sh' contents:\n#{env_contents}" |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def copy_variables(variables, configuration) |
| 71 | + configuration.each do | key, value | |
| 72 | + key = key.strip |
| 73 | + variables[key] = value unless value.is_a?(Hash) || value.is_a?(Array) || key.empty? |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def profiles |
| 78 | + profiles_var = ENV['IBM_ENV_PROFILE'] |
| 79 | + if profiles_var.nil? |
| 80 | + region = ENV['BLUEMIX_REGION'] |
| 81 | + region.nil? ? [] : [region] |
| 82 | + else |
| 83 | + profiles_var.split(',') |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments