Class: Google::Cloud::Env
- Inherits:
-
Object
- Object
- Google::Cloud::Env
- Defined in:
- lib/google/cloud/env.rb,
lib/google/cloud/env/version.rb
Overview
Google Cloud hosting environment
This library provides access to information about the application's hosting environment if it is running on Google Cloud Platform. You may use this library to determine which Google Cloud product is hosting your application (e.g. App Engine, Kubernetes Engine), information about the Google Cloud project hosting the application, information about the virtual machine instance, authentication information, and so forth.
Usage
Obtain an instance of the environment info with:
require "google/cloud/env"
env = Google::Cloud.env
Then you can interrogate any fields using methods on the object.
if env.app_engine?
# App engine specific logic
end
Any item that does not apply to the current environment will return nil. For example:
unless env.app_engine?
service = env.app_engine_service_id # => nil
end
Constant Summary collapse
- VERSION =
"1.0.2".freeze
Class Method Summary collapse
-
.get ⇒ Google::Cloud::Env
Returns the global instance of Env.
Instance Method Summary collapse
-
#app_engine? ⇒ Boolean
Determine whether the application is running on Google App Engine.
-
#app_engine_memory_mb ⇒ Integer?
Returns the amount of memory reserved for the current App Engine instance, or
nil
if the current code is not running in App Engine. -
#app_engine_service_id ⇒ String?
Returns the name of the running App Engine service, or
nil
if the current code is not running in App Engine. -
#app_engine_service_version ⇒ String?
Returns the version of the running App Engine service, or
nil
if the current code is not running in App Engine. -
#cloud_shell? ⇒ Boolean
Determine whether the application is running on Google Cloud Shell.
-
#compute_engine? ⇒ Boolean
Determine whether the application is running on Google Compute Engine.
-
#initialize(env: nil, connection: nil, metadata_cache: nil) ⇒ Env
constructor
Create a new instance of the environment information.
-
#instance_attribute(key) ⇒ String?
Returns the value of the given instance attribute for the VM instance hosting the application, or
nil
if the given key does not exist or application is not running on Google Cloud. -
#instance_attribute_keys ⇒ Array<String>?
Returns an array (which may be empty) of all attribute keys present for the VM instance hosting the application, or
nil
if the application is not running on Google Cloud. -
#instance_description ⇒ String?
Returns the description field (which may be the empty string) of the VM instance hosting the application, or
nil
if the application is not running on Google Cloud. -
#instance_machine_type ⇒ String?
Returns the machine type of the VM instance hosting the application, or
nil
if the application is not running on Google Cloud. -
#instance_name ⇒ String?
Returns the name of the VM instance hosting the application, or
nil
if the application is not running on Google Cloud. -
#instance_tags ⇒ Array<String>?
Returns an array (which may be empty) of all tags set on the VM instance hosting the application, or
nil
if the application is not running on Google Cloud. -
#instance_zone ⇒ String?
Returns the zone (for example "
us-central1-c
") in which the instance hosting the application lives. -
#kubernetes_engine? ⇒ Boolean
(also: #container_engine?)
Determine whether the application is running on Google Kubernetes Engine (GKE).
-
#kubernetes_engine_cluster_name ⇒ String?
(also: #container_engine_cluster_name)
Returns the name of the Kubernetes Engine cluster hosting the application, or
nil
if the current code is not running in Kubernetes Engine. -
#kubernetes_engine_namespace_id ⇒ String?
(also: #container_engine_namespace_id)
Returns the name of the Kubernetes Engine namespace hosting the application, or
nil
if the current code is not running in Kubernetes Engine. -
#lookup_metadata(type, entry) ⇒ String?
Retrieve info from the Google Compute Engine Metadata Service.
-
#metadata? ⇒ Boolean
Determine whether the Google Compute Engine Metadata Service is running.
-
#numeric_project_id ⇒ Integer?
Returns the unique numeric ID of the project hosting the application, or
nil
if the application is not running on Google Cloud. -
#project_id ⇒ String?
Returns the unique string ID of the project hosting the application, or
nil
if the application is not running on Google Cloud. -
#raw_compute_engine? ⇒ Boolean
Determine whether the application is running on "raw" Google Compute Engine without using a higher level hosting product such as App Engine or Kubernetes Engine.
Constructor Details
#initialize(env: nil, connection: nil, metadata_cache: nil) ⇒ Env
Create a new instance of the environment information.
Most client should not need to call this directly. Obtain a singleton
instance of the information from Google::Cloud.env
. This constructor
is provided for internal testing and allows mocking of the data.
72 73 74 75 76 77 78 |
# File 'lib/google/cloud/env.rb', line 72 def initialize env: nil, connection: nil, metadata_cache: nil @metadata_cache = || {} @env = env || ::ENV @connection = connection || ::Faraday.new(url: METADATA_HOST, request: { timeout: 0.1 }) end |
Class Method Details
.get ⇒ Google::Cloud::Env
Returns the global instance of Google::Cloud::Env.
359 360 361 |
# File 'lib/google/cloud/env.rb', line 359 def self.get ::Google::Cloud.env end |
Instance Method Details
#app_engine? ⇒ Boolean
Determine whether the application is running on Google App Engine.
85 86 87 |
# File 'lib/google/cloud/env.rb', line 85 def app_engine? env["GAE_INSTANCE"] ? true : false end |
#app_engine_memory_mb ⇒ Integer?
Returns the amount of memory reserved for the current App Engine
instance, or nil
if the current code is not running in App Engine.
272 273 274 275 |
# File 'lib/google/cloud/env.rb', line 272 def app_engine_memory_mb result = env["GAE_MEMORY_MB"] result.nil? ? nil : result.to_i end |
#app_engine_service_id ⇒ String?
Returns the name of the running App Engine service, or nil
if the
current code is not running in App Engine.
252 253 254 |
# File 'lib/google/cloud/env.rb', line 252 def app_engine_service_id env["GAE_SERVICE"] end |
#app_engine_service_version ⇒ String?
Returns the version of the running App Engine service, or nil
if the
current code is not running in App Engine.
262 263 264 |
# File 'lib/google/cloud/env.rb', line 262 def app_engine_service_version env["GAE_VERSION"] end |
#cloud_shell? ⇒ Boolean
Determine whether the application is running on Google Cloud Shell.
105 106 107 |
# File 'lib/google/cloud/env.rb', line 105 def cloud_shell? env["DEVSHELL_GCLOUD_CONFIG"] ? true : false end |
#compute_engine? ⇒ Boolean
Determine whether the application is running on Google Compute Engine.
Note that most other products (e.g. App Engine, Kubernetes Engine, Cloud Shell) themselves use Compute Engine under the hood, so this method will return true for all the above products. If you want to determine whether the application is running on a "raw" Compute Engine VM without using a higher level hosting product, use #raw_compute_engine?.
121 122 123 |
# File 'lib/google/cloud/env.rb', line 121 def compute_engine? end |
#instance_attribute(key) ⇒ String?
Returns the value of the given instance attribute for the VM instance
hosting the application, or nil
if the given key does not exist or
application is not running on Google Cloud.
242 243 244 |
# File 'lib/google/cloud/env.rb', line 242 def instance_attribute key "instance", "attributes/#{key}" end |
#instance_attribute_keys ⇒ Array<String>?
Returns an array (which may be empty) of all attribute keys present
for the VM instance hosting the application, or nil
if the
application is not running on Google Cloud.
229 230 231 232 |
# File 'lib/google/cloud/env.rb', line 229 def instance_attribute_keys result = "instance", "attributes/" result.nil? ? nil : result.split end |
#instance_description ⇒ String?
Returns the description field (which may be the empty string) of the
VM instance hosting the application, or nil
if the application is
not running on Google Cloud.
183 184 185 |
# File 'lib/google/cloud/env.rb', line 183 def instance_description "instance", "description" end |
#instance_machine_type ⇒ String?
Returns the machine type of the VM instance hosting the application,
or nil
if the application is not running on Google Cloud.
205 206 207 208 |
# File 'lib/google/cloud/env.rb', line 205 def instance_machine_type result = "instance", "machine-type" result.nil? ? nil : result.split("/").last end |
#instance_name ⇒ String?
Returns the name of the VM instance hosting the application, or nil
if the application is not running on Google Cloud.
172 173 174 |
# File 'lib/google/cloud/env.rb', line 172 def instance_name env["GAE_INSTANCE"] || ("instance", "name") end |
#instance_tags ⇒ Array<String>?
Returns an array (which may be empty) of all tags set on the VM
instance hosting the application, or nil
if the application is not
running on Google Cloud.
217 218 219 220 |
# File 'lib/google/cloud/env.rb', line 217 def result = "instance", "tags" result.nil? ? nil : JSON.parse(result) end |
#instance_zone ⇒ String?
Returns the zone (for example "us-central1-c
") in which the instance
hosting the application lives. Returns nil
if the application is
not running on Google Cloud.
194 195 196 197 |
# File 'lib/google/cloud/env.rb', line 194 def instance_zone result = "instance", "zone" result.nil? ? nil : result.split("/").last end |
#kubernetes_engine? ⇒ Boolean Also known as: container_engine?
Determine whether the application is running on Google Kubernetes Engine (GKE).
95 96 97 |
# File 'lib/google/cloud/env.rb', line 95 def kubernetes_engine? kubernetes_engine_cluster_name ? true : false end |
#kubernetes_engine_cluster_name ⇒ String? Also known as: container_engine_cluster_name
Returns the name of the Kubernetes Engine cluster hosting the
application, or nil
if the current code is not running in
Kubernetes Engine.
284 285 286 |
# File 'lib/google/cloud/env.rb', line 284 def kubernetes_engine_cluster_name instance_attribute "cluster-name" end |
#kubernetes_engine_namespace_id ⇒ String? Also known as: container_engine_namespace_id
Returns the name of the Kubernetes Engine namespace hosting the
application, or nil
if the current code is not running in
Kubernetes Engine.
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/google/cloud/env.rb', line 296 def kubernetes_engine_namespace_id # The Kubernetes namespace is difficult to obtain without help from # the application using the Downward API. The environment variable # below is set in some older versions of GKE, and the file below is # present in Kubernetes as of version 1.9, but it is possible that # alternatives will need to be found in the future. env["GKE_NAMESPACE_ID"] || ::IO.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace") rescue SystemCallError nil end |
#lookup_metadata(type, entry) ⇒ String?
Retrieve info from the Google Compute Engine Metadata Service.
Returns nil
if the Metadata Service is not running or the given
data is not present.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/google/cloud/env.rb', line 338 def type, entry path = "#{METADATA_PATH_BASE}/#{type}/#{entry}" if !.include?(path) && begin resp = connection.get path do |req| req.headers = { "Metadata-Flavor" => "Google" } end [path] = resp.status == 200 ? resp.body.strip : nil rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed, Errno::EHOSTDOWN [path] = nil end end [path] end |
#metadata? ⇒ Boolean
Determine whether the Google Compute Engine Metadata Service is running.
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/google/cloud/env.rb', line 314 def unless .include?(METADATA_ROOT_PATH) begin resp = connection.get METADATA_ROOT_PATH [METADATA_ROOT_PATH] = \ resp.status == 200 && resp.headers["Metadata-Flavor"] == "Google" rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed, Errno::EHOSTDOWN [METADATA_ROOT_PATH] = false end end [METADATA_ROOT_PATH] end |
#numeric_project_id ⇒ Integer?
Returns the unique numeric ID of the project hosting the application,
or nil
if the application is not running on Google Cloud.
Caveat: this method does not work and returns nil
on CloudShell.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/google/cloud/env.rb', line 155 def numeric_project_id # CloudShell's metadata server seems to run in a dummy project. # We can get the user's normal project ID via environment variables, # but the numeric ID from the metadata service is not correct. So # disable this for CloudShell to avoid confusion. return nil if cloud_shell? result = "project", "numeric-project-id" result.nil? ? nil : result.to_i end |
#project_id ⇒ String?
Returns the unique string ID of the project hosting the application,
or nil
if the application is not running on Google Cloud.
142 143 144 145 |
# File 'lib/google/cloud/env.rb', line 142 def project_id env["GCLOUD_PROJECT"] || env["DEVSHELL_PROJECT_ID"] || ("project", "project-id") end |
#raw_compute_engine? ⇒ Boolean
Determine whether the application is running on "raw" Google Compute Engine without using a higher level hosting product such as App Engine or Kubernetes Engine.
132 133 134 |
# File 'lib/google/cloud/env.rb', line 132 def raw_compute_engine? !app_engine? && !cloud_shell? && && !kubernetes_engine? end |