launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #28409
[Merge] ~jugmac00/lpcraft:set-env-varibales-via-cli into lpcraft:main
Jürgen Gmach has proposed merging ~jugmac00/lpcraft:set-env-varibales-via-cli into lpcraft:main.
Commit message:
Add optional CLI argument to pass in environment variables
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/420755
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:set-env-varibales-via-cli into lpcraft:main.
diff --git a/NEWS.rst b/NEWS.rst
index 2aba06c..d44dec6 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -10,6 +10,9 @@ Version history
- Add minimal CLI interface documentation.
+- Add new optional and repeatable argument ``--set-env`` which allows passing
+ in environment variables.
+
0.0.10 (2022-04-27)
====================
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index 3a2d4a8..b7d5a3b 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -199,6 +199,7 @@ def _run_job(
provider: Provider,
output: Optional[Path],
apt_replacement_repositories: Optional[List[str]] = None,
+ env_from_cli: Optional[List[str]] = None,
) -> None:
"""Run a single job."""
# XXX jugmac00 2022-04-27: we should create a configuration object to be
@@ -235,6 +236,9 @@ def _run_job(
if env_from_configuration is not None:
env_from_plugin.update(env_from_configuration)
environment = env_from_plugin
+ if env_from_cli:
+ pairs_from_cli = dict(pair.split("=") for pair in env_from_cli)
+ environment.update(pairs_from_cli)
cwd = Path.cwd()
remote_cwd = env.get_managed_environment_project_path()
@@ -384,6 +388,7 @@ def run(args: Namespace) -> int:
apt_replacement_repositories=getattr(
args, "apt_replace_repositories", None
),
+ env_from_cli=getattr(args, "set_env", None),
)
except CommandError as e:
if len(stage) == 1:
@@ -438,6 +443,7 @@ def run_one(args: Namespace) -> int:
apt_replacement_repositories=getattr(
args, "apt_replace_repositories", None
),
+ env_from_cli=getattr(args, "set_env", None),
)
finally:
should_clean_environment = getattr(args, "clean", False)
diff --git a/lpcraft/commands/tests/test_run.py b/lpcraft/commands/tests/test_run.py
index bc453c0..727e8a8 100644
--- a/lpcraft/commands/tests/test_run.py
+++ b/lpcraft/commands/tests/test_run.py
@@ -775,7 +775,7 @@ class TestRun(RunBaseTestCase):
@patch("lpcraft.commands.run.get_provider")
@patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
- def test_pass_in_environment_variables(
+ def test_set_environment_variables_via_configuration(
self, mock_get_host_architecture, mock_get_provider
):
launcher = Mock(spec=launch)
@@ -815,6 +815,48 @@ class TestRun(RunBaseTestCase):
execute_run.call_args_list,
)
+ @patch("lpcraft.commands.run.get_provider")
+ @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+ def test_set_environment_variables_via_cli(
+ self, mock_get_host_architecture, mock_get_provider
+ ):
+ launcher = Mock(spec=launch)
+ provider = 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
+ """
+ )
+ Path(".launchpad.yaml").write_text(config)
+
+ result = self.run_command(
+ "run", "--set-env", "PIP_INDEX_URL=http://pypi.example.com/simple"
+ )
+
+ self.assertEqual(0, result.exit_code)
+ self.assertEqual(
+ [
+ call(
+ ["bash", "--noprofile", "--norc", "-ec", "tox"],
+ cwd=Path("/root/lpcraft/project"),
+ env={"PIP_INDEX_URL": "http://pypi.example.com/simple"},
+ stdout=ANY,
+ stderr=ANY,
+ )
+ ],
+ execute_run.call_args_list,
+ )
+
@patch("lpcraft.env.get_managed_environment_project_path")
@patch("lpcraft.commands.run.get_provider")
@patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
@@ -2323,3 +2365,49 @@ class TestRunOne(RunBaseTestCase):
self.assertEqual("0644", mock_info["file_mode"])
self.assertEqual("root", mock_info["group"])
self.assertEqual("root", mock_info["user"])
+
+ @patch("lpcraft.commands.run.get_provider")
+ @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+ def test_set_environment_variables_via_cli(
+ self, mock_get_host_architecture, mock_get_provider
+ ):
+ launcher = Mock(spec=launch)
+ provider = 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
+ """
+ )
+ Path(".launchpad.yaml").write_text(config)
+
+ result = self.run_command(
+ "run-one",
+ "--set-env",
+ "PIP_INDEX_URL=http://pypi.example.com/simple",
+ "test",
+ "0",
+ )
+
+ self.assertEqual(0, result.exit_code)
+ self.assertEqual(
+ [
+ call(
+ ["bash", "--noprofile", "--norc", "-ec", "tox"],
+ cwd=Path("/root/lpcraft/project"),
+ env={"PIP_INDEX_URL": "http://pypi.example.com/simple"},
+ stdout=ANY,
+ stderr=ANY,
+ )
+ ],
+ execute_run.call_args_list,
+ )
diff --git a/lpcraft/main.py b/lpcraft/main.py
index a54867a..c9af819 100644
--- a/lpcraft/main.py
+++ b/lpcraft/main.py
@@ -102,6 +102,11 @@ def main(argv: Optional[List[str]] = None) -> int:
action="append",
help="Overwrite /etc/apt/sources.list.",
)
+ parser_run.add_argument(
+ "--set-env",
+ action="append",
+ help="Set an environment variable.",
+ )
parser_run.set_defaults(func=run)
parser_run_one = subparsers.add_parser(
@@ -139,6 +144,11 @@ def main(argv: Optional[List[str]] = None) -> int:
action="append",
help="Overwrite /etc/apt/sources.list.",
)
+ parser_run_one.add_argument(
+ "--set-env",
+ action="append",
+ help="Set an environment variable.",
+ )
parser_run_one.set_defaults(func=run_one)
parser_version = subparsers.add_parser(