Skip to content

Commit

Permalink
fix(remotebuild): parse comma-separated architectures
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal committed Sep 11, 2024
1 parent 0d06f80 commit 5e77db7
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 53 deletions.
85 changes: 44 additions & 41 deletions snapcraft/commands/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,51 +77,51 @@ class RemoteBuildCommand(ExtensibleCommand):
@overrides
def _fill_parser(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--recover", action="store_true", help="recover an interrupted build"
"--recover", action="store_true", help="Recover an interrupted build"
)
parser.add_argument(
"--launchpad-accept-public-upload",
action="store_true",
help="acknowledge that uploaded code will be publicly available.",
help="Acknowledge that uploaded code will be publicly available",
)
parser.add_argument(
"--launchpad-timeout",
type=int,
default=0,
metavar="<seconds>",
help="Time in seconds to wait for launchpad to build.",
help="Time in seconds to wait for launchpad to build",
)

parser.add_argument(
"--status", action="store_true", help="display remote build status"
"--status", action="store_true", help="Display remote build status"
)
parser.add_argument(
"--build-id", metavar="build-id", help="specific build id to retrieve"
"--build-id", metavar="build-id", help="Specific build id to retrieve"
)

group = parser.add_mutually_exclusive_group()
group.add_argument(
"--platform",
type=str,
type=lambda arg: [arch.strip() for arch in arg.split(",")],
metavar="name",
default=os.getenv("CRAFT_PLATFORM"),
help="Set platform to build for",
help="Comma-separated list of platforms to build for",
# '--platform' needs to be handled differently since remote-build can
# build for an architecture that is not in the project metadata
dest="remote_build_platform",
dest="remote_build_platforms",
)
group.add_argument(
"--build-for",
type=str,
type=lambda arg: [arch.strip() for arch in arg.split(",")],
metavar="arch",
default=os.getenv("CRAFT_BUILD_FOR"),
help="Set architecture to build for",
help="Comma-separated list of architectures to build for",
# '--build-for' needs to be handled differently since remote-build can
# build for architecture that is not in the project metadata
dest="remote_build_build_for",
dest="remote_build_build_fors",
)
parser.add_argument(
"--project", help="upload to the specified Launchpad project"
"--project", help="Upload to the specified Launchpad project"
)

def _validate(self, parsed_args: argparse.Namespace) -> None:
Expand Down Expand Up @@ -154,9 +154,9 @@ def _validate(self, parsed_args: argparse.Namespace) -> None:
retcode=os.EX_NOPERM,
)

build_for = parsed_args.remote_build_build_for or None
platform = parsed_args.remote_build_platform or None
parameter = "--build-for" if build_for else "--platform" if platform else None
build_fors = parsed_args.remote_build_build_fors or None
platforms = parsed_args.remote_build_platforms or None
parameter = "--build-for" if build_fors else "--platform" if platforms else None
keyword = (
"architectures"
if self.project._architectures_in_yaml
Expand All @@ -171,35 +171,38 @@ def _validate(self, parsed_args: argparse.Namespace) -> None:
retcode=os.EX_CONFIG,
)

if platform:
if platforms:
if self.project.get_effective_base() == "core22":
raise errors.RemoteBuildError(
"'--platform' cannot be used for core22 snaps.",
resolution="Use '--build-for' instead.",
doc_slug="/explanation/remote-build.html",
retcode=os.EX_CONFIG,
)
if platform not in SUPPORTED_ARCHS:
raise errors.RemoteBuildError(
f"Unsupported platform {parsed_args.remote_build_platform!r}.",
resolution=(
"Use a supported debian architecture. Supported "
f"architectures are: {humanize_list(SUPPORTED_ARCHS, 'and')}"
),
doc_slug="/explanation/remote-build.html",
retcode=os.EX_CONFIG,
)

if build_for and build_for not in SUPPORTED_ARCHS:
raise errors.RemoteBuildError(
f"Unsupported build-for architecture {parsed_args.remote_build_build_for!r}.",
resolution=(
"Use a supported debian architecture. Supported "
f"architectures are: {humanize_list(SUPPORTED_ARCHS, 'and')}"
),
doc_slug="/explanation/remote-build.html",
retcode=os.EX_CONFIG,
)
for platform in platforms:
if platform not in SUPPORTED_ARCHS:
raise errors.RemoteBuildError(
f"Unsupported platform {platform!r}.",
resolution=(
"Use a supported debian architecture. Supported "
f"architectures are: {humanize_list(SUPPORTED_ARCHS, 'and')}"
),
doc_slug="/explanation/remote-build.html",
retcode=os.EX_CONFIG,
)

if build_fors:
for build_for in build_fors:
if build_for not in SUPPORTED_ARCHS:
raise errors.RemoteBuildError(
f"Unsupported build-for architecture {build_for!r}.",
resolution=(
"Use a supported debian architecture. Supported "
f"architectures are: {humanize_list(SUPPORTED_ARCHS, 'and')}"
),
doc_slug="/explanation/remote-build.html",
retcode=os.EX_CONFIG,
)

self._validate_single_artifact_per_build_on()

Expand Down Expand Up @@ -271,10 +274,10 @@ def _run( # noqa: PLR0915 [too-many-statements]
emit.trace(f"Project directory: {project_dir}")
self._validate(parsed_args)

if parsed_args.remote_build_build_for:
architectures = [parsed_args.remote_build_build_for]
elif parsed_args.remote_build_platform:
architectures = [parsed_args.remote_build_platform]
if parsed_args.remote_build_build_fors:
architectures = parsed_args.remote_build_build_fors
elif parsed_args.remote_build_platforms:
architectures = parsed_args.remote_build_platforms
else:
architectures = self._get_project_build_fors()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--build-for amd64,s390x
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-snap-build-for-arg-core22_1.0_amd64.snap
test-snap-build-for-arg-core22_1.0_s390x.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: test-snap-build-for-arg-core22
base: core22
version: "1.0"
summary: Test snap for remote build
description: Test snap for remote build

grade: stable
confinement: strict

parts:
my-part:
plugin: nil
9 changes: 8 additions & 1 deletion tests/spread/core22/remote-build/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:
SNAP/all: all
SNAP/no_architectures: no-architectures
SNAP/architectures: architectures
SNAP/build_for_arg: build-for-arg
SNAPCRAFT_REMOTE_BUILD_STRATEGY: disable-fallback
CREDENTIALS_FILE: "$HOME/.local/share/snapcraft/launchpad-credentials"
CREDENTIALS_FILE/new_credentials: "$HOME/.local/share/snapcraft/launchpad-credentials"
Expand Down Expand Up @@ -39,7 +40,13 @@ restore: |
execute: |
cd "./snaps/$SNAP"
snapcraft remote-build --launchpad-accept-public-upload
call_args=""
if [[ -e "arguments.txt" ]]; then
call_args=$(cat "arguments.txt")
fi
# shellcheck disable=SC2086
snapcraft remote-build --launchpad-accept-public-upload $call_args
find . -maxdepth 1 -name "*.snap" | MATCH ".snap"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--build-for amd64,s390x
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-snap-build-for-arg-core24_1.0_amd64.snap
test-snap-build-for-arg-core24_1.0_s390x.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: test-snap-build-for-arg-core24
base: core24
version: "1.0"
summary: Test snap for remote build
description: Test snap for remote build

grade: stable
confinement: strict

parts:
my-part:
plugin: nil
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--platform amd64,s390x
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-snap-platform-arg-core24_1.0_amd64.snap
test-snap-platform-arg-core24_1.0_s390x.snap
12 changes: 12 additions & 0 deletions tests/spread/core24/remote-build/snaps/platform-arg/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: test-snap-platform-arg-core24
base: core24
version: "1.0"
summary: Test snap for remote build
description: Test snap for remote build

grade: stable
confinement: strict

parts:
my-part:
plugin: nil
10 changes: 9 additions & 1 deletion tests/spread/core24/remote-build/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ environment:
SNAP/all: all
SNAP/platforms: platforms
SNAP/no_platforms: no-platforms
SNAP/platform_arg: platform-arg
SNAP/build_for_arg: build-for-arg
CREDENTIALS_FILE: "$HOME/.local/share/snapcraft/launchpad-credentials"
CREDENTIALS_FILE/new_credentials: "$HOME/.local/share/snapcraft/launchpad-credentials"
CREDENTIALS_FILE/old_credentials: "$HOME/.local/share/snapcraft/provider/launchpad/credentials"
Expand Down Expand Up @@ -42,7 +44,13 @@ restore: |
execute: |
cd "./snaps/$SNAP"
snapcraft remote-build --launchpad-accept-public-upload
call_args=""
if [[ -e "arguments.txt" ]]; then
call_args=$(cat "arguments.txt")
fi
# shellcheck disable=SC2086
snapcraft remote-build --launchpad-accept-public-upload $call_args
find . -maxdepth 1 -name "*.snap" | MATCH ".snap"
Expand Down
Loading

0 comments on commit 5e77db7

Please sign in to comment.