← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:add-configuration-to-provide-packages-to-install into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:add-configuration-to-provide-packages-to-install into lpcraft:main.

Commit message:
Declare system package dependencies for jobs

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/412477
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:add-configuration-to-provide-packages-to-install into lpcraft:main.
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index 02e844a..7cfd763 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -35,8 +35,6 @@ def run(args: Namespace) -> int:
                     f"does not set 'run'"
                 )
 
-            cmd = ["bash", "--noprofile", "--norc", "-ec", job.run]
-
             emit.progress(
                 f"Launching environment for {job.series}/{host_architecture}"
             )
@@ -46,10 +44,26 @@ def run(args: Namespace) -> int:
                 series=job.series,
                 architecture=host_architecture,
             ) as instance:
+                clean_bash = ["bash", "--noprofile", "--norc", "-ec"]
+                if job.packages:
+                    packages_cmd = (
+                        clean_bash + ["apt", "install", "-y"] + job.packages
+                    )
+                    emit.progress("Installing system packages")
+                    with emit.open_stream(f"Running {packages_cmd}") as stream:
+                        proc = instance.execute_run(
+                            packages_cmd,
+                            cwd=env.get_managed_environment_project_path(),
+                            env=job.environment,
+                            stdout=stream,
+                            stderr=stream,
+                        )
+
+                run_cmd = clean_bash + [job.run]
                 emit.progress("Running the job")
-                with emit.open_stream(f"Running {cmd}") as stream:
+                with emit.open_stream(f"Running {run_cmd}") as stream:
                     proc = instance.execute_run(
-                        cmd,
+                        run_cmd,
                         cwd=env.get_managed_environment_project_path(),
                         env=job.environment,
                         stdout=stream,
diff --git a/lpcraft/commands/tests/test_run.py b/lpcraft/commands/tests/test_run.py
index 5b8b6bc..c95704b 100644
--- a/lpcraft/commands/tests/test_run.py
+++ b/lpcraft/commands/tests/test_run.py
@@ -418,3 +418,61 @@ class TestRun(CommandBaseTestCase):
             ],
             execute_run.call_args_list,
         )
+
+    @patch("lpcraft.commands.run.get_provider")
+    @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+    def test_install_system_packages(
+        self, mock_get_host_architecture, mock_get_provider
+    ):
+        launcher = Mock(spec=launch)
+        provider = self.makeLXDProvider(lxd_launcher=launcher)
+        mock_get_provider.return_value = provider
+        execute_run = launcher.return_value.execute_run
+        execute_run.return_value = subprocess.CompletedProcess([], 0)
+        config = dedent(
+            """
+            pipeline:
+                - test
+
+            jobs:
+                test:
+                    series: focal
+                    architectures: amd64
+                    run: tox
+                    packages: [nginx, apache2]
+            """
+        )
+        Path(".launchpad.yaml").write_text(config)
+
+        result = self.run_command("run")
+        self.assertEqual(0, result.exit_code)
+
+        self.assertEqual(
+            [
+                call(
+                    [
+                        "bash",
+                        "--noprofile",
+                        "--norc",
+                        "-ec",
+                        "apt",
+                        "install",
+                        "-y",
+                        "nginx",
+                        "apache2",
+                    ],
+                    cwd=Path("/root/project"),
+                    env=None,
+                    stdout=ANY,
+                    stderr=ANY,
+                ),
+                call(
+                    ["bash", "--noprofile", "--norc", "-ec", "tox"],
+                    cwd=Path("/root/project"),
+                    env=None,
+                    stdout=ANY,
+                    stderr=ANY,
+                ),
+            ],
+            execute_run.call_args_list,
+        )
diff --git a/lpcraft/config.py b/lpcraft/config.py
index e4011f3..b3ea10f 100644
--- a/lpcraft/config.py
+++ b/lpcraft/config.py
@@ -27,6 +27,7 @@ class Job(ModelConfigDefaults):
     architectures: List[StrictStr]
     run: Optional[StrictStr]
     environment: Optional[Dict[str, Optional[str]]]
+    packages: Optional[List[StrictStr]]
 
     @pydantic.validator("architectures", pre=True)
     def validate_architectures(
@@ -36,6 +37,14 @@ class Job(ModelConfigDefaults):
             v = [v]
         return v
 
+    @pydantic.validator("packages", pre=True)
+    def validate_packages(
+        cls, v: Optional[Union[StrictStr, List[StrictStr]]]
+    ) -> Optional[List[StrictStr]]:
+        if isinstance(v, str):
+            v = [v]
+        return v
+
 
 def _expand_job_values(
     values: Dict[StrictStr, Any]
diff --git a/lpcraft/tests/test_config.py b/lpcraft/tests/test_config.py
index 16649ad..b1e609d 100644
--- a/lpcraft/tests/test_config.py
+++ b/lpcraft/tests/test_config.py
@@ -150,3 +150,58 @@ class TestConfig(TestCase):
         self.assertEqual(
             {"ACTIVE": "1", "SKIP": "0"}, config.jobs["test"][0].environment
         )
+
+    def test_load_single_package(self):
+        # A single package can be written as a string, and is automatically
+        # wrapped in a list.
+        path = self.create_config(
+            dedent(
+                """
+                pipeline:
+                    - test
+
+                jobs:
+                    test:
+                        series: focal
+                        architectures: amd64
+                        packages: nginx
+                """
+            )
+        )
+        config = Config.load(path)
+        self.assertEqual(["nginx"], config.jobs["test"][0].packages)
+
+    def test_load_multiple_package(self):
+        path = self.create_config(
+            dedent(
+                """
+                pipeline:
+                    - test
+
+                jobs:
+                    test:
+                        series: focal
+                        architectures: amd64
+                        packages: [nginx, apache2]
+                """
+            )
+        )
+        config = Config.load(path)
+        self.assertEqual(["nginx", "apache2"], config.jobs["test"][0].packages)
+
+    def test_load_config_without_packages(self):
+        path = self.create_config(
+            dedent(
+                """
+                pipeline:
+                    - test
+
+                jobs:
+                    test:
+                        series: focal
+                        architectures: amd64
+                """
+            )
+        )
+        config = Config.load(path)
+        self.assertEqual(None, config.jobs["test"][0].packages)