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
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gcloud/gce.rb', line 27 def self.gce? = {} conn = [:connection] || Faraday.default_connection resp = conn.get CHECK_URI do |req| req..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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gcloud/gce.rb', line 39 def self.project_id = {} @gce ||= {} return @gce[:project_id] if @gce.key? :project_id conn = [:connection] || Faraday.default_connection conn.headers = { "Metadata-Flavor" => "Google" } resp = conn.get PROJECT_URI do |req| req..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 |