コンテンツにスキップ

Pants で HTTP リソースを取得する

Web 上のリソースを取得して、Pants のターゲットとして扱う方法について調べた。 BUILDファイルにhttp_sourceを書いて管理する例を見つけたが、adhoc_toolのターゲットを使っても管理できるのではないかと考えている。

http_sourceを使う

このhttp_sourceの実装部分はここにある。

http_sourceのターゲットにはlensha256が必要になる。これらは次のようにして取得できる。

curl -L $URL | tee >(wc -c) >(shasum -a 256) >/dev/null

公式レポジトリ](https://github.com/pantsbuild/pants/blob/7a1cd54d98a43485027d2ac53099565900659811/3rdparty/tools/gh/BUILD)ではhttp_sourceを使ってghを取得している。

# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

# Targets for running the GitHub CLI (or `gh`): https://cli.github.com/
# Use `pants run 3rdparty/tools/gh -- <args>`

file(
    name="downloaded-gh",
    source=per_platform(
        macos_x86_64=http_source(
            url="https://github.com/cli/cli/releases/download/v2.32.0/gh_2.32.0_macOS_amd64.zip",
            len=10957234,
            sha256="e6dd7cb61338c6aab27dfc744bf74817f26a938e505a8e817316513ebf02eb57",
            filename="gh.archive",
        ),
        macos_arm64=http_source(
            url="https://github.com/cli/cli/releases/download/v2.32.0/gh_2.32.0_macOS_arm64.zip",
            len=10318229,
            sha256="6df08a326a4c00e6d33f49c458402c8a3921fd71a2f3187ce77ddb6c452d73f0",
            filename="gh.archive",
        ),
        linux_x86_64=http_source(
            url="https://github.com/cli/cli/releases/download/v2.32.0/gh_2.32.0_linux_amd64.tar.gz",
            len=10315710,
            sha256="2e306f118a46764bc1763bacc52e7b18eeca5aa6fd59d2b5fd260f0ef3cd87ae",
            filename="gh.archive",
        ),
        linux_arm64=http_source(
            url="https://github.com/cli/cli/releases/download/v2.32.0/gh_2.32.0_linux_arm64.tar.gz",
            len=9410807,
            sha256="9eff1eb5d13a3fa858859bd2995077f8987a89d352548a3c116af0536f49afc0",
            filename="gh.archive",
        ),
    ),
)

shell_source(
    name="extract.sh",
    source="extract.sh",
)

shell_command(
    name="extracted-gh",
    command="./extract.sh",
    tools=["tar", "gzip", "zip", "mv", "mkdir", "bash"],
    execution_dependencies=[":downloaded-gh", ":extract.sh"],
    output_directories=["gh"],
)

relocated_files(
    name="relocated-gh",
    files_targets=[":extracted-gh"],
    src="3rdparty/tools/gh/gh",
    dest="3rdparty/tools/gh",
)

run_shell_command(
    name="gh",
    command="{chroot}/3rdparty/tools/gh/bin/gh",
    execution_dependencies=[":relocated-gh"],
)

http_sourceのターゲットで取得したファイルをrelocated_filesで移動している。

http_sourceで指定した len と sha256 は、他のターゲットで使うことがある。 例えば、download-terraformではインストールするバージョンを指定して、そのバイナリを取得している。 公式でいくつかのバージョンを利用できるが、それ以外のバージョンを利用する場合は、http_sourceで取得する必要があるかもしれない(未確認)。

実際に terraform v1.4.6 のバイナリを取得してみる。

 URL=https://releases.hashicorp.com/terraform/1.4.6/terraform_1.4.6_linux_amd64.zip;
curl -L $URL | tee >(wc -c) >(shasum -a 256) >/dev/null
実行結果
e079db1a8945e39b1f8ba4e513946b3ab9f32bd5a2bdf19b9b186d22c5a3d53b  -
 20779821

この結果は公式のknown_versionsの v1.4.6 と一致している。