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::EnvReturns the global instance of Env.
#app_engine?
def app_engine?() -> booleanDetermine whether the application is running on Google App Engine.
- (boolean)
#app_engine_flexible?
def app_engine_flexible?() -> booleanDetermine whether the application is running on Google App Engine Flexible Environment.
- (boolean)
#app_engine_memory_mb
def app_engine_memory_mb() -> Integer, nilReturns the amount of memory reserved for the current App Engine
instance, or nil if the current code is not running in App Engine.
- (Integer, nil)
#app_engine_service_id
def app_engine_service_id() -> String, nilReturns the name of the running App Engine service, or nil if the
current code is not running in App Engine.
- (String, nil)
#app_engine_service_name
def app_engine_service_name() -> String, nilReturns the name of the running App Engine service, or nil if the
current code is not running in App Engine.
- (String, nil)
#app_engine_service_version
def app_engine_service_version() -> String, nilReturns the version of the running App Engine service, or nil if the
current code is not running in App Engine.
- (String, nil)
#app_engine_standard?
def app_engine_standard?() -> booleanDetermine whether the application is running on Google App Engine Standard Environment.
- (boolean)
#cloud_shell?
def cloud_shell?() -> booleanDetermine whether the application is running on Google Cloud Shell.
- (boolean)
#compute_engine?
def compute_engine?() -> booleanDetermine 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?.
- (boolean)
#compute_metadata
def compute_metadata() -> Google::Cloud::Env::ComputeMetadataThe 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::ComputeSMBIOSThe 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?() -> booleanDetermine whether the application is running on Google Kubernetes Engine (GKE).
- (boolean)
#container_engine_cluster_name
def container_engine_cluster_name() -> String, nilReturns the name of the Kubernetes Engine cluster hosting the
application, or nil if the current code is not running in
Kubernetes Engine.
- (String, nil)
#container_engine_namespace_id
def container_engine_namespace_id() -> String, nilReturns the name of the Kubernetes Engine namespace hosting the
application, or nil if the current code is not running in
Kubernetes Engine.
- (String, nil)
#ensure_metadata
def ensure_metadata(timeout: nil) -> :confirmedAssert 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.
- timeout (Numeric, nil) (defaults to: nil) — a timeout in seconds, or nil to wait until we have conclusively decided one way or the other.
- (:confirmed) — if we were able to confirm connection.
- (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::FileSystemThe 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() -> EnvCreate 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.
- (Env) — a new instance of Env
#instance_attribute
def instance_attribute(key) -> String, nilReturns 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.
- key (String) — Attribute key to look up.
- (String, nil)
#instance_attribute_keys
def instance_attribute_keys() -> Array<String>, nilReturns 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.
- (Array<String>, nil)
#instance_description
def instance_description() -> String, nilReturns 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.
- (String, nil)
#instance_machine_type
def instance_machine_type() -> String, nilReturns the machine type of the VM instance hosting the application,
or nil if the application is not running on Google Cloud.
- (String, nil)
#instance_name
def instance_name() -> String, nilReturns the name of the VM instance hosting the application, or nil
if the application is not running on Google Cloud.
- (String, nil)
#instance_tags
def instance_tags() -> Array<String>, nilReturns 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.
- (Array<String>, nil)
#instance_zone
def instance_zone() -> String, nilReturns 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.
- (String, nil)
#knative?
def knative?() -> booleanDetermine whether the application is running on a Knative-based hosting platform, such as Cloud Run or Cloud Functions.
- (boolean)
#knative_service_id
def knative_service_id() -> String, nilReturns the name of the running Knative service, or nil if the
current code is not running on Knative.
- (String, nil)
#knative_service_name
def knative_service_name() -> String, nilReturns the name of the running Knative service, or nil if the
current code is not running on Knative.
- (String, nil)
#knative_service_revision
def knative_service_revision() -> String, nilReturns the revision of the running Knative service, or nil if the
current code is not running on Knative.
- (String, nil)
#kubernetes_engine?
def kubernetes_engine?() -> booleanDetermine whether the application is running on Google Kubernetes Engine (GKE).
- (boolean)
#kubernetes_engine_cluster_name
def kubernetes_engine_cluster_name() -> String, nilReturns the name of the Kubernetes Engine cluster hosting the
application, or nil if the current code is not running in
Kubernetes Engine.
- (String, nil)
#kubernetes_engine_namespace_id
def kubernetes_engine_namespace_id() -> String, nilReturns the name of the Kubernetes Engine namespace hosting the
application, or nil if the current code is not running in
Kubernetes Engine.
- (String, nil)
#logging_agent_expected?
def logging_agent_expected?() -> booleanDetermine 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.
- (boolean)
#lookup_metadata
def lookup_metadata(type, entry, query: nil) -> String, nilRetrieve info from the Google Compute Engine Metadata Service.
Returns nil if the given data is not present.
- 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.
- (String) — the data
- (nil) — if there is no data for the specified type and entry
- (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.
#lookup_metadata_response
def lookup_metadata_response(type, entry, query: nil) -> Google::Cloud::Env::ComputeMetadata::ResponseRetrieve 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.
- 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.
- (Google::Cloud::Env::ComputeMetadata::Response) — the response
- (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.
#metadata?
def metadata?() -> booleanDetermine 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.
- (boolean)
#numeric_project_id
def numeric_project_id() -> Integer, nilReturns 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.
- (Integer, nil)
#project_id
def project_id() -> String, nilReturns the unique string ID of the project hosting the application,
or nil if the application is not running on Google Cloud.
- (String, nil)
#raw_compute_engine?
def raw_compute_engine?() -> booleanDetermine whether the application is running on "raw" Google Compute Engine without using a higher level hosting product such as App Engine or Kubernetes Engine.
- (boolean)
#variables
def variables() -> Google::Cloud::Env::VariablesThe 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.3.1".freeze
Library version