Google Cloud Platform environment queries - Class Google::Cloud::Env (v2.2.2)

Reference documentation and code samples for the Google Cloud Platform environment queries class Google::Cloud::Env.

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 can 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:

ruby require "google/cloud/env" env = Google::Cloud.env

Then you can interrogate any fields using methods on the object.

ruby if env.app_engine? # App engine specific logic end

Any item that does not apply to the current environment will return nil. For example:

ruby unless env.app_engine? service = env.app_engine_service_id # => nil end

Inherits

  • Object

Methods

.get

  def 
  
 self 
 . 
 get 
 () 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 

Returns the global instance of Env .

Returns

#app_engine?

  def 
  
 app_engine? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google App Engine.

Returns
  • (boolean)

#app_engine_flexible?

  def 
  
 app_engine_flexible? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google App Engine Flexible Environment.

Returns
  • (boolean)

#app_engine_memory_mb

  def 
  
 app_engine_memory_mb 
 () 
  
 - 
>  
 Integer 
 , 
  
 nil 
 

Returns the amount of memory reserved for the current App Engine instance, or nil if the current code is not running in App Engine.

Returns
  • (Integer, nil)

#app_engine_service_id

  def 
  
 app_engine_service_id 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the running App Engine service, or nil if the current code is not running in App Engine.

Returns
  • (String, nil)

#app_engine_service_name

  def 
  
 app_engine_service_name 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the running App Engine service, or nil if the current code is not running in App Engine.

Returns
  • (String, nil)

#app_engine_service_version

  def 
  
 app_engine_service_version 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the version of the running App Engine service, or nil if the current code is not running in App Engine.

Returns
  • (String, nil)

#app_engine_standard?

  def 
  
 app_engine_standard? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google App Engine Standard Environment.

Returns
  • (boolean)

#cloud_shell?

  def 
  
 cloud_shell? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google Cloud Shell.

Returns
  • (boolean)

#compute_engine?

  def 
  
 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? .

Returns
  • (boolean)
  def 
  
 compute_metadata 
 () 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 :: 
 ComputeMetadata 
 

The compute metadata access object. Use this to make direct calls to compute metadata or configure how metadata server queries are done, or to mock out the metadata server for testing.

#compute_smbios

  def 
  
 compute_smbios 
 () 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 :: 
 ComputeSMBIOS 
 

The compute SMBIOS access object. Use this to make direct queries for compute SMBIOS information, or to mock out the SMBIOS for testing.

#container_engine?

  def 
  
 container_engine? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google Kubernetes Engine (GKE).

Returns
  • (boolean)

#container_engine_cluster_name

  def 
  
 container_engine_cluster_name 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the Kubernetes Engine cluster hosting the application, or nil if the current code is not running in Kubernetes Engine.

Returns
  • (String, nil)

#container_engine_namespace_id

  def 
  
 container_engine_namespace_id 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the Kubernetes Engine namespace hosting the application, or nil if the current code is not running in Kubernetes Engine.

Returns
  • (String, nil)
  def 
  
 ensure_metadata 
 ( 
 timeout 
 : 
  
 nil 
 ) 
  
 - 
>  
 :confirmed 
 

Assert that the Metadata Server should be present, and wait for a confirmed connection to ensure it is up. In general, this will run at most ComputeMetadata::DEFAULT_WARMUP_TIME seconds to wait out the expected maximum warmup time, but a shorter timeout can be provided.

This method is useful call during application initialization to wait for the Metadata Server to warm up and ensure that subsequent lookups should succeed.

Parameter
  • timeout(Numeric, nil) (defaults to: nil) — a timeout in seconds, or nil to wait until we have conclusively decided one way or the other.
Returns
  • (:confirmed) — if we were able to confirm connection.
Raises
  • ( MetadataServerNotResponding ) — if we were unable to confirm connection with the Metadata Server, either because the timeout expired or because the server seems to be down

#file_system

  def 
  
 file_system 
 () 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 :: 
 FileSystem 
 

The variables access object. Use this to make direct queries for information from the file system, or to mock out the file system for testing.

#initialize

  def 
  
 initialize 
 () 
  
 - 
>  
 Env 
 

Create a new instance of the environment information. Most clients should not need to call this directly. Obtain a singleton instance of the information from Google::Cloud.env .

Returns
  • ( Env ) — a new instance of Env

#instance_attribute

  def 
  
 instance_attribute 
 ( 
 key 
 ) 
  
 - 
>  
 String 
 , 
  
 nil 
 

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.

Parameter
  • key(String) — Attribute key to look up.
Returns
  • (String, nil)

#instance_attribute_keys

  def 
  
 instance_attribute_keys 
 () 
  
 - 
>  
 Array<String> 
 , 
  
 nil 
 

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.

Returns
  • (Array<String>, nil)

#instance_description

  def 
  
 instance_description 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

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.

Returns
  • (String, nil)

#instance_machine_type

  def 
  
 instance_machine_type 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the machine type of the VM instance hosting the application, or nil if the application is not running on Google Cloud.

Returns
  • (String, nil)

#instance_name

  def 
  
 instance_name 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the VM instance hosting the application, or nil if the application is not running on Google Cloud.

Returns
  • (String, nil)

#instance_tags

  def 
  
 instance_tags 
 () 
  
 - 
>  
 Array<String> 
 , 
  
 nil 
 

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.

Returns
  • (Array<String>, nil)

#instance_zone

  def 
  
 instance_zone 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

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.

Returns
  • (String, nil)

#knative?

  def 
  
 knative? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on a Knative-based hosting platform, such as Cloud Run or Cloud Functions.

Returns
  • (boolean)

#knative_service_id

  def 
  
 knative_service_id 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the running Knative service, or nil if the current code is not running on Knative.

Returns
  • (String, nil)

#knative_service_name

  def 
  
 knative_service_name 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the running Knative service, or nil if the current code is not running on Knative.

Returns
  • (String, nil)

#knative_service_revision

  def 
  
 knative_service_revision 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the revision of the running Knative service, or nil if the current code is not running on Knative.

Returns
  • (String, nil)

#kubernetes_engine?

  def 
  
 kubernetes_engine? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running on Google Kubernetes Engine (GKE).

Returns
  • (boolean)

#kubernetes_engine_cluster_name

  def 
  
 kubernetes_engine_cluster_name 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the Kubernetes Engine cluster hosting the application, or nil if the current code is not running in Kubernetes Engine.

Returns
  • (String, nil)

#kubernetes_engine_namespace_id

  def 
  
 kubernetes_engine_namespace_id 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the name of the Kubernetes Engine namespace hosting the application, or nil if the current code is not running in Kubernetes Engine.

Returns
  • (String, nil)

#logging_agent_expected?

  def 
  
 logging_agent_expected? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the application is running in an environment where a Google Cloud logging agent is expected to be running. In such an environment, we expect that the standard output and error streams are likely to be parsed by the logging agent and log entries are written to the Google Cloud Logging service.

Returns
  • (boolean)
  def 
  
 lookup_metadata 
 ( 
 type 
 , 
  
 entry 
 , 
  
 query 
 : 
  
 nil 
 ) 
  
 - 
>  
 String 
 , 
  
 nil 
 

Retrieve info from the Google Compute Engine Metadata Service. Returns nil if the given data is not present.

Parameters
  • type(String) — Type of metadata to look up. Currently supported values are "project" and "instance".
  • entry(String) — Metadata entry path to look up.
  • query(Hash{String => String}) (defaults to: nil) — Any additional query parameters to send with the request.
Returns
  • (String) — the data
  • (nil) — if there is no data for the specified type and entry
Raises
  • ( MetadataServerNotResponding ) — if the Metadata Server is not responding. This could either be because the metadata service is not present in the current environment, or if it is expected to be present but is overloaded or has not finished initializing.
  def 
  
 lookup_metadata_response 
 ( 
 type 
 , 
  
 entry 
 , 
  
 query 
 : 
  
 nil 
 ) 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 :: 
 ComputeMetadata 
 :: 
 Response 
 

Retrieve an HTTP response from the Google Compute Engine Metadata Service. Returns a ComputeMetadata::Response with a status code, data, and headers. The response could be 200 for success, 404 if the given entry is not present, or other HTTP result code for authorization or other errors.

Parameters
  • type(String) — Type of metadata to look up. Currently supported values are "project" and "instance".
  • entry(String) — Metadata entry path to look up.
  • query(Hash{String => String}) (defaults to: nil) — Any additional query parameters to send with the request.
Returns
Raises
  • ( MetadataServerNotResponding ) — if the Metadata Server is not responding. This could either be because the metadata service is not present in the current environment, or if it is expected to be present but is overloaded or has not finished initializing.
  def 
  
 metadata? 
 () 
  
 - 
>  
 boolean 
 

Determine whether the Google Compute Engine Metadata Service is running.

This method is conservative. It may block for a short period (up to about 1.5 seconds) while attempting to contact the server, but if this fails, this method will return false, even though it is possible that a future call could succeed. In particular, this might happen in environments where there is a warmup time for the Metadata Server. Early calls before the Server has warmed up may return false, while later calls return true.

Returns
  • (boolean)

#numeric_project_id

  def 
  
 numeric_project_id 
 () 
  
 - 
>  
 Integer 
 , 
  
 nil 
 

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.

Returns
  • (Integer, nil)

#project_id

  def 
  
 project_id 
 () 
  
 - 
>  
 String 
 , 
  
 nil 
 

Returns the unique string ID of the project hosting the application, or nil if the application is not running on Google Cloud.

Returns
  • (String, nil)

#raw_compute_engine?

  def 
  
 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.

Returns
  • (boolean)

#variables

  def 
  
 variables 
 () 
  
 - 
>  
 Google 
 :: 
 Cloud 
 :: 
 Env 
 :: 
 Variables 
 

The variables access object. Use this to make direct queries for environment variable information, or to mock out environment variables for testing.

Constants

VERSION

value: "2.2.2".freeze
Library version

Design a Mobile Site
View Site in Mobile | Classic
Share by: