forked from gooddata/gooddata-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.rb
54 lines (49 loc) · 1.93 KB
/
environment.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
# encoding: UTF-8
#
# Copyright (c) 2010-2017 GoodData Corporation. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
require 'yaml'
require 'active_support/core_ext/hash/keys'
require 'securerandom'
module GoodData
module Environment
class << self
BRANCH_TO_ENVIRONMENT = {
develop: 'testing',
hotfix: 'production'
}
def load(env = ENV['GD_ENV'] || 'testing')
require_relative 'default'
env = branch_to_environment(env)
puts "USING ENVIRONMENT: #{env}"
require_relative env
env_secrets = decrypt_secrets(env)
GoodData::Environment::ConnectionHelper.set_const('SECRETS', env_secrets)
ENV['GD_SERVER'] = GoodData::Environment::ConnectionHelper::DEFAULT_SERVER
# VCR is enabled by default - set VCR_ON=false to disable
vcr_on = ENV['VCR_ON'].nil? || ENV['VCR_ON'].downcase == 'true'
GoodData::Environment.set_const(:VCR_ON, vcr_on)
suffix = SecureRandom.urlsafe_base64(5).gsub('-', '_')
GoodData::Environment.set_const(:RANDOM_STRING, suffix)
end
private
# Translates ci-infra branch to environment config name
# @param branch Name of the branch we are testing against
def branch_to_environment(branch)
BRANCH_TO_ENVIRONMENT[branch.to_sym] || branch
end
def decrypt_secrets(env)
secrets_path = File.join(File.dirname(__FILE__), 'secrets.yaml')
secrets = YAML.load_file(secrets_path)
env_secrets = secrets[env.downcase].symbolize_keys
env_secrets.merge!(secrets['global'].symbolize_keys)
encryption_key = ENV['GD_SPEC_PASSWORD'] || ENV['BIA_ENCRYPTION_KEY']
env_secrets.each do |_, value|
decrypted = GoodData::Helpers.decrypt(value, encryption_key)
value.replace(decrypted)
end
end
end
end
end