|
| 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 all MtlManager processes on the remote host using sudo pkill. |
| 57 | + """ |
| 58 | + connection = self.host.connection |
| 59 | + try: |
| 60 | + logger.info("Stopping MtlManager using sudo pkill MtlManager...") |
| 61 | + connection.execute_command("sudo pkill MtlManager") |
| 62 | + logger.info("MtlManager stopped (via pkill).") |
| 63 | + except ConnectionCalledProcessError as e: |
| 64 | + logger.error(f"Failed to stop MtlManager: {e}") |
0 commit comments