Module: Gcloud::GCE

Defined in:
lib/gcloud/gce.rb

Overview

Represents the Google Compute Engine environment.

Constant Summary collapse

CHECK_URI =

:nodoc:

"http://169.254.169.254"
PROJECT_URI =
"#{CHECK_URI}/computeMetadata/v1/project/project-id"

Class Method Summary collapse

Class Method Details

.gce?(options = {}) ⇒ Boolean

rubocop:disable all Disabled rubocop because this is private and we don’t need more methods.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gcloud/gce.rb', line 30

def self.gce? options = {}
  conn = options[:connection] || Faraday.default_connection
  resp = conn.get CHECK_URI do |req|
    req.options.timeout = 0.1
  end
  return false unless resp.status == 200
  return false unless resp.headers.key? "Metadata-Flavor"
  return resp.headers["Metadata-Flavor"] == "Google"
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
  return false
end

.project_id(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gcloud/gce.rb', line 42

def self.project_id options = {}
  @gce ||= {}
  return @gce[:project_id] if @gce.key? :project_id
  conn = options[:connection] || Faraday.default_connection
  conn.headers = { "Metadata-Flavor" => "Google" }
  resp = conn.get PROJECT_URI do |req|
    req.options.timeout = 0.1
  end
  if resp.status == 200
    @gce[:project_id] = resp.body
  else
    @gce[:project_id] = nil
  end
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
  @gce ||= {}
  @gce[:project_id] = nil
end