|
| 1 | +import logging |
| 2 | + |
| 3 | +from mfd_connect.exceptions import ConnectionCalledProcessError |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +class MtlManager: |
| 9 | + """ |
| 10 | + Class to manage the lifecycle of the MtlManager process on a remote host. |
| 11 | +
|
| 12 | + Attributes: |
| 13 | + host: Host object containing a .connection attribute for remote command execution. |
| 14 | + mtl_manager_process: The running MtlManager process object (if started). |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, host): |
| 18 | + """ |
| 19 | + Initialize the MtlManager with a host object. |
| 20 | + :param host: Host object with a .connection attribute. |
| 21 | + """ |
| 22 | + self.host = host |
| 23 | + self.cmd = "sudo MtlManager" |
| 24 | + self.mtl_manager_process = None |
| 25 | + |
| 26 | + def start(self): |
| 27 | + """ |
| 28 | + Starts the MtlManager process on the remote host using sudo. |
| 29 | + Returns True if started successfully, False otherwise. |
| 30 | + """ |
| 31 | + if not self.mtl_manager_process or not self.mtl_manager_process.running: |
| 32 | + connection = self.host.connection |
| 33 | + try: |
| 34 | + logger.info(f"Running command on host {self.host.name}: {self.cmd}") |
| 35 | + self.mtl_manager_process = connection.start_process( |
| 36 | + self.cmd, stderr_to_stdout=True |
| 37 | + ) |
| 38 | + |
| 39 | + if not self.mtl_manager_process.running: |
| 40 | + err = self.mtl_manager_process.stderr_text |
| 41 | + logger.error(f"MtlManager failed to start. Error output:\n{err}") |
| 42 | + return False |
| 43 | + logger.info( |
| 44 | + f"MtlManager started with PID {self.mtl_manager_process.pid}." |
| 45 | + ) |
| 46 | + return True |
| 47 | + except ConnectionCalledProcessError as e: |
| 48 | + logger.error(f"Failed to start MtlManager: {e}") |
| 49 | + return False |
| 50 | + else: |
| 51 | + logger.info("MtlManager is already running.") |
| 52 | + return True |
| 53 | + |
| 54 | + def stop(self): |
| 55 | + """ |
| 56 | + Stops the MtlManager process on the remote host. |
| 57 | + First attempts to gracefully stop the process if it's tracked, |
| 58 | + then falls back to pkill if needed. |
| 59 | + """ |
| 60 | + if self.mtl_manager_process and self.mtl_manager_process.running: |
| 61 | + try: |
| 62 | + logger.info("Stopping MtlManager using process object methods...") |
| 63 | + # Try graceful termination first |
| 64 | + self.mtl_manager_process.stop() |
| 65 | + |
| 66 | + # Check if the process stopped gracefully |
| 67 | + if self.mtl_manager_process.running: |
| 68 | + logger.info("MtlManager still running, trying kill...") |
| 69 | + self.mtl_manager_process.kill() |
| 70 | + |
| 71 | + # Check logs for errors |
| 72 | + log_output = self.mtl_manager_process.stdout_text |
| 73 | + if log_output: |
| 74 | + if "error" in log_output.lower() or "fail" in log_output.lower(): |
| 75 | + logger.error(f"Errors found in MtlManager logs: {log_output}") |
| 76 | + |
| 77 | + logger.info("MtlManager stopped successfully.") |
| 78 | + return |
| 79 | + except Exception as e: |
| 80 | + logger.error(f"Error while stopping MtlManager process: {e}") |
| 81 | + |
| 82 | + # Fallback to pkill if the process object is not available or the above failed |
| 83 | + connection = self.host.connection |
| 84 | + try: |
| 85 | + logger.info("Stopping MtlManager using sudo pkill MtlManager (fallback)...") |
| 86 | + connection.execute_command("sudo pkill MtlManager") |
| 87 | + logger.info("MtlManager stopped (via pkill).") |
| 88 | + except ConnectionCalledProcessError as e: |
| 89 | + logger.error(f"Failed to stop MtlManager via pkill: {e}") |
0 commit comments