Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 1 | """This script mirrors the dependencies from go_deps.bzl as Build-Dependencies. |
| 2 | |
| 3 | We use "go mod download" to manually download each Go dependency. We then tar |
| 4 | up all the dependencies and copy them to the Build-Dependencies server for |
| 5 | hosting. |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import hashlib |
| 10 | import json |
| 11 | import os |
| 12 | from pathlib import Path |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tarfile |
| 16 | from typing import List, Dict |
Philipp Schrader | cc016b3 | 2021-12-30 08:59:58 -0800 | [diff] [blame] | 17 | |
| 18 | # Need a fully qualified import here because @bazel_tools interferes. |
| 19 | import org_frc971.tools.go.mirror_lib |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 20 | |
| 21 | GO_DEPS_WWWW_DIR = "/var/www/html/files/frc971/Build-Dependencies/go_deps" |
| 22 | |
| 23 | def compute_sha256(filepath: str) -> str: |
| 24 | """Computes the SHA256 of a file at the specified location.""" |
| 25 | with open(filepath, "rb") as file: |
| 26 | contents = file.read() |
| 27 | return hashlib.sha256(contents).hexdigest() |
| 28 | |
| 29 | def get_existing_mirrored_repos(ssh_host: str) -> Dict[str, str]: |
| 30 | """Gathers information about the libraries that are currently mirrored.""" |
| 31 | run_result = subprocess.run(["ssh", ssh_host, f"bash -c 'sha256sum {GO_DEPS_WWWW_DIR}/*'"], check=True, stdout=subprocess.PIPE) |
| 32 | |
| 33 | existing_mirrored_repos = {} |
| 34 | for line in run_result.stdout.decode("utf-8").splitlines(): |
| 35 | sha256, fullpath = line.split() |
| 36 | existing_mirrored_repos[Path(fullpath).name] = sha256 |
| 37 | |
| 38 | return existing_mirrored_repos |
| 39 | |
| 40 | def download_repos( |
| 41 | repos: Dict[str, str], |
| 42 | existing_mirrored_repos: Dict[str, str], |
| 43 | tar: tarfile.TarFile) -> Dict[str, str]: |
| 44 | """Downloads the not-yet-mirrored repos into a tarball.""" |
| 45 | cached_info = {} |
| 46 | |
| 47 | for repo in repos: |
| 48 | print(f"Downloading file for {repo['name']}") |
| 49 | importpath = repo["importpath"] |
| 50 | version = repo["version"] |
| 51 | module = f"{importpath}@{version}" |
| 52 | |
| 53 | download_result = subprocess.run( |
| 54 | ["external/go_sdk/bin/go", "mod", "download", "-json", module], |
| 55 | check=True, stdout=subprocess.PIPE) |
| 56 | if download_result.returncode != 0: |
| 57 | print("Failed to download file.") |
| 58 | return 1 |
| 59 | |
| 60 | module_info = json.loads(download_result.stdout.decode("utf-8")) |
| 61 | |
| 62 | name = repo["name"] |
| 63 | zip_path = Path(module_info["Zip"]) |
| 64 | mirrored_name = f"{name}__{zip_path.name}" |
| 65 | if mirrored_name not in existing_mirrored_repos: |
| 66 | # We only add the Go library to the tarball if it's not already |
| 67 | # mirrored. We don't want to overwrite files. |
| 68 | tar.add(zip_path, arcname=mirrored_name) |
| 69 | sha256 = compute_sha256(zip_path) |
| 70 | else: |
| 71 | # Use the already-computed checksum for consistency. |
| 72 | sha256 = existing_mirrored_repos[mirrored_name] |
| 73 | |
| 74 | cached_info[name] = { |
| 75 | "strip_prefix": module, |
| 76 | "filename": mirrored_name, |
| 77 | "sha256": sha256, |
| 78 | "version": version, |
| 79 | "importpath": importpath, |
| 80 | } |
| 81 | |
| 82 | return cached_info |
| 83 | |
| 84 | def copy_to_host_and_unpack(filename: str, ssh_host: str) -> None: |
| 85 | subprocess.run(["scp", filename, f"{ssh_host}:"], check=True) |
| 86 | |
| 87 | # Be careful not to use single quotes in these commands to avoid breaking |
| 88 | # the subprocess.run() invocation below. |
| 89 | command = " && ".join([ |
| 90 | f"tar -C {GO_DEPS_WWWW_DIR} --no-same-owner -xvaf {filename}", |
| 91 | # Change the permissions so other users can read them (and checksum |
| 92 | # them). |
| 93 | f"find {GO_DEPS_WWWW_DIR}/ -type f -exec chmod 644 {{}} +", |
| 94 | ]) |
| 95 | |
| 96 | print("You might be asked for your sudo password shortly.") |
| 97 | subprocess.run(["ssh", "-t", ssh_host, f"sudo -u www-data bash -c '{command}'"], check=True) |
| 98 | |
| 99 | def main(argv): |
| 100 | parser = argparse.ArgumentParser() |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 101 | group = parser.add_mutually_exclusive_group() |
| 102 | group.add_argument( |
| 103 | "--prune", |
| 104 | action="store_true", |
| 105 | help=("When set, makes the tool prune go_mirrors_bzl to match the " |
| 106 | "repositories specified in go_deps_bzl. Incompatible with " |
| 107 | "--ssh_host.")) |
| 108 | group.add_argument( |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 109 | "--ssh_host", |
| 110 | type=str, |
| 111 | help=("The SSH host to copy the downloaded Go repositories to. This " |
| 112 | "should be software.971spartans.net where all the " |
| 113 | "Build-Dependencies files live. Only specify this if you have " |
| 114 | "access to the server.")) |
| 115 | parser.add_argument("--go_deps_bzl", type=str, default="go_deps.bzl") |
| 116 | parser.add_argument("--go_mirrors_bzl", type=str, default="tools/go/go_mirrors.bzl") |
| 117 | args = parser.parse_args(argv[1:]) |
| 118 | |
| 119 | os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"]) |
| 120 | |
Philipp Schrader | cc016b3 | 2021-12-30 08:59:58 -0800 | [diff] [blame] | 121 | repos = org_frc971.tools.go.mirror_lib.parse_go_repositories(args.go_deps_bzl) |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 122 | |
| 123 | if args.ssh_host: |
| 124 | existing_mirrored_repos = get_existing_mirrored_repos(args.ssh_host) |
| 125 | else: |
| 126 | existing_mirrored_repos = {} |
| 127 | |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 128 | exit_code = 0 |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 129 | |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 130 | if args.prune: |
| 131 | # Delete all mirror info that is not needed anymore. |
| 132 | existing_cache_info = org_frc971.tools.go.mirror_lib.parse_go_mirror_info(args.go_mirrors_bzl) |
| 133 | cached_info = {} |
| 134 | for repo in repos: |
| 135 | try: |
| 136 | cached_info[repo["name"]] = existing_cache_info[repo["name"]] |
| 137 | except KeyError: |
| 138 | print(f"{repo['name']} needs to be mirrored still.") |
| 139 | exit_code = 1 |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 140 | else: |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 141 | # Download all the repositories that need to be mirrored. |
| 142 | with tarfile.open("go_deps.tar", "w") as tar: |
| 143 | cached_info = download_repos(repos, existing_mirrored_repos, tar) |
| 144 | num_not_already_mirrored = len(tar.getnames()) |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 145 | |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 146 | print(f"Found {num_not_already_mirrored}/{len(cached_info)} libraries " |
| 147 | "that need to be mirrored.") |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 148 | |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 149 | # Only mirror the deps if we've specified an SSH host and we actually have |
| 150 | # something to mirror. |
| 151 | if args.ssh_host and num_not_already_mirrored: |
| 152 | copy_to_host_and_unpack("go_deps.tar", args.ssh_host) |
| 153 | else: |
| 154 | print("Skipping mirroring because of lack of --ssh_host or there's " |
| 155 | "nothing to actually mirror.") |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 156 | |
Philipp Schrader | d96d4cb | 2022-02-06 15:37:29 -0800 | [diff] [blame^] | 157 | org_frc971.tools.go.mirror_lib.write_go_mirror_info(args.go_mirrors_bzl, cached_info) |
| 158 | |
| 159 | return exit_code |
Philipp Schrader | cc016b3 | 2021-12-30 08:59:58 -0800 | [diff] [blame] | 160 | |
| 161 | |
Philipp Schrader | 37fdbb6 | 2021-12-18 00:30:37 -0800 | [diff] [blame] | 162 | if __name__ == "__main__": |
| 163 | sys.exit(main(sys.argv)) |