trex_daemon_server: check ZMQ port matches between the server and TRex platform confi... 82/5382/1
authorYaroslav Brustinov <[email protected]>
Sun, 5 Feb 2017 08:03:25 +0000 (10:03 +0200)
committerYaroslav Brustinov <[email protected]>
Sun, 5 Feb 2017 08:03:25 +0000 (10:03 +0200)
Change-Id: I1a6d6dc45513e4ba5f7c50f11f4730c8996c7731
Signed-off-by: Yaroslav Brustinov <[email protected]>
scripts/automation/trex_control_plane/client_utils/external_packages.py
scripts/automation/trex_control_plane/server/outer_packages.py
scripts/automation/trex_control_plane/server/trex_server.py

index 049cc59..ac181ab 100644 (file)
@@ -11,9 +11,9 @@ CURRENT_PATH        = os.path.dirname(os.path.realpath(__file__))
 ROOT_PATH           = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir))     # path to trex_control_plane directory
 PATH_TO_PYTHON_LIB  = os.path.abspath(os.path.join(ROOT_PATH, os.pardir, os.pardir, 'external_libs'))
 ZMQ_PATH            = os.path.abspath(os.path.join(PATH_TO_PYTHON_LIB, 'pyzmq-14.5.0', python_ver, ucs_ver, '64bit'))
+YAML_PATH           = os.path.abspath(os.path.join(PATH_TO_PYTHON_LIB, 'pyyaml-3.11', python_ver))
 
 CLIENT_UTILS_MODULES = ['dpkt-1.8.6',
-                        'yaml-3.11',
                         'texttable-0.8.4',
                         'scapy-2.3.1'
                         'zmq',
@@ -25,11 +25,9 @@ def import_client_utils_modules():
     if PATH_TO_PYTHON_LIB not in sys.path:
         sys.path.insert(0, PATH_TO_PYTHON_LIB)
 
-    if ROOT_PATH not in sys.path:
-        sys.path.append(ROOT_PATH)
-
-    if ZMQ_PATH not in sys.path:
-        sys.path.append(ZMQ_PATH)
+    for path in (ROOT_PATH, ZMQ_PATH, YAML_PATH):
+        if path not in sys.path:
+            sys.path.append(path)
 
     import_module_list(CLIENT_UTILS_MODULES)
 
index c21c8cb..004c2f7 100755 (executable)
@@ -8,7 +8,8 @@ ucs_ver = 'ucs2' if sys.maxunicode == 65535 else 'ucs4'
 CURRENT_PATH         = os.path.dirname(os.path.realpath(__file__))                
 ROOT_PATH            = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir))     # path to trex_control_plane directory
 PATH_TO_PYTHON_LIB   = os.path.abspath(os.path.join(ROOT_PATH, os.pardir, os.pardir, 'external_libs'))
-ZMQ_PATH = os.path.abspath(os.path.join(PATH_TO_PYTHON_LIB, 'pyzmq-14.5.0', python_ver, ucs_ver, '64bit'))
+ZMQ_PATH             = os.path.abspath(os.path.join(PATH_TO_PYTHON_LIB, 'pyzmq-14.5.0', python_ver, ucs_ver, '64bit'))
+YAML_PATH            = os.path.abspath(os.path.join(PATH_TO_PYTHON_LIB, 'pyyaml-3.11', python_ver))
 
 SERVER_MODULES = ['enum34-1.0.4',
                   'zmq',
@@ -23,10 +24,9 @@ def import_server_modules():
     # must be in a higher priority
     if PATH_TO_PYTHON_LIB not in sys.path:
         sys.path.insert(0, PATH_TO_PYTHON_LIB)
-    if ZMQ_PATH not in sys.path:
-        sys.path.insert(0, ZMQ_PATH)
-    if ROOT_PATH not in sys.path:
-        sys.path.append(ROOT_PATH)
+    for path in (ROOT_PATH, ZMQ_PATH, YAML_PATH):
+        if path not in sys.path:
+            sys.path.insert(0, path)
     import_module_list(SERVER_MODULES)
 
 
index 2b718a6..8abd883 100755 (executable)
@@ -7,6 +7,7 @@ import sys
 import time
 import outer_packages
 import zmq
+import yaml
 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
 import jsonrpclib
 from jsonrpclib import Fault
@@ -471,6 +472,8 @@ class CTRexServer(object):
         if trex_args:
             trex_cmd_options += ' %s' % trex_args
 
+        self._check_zmq_port(trex_cmd_options)
+
         if not stateless:
             if 'f' not in kwargs:
                 raise Exception('Argument -f should be specified in stateful command')
@@ -489,6 +492,28 @@ class CTRexServer(object):
         return (cmd, export_path, kwargs.get('d', 0))
 
 
+    def _check_zmq_port(self, trex_cmd_options):
+        zmq_cfg_port = 4500 # default
+        parser = ArgumentParser()
+        parser.add_argument('--cfg', default = '/etc/trex_cfg.yaml')
+        args, _ = parser.parse_known_args(shlex.split(trex_cmd_options))
+        if not os.path.exists(args.cfg):
+            raise Exception('Platform config file "%s" does not exist!' % args.cfg)
+        with open(args.cfg) as f:
+            trex_cfg = yaml.safe_load(f.read())
+        if type(trex_cfg) is not list:
+            raise Exception('Platform config file "%s" content should be array.' % args.cfg)
+        if not len(trex_cfg):
+            raise Exception('Platform config file "%s" content should be array with one element.' % args.cfg)
+        trex_cfg = trex_cfg[0]
+        if 'enable_zmq_pub' in trex_cfg and trex_cfg['enable_zmq_pub'] == False:
+            raise Exception('TRex daemon expects ZMQ publisher to be enabled. Please change "enable_zmq_pub" to true.')
+        if 'zmq_pub_port' in trex_cfg:
+            zmq_cfg_port = trex_cfg['zmq_pub_port']
+        if zmq_cfg_port != self.trex_zmq_port:
+            raise Exception('ZMQ port does not match: platform config file is configured to: %s, daemon server to: %s' % (zmq_cfg_port, self.trex_zmq_port))
+
+
     def __check_trex_path_validity(self):
         # check for executable existance
         if not os.path.exists(self.TREX_PATH+'/t-rex-64'):