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

Instance Method Details

#credentialsObject



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_varsObject



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

Returns:

  • (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 error_message(message=nil)
  all_vars = format_list(env_vars)
  unset_vars = format_list(env_vars.reject { |var| ENV[var] })
  message ||= <<~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(message)
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?
    error_message
  end
end

#gcloud_installed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/gcp_data.rb', line 49

def gcloud_installed?
  system("type gcloud > /dev/null 2>&1")
end

#projectObject



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

#regionObject



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(message)
  if ENV['GCP_DATA_RAISE_ERROR']
    raise Error.new(message)
  else
    puts message
    exit 1
  end
end

#zoneObject



18
19
20
# File 'lib/gcp_data.rb', line 18

def zone
  ENV['GOOGLE_ZONE'] ||= gcloud_config("compute/zone") || 'us-central1a'
end