|
| 1 | +import multiprocessing |
| 2 | +import signal |
| 3 | +import sys |
| 4 | +import time |
| 5 | +import traceback |
| 6 | +from multiprocessing import set_start_method |
| 7 | + |
| 8 | +from avocado.core.exceptions import TestInterrupt |
| 9 | +from avocado.core.nrunner.app import BaseRunnerApp |
| 10 | +from avocado.core.nrunner.runner import ( |
| 11 | + RUNNER_RUN_CHECK_INTERVAL, |
| 12 | + RUNNER_RUN_STATUS_INTERVAL, |
| 13 | + BaseRunner, |
| 14 | +) |
| 15 | +from avocado.core.utils import messages |
| 16 | +from avocado.core.utils.messages import start_logging |
| 17 | +from avocado.plugins.vmimage import download_image |
| 18 | +from avocado.utils import vmimage |
| 19 | + |
| 20 | + |
| 21 | +class VMImageRunner(BaseRunner): |
| 22 | + """ |
| 23 | + Runner for dependencies of type vmimage. |
| 24 | + This runner uses the vmimage plugin's download_image function which handles: |
| 25 | + 1. Checking if the image exists in cache |
| 26 | + 2. Downloading the image if not in cache |
| 27 | + 3. Storing the image in the configured cache directory |
| 28 | + 4. Returning the cached image path |
| 29 | + """ |
| 30 | + |
| 31 | + name = "vmimage" |
| 32 | + description = "Runner for dependencies of type vmimage" |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def signal_handler(signum, frame): # pylint: disable=W0613 |
| 36 | + if signum == signal.SIGTERM.value: |
| 37 | + raise TestInterrupt("VM image operation interrupted: Timeout reached") |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def _run_vmimage_operation(runnable, queue): |
| 41 | + try: |
| 42 | + signal.signal(signal.SIGTERM, VMImageRunner.signal_handler) |
| 43 | + start_logging(runnable.config, queue) |
| 44 | + # Get parameters from runnable.kwargs |
| 45 | + provider = runnable.kwargs.get("provider") |
| 46 | + version = runnable.kwargs.get("version") |
| 47 | + arch = runnable.kwargs.get("arch") |
| 48 | + |
| 49 | + if not provider: |
| 50 | + stderr = "Missing required parameter: provider" |
| 51 | + queue.put(messages.StderrMessage.get(stderr.encode())) |
| 52 | + queue.put(messages.FinishedMessage.get("error")) |
| 53 | + return |
| 54 | + |
| 55 | + message = f"Getting VM image for {provider}" |
| 56 | + if version: |
| 57 | + message += f" {version}" |
| 58 | + if arch: |
| 59 | + message += f" {arch}" |
| 60 | + queue.put(messages.StdoutMessage.get(message.encode())) |
| 61 | + |
| 62 | + queue.put( |
| 63 | + messages.StdoutMessage.get( |
| 64 | + f"Attempting to download image for {provider} (version: {version or 'latest'}, arch: {arch})".encode() |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + image = download_image(provider, version, arch) |
| 69 | + if not image: |
| 70 | + raise RuntimeError("Failed to get image") |
| 71 | + |
| 72 | + queue.put( |
| 73 | + messages.StdoutMessage.get( |
| 74 | + f"Successfully retrieved VM image from cache or downloaded to: {image['file']}".encode() |
| 75 | + ) |
| 76 | + ) |
| 77 | + queue.put(messages.FinishedMessage.get("pass")) |
| 78 | + |
| 79 | + except (AttributeError, RuntimeError, vmimage.ImageProviderError) as e: |
| 80 | + # AttributeError: provider not found |
| 81 | + # RuntimeError: failed to get image |
| 82 | + # ImageProviderError: provider-specific errors |
| 83 | + queue.put( |
| 84 | + messages.StderrMessage.get( |
| 85 | + f"Failed to download image: {str(e)}".encode() |
| 86 | + ) |
| 87 | + ) |
| 88 | + queue.put( |
| 89 | + messages.FinishedMessage.get( |
| 90 | + "error", |
| 91 | + fail_reason=str(e), |
| 92 | + fail_class=e.__class__.__name__, |
| 93 | + traceback=traceback.format_exc(), |
| 94 | + ) |
| 95 | + ) |
| 96 | + except (TestInterrupt, multiprocessing.TimeoutError) as e: |
| 97 | + queue.put( |
| 98 | + messages.StderrMessage.get(f"Operation interrupted: {str(e)}".encode()) |
| 99 | + ) |
| 100 | + queue.put( |
| 101 | + messages.FinishedMessage.get( |
| 102 | + "error", |
| 103 | + fail_reason=str(e), |
| 104 | + fail_class=e.__class__.__name__, |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def _monitor(queue): |
| 110 | + most_recent_status_time = None |
| 111 | + while True: |
| 112 | + time.sleep(RUNNER_RUN_CHECK_INTERVAL) |
| 113 | + |
| 114 | + if queue.empty(): |
| 115 | + now = time.monotonic() |
| 116 | + if ( |
| 117 | + most_recent_status_time is None |
| 118 | + or now >= most_recent_status_time + RUNNER_RUN_STATUS_INTERVAL |
| 119 | + ): |
| 120 | + most_recent_status_time = now |
| 121 | + yield messages.RunningMessage.get() |
| 122 | + continue |
| 123 | + |
| 124 | + message = queue.get() |
| 125 | + yield message |
| 126 | + if message.get("status") == "finished": |
| 127 | + break |
| 128 | + |
| 129 | + def run(self, runnable): |
| 130 | + signal.signal(signal.SIGTERM, VMImageRunner.signal_handler) |
| 131 | + yield messages.StartedMessage.get() |
| 132 | + |
| 133 | + queue = multiprocessing.SimpleQueue() |
| 134 | + process = multiprocessing.Process( |
| 135 | + target=self._run_vmimage_operation, args=(runnable, queue) |
| 136 | + ) |
| 137 | + |
| 138 | + try: |
| 139 | + process.start() |
| 140 | + |
| 141 | + for message in self._monitor(queue): |
| 142 | + yield message |
| 143 | + |
| 144 | + except TestInterrupt: |
| 145 | + process.terminate() |
| 146 | + for message in self._monitor(queue): |
| 147 | + yield message |
| 148 | + except (multiprocessing.ProcessError, OSError) as e: |
| 149 | + # ProcessError: Issues with process management |
| 150 | + # OSError: System-level errors (e.g. resource limits) |
| 151 | + yield messages.StderrMessage.get(f"Process error: {str(e)}".encode()) |
| 152 | + yield messages.FinishedMessage.get( |
| 153 | + "error", |
| 154 | + fail_reason=str(e), |
| 155 | + fail_class=e.__class__.__name__, |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +class RunnerApp(BaseRunnerApp): |
| 160 | + PROG_NAME = "avocado-runner-vmimage" |
| 161 | + PROG_DESCRIPTION = "nrunner application for dependencies of type vmimage" |
| 162 | + RUNNABLE_KINDS_CAPABLE = ["vmimage"] |
| 163 | + |
| 164 | + |
| 165 | +def main(): |
| 166 | + if sys.platform == "darwin": |
| 167 | + set_start_method("fork") |
| 168 | + app = RunnerApp(print) |
| 169 | + app.run() |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + main() |
0 commit comments