← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:add-optional-argument-for-add-apt-repository into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:add-optional-argument-for-add-apt-repository into lpcraft:main.

Commit message:
Add optional argument to add apt repository

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/420656
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:add-optional-argument-for-add-apt-repository into lpcraft:main.
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index e5bc296..b774184 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -194,9 +194,15 @@ def _copy_output_properties(
 
 
 def _run_job(
-    job_name: str, job: Job, provider: Provider, output: Optional[Path]
+    job_name: str,
+    job: Job,
+    provider: Provider,
+    output: Optional[Path],
+    additional_repositories: Optional[List[str]] = None,
 ) -> None:
     """Run a single job."""
+    # XXX jugmac00 2022-04-27: we should create a configuration object to be
+    # passed in and not so many arguments
     host_architecture = get_host_architecture()
     if host_architecture not in job.architectures:
         return
@@ -253,6 +259,52 @@ def _run_job(
             )
         packages = list(itertools.chain(*pm.hook.lpcraft_install_packages()))
         if packages:
+            if additional_repositories:
+                # update sources list
+                lines = "\n".join(additional_repositories)
+                # see https://stackoverflow.com/a/53814440/672833
+                update_sources_list = [
+                    "sed",
+                    "-i",
+                    f"1i {lines}",
+                    "/etc/apt/sources.list",
+                ]
+                with emit.open_stream(
+                    f"Running {update_sources_list}"
+                ) as stream:
+                    proc = instance.execute_run(
+                        update_sources_list,
+                        cwd=remote_cwd,
+                        env=environment,
+                        stdout=stream,
+                        stderr=stream,
+                    )
+                if proc.returncode != 0:
+                    raise CommandError(
+                        f"Job {job_name!r} for "
+                        f"{job.series}/{host_architecture} failed with "
+                        f"exit status {proc.returncode} "
+                        f"while running `{shlex.join(update_sources_list)}`.",
+                        retcode=proc.returncode,
+                    )
+                # update local repository information
+                apt_update = ["apt", "update"]
+                with emit.open_stream(f"Running {apt_update}") as stream:
+                    proc = instance.execute_run(
+                        apt_update,
+                        cwd=remote_cwd,
+                        env=environment,
+                        stdout=stream,
+                        stderr=stream,
+                    )
+                if proc.returncode != 0:
+                    raise CommandError(
+                        f"Job {job_name!r} for "
+                        f"{job.series}/{host_architecture} failed with "
+                        f"exit status {proc.returncode} "
+                        f"while running `{shlex.join(apt_update)}`.",
+                        retcode=proc.returncode,
+                    )
             packages_cmd = ["apt", "install", "-y"] + packages
             emit.progress("Installing system packages")
             with emit.open_stream(f"Running {packages_cmd}") as stream:
@@ -344,6 +396,9 @@ def run(args: Namespace) -> int:
                             job,
                             provider,
                             getattr(args, "output_directory", None),
+                            additional_repositories=getattr(
+                                args, "add_apt_repository", None
+                            ),
                         )
                 except CommandError as e:
                     if len(stage) == 1:
@@ -391,7 +446,11 @@ def run_one(args: Namespace) -> int:
 
     try:
         _run_job(
-            args.job, job, provider, getattr(args, "output_directory", None)
+            args.job,
+            job,
+            provider,
+            getattr(args, "output_directory", None),
+            additional_repositories=getattr(args, "add_apt_repository", None),
         )
     finally:
         should_clean_environment = getattr(args, "clean", False)
diff --git a/lpcraft/main.py b/lpcraft/main.py
index 42a68e5..c4f93a0 100644
--- a/lpcraft/main.py
+++ b/lpcraft/main.py
@@ -97,6 +97,11 @@ def main(argv: Optional[List[str]] = None) -> int:
             "for the pipeline after the running it."
         ),
     )
+    parser_run.add_argument(
+        "--add-apt-repository",
+        action="append",
+        help="Add an additional repository for packages.",
+    )
     parser_run.set_defaults(func=run)
 
     parser_run_one = subparsers.add_parser(

References