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