summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/server
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-06-06 11:10:44 +0300
committerYaroslav Brustinov <ybrustin@cisco.com>2016-06-06 11:10:44 +0300
commit1454985e5408bdf796a9c9978622f2ba66ec04d8 (patch)
tree6319682d51fcf6756c3166491337ac0c85b149f6 /scripts/automation/trex_control_plane/server
parent2aaf83ee88b49e3a10543f8415f73652be606ecc (diff)
daemons fixes
Diffstat (limited to 'scripts/automation/trex_control_plane/server')
-rwxr-xr-xscripts/automation/trex_control_plane/server/singleton_daemon.py13
-rwxr-xr-xscripts/automation/trex_control_plane/server/trex_launch_thread.py3
-rwxr-xr-xscripts/automation/trex_control_plane/server/trex_server.py27
3 files changed, 16 insertions, 27 deletions
diff --git a/scripts/automation/trex_control_plane/server/singleton_daemon.py b/scripts/automation/trex_control_plane/server/singleton_daemon.py
index 81b384c5..8fdedc6e 100755
--- a/scripts/automation/trex_control_plane/server/singleton_daemon.py
+++ b/scripts/automation/trex_control_plane/server/singleton_daemon.py
@@ -11,7 +11,7 @@ import jsonrpclib
# uses Unix sockets for determine running process.
# (assumes used daemons will register proper socket)
-# all daemons should use -p argument as listening tcp port
+# all daemons should use -p argument as listening tcp port and check_connectivity RPC method
class SingletonDaemon(object):
# run_cmd can be function of how to run daemon or a str to run at subprocess
@@ -102,15 +102,10 @@ class SingletonDaemon(object):
poll_rate = 0.1
for i in range(int(timeout/poll_rate)):
try:
- daemon.not_existing_function_asdfasd()
+ daemon.check_connectivity()
+ return True
except socket.error: # daemon is not up yet
sleep(poll_rate)
- except Exception as e: # expect error of not supported function
- if type(e.args) is tuple and\
- type(e.args[0]) is tuple and\
- e.args[0][0] == -32601: # error code is written hardcoded in JsonRPC Server
- return True
- raise
return False
# start daemon
@@ -175,6 +170,8 @@ def run_command(command, timeout = 15, cwd = None):
if proc.poll() is None:
proc.kill() # timeout
return (errno.ETIME, '', 'Timeout on running: %s' % command)
+ else:
+ proc.wait()
stdout_file.seek(0)
stderr_file.seek(0)
return (proc.returncode, stdout_file.read().decode(errors = 'replace'), stderr_file.read().decode(errors = 'replace'))
diff --git a/scripts/automation/trex_control_plane/server/trex_launch_thread.py b/scripts/automation/trex_control_plane/server/trex_launch_thread.py
index 74ce1750..82a7f996 100755
--- a/scripts/automation/trex_control_plane/server/trex_launch_thread.py
+++ b/scripts/automation/trex_control_plane/server/trex_launch_thread.py
@@ -6,6 +6,7 @@ import signal
import socket
from common.trex_status_e import TRexStatus
import subprocess
+import shlex
import time
import threading
import logging
@@ -32,7 +33,7 @@ class AsynchronousTRexSession(threading.Thread):
with open(os.devnull, 'w') as DEVNULL:
self.time_stamps['start'] = self.time_stamps['run_time'] = time.time()
- self.session = subprocess.Popen("exec "+self.cmd, cwd = self.launch_path, shell=True, stdin = DEVNULL, stderr = subprocess.PIPE, preexec_fn=os.setsid)
+ self.session = subprocess.Popen(shlex.split(self.cmd), cwd = self.launch_path, stdin = DEVNULL, stderr = subprocess.PIPE, preexec_fn=os.setsid, close_fds = True)
logger.info("TRex session initialized successfully, Parent process pid is {pid}.".format( pid = self.session.pid ))
while self.session.poll() is None: # subprocess is NOT finished
time.sleep(0.5)
diff --git a/scripts/automation/trex_control_plane/server/trex_server.py b/scripts/automation/trex_control_plane/server/trex_server.py
index 45ef9ac1..8f7e99f0 100755
--- a/scripts/automation/trex_control_plane/server/trex_server.py
+++ b/scripts/automation/trex_control_plane/server/trex_server.py
@@ -30,9 +30,9 @@ import shlex
import tempfile
try:
- from .singleton_daemon import register_socket
+ from .singleton_daemon import register_socket, run_command
except:
- from singleton_daemon import register_socket
+ from singleton_daemon import register_socket, run_command
# setup the logger
@@ -134,6 +134,7 @@ class CTRexServer(object):
self.server.register_function(self.add)
self.server.register_function(self.cancel_reservation)
self.server.register_function(self.connectivity_check)
+ self.server.register_function(self.connectivity_check, 'check_connectivity') # alias
self.server.register_function(self.force_trex_kill)
self.server.register_function(self.get_file)
self.server.register_function(self.get_files_list)
@@ -164,16 +165,6 @@ class CTRexServer(object):
self.server.shutdown()
#self.server.server_close()
- def _run_command(self, command, timeout = 15, cwd = None):
- if timeout:
- command = 'timeout %s %s' % (timeout, command)
- # pipes might stuck, even with timeout
- with tempfile.TemporaryFile() as stdout_file, tempfile.TemporaryFile() as stderr_file:
- proc = subprocess.Popen(shlex.split(command), stdout=stdout_file, stderr=stderr_file, cwd = cwd)
- proc.wait()
- stdout_file.seek(0)
- stderr_file.seek(0)
- return (proc.returncode, stdout_file.read().decode(errors = 'replace'), stderr_file.read().decode(errors = 'replace'))
# get files from Trex server and return their content (mainly for logs)
@staticmethod
@@ -234,7 +225,7 @@ class CTRexServer(object):
try:
logger.info("Processing get_trex_version() command.")
if not self.trex_version:
- ret_code, stdout, stderr = self._run_command('./t-rex-64 --help', cwd = self.TREX_PATH, timeout = 0)
+ ret_code, stdout, stderr = run_command('./t-rex-64 --help', cwd = self.TREX_PATH)
search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL)
if not search_result:
raise Exception('Could not determine version from ./t-rex-64 --help')
@@ -383,7 +374,7 @@ class CTRexServer(object):
# returns list of tuples (pid, command line) of running TRex(es)
def get_trex_cmds(self):
logger.info('Processing get_trex_cmds() command.')
- ret_code, stdout, stderr = self._run_command('ps -u root --format pid,comm,cmd')
+ ret_code, stdout, stderr = run_command('ps -u root --format pid,comm,cmd')
if ret_code:
raise Exception('Failed to determine running processes, stderr: %s' % stderr)
trex_cmds_list = []
@@ -403,12 +394,12 @@ class CTRexServer(object):
return False
for pid, cmd in trex_cmds_list:
logger.info('Killing process %s %s' % (pid, cmd))
- self._run_command('kill %s' % pid)
- ret_code_ps, _, _ = self._run_command('ps -p %s' % pid)
+ run_command('kill %s' % pid)
+ ret_code_ps, _, _ = run_command('ps -p %s' % pid)
if not ret_code_ps:
logger.info('Killing with -9.')
- self._run_command('kill -9 %s' % pid)
- ret_code_ps, _, _ = self._run_command('ps -p %s' % pid)
+ run_command('kill -9 %s' % pid)
+ ret_code_ps, _, _ = run_command('ps -p %s' % pid)
if not ret_code_ps:
logger.info('Could not kill process.')
raise Exception('Could not kill process %s %s' % (pid, cmd))