Files
nixos-on-gcp/upload.py
T
2024-10-27 08:49:38 -04:00

49 lines
1.3 KiB
Python

from pathlib import Path
import sys
import click
from subprocess import check_call
def index_of(str, char, cnt):
for i, char_ in enumerate(str):
if char == char_:
cnt -= 1
if cnt == 0:
return i
return -1
@click.command()
@click.option('--file', required=True, type=click.Path(exists=True,
dir_okay=False,
resolve_path=True,
path_type=Path))
@click.option('--bucket-name', envvar='BUCKET_NAME')
def main(file, bucket_name):
"""Upload the gce cloud image."""
img_id = file.name[:file.name.index('.raw.tar.gz')].replace('.', '-').replace('_', '-')
img_family = 'nixos-image-24-11'
s3_loc = f"gs://{bucket_name}/{file.name}"
cmd = [
'gsutil', 'cp', file, s3_loc
]
print(" ".join([str(s) for s in cmd]))
# return
cmd = [
'gcloud', 'compute','images','create', img_id,
'--source-uri', s3_loc, '--family', img_family
]
# check_call(cmd)
cmd = [
'gcloud', 'compute', 'images', 'add-iam-policy-binding',
img_id, "--member", 'allAuthenticatedUsers', "--role", 'roles/compute.imageUser'
]
check_call(cmd)
if __name__ == '__main__':
main()