-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c59af06
commit 20d665b
Showing
2 changed files
with
178 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import subprocess | ||
import uuid | ||
import time | ||
|
||
import click | ||
|
||
|
||
def wait_for_instance(name): | ||
while True: | ||
try: | ||
subprocess.check_output( | ||
["gcloud", "compute", "ssh", f"audius@{name}", "--", "exit 0"] | ||
) | ||
break | ||
except subprocess.CalledProcessError: | ||
time.sleep(1) | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command() | ||
@click.pass_context | ||
def create_image(ctx): | ||
name = f"audius-cloud-{uuid.uuid4()}" | ||
|
||
ctx.invoke( | ||
create_raw, | ||
name=name, | ||
gcloud_args=[ | ||
"--boot-disk-size=64G", | ||
"--no-boot-disk-auto-delete", | ||
"--boot-disk-type=pd-balanced", | ||
], | ||
) | ||
|
||
ctx.invoke(delete, gcloud_args=[name]) | ||
|
||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"images", | ||
"create", | ||
"audius-protocol-dev", | ||
f"--source-disk={name}", | ||
], | ||
check=True, | ||
) | ||
|
||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"disks", | ||
"delete", | ||
"--quiet", # disable prompt | ||
name, | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
@cli.command(context_settings=dict(ignore_unknown_options=True)) | ||
@click.argument("name") | ||
@click.argument("gcloud-args", nargs=-1, type=click.UNPROCESSED) | ||
def create(name, gcloud_args): | ||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"instances", | ||
"create", | ||
"--machine-type=e2-custom-6-24576", | ||
"--boot-disk-size=256G", | ||
"--image=audius-protocol-dev", | ||
name, | ||
*gcloud_args, | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
@cli.command(context_settings=dict(ignore_unknown_options=True)) | ||
@click.argument("name") | ||
@click.argument("gcloud-args", nargs=-1, type=click.UNPROCESSED) | ||
def create_raw(name, gcloud_args): | ||
click.secho(f"Creating instance {name}", fg="green") | ||
|
||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"instances", | ||
"create", | ||
"--machine-type=e2-custom-6-24576", | ||
"--boot-disk-size=256G", | ||
name, | ||
*gcloud_args, | ||
], | ||
check=True, | ||
) | ||
|
||
click.secho(f"Waiting for instance {name} to be ready", fg="green") | ||
|
||
wait_for_instance(name) | ||
|
||
click.secho("Setting up audius-protocol on instance", fg="green") | ||
|
||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"ssh", | ||
f"audius@{name}", | ||
"--", | ||
"curl 'https://raw.githubusercontent.com/AudiusProject/audius-protocol/master/dev-tools/setup.sh' | bash", | ||
], | ||
check=True, | ||
) | ||
|
||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"ssh", | ||
f"audius@{name}", | ||
"--", | ||
"cd ~/audius-protocol && PROTOCOL_DIR=$PWD ~/.local/bin/audius-compose build && docker compose pull", | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
@cli.command(context_settings=dict(ignore_unknown_options=True)) | ||
@click.argument("gcloud-args", nargs=-1, type=click.UNPROCESSED) | ||
def delete(gcloud_args): | ||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"instances", | ||
"delete", | ||
"--quiet", # disable prompt | ||
*gcloud_args, | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
@cli.command(context_settings=dict(ignore_unknown_options=True)) | ||
@click.argument("gcloud-args", nargs=-1, type=click.UNPROCESSED) | ||
def describe(gcloud_args): | ||
subprocess.run( | ||
[ | ||
"gcloud", | ||
"compute", | ||
"instances", | ||
"describe", | ||
*gcloud_args, | ||
], | ||
check=True, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
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