gcp_data 0.2.0 → 0.2.1
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 +4 -0
- data/README.md +4 -4
- data/lib/gcp_data/version.rb +1 -1
- data/lib/gcp_data.rb +62 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e4fe524be4163e8b796361a35270f4e0230f1908b1380d58d17063ec5dbb01b
|
4
|
+
data.tar.gz: 0fb3fde5e05fb32529666d1b83eaa50fbef7db2c40ba15cfd28a9c89cbc4c2d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f82401965e69cf29d9b18e5b3956c305b4f4c033eb31a63680193917e9ee93b34620b3830c980d9cea51bd05a5d3441e23287e392279857bba4e895aba094b9
|
7
|
+
data.tar.gz: db5b4acea8d9dcda32b74fc4735a5f5919e383d69bf32f70abdd0cbb9b53fb459ff8a07b9c24af61abff441d54b77404c0aaf52b303dfa226d85fdf98f03bf69
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
## [0.2.1] - 2021-12-27
|
7
|
+
- [#2](https://github.com/tongueroo/gcp_data/pull/2) friendly error message when gcloud cli not installed
|
8
|
+
- [#3](https://github.com/tongueroo/gcp_data/pull/3) autoset GOOGLE_PROJECT GOOGLE_REGION GOOGLE_ZONE from gcloud config o…
|
9
|
+
|
6
10
|
## [0.2.0]
|
7
11
|
- #1 additional precedence lookups
|
8
12
|
|
data/README.md
CHANGED
@@ -29,11 +29,11 @@ GcpData.region
|
|
29
29
|
|
30
30
|
## Precedence
|
31
31
|
|
32
|
-
This library will return
|
32
|
+
This library will return project and region info using different sources with this precedence:
|
33
33
|
|
34
34
|
1. Environment variables: GOOGLE_PROJECT, GOOGLE_REGION, GOOGLE_ZONE
|
35
35
|
2. Google Credentials file: only project id available from the GOOGLE\_APPLICATION_CREDENTIALS file
|
36
|
-
3. CLI: gcloud
|
36
|
+
3. CLI: gcloud: project, region, and zone available
|
37
37
|
4. Defaults: region=us-central1 and zone==us-central1a
|
38
38
|
|
39
39
|
### 1. Environment variables
|
@@ -68,11 +68,11 @@ The commands saves to a file in ~/.config/gcloud. The file looks something like
|
|
68
68
|
|
69
69
|
### 4. Defaults
|
70
70
|
|
71
|
-
The library will
|
71
|
+
The library will fall back to default values when it's unable to lookup the region and zone. The default values are:
|
72
72
|
|
73
73
|
region=us-central1
|
74
74
|
zone==us-central1a
|
75
75
|
|
76
76
|
## Contributing
|
77
77
|
|
78
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/boltops-tools/gcp_data.
|
data/lib/gcp_data/version.rb
CHANGED
data/lib/gcp_data.rb
CHANGED
@@ -6,17 +6,17 @@ module GcpData
|
|
6
6
|
extend Memoist
|
7
7
|
|
8
8
|
def project
|
9
|
-
ENV['GOOGLE_PROJECT']
|
9
|
+
ENV['GOOGLE_PROJECT'] ||= gcloud_config("core/project") || creds("project_id") || raise("Unable to look up google project_id")
|
10
10
|
end
|
11
11
|
memoize :project
|
12
12
|
|
13
13
|
def region
|
14
|
-
ENV['GOOGLE_REGION']
|
14
|
+
ENV['GOOGLE_REGION'] ||= gcloud_config("compute/region") || 'us-central1'
|
15
15
|
end
|
16
16
|
memoize :region
|
17
17
|
|
18
18
|
def zone
|
19
|
-
ENV['GOOGLE_ZONE']
|
19
|
+
ENV['GOOGLE_ZONE'] ||= gcloud_config("compute/zone") || 'us-central1a'
|
20
20
|
end
|
21
21
|
memoize :zone
|
22
22
|
|
@@ -31,15 +31,67 @@ module GcpData
|
|
31
31
|
memoize :credentials
|
32
32
|
|
33
33
|
def gcloud_config(key)
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
if gcloud_installed?
|
35
|
+
# redirect stderr to stdout because (unset) is printed to stderr when a value is not set. IE:
|
36
|
+
#
|
37
|
+
# $ gcloud config get-value compute/region
|
38
|
+
# (unset)
|
39
|
+
#
|
40
|
+
command = "gcloud config get-value #{key} 2>&1"
|
41
|
+
puts "RUNNING: #{command}" if ENV['GCP_DATA_DEBUG']
|
42
|
+
val = `#{command}`.strip
|
43
|
+
val unless ['', '(unset)'].include?(val)
|
44
|
+
elsif !env_vars_set?
|
45
|
+
error_message
|
46
|
+
end
|
37
47
|
end
|
38
48
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
49
|
+
def gcloud_installed?
|
50
|
+
system("type gcloud > /dev/null 2>&1")
|
51
|
+
end
|
52
|
+
|
53
|
+
def env_vars
|
54
|
+
%w[
|
55
|
+
GOOGLE_APPLICATION_CREDENTIALS
|
56
|
+
GOOGLE_PROJECT
|
57
|
+
GOOGLE_REGION
|
58
|
+
]
|
59
|
+
end
|
60
|
+
|
61
|
+
def env_vars_set?
|
62
|
+
env_vars.all? { |var| ENV[var] }
|
63
|
+
end
|
64
|
+
|
65
|
+
def error_message(message=nil)
|
66
|
+
all_vars = format_list(env_vars)
|
67
|
+
unset_vars = format_list(env_vars.reject { |var| ENV[var] })
|
68
|
+
message ||= <<~EOL
|
69
|
+
ERROR: gcloud is not installed. Please install the gcloud CLI and configure it.
|
70
|
+
GcpData uses it to detect google cloud project, region, zone.
|
71
|
+
|
72
|
+
You can also configure these environment variables instead of installing the google CLI.
|
73
|
+
|
74
|
+
#{all_vars}
|
75
|
+
|
76
|
+
Currently, the unset vars are:
|
77
|
+
|
78
|
+
#{unset_vars}
|
79
|
+
|
80
|
+
EOL
|
81
|
+
show_error(message)
|
82
|
+
end
|
83
|
+
|
84
|
+
def show_error(message)
|
85
|
+
if ENV['GCP_DATA_RAISE_ERROR']
|
86
|
+
raise Error.new(message)
|
87
|
+
else
|
88
|
+
puts message
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def format_list(vars)
|
94
|
+
vars.map { |var| " #{var}" }.join("\n")
|
43
95
|
end
|
44
96
|
|
45
97
|
extend self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gcp_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: memoist
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
- !ruby/object:Gem::Version
|
65
65
|
version: '0'
|
66
66
|
requirements: []
|
67
|
-
rubygems_version: 3.
|
67
|
+
rubygems_version: 3.2.32
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: Simple module to get current gcp data project and region
|