Module: Google::Cloud::Core::GCE

Defined in:
lib/google/cloud/core/gce.rb

Overview

Represents the Google Compute Engine environment.

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.gce?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/google/cloud/core/gce.rb', line 28

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/google/cloud/core/gce.rb', line 40

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