Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include complete platforms for FaaS environments for more reliable building #19253

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions build-support/bin/generate_faas_complete_platforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path

from pants.backend.awslambda.python.target_types import PythonAwsLambdaRuntime
from pants.backend.python.util_rules.faas import PythonFaaSRuntimeField
from pants.base.build_environment import get_buildroot

COMMAND = "pip install pex 1>&2 && pex3 interpreter inspect --markers --tags"


RUNTIME_FIELDS = [
PythonAwsLambdaRuntime,
# TODO: what docker images to use for GCF?
]


def extract_complete_platform(repo: str, tag: str) -> object:
image = f"{repo}:{tag}"
print(f"Extracting complete platform for {image}", file=sys.stderr)
result = subprocess.run(
["docker", "run", "--entrypoint", "sh", image, "-c", COMMAND],
check=True,
stdout=subprocess.PIPE,
)
return json.loads(result.stdout)


def run(runtime_field: type[PythonFaaSRuntimeField], python_base: Path) -> None:
cp_dir = python_base / runtime_field.known_runtimes_complete_platforms_module().replace(
".", "/"
)
print(f"Generating for {runtime_field.__name__}, writing to {cp_dir}", file=sys.stderr)
for rt in runtime_field.known_runtimes:
cp = extract_complete_platform(runtime_field.known_runtimes_docker_repo, rt.tag)

fname = cp_dir / rt.file_name()
with fname.open("w") as f:
json.dump(cp, f, indent=2)


def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Generates the complete platform JSON files for AWS Lambda and GCF"
)
return parser


def main() -> None:
create_parser().parse_args()

build_root = Path(get_buildroot()) / "src/python"
for runtime_field in RUNTIME_FIELDS:
run(runtime_field, build_root)


if __name__ == "__main__":
main()
5 changes: 4 additions & 1 deletion src/python/pants/backend/awslambda/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
python_sources(overrides={"target_types.py": {"dependencies": [":complete_platforms"]}})

# generated by build-support/bin/generate_faas_complete_platforms.py
resources(name="complete_platforms", sources=["complete_platform_*.json"])

python_tests(name="target_types_test", sources=["target_types_test.py"], timeout=120)
python_tests(
Expand Down
Loading