Created
December 23, 2019 23:39
-
-
Save joshourisman/d16b444e677c9d09727ec718d4fd6a67 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from invoke import task | |
from tablib import Dataset | |
from yaml import load | |
try: | |
from yaml import CLoader as Loader | |
except ImportError: | |
from yaml import Loader | |
from .base import set_gcloud_config, SERVICES | |
@task(default=True) | |
def command(c, cmd, service="email"): | |
""" | |
Retrieve the appropriate configuration from Cloud Run and run a shell command | |
in a configured local Docker container. | |
""" | |
configuration = get_configuration(c, service=service) | |
container = configuration["spec"]["template"]["spec"]["containers"][0] | |
image = container["image"] | |
print(image) | |
env = {setting["name"]: setting["value"] for setting in container["env"]} | |
flags = " ".join([f"-e {key}={value}" for key, value in env.items()]) | |
c.run(f"docker run -it {flags} {image} {cmd}", pty=True) | |
@task(name="manage", pre=[set_gcloud_config]) | |
def management_command(c, cmd, service="email"): | |
""" | |
Retrieve the appropriate configuration from Cloud Run and run a Django | |
managment command in a configured local Docker container. | |
""" | |
cmd = f"pipenv run python manage.py {cmd}" | |
command(c, cmd, service=service) | |
@task(name="shell", pre=[set_gcloud_config]) | |
def django_shell(c, service="email"): | |
"""Open a Django Shell for the Google Cloud Platform environment.""" | |
management_command(c, "shell_plus", service=service) | |
@task(name="ssh", pre=[set_gcloud_config]) | |
def terminal_shell(c, service="email"): | |
"""Open a terminal shell with the Google Cloud Platform environment.""" | |
command(c, "sh", service=service) | |
def get_configuration(c, service="email"): | |
"""Retrieve the GCP configuration for the specificed service.""" | |
configuration = c.run( | |
f"gcloud beta run configurations describe {service} --platform managed", | |
hide=True, | |
).stdout | |
return load(configuration, Loader=Loader) | |
@task | |
def deployed(c): | |
"""Fetch and display the currently-deployed images for each service.""" | |
services = {} | |
for service in SERVICES.values(): | |
configuration = get_configuration(c, service=service) | |
container = configuration["spec"]["template"]["spec"]["containers"][0] | |
image = container["image"] | |
services[service] = image | |
data = Dataset(headers=("Serivce", "Image")) | |
for item in services.items(): | |
data.append(item) | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment