From 27f37d4da828ba8b0e31c31f1ba9888d7fddcc33 Mon Sep 17 00:00:00 2001 From: Kate Temkin Date: Fri, 12 Apr 2024 04:53:39 -0600 Subject: [PATCH] tool: add an s3 upload script --- README.md | 2 +- build-all.xsh | 2 + upload-to-lix.xsh | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100755 upload-to-lix.xsh diff --git a/README.md b/README.md index 0194d2d..7bd6f9c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Based on the [Determinate Installer](https://install.determinate.systems). ```bash -curl --proto '=https' --tlsv1.2 -sSf -L https://install.lix.systems/nix | sh -s -- install +curl --proto '=https' --tlsv1.2 -sSf -L https://install.lix.systems/lix | sh -s -- install ``` ## Usage diff --git a/build-all.xsh b/build-all.xsh index 170087f..9ab3e89 100755 --- a/build-all.xsh +++ b/build-all.xsh @@ -8,6 +8,8 @@ # """ Lix installer generation script. +This uses cargo-zigbuild to generate a cross-compiled variant for each platform, +and places the results in the `results` subdirectory of the current working dir. """ import sys diff --git a/upload-to-lix.xsh b/upload-to-lix.xsh new file mode 100755 index 0000000..bde2a99 --- /dev/null +++ b/upload-to-lix.xsh @@ -0,0 +1,109 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i xonsh -p xonsh awscli2 +# +# vim: ts=4 sw=4 et +# +# If the shebang line above was necessary, you probably should have used +# the flake, instead. But that's okay! You're valid. <3 +# +""" Lix installer uploader. + +Uploads our installers and install script to an S3 instance. +""" + +import sys +import argparse +import functools + +# Specify the platforms we want to build for. +TARGET_PLATFORMS = { + "aarch64-apple-darwin": "aarch64-darwin", + "x86_64-apple-darwin": "x86_64-darwin", + "aarch64-unknown-linux-musl": "aarch64-linux", + "x86_64-unknown-linux-musl": "x86_64-linux", + +} + +# Helpers functions. +printerr = functools.partial(print, file=sys.stderr) + +# +# Arguments -- parsed while you wait! +# +parser = argparse.ArgumentParser(description="upload a lix-installer binary") +parser.add_argument("tag", help="the tag name to use while uploading") +parser.add_argument("folder", help="the results folder to use for uploading") +parser.add_argument("--make-default", help="makes this version the default for new installations", + action="store_true") +parser.add_argument("-E", "--endpoint", help="the endpoint URL to use for S3", default="https://s3.lix.systems") +parser.add_argument("-R", "--region", help="the region to use for the S3 upload", default="garage") +parser.add_argument("-B", "--bucket", help="the s3 bucket to target", default="install") +parser.add_argument("--force", help="allows overwriting an existing tag", action="store_true") +args = parser.parse_args() + +# Extract our AWS command arguments from our argparse ones. +path_for = lambda platform : pf"{args.folder}/lix-installer-{platform}" +aws_args = [ + "--endpoint-url", + args.endpoint, + "--region", + args.region +] + +# Validate that we have the environment variables necessary to build. +if ('AWS_ACCESS_KEY_ID' not in ${...}) or ('AWS_SECRET_ACCESS_KEY' not in ${...}): + printerr("ERROR: the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables must be set") + sys.exit(-1) + + +# +# First, make sure we have all of the artifacts that we need before we start. +# +found_all_files = True +for platform in TARGET_PLATFORMS: + if not path_for(platform).exists(): + printerr(f"ERROR: {platform} installer not found in {path_for(platform)}\n") + found_all_files = False + +if not found_all_files: + printerr("Aborting due to missing results. Perhaps you want to run `build-all.xsh`?\n") + sys.exit(-2) + +# +# Next, handle our uploads. +# +tag = args.tag +bucket = args.bucket +folder = args.folder +target_path = f"s3://{bucket}/lix/{tag}" +default_path = f"s3://{bucket}/lix" + +# First, check to ensure that the relevant tag does not exist. +tag_exists = !(aws s3 @(aws_args) ls @(target_path)) +if tag_exists: + if args.force: + printerr(f"WARNING: Overwriting existing tag '{tag}' due to --force!") + else: + printerr(f"ERROR: Tag '{tag}' already exists! Refusing to overwrite without --force.\n") + sys.exit(-3) + + +# From this point forward, fail if any of our subcommands do. +$RAISE_SUBPROC_ERROR=True + +# Copy the core inner pieces... +printerr(f"\n>> Uploading tag '{tag}' from folder '{folder}'.") +for in_filename, out_filename in TARGET_PLATFORMS.items(): + aws s3 @(aws_args) cp @(folder)/lix-installer-@(in_filename) @(target_path)/lix-installer-@(out_filename) --acl public-read + +# ... and, if requested, copy the pieces that make this the default. +if args.make_default: + printerr(f"\n>> Installing {tag} as the default install provider.") + for in_filename, out_filename in TARGET_PLATFORMS.items(): + aws s3 @(aws_args) cp @(folder)/lix-installer-@(in_filename) @(default_path)/lix-installer-@(out_filename) --acl public-read + + printerr(f"\n>> Updating base install script...") + aws s3 @(aws_args) cp nix-installer.sh @(default_path) --acl public-read + +# Make sure all of our lines are out. +sys.stderr.flush()