rcloadenv 0.0.1.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ # Copyright 2017 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ ;
15
+
16
+ require "rcloadenv/api_client"
17
+ require "google/cloud/env"
18
+
19
+ module RCLoadEnv
20
+
21
+ ##
22
+ # A class that loads environment variables from a RuntimeConfig resource.
23
+ #
24
+ class Loader
25
+
26
+ ##
27
+ # Error thrown when inputs are malformed.
28
+ #
29
+ class UsageError < StandardError
30
+ end
31
+
32
+ ##
33
+ # Create a loader. Alwaus uses application default credentials.
34
+ #
35
+ # @param [String] config_name Name of the runtime config resource
36
+ # @param [Array<String>] exclude Config keys to exclude (optional)
37
+ # @param [Array<String>] include Config keys to include (optional)
38
+ # @param [Boolean] override Whether to override existing environment
39
+ # variables (default: false)
40
+ # @param [String,nil] project Optional name of the cloud project. If not
41
+ # set, attempts to infer one from the environment.
42
+ # @param [Boolean] debug Whether to print debug output (default: false)
43
+ #
44
+ def initialize config_name,
45
+ exclude: [], include: [], override: false,
46
+ project: nil, debug: false
47
+ @config_name = config_name.to_s
48
+ @project = (project || default_project).to_s
49
+ @exclude = exclude
50
+ @include = include
51
+ @override = override
52
+ @debug = debug
53
+ end
54
+
55
+ ## @return [String] The Runtime Config resource name
56
+ attr_reader :config_name
57
+ ## @return [String] The cloud project
58
+ attr_reader :project
59
+
60
+ ##
61
+ # Modify the given environment with the configuration. The given hash
62
+ # is modified in place and returned. If no hash is provided, a new one is
63
+ # created and returned.
64
+ #
65
+ # @param [Hash<String,String>] env The environment to modify.
66
+ # @return the modified environment.
67
+ #
68
+ def modify_env env={}
69
+ raw_variables.each do |k, v|
70
+ if !@exclude.empty? && @exclude.include?(k) ||
71
+ !@include.empty? && !@include.include?(k)
72
+ debug "Skipping config variable #{k}"
73
+ else
74
+ debug "Found config variable #{k}"
75
+ key = k.split("/").last.upcase.gsub("-", "_")
76
+ if !env.include?(key)
77
+ debug "Setting envvar: #{key}"
78
+ env[key] = v
79
+ elsif @override
80
+ debug "Overriding envvar: #{key}"
81
+ env[key] = v
82
+ else
83
+ debug "Envvar already set: #{key}"
84
+ end
85
+ end
86
+ end
87
+ env
88
+ end
89
+
90
+ ##
91
+ # Returns the has of variables retrieved from the runtime config.
92
+ # Variable names have the parent (project and config name) stripped.
93
+ # Values are all converted to strings.
94
+ #
95
+ # @return [Hash<String,String>] the variables.
96
+ #
97
+ def raw_variables
98
+ @raw_variables ||= load_raw_variables
99
+ end
100
+
101
+ ## @private
102
+ def service
103
+ @service ||= create_service
104
+ end
105
+
106
+ ## @private Used to mock the service for testing
107
+ def service= mock
108
+ @service = mock
109
+ end
110
+
111
+ ## @private
112
+ def load_raw_variables
113
+ if project.empty?
114
+ raise UsageError, "Project name must be provided."
115
+ end
116
+ if config_name.empty?
117
+ raise UsageError, "Config name must be provided."
118
+ end
119
+ parent_path = "projects/#{project}/configs/#{config_name}"
120
+ debug "Loading #{config_name} from project #{project}"
121
+ response = service.list_project_config_variables \
122
+ parent_path, return_values: true
123
+ raw_variables = {}
124
+ (response.to_h[:variables] || []).each do |var_info|
125
+ key = var_info[:name].sub "#{parent_path}/variables/", ""
126
+ raw_variables[key] = var_info[:text] || var_info[:value]
127
+ end
128
+ raw_variables
129
+ end
130
+
131
+ ## @private
132
+ def default_project
133
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
134
+ ENV["GCLOUD_PROJECT"] ||
135
+ Google::Cloud.env.project_id ||
136
+ `gcloud config get-value project 2>/dev/null`.strip
137
+ end
138
+
139
+ ## @private
140
+ def create_service
141
+ s = Google::Apis::RuntimeconfigV1beta1::CloudRuntimeConfigService.new
142
+ s.client_options.application_name = "rcloadenv-ruby"
143
+ s.client_options.application_version = RCLoadEnv::VERSION
144
+ s.request_options.retries = 3
145
+ s.request_options.header ||= {}
146
+ s.request_options.header["x-goog-api-client"] = \
147
+ "gl-ruby/#{RUBY_VERSION} rcloadenv/#{RCLoadEnv::VERSION}"
148
+ s.authorization = RCLoadEnv::Credentials.default.client
149
+ s
150
+ end
151
+
152
+ ## @private
153
+ def debug str
154
+ STDERR.puts "RCLOADENV DEBUG: #{str}" if @debug
155
+ end
156
+ end
157
+
158
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright 2017 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ ;
15
+
16
+ module RCLoadEnv
17
+
18
+ ## The current version of this gem, as a string.
19
+ VERSION = '0.0.1.rc1'.freeze
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcloadenv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Azuma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-cloud-env
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '11.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '11.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
125
+ description: rcloadenv is a tool for loading configuration from the Google Runtime
126
+ Config API into environment variables. The rcloadenv ruby gem is a ruby implementation
127
+ of the tool that may be installed as a ruby gem or included in a ruby Gemfile.
128
+ email:
129
+ - dazuma@gmail.com
130
+ executables:
131
+ - rcloadenv
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - ".yardopts"
136
+ - CHANGELOG.md
137
+ - README.md
138
+ - bin/rcloadenv
139
+ - lib/rcloadenv.rb
140
+ - lib/rcloadenv/api_client.rb
141
+ - lib/rcloadenv/cli.rb
142
+ - lib/rcloadenv/credentials.rb
143
+ - lib/rcloadenv/google/apis/runtimeconfig_v1beta1.rb
144
+ - lib/rcloadenv/google/apis/runtimeconfig_v1beta1/classes.rb
145
+ - lib/rcloadenv/google/apis/runtimeconfig_v1beta1/representations.rb
146
+ - lib/rcloadenv/google/apis/runtimeconfig_v1beta1/service.rb
147
+ - lib/rcloadenv/loader.rb
148
+ - lib/rcloadenv/version.rb
149
+ homepage: https://github.com/GoogleCloudPlatform/rcloadenv
150
+ licenses:
151
+ - Apache 2.0
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: 2.0.0
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.3.1
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.6.11
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Load Google Runtime Config data into environment variables
173
+ test_files: []