forked from p15670423/monkey
Added GCP instance as well
This commit is contained in:
parent
26355540bd
commit
422fe6ff06
|
@ -2,9 +2,10 @@ from typing import List
|
|||
|
||||
from common.cloud.aws.aws_instance import AwsInstance
|
||||
from common.cloud.azure.azure_instance import AzureInstance
|
||||
from common.cloud.gcp.gcp_instance import GcpInstance
|
||||
from common.cloud.instance import CloudInstance
|
||||
|
||||
all_cloud_instances = [AwsInstance(), AzureInstance()]
|
||||
all_cloud_instances = [AwsInstance(), AzureInstance(), GcpInstance()]
|
||||
|
||||
|
||||
def get_all_cloud_instances() -> List[CloudInstance]:
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
import logging
|
||||
import requests
|
||||
|
||||
from common.cloud.environment_names import GCP
|
||||
from common.cloud.instance import CloudInstance
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
GCP_METADATA_SERVICE_URL = "http://metadata.google.internal/"
|
||||
|
||||
|
||||
class GcpInstance(CloudInstance):
|
||||
def is_instance(self):
|
||||
return self.on_gcp
|
||||
|
||||
def get_cloud_provider_name(self) -> str:
|
||||
return GCP
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Determines if on GCP.
|
||||
"""
|
||||
self.on_gcp = False
|
||||
|
||||
try:
|
||||
# If not on GCP, this domain shouldn't resolve.
|
||||
response = requests.get(GCP_METADATA_SERVICE_URL)
|
||||
|
||||
if response:
|
||||
logger.debug("Got response, so probably on GCP. Trying to parse.")
|
||||
self.on_gcp = True
|
||||
|
||||
if "Metadata-Flavor" not in response.headers:
|
||||
logger.warning("Got unexpected GCP Metadata format")
|
||||
else:
|
||||
if not response.headers["Metadata-Flavor"] == "Google":
|
||||
logger.warning("Got unexpected Metadata flavor: {}".format(response.headers["Metadata-Flavor"]))
|
||||
else:
|
||||
logger.warning("On GCP, but metadata response not ok: {}".format(response.status_code))
|
||||
except requests.RequestException:
|
||||
logger.debug("Failed to get response from GCP metadata service: This instance is not on GCP.")
|
||||
self.on_gcp = False
|
Loading…
Reference in New Issue