class Google::Cloud::Env
“‘
end
service = env.app_engine_service_id # => nil
unless env.app_engine?
“`ruby
For example:
Any item that does not apply to the current environment will return nil.
“`
end
# App engine specific logic
if env.app_engine?
“`ruby
Then you can interrogate any fields using methods on the object.
“`
env = Google::Cloud.env
require “google/cloud/env”
“`ruby
Obtain an instance of the environment info with:
### Usage
virtual machine instance, authentication information, and so forth.
Google Cloud project hosting the application, information about the
application (e.g. App Engine, Kubernetes Engine), information about the
use this library to determine which Google Cloud product is hosting your
hosting environment if it is running on Google Cloud Platform. You can
This library provides access to information about the application’s
## Google Cloud hosting environment
#
def self.get
-
(Google::Cloud::Env)-
def self.get ::Google::Cloud.env end
def app_engine?
-
(boolean)-
def app_engine? variables["GAE_INSTANCE"] ? true : false end
def app_engine_flexible?
-
(boolean)-
def app_engine_flexible? app_engine? && variables["GAE_ENV"] != "standard" end
def app_engine_memory_mb
-
(Integer, nil)-
def app_engine_memory_mb result = variables["GAE_MEMORY_MB"] result&.to_i end
def app_engine_service_id
-
(String, nil)-
def app_engine_service_id variables["GAE_SERVICE"] end
def app_engine_service_version
-
(String, nil)-
def app_engine_service_version variables["GAE_VERSION"] end
def app_engine_standard?
-
(boolean)-
def app_engine_standard? app_engine? && variables["GAE_ENV"] == "standard" end
def cloud_shell?
-
(boolean)-
def cloud_shell? variables["GOOGLE_CLOUD_SHELL"] ? true : false end
def compute_engine?
-
(boolean)-
def compute_engine? compute_smbios.google_compute? end
def ensure_metadata timeout: nil
-
(MetadataServerNotResponding)- if we were unable to confirm
Returns:
-
(:confirmed)- if we were able to confirm connection.
Parameters:
-
timeout(Numeric, nil) -- a timeout in seconds, or nil to wait
def ensure_metadata timeout: nil compute_metadata.ensure_existence timeout: timeout end
def initialize
instance of the information from `Google::Cloud.env`.
Most clients should not need to call this directly. Obtain a singleton
Create a new instance of the environment information.
#
def initialize @variables = Variables.new @file_system = FileSystem.new @compute_smbios = ComputeSMBIOS.new @compute_metadata = ComputeMetadata.new variables: @variables, compute_smbios: @compute_smbios end
def instance_attribute key
-
(String, nil)-
Parameters:
-
key(String) -- Attribute key to look up.
def instance_attribute key compute_metadata.lookup "instance/attributes/#{key}" rescue MetadataServerNotResponding nil end
def instance_attribute_keys
-
(Array-, nil)
def instance_attribute_keys result = compute_metadata.lookup "instance/attributes/" result&.split rescue MetadataServerNotResponding nil end
def instance_description
-
(String, nil)-
def instance_description compute_metadata.lookup "instance/description" rescue MetadataServerNotResponding nil end
def instance_machine_type
-
(String, nil)-
def instance_machine_type result = compute_metadata.lookup "instance/machine-type" result&.split("/")&.last rescue MetadataServerNotResponding nil end
def instance_name
-
(String, nil)-
def instance_name variables["GAE_INSTANCE"] || compute_metadata.lookup("instance/name") rescue MetadataServerNotResponding nil end
def instance_tags
-
(Array-, nil)
def instance_tags result = compute_metadata.lookup "instance/tags" result.nil? ? nil : JSON.parse(result) rescue MetadataServerNotResponding nil end
def instance_zone
-
(String, nil)-
def instance_zone result = compute_metadata.lookup "instance/zone" result&.split("/")&.last rescue MetadataServerNotResponding nil end
def knative?
-
(boolean)-
def knative? variables["K_SERVICE"] ? true : false end
def knative_service_id
-
(String, nil)-
def knative_service_id variables["K_SERVICE"] end
def knative_service_revision
-
(String, nil)-
def knative_service_revision variables["K_REVISION"] end
def kubernetes_engine?
-
(boolean)-
def kubernetes_engine? kubernetes_engine_cluster_name ? true : false end
def kubernetes_engine_cluster_name
-
(String, nil)-
def kubernetes_engine_cluster_name instance_attribute "cluster-name" rescue MetadataServerNotResponding nil end
def kubernetes_engine_namespace_id
-
(String, nil)-
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. variables["GKE_NAMESPACE_ID"] || file_system.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace") end
def logging_agent_expected?
-
(boolean)-
def logging_agent_expected? compute_engine? && !cloud_shell? && (app_engine? || knative? || kubernetes_engine?) end
def lookup_metadata type, entry, query: nil
-
(MetadataServerNotResponding)- if the Metadata Server is not
Returns:
-
(nil)- if there is no data for the specified type and entry -
(String)- the data
Parameters:
-
query(Hash{String => String}) -- Any additional query parameters -
entry(String) -- Metadata entry path to look up. -
type(String) -- Type of metadata to look up. Currently supported
def lookup_metadata type, entry, query: nil compute_metadata.lookup "#{type}/#{entry}", query: query end
def lookup_metadata_response type, entry, query: nil
-
(MetadataServerNotResponding)- if the Metadata Server is not
Returns:
-
(Google::Cloud::Env::ComputeMetadata::Response)- the response
Parameters:
-
query(Hash{String => String}) -- Any additional query parameters -
entry(String) -- Metadata entry path to look up. -
type(String) -- Type of metadata to look up. Currently supported
def lookup_metadata_response type, entry, query: nil compute_metadata.lookup_response "#{type}/#{entry}", query: query end
def metadata?
-
(boolean)-
def metadata? compute_metadata.check_existence == :confirmed end
def numeric_project_id
-
(Integer, nil)-
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 = begin compute_metadata.lookup "project/numeric-project-id" rescue MetadataServerNotResponding nil end result&.to_i end
def project_id
-
(String, nil)-
def project_id variables["GOOGLE_CLOUD_PROJECT"] || variables["GCLOUD_PROJECT"] || variables["DEVSHELL_PROJECT_ID"] || compute_metadata.lookup("project/project-id") rescue MetadataServerNotResponding nil end
def raw_compute_engine?
-
(boolean)-
def raw_compute_engine? compute_engine? && !knative? && !app_engine? && !cloud_shell? && !kubernetes_engine? end