google-cloud-env 1.0.5 → 1.3.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/CONTRIBUTING.md +1 -1
- data/README.md +6 -6
- data/lib/google/cloud/env.rb +126 -27
- data/lib/google/cloud/env/version.rb +1 -1
- metadata +23 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fe4fb0b390a089e7efb8cde45fb7f6df9e86b25ab81b84292a67fe481341137
|
4
|
+
data.tar.gz: 5efcbb333c4796812b99332cfc9919ae00f1e39104f092edc1392ba558d40caf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b74d450efba00970ed06f659dbedca0391f3128bb9ce6e0abb7d5580c18d55fd58a65ac81ffb6be5a10bc4b07ca41c9121f32df1ef87de455b96993d716150f0
|
7
|
+
data.tar.gz: 2dde707316115cda0096dbc949d8a6f8dae5f4110540a986bcb89aa1cbd258aebc496ca159b4a47f17fc2dda4277ac0d83c332a9519186451ec027f67633ca0d
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 1.3.0 / 2019-10-23
|
4
|
+
|
5
|
+
Now requires Ruby 2.4 or later.
|
6
|
+
|
7
|
+
#### Features
|
8
|
+
|
9
|
+
* Recognize App Engine Standard and Knative
|
10
|
+
|
11
|
+
### 1.2.1 / 2019-08-23
|
12
|
+
|
13
|
+
#### Bug Fixes
|
14
|
+
|
15
|
+
* Send Metadata-Flavor header when testing the metadata server root
|
16
|
+
|
17
|
+
#### Documentation
|
18
|
+
|
19
|
+
* Update documentation
|
20
|
+
|
21
|
+
### 1.2.0 / 2019-06-19
|
22
|
+
|
23
|
+
* Support separate timeout for connecting to the metadata server vs the entire request
|
24
|
+
|
25
|
+
### 1.1.0 / 2019-05-29
|
26
|
+
|
27
|
+
* Support disabling of the metadata cache
|
28
|
+
* Support configurable retries when querying the metadata server
|
29
|
+
* Support configuration of the metadata request timeout
|
30
|
+
|
3
31
|
### 1.0.5 / 2018-09-20
|
4
32
|
|
5
33
|
* Update documentation.
|
data/CONTRIBUTING.md
CHANGED
@@ -24,7 +24,7 @@ be able to accept your pull requests.
|
|
24
24
|
In order to use the google-cloud-env console and run the project's tests,
|
25
25
|
there is a small amount of setup:
|
26
26
|
|
27
|
-
1. Install Ruby. google-cloud-env requires Ruby 2.
|
27
|
+
1. Install Ruby. google-cloud-env requires Ruby 2.4+. You may choose to
|
28
28
|
manage your Ruby and gem installations with [RVM](https://rvm.io/),
|
29
29
|
[rbenv](https://github.com/rbenv/rbenv), or
|
30
30
|
[chruby](https://github.com/postmodern/chruby).
|
data/README.md
CHANGED
@@ -3,15 +3,15 @@
|
|
3
3
|
This library provides information on the application hosting environment for
|
4
4
|
apps running on Google Cloud Platform.
|
5
5
|
|
6
|
-
- [google-cloud-env API documentation](https://googleapis.
|
6
|
+
- [google-cloud-env API documentation](https://googleapis.dev/ruby/google-cloud-env/latest)
|
7
7
|
|
8
8
|
## Supported Ruby Versions
|
9
9
|
|
10
|
-
This library is supported on Ruby 2.
|
10
|
+
This library is supported on Ruby 2.4+.
|
11
11
|
|
12
12
|
Google provides official support for Ruby versions that are actively supported
|
13
13
|
by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
|
14
|
-
security maintenance, and not end of life. Currently, this means Ruby 2.
|
14
|
+
security maintenance, and not end of life. Currently, this means Ruby 2.4 and
|
15
15
|
later. Older versions of Ruby _may_ still work, but are unsupported and not
|
16
16
|
recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
|
17
17
|
about the Ruby support schedule.
|
@@ -28,18 +28,18 @@ change at any time and the public API should not be considered stable.
|
|
28
28
|
Contributions to this library are always welcome and highly encouraged.
|
29
29
|
|
30
30
|
See the [Contributing
|
31
|
-
Guide](https://googleapis.
|
31
|
+
Guide](https://googleapis.dev/ruby/google-cloud-env/latest/file.CONTRIBUTING.html)
|
32
32
|
for more information on how to get started.
|
33
33
|
|
34
34
|
Please note that this project is released with a Contributor Code of Conduct. By
|
35
35
|
participating in this project you agree to abide by its terms. See [Code of
|
36
|
-
Conduct](https://googleapis.
|
36
|
+
Conduct](https://googleapis.dev/ruby/google-cloud-env/latest/file.CODE_OF_CONDUCT.html)
|
37
37
|
for more information.
|
38
38
|
|
39
39
|
## License
|
40
40
|
|
41
41
|
This library is licensed under Apache 2.0. Full license text is available in
|
42
|
-
[LICENSE](https://googleapis.
|
42
|
+
[LICENSE](https://googleapis.dev/ruby/google-cloud-env/latest/file.LICENSE.html).
|
43
43
|
|
44
44
|
## Support
|
45
45
|
|
data/lib/google/cloud/env.rb
CHANGED
@@ -65,22 +65,66 @@ module Google
|
|
65
65
|
# @private URL path for metadata server root.
|
66
66
|
METADATA_ROOT_PATH = "/".freeze
|
67
67
|
|
68
|
+
# @private
|
69
|
+
METADATA_FAILURE_EXCEPTIONS = [
|
70
|
+
Faraday::TimeoutError,
|
71
|
+
Faraday::ConnectionFailed,
|
72
|
+
Errno::EHOSTDOWN,
|
73
|
+
Errno::ETIMEDOUT,
|
74
|
+
Timeout::Error
|
75
|
+
].freeze
|
76
|
+
|
68
77
|
##
|
69
78
|
# Create a new instance of the environment information.
|
70
79
|
# Most client should not need to call this directly. Obtain a singleton
|
71
80
|
# instance of the information from `Google::Cloud.env`. This constructor
|
72
|
-
# is provided
|
81
|
+
# is provided to allow customization of the timeout/retry settings, as
|
82
|
+
# well as mocking for testing.
|
73
83
|
#
|
74
84
|
# @param [Hash] env Mock environment variables.
|
85
|
+
# @param [Hash,false] metadata_cache The metadata cache. You may pass
|
86
|
+
# a prepopuated cache, an empty cache (the default) or `false` to
|
87
|
+
# disable the cache completely.
|
88
|
+
# @param [Numeric] open_timeout Timeout for opening http connections.
|
89
|
+
# Defaults to 0.1.
|
90
|
+
# @param [Numeric] request_timeout Timeout for entire http requests.
|
91
|
+
# Defaults to 1.0.
|
92
|
+
# @param [Integer] retry_count Number of times to retry http requests.
|
93
|
+
# Defaults to 1. Note that retry remains in effect even if a custom
|
94
|
+
# `connection` is provided.
|
95
|
+
# @param [Numeric] retry_interval Time between retries in seconds.
|
96
|
+
# Defaults to 0.1.
|
97
|
+
# @param [Numeric] retry_backoff_factor Multiplier applied to the retry
|
98
|
+
# interval on each retry. Defaults to 1.5.
|
99
|
+
# @param [Numeric] retry_max_interval Maximum time between retries in
|
100
|
+
# seconds. Defaults to 0.5.
|
75
101
|
# @param [Faraday::Connection] connection Faraday connection to use.
|
76
|
-
#
|
77
|
-
#
|
78
|
-
|
102
|
+
# If specified, overrides the `request_timeout` and `open_timeout`
|
103
|
+
# settings.
|
104
|
+
#
|
105
|
+
def initialize env: nil, connection: nil, metadata_cache: nil,
|
106
|
+
open_timeout: 0.1, request_timeout: 1.0,
|
107
|
+
retry_count: 2, retry_interval: 0.1,
|
108
|
+
retry_backoff_factor: 1.5, retry_max_interval: 0.5
|
109
|
+
@disable_metadata_cache = metadata_cache == false
|
79
110
|
@metadata_cache = metadata_cache || {}
|
80
111
|
@env = env || ::ENV
|
81
|
-
@
|
82
|
-
|
83
|
-
|
112
|
+
@retry_count = retry_count
|
113
|
+
@retry_interval = retry_interval
|
114
|
+
@retry_backoff_factor = retry_backoff_factor
|
115
|
+
@retry_max_interval = retry_max_interval
|
116
|
+
request_opts = { timeout: request_timeout, open_timeout: open_timeout }
|
117
|
+
@connection = connection || ::Faraday.new(url: METADATA_HOST, request: request_opts)
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# Determine whether the application is running on a Knative-based
|
122
|
+
# hosting platform, such as Cloud Run or Cloud Functions.
|
123
|
+
#
|
124
|
+
# @return [Boolean]
|
125
|
+
#
|
126
|
+
def knative?
|
127
|
+
env["K_SERVICE"] ? true : false
|
84
128
|
end
|
85
129
|
|
86
130
|
##
|
@@ -92,6 +136,26 @@ module Google
|
|
92
136
|
env["GAE_INSTANCE"] ? true : false
|
93
137
|
end
|
94
138
|
|
139
|
+
##
|
140
|
+
# Determine whether the application is running on Google App Engine
|
141
|
+
# Flexible Environment.
|
142
|
+
#
|
143
|
+
# @return [Boolean]
|
144
|
+
#
|
145
|
+
def app_engine_flexible?
|
146
|
+
app_engine? && env["GAE_ENV"] != "standard"
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# Determine whether the application is running on Google App Engine
|
151
|
+
# Standard Environment.
|
152
|
+
#
|
153
|
+
# @return [Boolean]
|
154
|
+
#
|
155
|
+
def app_engine_standard?
|
156
|
+
app_engine? && env["GAE_ENV"] == "standard"
|
157
|
+
end
|
158
|
+
|
95
159
|
##
|
96
160
|
# Determine whether the application is running on Google Kubernetes
|
97
161
|
# Engine (GKE).
|
@@ -136,7 +200,7 @@ module Google
|
|
136
200
|
# @return [Boolean]
|
137
201
|
#
|
138
202
|
def raw_compute_engine?
|
139
|
-
!app_engine? && !cloud_shell? && metadata? && !kubernetes_engine?
|
203
|
+
!knative? && !app_engine? && !cloud_shell? && metadata? && !kubernetes_engine?
|
140
204
|
end
|
141
205
|
|
142
206
|
##
|
@@ -146,8 +210,7 @@ module Google
|
|
146
210
|
# @return [String,nil]
|
147
211
|
#
|
148
212
|
def project_id
|
149
|
-
env["GCLOUD_PROJECT"] || env["DEVSHELL_PROJECT_ID"] ||
|
150
|
-
lookup_metadata("project", "project-id")
|
213
|
+
env["GCLOUD_PROJECT"] || env["DEVSHELL_PROJECT_ID"] || lookup_metadata("project", "project-id")
|
151
214
|
end
|
152
215
|
|
153
216
|
##
|
@@ -249,6 +312,27 @@ module Google
|
|
249
312
|
lookup_metadata "instance", "attributes/#{key}"
|
250
313
|
end
|
251
314
|
|
315
|
+
##
|
316
|
+
# Returns the name of the running Knative service, or `nil` if the
|
317
|
+
# current code is not running on Knative.
|
318
|
+
#
|
319
|
+
# @return [String,nil]
|
320
|
+
#
|
321
|
+
def knative_service_id
|
322
|
+
env["K_SERVICE"]
|
323
|
+
end
|
324
|
+
alias knative_service_name knative_service_id
|
325
|
+
|
326
|
+
##
|
327
|
+
# Returns the revision of the running Knative service, or `nil` if the
|
328
|
+
# current code is not running on Knative.
|
329
|
+
#
|
330
|
+
# @return [String,nil]
|
331
|
+
#
|
332
|
+
def knative_service_revision
|
333
|
+
env["K_REVISION"]
|
334
|
+
end
|
335
|
+
|
252
336
|
##
|
253
337
|
# Returns the name of the running App Engine service, or `nil` if the
|
254
338
|
# current code is not running in App Engine.
|
@@ -258,6 +342,7 @@ module Google
|
|
258
342
|
def app_engine_service_id
|
259
343
|
env["GAE_SERVICE"]
|
260
344
|
end
|
345
|
+
alias app_engine_service_name app_engine_service_id
|
261
346
|
|
262
347
|
##
|
263
348
|
# Returns the version of the running App Engine service, or `nil` if the
|
@@ -305,8 +390,7 @@ module Google
|
|
305
390
|
# below is set in some older versions of GKE, and the file below is
|
306
391
|
# present in Kubernetes as of version 1.9, but it is possible that
|
307
392
|
# alternatives will need to be found in the future.
|
308
|
-
env["GKE_NAMESPACE_ID"] ||
|
309
|
-
::IO.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
393
|
+
env["GKE_NAMESPACE_ID"] || ::IO.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
310
394
|
rescue SystemCallError
|
311
395
|
nil
|
312
396
|
end
|
@@ -318,17 +402,16 @@ module Google
|
|
318
402
|
# @return [Boolean]
|
319
403
|
#
|
320
404
|
def metadata?
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
metadata_cache[METADATA_ROOT_PATH] = false
|
405
|
+
path = METADATA_ROOT_PATH
|
406
|
+
if @disable_metadata_cache || !metadata_cache.include?(path)
|
407
|
+
metadata_cache[path] = retry_or_fail_with false do
|
408
|
+
resp = connection.get path do |req|
|
409
|
+
req.headers = { "Metadata-Flavor" => "Google" }
|
410
|
+
end
|
411
|
+
resp.status == 200 && resp.headers["Metadata-Flavor"] == "Google"
|
329
412
|
end
|
330
413
|
end
|
331
|
-
metadata_cache[
|
414
|
+
metadata_cache[path]
|
332
415
|
end
|
333
416
|
|
334
417
|
##
|
@@ -342,16 +425,15 @@ module Google
|
|
342
425
|
# @return [String,nil]
|
343
426
|
#
|
344
427
|
def lookup_metadata type, entry
|
428
|
+
return nil unless metadata?
|
429
|
+
|
345
430
|
path = "#{METADATA_PATH_BASE}/#{type}/#{entry}"
|
346
|
-
if !metadata_cache.include?(path)
|
347
|
-
|
431
|
+
if @disable_metadata_cache || !metadata_cache.include?(path)
|
432
|
+
metadata_cache[path] = retry_or_fail_with nil do
|
348
433
|
resp = connection.get path do |req|
|
349
434
|
req.headers = { "Metadata-Flavor" => "Google" }
|
350
435
|
end
|
351
|
-
|
352
|
-
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed,
|
353
|
-
Errno::EHOSTDOWN
|
354
|
-
metadata_cache[path] = nil
|
436
|
+
resp.status == 200 ? resp.body.strip : nil
|
355
437
|
end
|
356
438
|
end
|
357
439
|
metadata_cache[path]
|
@@ -371,6 +453,23 @@ module Google
|
|
371
453
|
attr_reader :connection
|
372
454
|
attr_reader :env
|
373
455
|
attr_reader :metadata_cache
|
456
|
+
|
457
|
+
def retry_or_fail_with error_result
|
458
|
+
retries_remaining = @retry_count
|
459
|
+
retry_interval = @retry_interval
|
460
|
+
begin
|
461
|
+
yield
|
462
|
+
rescue *METADATA_FAILURE_EXCEPTIONS
|
463
|
+
retries_remaining -= 1
|
464
|
+
if retries_remaining >= 0
|
465
|
+
sleep retry_interval
|
466
|
+
retry_interval *= @retry_backoff_factor
|
467
|
+
retry_interval = @retry_max_interval if retry_interval > @retry_max_interval
|
468
|
+
retry
|
469
|
+
end
|
470
|
+
error_result
|
471
|
+
end
|
472
|
+
end
|
374
473
|
end
|
375
474
|
|
376
475
|
@env = Env.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-cloud-env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Azuma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -25,63 +25,63 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.11'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: autotest-suffix
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '1.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: google-style
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 1.24.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 1.24.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name: minitest
|
56
|
+
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '5.10'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '5.10'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: minitest-
|
70
|
+
name: minitest-autotest
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '1.0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '1.0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: minitest-focus
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
@@ -95,33 +95,33 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '1.1'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: minitest-rg
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '5.2'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '5.2'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
112
|
+
name: redcarpet
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: '3.0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: '3.0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: simplecov
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -195,15 +195,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
195
195
|
requirements:
|
196
196
|
- - ">="
|
197
197
|
- !ruby/object:Gem::Version
|
198
|
-
version: 2.
|
198
|
+
version: '2.4'
|
199
199
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
200
|
requirements:
|
201
201
|
- - ">="
|
202
202
|
- !ruby/object:Gem::Version
|
203
203
|
version: '0'
|
204
204
|
requirements: []
|
205
|
-
|
206
|
-
rubygems_version: 2.7.7
|
205
|
+
rubygems_version: 3.0.6
|
207
206
|
signing_key:
|
208
207
|
specification_version: 4
|
209
208
|
summary: Google Cloud Platform hosting environment information.
|