forked from claffin/cloudproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request claffin#38 from dusancz/feature/google-cloud-provider
Add Google Cloud provider
- Loading branch information
Showing
8 changed files
with
336 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import json | ||
import uuid | ||
|
||
from loguru import logger | ||
|
||
import googleapiclient.discovery | ||
from google.oauth2 import service_account | ||
|
||
from cloudproxy.providers.config import set_auth | ||
from cloudproxy.providers.settings import config | ||
|
||
gcp = config["providers"]["gcp"] | ||
try: | ||
credentials = service_account.Credentials.from_service_account_info( | ||
json.loads(gcp["secrets"]["service_account_key"]) | ||
) | ||
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials) | ||
except TypeError: | ||
logger.error("GCP -> Invalid service account key") | ||
|
||
|
||
def create_proxy(): | ||
image_response = compute.images().getFromFamily( | ||
project=gcp["image_project"], | ||
family=gcp["image_family"] | ||
).execute() | ||
source_disk_image = image_response['selfLink'] | ||
|
||
body = { | ||
'name': 'cloudproxy-' + str(uuid.uuid4()), | ||
'machineType': | ||
f"zones/{gcp['zone']}/machineTypes/{gcp['size']}", | ||
'tags': { | ||
'items': [ | ||
'cloudproxy' | ||
] | ||
}, | ||
"labels": { | ||
'cloudproxy': 'cloudproxy' | ||
}, | ||
'disks': [ | ||
{ | ||
'boot': True, | ||
'autoDelete': True, | ||
'initializeParams': { | ||
'sourceImage': source_disk_image, | ||
} | ||
} | ||
], | ||
'networkInterfaces': [{ | ||
'network': 'global/networks/default', | ||
'accessConfigs': [ | ||
{ | ||
'name': 'External NAT', | ||
'type': 'ONE_TO_ONE_NAT', | ||
'networkTier': 'STANDARD' | ||
} | ||
] | ||
}], | ||
'metadata': { | ||
'items': [{ | ||
'key': 'startup-script', | ||
'value': set_auth(config["auth"]["username"], config["auth"]["password"]) | ||
}] | ||
} | ||
} | ||
|
||
return compute.instances().insert( | ||
project=gcp["project"], | ||
zone=gcp["zone"], | ||
body=body | ||
).execute() | ||
|
||
def delete_proxy(name): | ||
try: | ||
return compute.instances().delete( | ||
project=gcp["project"], | ||
zone=gcp["zone"], | ||
instance=name | ||
).execute() | ||
except(googleapiclient.errors.HttpError): | ||
logger.info(f"GCP --> HTTP Error when trying to delete proxy {name}. Probably has already been deleted.") | ||
return None | ||
|
||
def stop_proxy(name): | ||
try: | ||
return compute.instances().stop( | ||
project=gcp["project"], | ||
zone=gcp["zone"], | ||
instance=name | ||
).execute() | ||
except(googleapiclient.errors.HttpError): | ||
logger.info(f"GCP --> HTTP Error when trying to stop proxy {name}. Probably has already been deleted.") | ||
return None | ||
|
||
def start_proxy(name): | ||
try: | ||
return compute.instances().start( | ||
project=gcp["project"], | ||
zone=gcp["zone"], | ||
instance=name | ||
).execute() | ||
except(googleapiclient.errors.HttpError): | ||
logger.info(f"GCP --> HTTP Error when trying to start proxy {name}. Probably has already been deleted.") | ||
return None | ||
|
||
def list_instances(): | ||
result = compute.instances().list( | ||
project=gcp["project"], | ||
zone=gcp["zone"], | ||
filter='labels.cloudproxy eq cloudproxy' | ||
).execute() | ||
return result['items'] if 'items' in result else [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import datetime | ||
import itertools | ||
|
||
from loguru import logger | ||
|
||
from cloudproxy.check import check_alive | ||
from cloudproxy.providers.gcp.functions import ( | ||
list_instances, | ||
create_proxy, | ||
delete_proxy, | ||
stop_proxy, | ||
start_proxy, | ||
) | ||
from cloudproxy.providers.settings import delete_queue, restart_queue, config | ||
|
||
def gcp_deployment(min_scaling): | ||
total_instances = len(list_instances()) | ||
if min_scaling < total_instances: | ||
logger.info("Overprovisioned: GCP destroying.....") | ||
for instance in itertools.islice( | ||
list_instances(), 0, (total_instances - min_scaling) | ||
): | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP']}" | ||
delete_proxy(instance['name']) | ||
logger.info("Destroyed: GCP -> " + msg) | ||
if min_scaling - total_instances < 1: | ||
logger.info("Minimum GCP instances met") | ||
else: | ||
total_deploy = min_scaling - total_instances | ||
logger.info("Deploying: " + str(total_deploy) + " GCP instances") | ||
for _ in range(total_deploy): | ||
create_proxy() | ||
logger.info("Deployed") | ||
return len(list_instances()) | ||
|
||
def gcp_check_alive(): | ||
ip_ready = [] | ||
for instance in list_instances(): | ||
try: | ||
elapsed = datetime.datetime.now( | ||
datetime.timezone.utc | ||
) - datetime.datetime.strptime(instance["creationTimestamp"], '%Y-%m-%dT%H:%M:%S.%f%z') | ||
|
||
if config["age_limit"] > 0 and elapsed > datetime.timedelta(seconds=config["age_limit"]): | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP'] if 'natIP' in access_configs else ''}" | ||
delete_proxy(instance['name']) | ||
logger.info("Recycling instance, reached age limit -> " + msg) | ||
|
||
elif instance['status'] == "TERMINATED": | ||
logger.info("Waking up: GCP -> Instance " + instance['name']) | ||
started = start_proxy(instance['name']) | ||
if not started: | ||
logger.info("Could not wake up, trying again later.") | ||
|
||
elif instance['status'] == "STOPPING": | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP'] if 'natIP' in access_configs else ''}" | ||
logger.info("Stopping: GCP -> " + msg) | ||
|
||
elif instance['status'] == "PROVISIONING" or instance['status'] == "STAGING": | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP'] if 'natIP' in access_configs else ''}" | ||
logger.info("Provisioning: GCP -> " + msg) | ||
|
||
# If none of the above, check if alive or not. | ||
elif check_alive(instance['networkInterfaces'][0]['accessConfigs'][0]['natIP']): | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP']}" | ||
logger.info("Alive: GCP -> " + msg) | ||
ip_ready.append(access_configs['natIP']) | ||
|
||
else: | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
msg = f"{instance['name']} {access_configs['natIP']}" | ||
if elapsed > datetime.timedelta(minutes=10): | ||
delete_proxy(instance['name']) | ||
logger.info("Destroyed: took too long GCP -> " + msg) | ||
else: | ||
logger.info("Waiting: GCP -> " + msg) | ||
except (TypeError, KeyError): | ||
logger.info("Pending: GCP -> Allocating IP") | ||
return ip_ready | ||
|
||
def gcp_check_delete(): | ||
for instance in list_instances(): | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
if 'natIP' in access_configs and access_configs['natIP'] in delete_queue: | ||
msg = f"{instance['name']}, {access_configs['natIP']}" | ||
delete_proxy(instance['name']) | ||
logger.info("Destroyed: not wanted -> " + msg) | ||
delete_queue.remove(access_configs['natIP']) | ||
|
||
def gcp_check_stop(): | ||
for instance in list_instances(): | ||
access_configs = instance['networkInterfaces'][0]['accessConfigs'][0] | ||
if 'natIP' in access_configs and access_configs['natIP'] in restart_queue: | ||
msg = f"{instance['name']}, {access_configs['natIP']}" | ||
stop_proxy(instance['name']) | ||
logger.info("Stopped: getting new IP -> " + msg) | ||
restart_queue.remove(access_configs['natIP']) | ||
|
||
def gcp_start(): | ||
gcp_check_delete() | ||
gcp_check_stop() | ||
gcp_deployment(config["providers"]["gcp"]["scaling"]["min_scaling"]) | ||
ip_ready = gcp_check_alive() | ||
return ip_ready |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.