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

Adds run_once_command rock build option #745

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions rockcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class Project(BuildPlanner, BaseProject): # type: ignore[misc]
services: dict[str, Service] | None = None
checks: dict[str, Check] | None = None
entrypoint_service: str | None = None
run_once_command: str | None = None
platforms: dict[str, Platform | None] # type: ignore[assignment]

model_config = pydantic.ConfigDict(
Expand Down Expand Up @@ -388,6 +389,29 @@ def _validate_entrypoint_service(

return entrypoint_service

@pydantic.field_validator("run_once_command")
@classmethod
def _validate_run_once_command(
cls, run_once_command: str | None, info: pydantic.ValidationInfo
) -> str | None:
"""Verify that the entrypoint-service or the services dict are not set."""
if not run_once_command:
return run_once_command

craft_cli.emit.message(
"Warning: defining a run-once-command will result in a rock with an "
"atypical OCI Entrypoint. While that might be acceptable for testing and "
"personal use, it shall require prior approval before submitting to a "
"Canonical registry namespace."
)

if info.data.get("entrypoint-service") or info.data.get("services"):
raise ValueError(
"Cannot have run-once-command and entrypoint-service / services at the same time."
)

return run_once_command

@pydantic.field_validator("environment")
@classmethod
def _forbid_env_var_bash_interpolation(
Expand Down
11 changes: 11 additions & 0 deletions rockcraft/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,17 @@ def set_default_user(self, user: str) -> None:
_config_image(image_path, params)
emit.progress(f"Default user set to {user}")

def set_run_once_entrypoint(self, command: str) -> None:
"""Set the OCI image entrypoint to the given command."""
emit.progress("Configuring entrypoint...")
image_path = self.path / self.image_name
params = ["--clear=config.entrypoint"]
for entry in shlex.split(command):
params.extend(["--config.entrypoint", entry])
params.extend(["--clear=config.cmd"])
_config_image(image_path, params)
emit.progress(f"Entrypoint set to {command}")

def set_entrypoint(self, entrypoint_service: str | None, build_base: str) -> None:
"""Set the OCI image entrypoint. It is always Pebble."""
emit.progress("Configuring entrypoint...")
Expand Down
17 changes: 10 additions & 7 deletions rockcraft/services/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,16 @@ def _pack(
emit.progress(f"Setting the default OCI user to be {project.run_user}")
new_image.set_default_user(project.run_user)

emit.progress("Adding Pebble entrypoint")

new_image.set_entrypoint(
project.entrypoint_service, project.build_base or project.base
)
if project.services and project.entrypoint_service in project.services:
new_image.set_cmd(project.services[project.entrypoint_service].command)
if project.run_once_command:
emit.progress("Adding run-once-command entrypoint")
new_image.set_run_once_entrypoint(project.run_once_command)
else:
emit.progress("Adding Pebble entrypoint")
new_image.set_entrypoint(
project.entrypoint_service, project.build_base or project.base
)
if project.services and project.entrypoint_service in project.services:
new_image.set_cmd(project.services[project.entrypoint_service].command)

new_image.set_default_path(project.base)

Expand Down
Loading