diff options
author | Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com> | 2022-12-21 17:47:27 +0100 |
---|---|---|
committer | Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com> | 2023-01-10 14:19:02 +0100 |
commit | 38c2070e794e48818801bb5628b4966b010d18ce (patch) | |
tree | e6dc55b52906a2aae0094d422e0e7509e8028eab | |
parent | 0924161b2c0cd2752312e00170e0165931ff24f9 (diff) | |
download | libvirt-ci-38c2070e794e48818801bb5628b4966b010d18ce.zip libvirt-ci-38c2070e794e48818801bb5628b4966b010d18ce.tar.gz libvirt-ci-38c2070e794e48818801bb5628b4966b010d18ce.tar.bz2 |
lcitool container: Add "run" subcommand
This adds the "run" subcommand to lcitool
which is used to run workload in a container
It is run with:
lcitool container run --help
Validate the command line arguments for "run"
to ensure that --script is required, and it
works with and without --workload-dir argument
It also stops running if image doesn't exist locally.
Signed-off-by: Abdulwasiu Apalowo <abdulwasiuapalowo@gmail.com>
-rw-r--r-- | lcitool/application.py | 40 | ||||
-rw-r--r-- | lcitool/commandline.py | 15 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lcitool/application.py b/lcitool/application.py index c5bbbab..a435a59 100644 --- a/lcitool/application.py +++ b/lcitool/application.py @@ -370,6 +370,46 @@ class Application: log.debug(f"Generated image tag --> {tag}") print(f"Image '{tag}' successfully built.") + def _action_container_run(self, args): + self._entrypoint_debug(args) + + params = {} + client = self._get_client(args.engine) + + container_tempdir = TemporaryDirectory(prefix="container", + dir=util.get_temp_dir()) + params["tempdir"] = container_tempdir.name + + if not client.image_exists(args.image): + print(f"Image '{args.image}' not found in local cache. Build it or pull from registry first.") + return + + params["image"] = args.image + params["user"] = args.user + if args.user.isdecimal(): + params["user"] = int(args.user) + + if args.env: + params["env"] = args.env + + if args.workload_dir: + workload_dir = Path(args.workload_dir) + if not workload_dir.is_dir(): + raise ApplicationError(f"'{workload_dir}' is not a directory") + params["datadir"] = workload_dir.resolve() + + script = Path(args.script) + if not script.is_file(): + raise ApplicationError(f"'{script}' is not a file") + params["script"] = script.resolve() + + params["container_cmd"] = "./script" + + try: + client.run(**params) + except ContainerError as ex: + raise ApplicationError(ex.message) + def run(self, args): try: util.set_extra_data_dir(args.data_dir) diff --git a/lcitool/commandline.py b/lcitool/commandline.py index 9e69c49..8835b4c 100644 --- a/lcitool/commandline.py +++ b/lcitool/commandline.py @@ -306,6 +306,13 @@ class CommandLine: ) build_containerparser.set_defaults(func=Application._action_container_build) + run_containerparser = containersubparser.add_parser( + "run", + help="run container action", + parents=[imageopt, containeropt, engineopt, workload_diropt, scriptopt] + ) + run_containerparser.set_defaults(func=Application._action_container_run) + # Validate "container" args def _validate(self, args): """ @@ -327,6 +334,14 @@ class CommandLine: else: log.error("--target and --projects are required") sys.exit(1) + + if args.container == "run": + # "run" subcommand only requires "--script" argument; + # it works with or without "--workload-dir" argument + if not args.script: + log.error("--script is required") + sys.exit(1) + return args def parse(self): |