def start_pagent(self): """ Construct and start the port agent. @retval port Port that was used for connection to agent """ # Create port agent object. config = { 'device_addr': self.device_addr, 'device_port': self.device_port, 'command_port': self.command_port, 'data_port': self.data_port, 'process_type': PortAgentProcessType.UNIX, 'log_level': 5, } self._pagent = PortAgentProcess.launch_process(config, timeout=60, test_mode=True) pid = self._pagent.get_pid() port = self._pagent.get_data_port() if (pid and port): mi_logger.info('Started port agent pid %d listening at port %d', pid, port) else: raise PortAgentLaunchException() return port
def get_process(cls, config, timeout=DEFAULT_TIMEOUT, test_mode=False): """ factory class to return the correct PortAgentProcess type based on the config. config must contain process_type and type. Currently both of these default to python and ethernet respectively because we only have one type of port agent could use variable length parameter lists (**kwargs) here, but I am following the same pattern the initial port agent used for passing in configurations. @param config dictionary containing configuration information for the port agent. @param timeout timeout for port agent launch. If exceeded an exception is raised @param test_mode enable test mode for the port agent """ # Default to unix port agent process_type = config.get("process_type", PortAgentProcessType.UNIX) if process_type == PortAgentProcessType.PYTHON: return PythonPortAgentProcess(config, timeout, test_mode) if process_type == PortAgentProcessType.UNIX: return UnixPortAgentProcess(config, timeout, test_mode) else: raise PortAgentLaunchException( "unknown port agent process type: %s" % process_type)
def __init__(self, config, timeout=DEFAULT_TIMEOUT, test_mode=False): """ Initialize the Python port agent object using the passed in config. This defaults to ethernet as the type because that is currently the only port agent we have. @raises PortAgentMissingConfig """ self._config = config self._timeout = timeout self._test_mode = test_mode # Verify our configuration is correct self._device_addr = config.get("device_addr") self._device_port = config.get("device_port") self._working_dir = config.get("working_dir", '/tmp/') self._delimiter = config.get("delimiter", ['<<', '>>']) self._type = config.get("type", PortAgentType.ETHERNET) if not self._device_addr: raise PortAgentMissingConfig("missing config: device_addr") if not self._device_port: raise PortAgentMissingConfig("missing config: device_port") if not self._type == PortAgentType.ETHERNET: raise PortAgentLaunchException("unknown port agent type: %s" % self._type)
def __init__(self, config, timeout=DEFAULT_TIMEOUT, test_mode=False): """ Initialize the Python port agent object using the passed in config. This defaults to ethernet as the type because that is currently the only port agent we have. @raises PortAgentMissingConfig """ self._config = config self._timeout = timeout self._test_mode = test_mode # Verify our configuration is correct self._device_addr = config.get("device_addr") self._device_port = config.get("device_port") self._binary_path = config.get("binary_path", "port_agent") self._command_port = config.get("command_port") self._pa_addr = config.get("port_agent_addr") self._data_port = config.get("data_port") self._log_level = config.get("log_level") self._type = config.get("type", PortAgentType.ETHERNET) if not self._pa_addr: self._pa_addr = LOCALHOST if not self._device_addr: raise PortAgentMissingConfig("missing config: device_addr") if not self._device_port: raise PortAgentMissingConfig("missing config: device_port") if not self._command_port: raise PortAgentMissingConfig("missing config: command_port") if not self._data_port: raise PortAgentMissingConfig("missing config: data_port") if not self._binary_path: raise PortAgentMissingConfig("missing config: binary_path") if not self._type == PortAgentType.ETHERNET: raise PortAgentLaunchException("unknown port agent type: %s" % self._type) self._tmp_config = self.get_config()
def run_command(self, command_line): log.debug("run command: " + str(command_line)) process = subprocess.Popen(command_line, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) gevent.sleep(1) process.poll() # We have failed! if (process.returncode and process.pid): output, error_message = process.communicate() log.error("Failed to run command: STDERR: %s" % (error_message)) raise PortAgentLaunchException("failed to launch port agent") log.debug("command successful. pid: %d" % (process.pid)) return process.pid
def __init__(self, config, timeout=DEFAULT_TIMEOUT, test_mode=False): """ Initialize the Python port agent object using the passed in config. This defaults to ethernet as the type because that is currently the only port agent we have. @raises PortAgentMissingConfig """ self._config = config self._timeout = timeout self._test_mode = test_mode # Verify our configuration is correct self._observatory_type = config.get("observatory_type", ObservatoryType.STANDARD) self._device_addr = config.get("device_addr") self._device_port = config.get("device_port") self._device_tx_port = config.get("device_tx_port") self._device_rx_port = config.get("device_rx_port") self._binary_path = config.get("binary_path", "port_agent") self._command_port = config.get("command_port") self._pa_addr = config.get("port_agent_addr") self._heartbeat_interval = config.get("heartbeat_interval") """ DHE: need to be able to retrieve multiple data ports here """ self._data_port = config.get("data_port") self._log_level = config.get("log_level") self._type = config.get("instrument_type", PortAgentType.ETHERNET) if self._type == PortAgentType.ETHERNET: self._device_addr = config.get("device_addr") self._device_port = config.get("device_port") elif self._type == PortAgentType.SERIAL: self._device_os_port = config.get("device_os_port", None) self._device_baud = config.get("device_baud", None) self._device_data_bits = config.get("device_data_bits", None) self._device_parity = config.get("device_parity", None) self._device_stop_bits = config.get("device_stop_bits", None) self._device_flow_control = config.get("device_flow_control", None) if not self._pa_addr: self._pa_addr = LOCALHOST if not self._heartbeat_interval: self._heartbeat_interval = DEFAULT_HEARTBEAT if PortAgentType.BOTPT == self._type: if not self._device_tx_port: raise PortAgentMissingConfig( "missing config: device_tx_port (BOTPT)") if not self._device_rx_port: raise PortAgentMissingConfig( "missing config: device_rx_port (BOTPT)") elif PortAgentType.ETHERNET == self._type: if not self._device_addr: raise PortAgentMissingConfig("missing config: device_addr") if not self._device_port: raise PortAgentMissingConfig( "missing config: device_port (ETHERNET)") elif PortAgentType.SERIAL == self._type: if self._device_os_port == None: raise PortAgentMissingConfig("missing config: device_os_port") if self._device_baud == None: raise PortAgentMissingConfig("missing config: device_baud") if self._device_data_bits == None: raise PortAgentMissingConfig( "missing config: device_data_bits") if self._device_parity == None: raise PortAgentMissingConfig("missing config: device_parity") if self._device_stop_bits == None: raise PortAgentMissingConfig( "missing config: device_stop_bits") if self._device_flow_control == None: raise PortAgentMissingConfig( "missing config: device_flow_control") else: raise PortAgentLaunchException("unknown port agent type: %s" % self._type) if not self._command_port: raise PortAgentMissingConfig("missing config: command_port") if ObservatoryType.MULTI == self._observatory_type: if not self._data_port: raise PortAgentMissingConfig("missing config: data_port") else: if not self._data_port: raise PortAgentMissingConfig("missing config: data_port") if not self._binary_path: raise PortAgentMissingConfig("missing config: binary_path") self._tmp_config = self.get_config()