|
| 1 | +from argparse import ArgumentParser, Namespace, Action |
| 2 | +from pathlib import Path |
| 3 | +import tarfile |
| 4 | +import sys |
| 5 | +import shutil |
| 6 | +import tempfile |
| 7 | +from typing import Iterable |
| 8 | + |
| 9 | +from pavilion import output |
| 10 | +from pavilion import schedulers |
| 11 | +from pavilion.config import PavConfig |
| 12 | +from pavilion.test_run import TestRun |
| 13 | +from pavilion.test_ids import TestID |
| 14 | +from pavilion.cmd_utils import get_last_test_id, get_tests_by_id, list_files |
| 15 | +from pavilion.utils import copytree_resolved |
| 16 | +from pavilion.scriptcomposer import ScriptComposer |
| 17 | +from pavilion.errors import SchedulerPluginError |
| 18 | +from pavilion.schedulers.config import validate_config, calc_node_range |
| 19 | +from .base_classes import Command |
| 20 | + |
| 21 | + |
| 22 | +class IsolateCommand(Command): |
| 23 | + """Isolates an existing test run in a form that can be run without Pavilion.""" |
| 24 | + |
| 25 | + IGNORE_FILES = ("series", "job") |
| 26 | + KICKOFF_FN = "kickoff.isolated" |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + super().__init__( |
| 30 | + "isolate", |
| 31 | + "Isolate an existing test run.", |
| 32 | + short_help="Isolate a test run." |
| 33 | + ) |
| 34 | + |
| 35 | + def _setup_arguments(self, parser: ArgumentParser) -> None: |
| 36 | + """Setup the argument parser for the isolate command.""" |
| 37 | + |
| 38 | + parser.add_argument( |
| 39 | + "test_id", |
| 40 | + type=TestID, |
| 41 | + nargs="?", |
| 42 | + help="test ID" |
| 43 | + ) |
| 44 | + |
| 45 | + parser.add_argument( |
| 46 | + "path", |
| 47 | + type=Path, |
| 48 | + help="isolation path" |
| 49 | + ) |
| 50 | + |
| 51 | + parser.add_argument( |
| 52 | + "-a", |
| 53 | + "--archive", |
| 54 | + action="store_true", |
| 55 | + default=False, |
| 56 | + help="archive the test" |
| 57 | + ) |
| 58 | + |
| 59 | + parser.add_argument( |
| 60 | + "-z", |
| 61 | + "--zip", |
| 62 | + default=False, |
| 63 | + help="compress the test archive", |
| 64 | + action="store_true" |
| 65 | + ) |
| 66 | + |
| 67 | + def run(self, pav_cfg: PavConfig, args: Namespace) -> int: |
| 68 | + """Run the isolate command.""" |
| 69 | + |
| 70 | + if args.zip and not args.archive: |
| 71 | + output.fprint(self.errfile, "--archive must be specified to use --zip.") |
| 72 | + |
| 73 | + return 1 |
| 74 | + |
| 75 | + test_id = args.test_id |
| 76 | + |
| 77 | + if args.test_id is None: |
| 78 | + test_id = get_last_test_id(pav_cfg, self.errfile) |
| 79 | + |
| 80 | + if test_id is None: |
| 81 | + output.fprint(self.errfile, "No last test found.", color=output.RED) |
| 82 | + |
| 83 | + return 2 |
| 84 | + |
| 85 | + tests = get_tests_by_id(pav_cfg, [test_id], self.errfile) |
| 86 | + |
| 87 | + if len(tests) == 0: |
| 88 | + output.fprint(self.errfile, "Could not find test '{}'".format(test_id)) |
| 89 | + |
| 90 | + return 3 |
| 91 | + |
| 92 | + elif len(tests) > 1: |
| 93 | + output.fprint( |
| 94 | + self.errfile, "Matched multiple tests. Printing file contents for first " |
| 95 | + "test only (test {})".format(tests[0].full_id), |
| 96 | + color=output.YELLOW) |
| 97 | + |
| 98 | + return 4 |
| 99 | + |
| 100 | + test = next(iter(tests)) |
| 101 | + |
| 102 | + return self._isolate(pav_cfg, test, args.path, args.archive, args.zip) |
| 103 | + |
| 104 | + @classmethod |
| 105 | + def _isolate(cls, pav_cfg: PavConfig, test: TestRun, dest: Path, archive: bool, |
| 106 | + zip: bool) -> int: |
| 107 | + """Given a test run and a destination path, isolate that test run, optionally |
| 108 | + creating a tarball.""" |
| 109 | + |
| 110 | + if not test.path.is_dir(): |
| 111 | + output.fprint(sys.stderr, "Directory '{}' does not exist." |
| 112 | + .format(test.path.as_posix()), color=output.RED) |
| 113 | + |
| 114 | + return 5 |
| 115 | + |
| 116 | + if dest.exists(): |
| 117 | + output.fprint( |
| 118 | + sys.stderr, |
| 119 | + f"Unable to isolate test {test.id}. Destination {dest} already exists.", |
| 120 | + color=output.RED) |
| 121 | + |
| 122 | + return 6 |
| 123 | + |
| 124 | + if archive: |
| 125 | + cls._write_tarball(pav_cfg, |
| 126 | + test, |
| 127 | + dest, |
| 128 | + zip, |
| 129 | + cls.IGNORE_FILES) |
| 130 | + |
| 131 | + else: |
| 132 | + try: |
| 133 | + copytree_resolved(test.path, dest, ignore_files=cls.IGNORE_FILES) |
| 134 | + except OSError as err: |
| 135 | + output.fprint( |
| 136 | + sys.stderr, |
| 137 | + f"Unable to isolate test {test.id} at {dest}: {err}", |
| 138 | + color=output.RED) |
| 139 | + |
| 140 | + return 8 |
| 141 | + |
| 142 | + pav_lib_bash = pav_cfg.pav_root / 'bin' / TestRun.PAV_LIB_FN |
| 143 | + shutil.copyfile(pav_lib_bash, dest / TestRun.PAV_LIB_FN) |
| 144 | + |
| 145 | + cls._write_kickoff_script(pav_cfg, test, dest / cls.KICKOFF_FN) |
| 146 | + |
| 147 | + return 0 |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def _write_tarball(cls, pav_cfg: PavConfig, test: TestRun, dest: Path, zip: bool, |
| 151 | + ignore_files: Iterable[str]) -> None: |
| 152 | + """Given a test run object, create a tarball of its run directory in the specified |
| 153 | + location.""" |
| 154 | + |
| 155 | + if zip: |
| 156 | + if len(dest.suffixes) == 0: |
| 157 | + dest = dest.with_suffix(".tgz") |
| 158 | + |
| 159 | + modestr = "w:gz" |
| 160 | + else: |
| 161 | + if len(dest.suffixes) == 0: |
| 162 | + dest = dest.with_suffix(".tar") |
| 163 | + |
| 164 | + modestr = "w:" |
| 165 | + |
| 166 | + with tempfile.TemporaryDirectory() as tmp: |
| 167 | + tmp = Path(tmp) |
| 168 | + tmp_dest = tmp / dest.stem |
| 169 | + tmp_dest.mkdir() |
| 170 | + copytree_resolved(test.path, tmp_dest, ignore_files=ignore_files) |
| 171 | + |
| 172 | + # Copy Pavilion bash library into tarball |
| 173 | + pav_lib_bash = pav_cfg.pav_root / 'bin' / TestRun.PAV_LIB_FN |
| 174 | + shutil.copyfile(pav_lib_bash, tmp_dest / TestRun.PAV_LIB_FN) |
| 175 | + |
| 176 | + cls._write_kickoff_script(pav_cfg, test, tmp_dest / cls.KICKOFF_FN) |
| 177 | + |
| 178 | + try: |
| 179 | + with tarfile.open(dest, modestr) as tarf: |
| 180 | + for fname in list_files(tmp): |
| 181 | + tarf.add( |
| 182 | + fname, |
| 183 | + arcname=fname.relative_to(tmp), |
| 184 | + recursive=False) |
| 185 | + except (tarfile.TarError, OSError): |
| 186 | + output.fprint( |
| 187 | + sys.stderr, |
| 188 | + f"Unable to isolate test {test.id} at {dest}.", |
| 189 | + color=output.RED) |
| 190 | + |
| 191 | + return 7 |
| 192 | + |
| 193 | + @classmethod |
| 194 | + def _write_kickoff_script(cls, pav_cfg: PavConfig, test: TestRun, script_path: Path) -> None: |
| 195 | + """Write a special kickoff script that can be used to run the given test independently of |
| 196 | + Pavilion.""" |
| 197 | + |
| 198 | + try: |
| 199 | + sched = schedulers.get_plugin(test.scheduler) |
| 200 | + except SchedulerPluginError: |
| 201 | + output.fprint( |
| 202 | + sys.stderr, |
| 203 | + f"Unable to generate kickoff script for test {test_id}: unable to load scheduler" |
| 204 | + f" {test.scheduler}." |
| 205 | + ) |
| 206 | + return 9 |
| 207 | + |
| 208 | + sched_config = validate_config(test.config['schedule']) |
| 209 | + node_range = calc_node_range(sched_config, sched_config['cluster_info']['node_count']) |
| 210 | + |
| 211 | + script = sched.create_kickoff_script( |
| 212 | + pav_cfg, |
| 213 | + test, |
| 214 | + isolate=True) |
| 215 | + |
| 216 | + script.write(script_path) |
0 commit comments