Module: GcpData
- Extended by:
- GcpData, Memoist
- Included in:
- GcpData
- Defined in:
- lib/gcp_data.rb,
lib/gcp_data/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Method Summary collapse
- #credentials ⇒ Object
- #creds(name) ⇒ Object
- #env_vars ⇒ Object
- #env_vars_set? ⇒ Boolean
- #error_message(message = nil) ⇒ Object
- #format_list(vars) ⇒ Object
- #gcloud_config(key) ⇒ Object
- #gcloud_installed? ⇒ Boolean
- #project ⇒ Object
- #region ⇒ Object
- #show_error(message) ⇒ Object
- #zone ⇒ Object
Instance Method Details
#credentials ⇒ Object
27 28 29 30 |
# File 'lib/gcp_data.rb', line 27 def credentials path = ENV['GOOGLE_APPLICATION_CREDENTIALS'] JSON.load(IO.read(path)) if path && File.exist?(path) end |
#creds(name) ⇒ Object
23 24 25 |
# File 'lib/gcp_data.rb', line 23 def creds(name) credentials[name] if credentials end |
#env_vars ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/gcp_data.rb', line 53 def env_vars %w[ GOOGLE_APPLICATION_CREDENTIALS GOOGLE_PROJECT GOOGLE_REGION ] end |
#env_vars_set? ⇒ Boolean
61 62 63 |
# File 'lib/gcp_data.rb', line 61 def env_vars_set? env_vars.all? { |var| ENV[var] } end |
#error_message(message = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gcp_data.rb', line 65 def (=nil) all_vars = format_list(env_vars) unset_vars = format_list(env_vars.reject { |var| ENV[var] }) ||= <<~EOL ERROR: gcloud is not installed. Please install the gcloud CLI and configure it. GcpData uses it to detect google cloud project, region, zone. You can also configure these environment variables instead of installing the google CLI. #{all_vars} Currently, the unset vars are: #{unset_vars} EOL show_error() end |
#format_list(vars) ⇒ Object
93 94 95 |
# File 'lib/gcp_data.rb', line 93 def format_list(vars) vars.map { |var| " #{var}" }.join("\n") end |
#gcloud_config(key) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gcp_data.rb', line 33 def gcloud_config(key) if gcloud_installed? # redirect stderr to stdout because (unset) is printed to stderr when a value is not set. IE: # # $ gcloud config get-value compute/region # (unset) # command = "gcloud config get-value #{key} 2>&1" puts "RUNNING: #{command}" if ENV['GCP_DATA_DEBUG'] val = `#{command}`.strip val unless ['', '(unset)'].include?(val) elsif !env_vars_set? end end |
#gcloud_installed? ⇒ Boolean
49 50 51 |
# File 'lib/gcp_data.rb', line 49 def gcloud_installed? system("type gcloud > /dev/null 2>&1") end |
#project ⇒ Object
8 9 10 |
# File 'lib/gcp_data.rb', line 8 def project ENV['GOOGLE_PROJECT'] ||= gcloud_config("core/project") || creds("project_id") || raise("Unable to look up google project_id") end |
#region ⇒ Object
13 14 15 |
# File 'lib/gcp_data.rb', line 13 def region ENV['GOOGLE_REGION'] ||= gcloud_config("compute/region") || 'us-central1' end |
#show_error(message) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/gcp_data.rb', line 84 def show_error() if ENV['GCP_DATA_RAISE_ERROR'] raise Error.new() else puts exit 1 end end |
#zone ⇒ Object
18 19 20 |
# File 'lib/gcp_data.rb', line 18 def zone ENV['GOOGLE_ZONE'] ||= gcloud_config("compute/zone") || 'us-central1a' end |