[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lnst-developers
Subject:    [PATCH 4/5] NetTestController: add support for PyRecipes
From:       Jiri Prochazka <jprochaz () redhat ! com>
Date:       2016-01-16 17:50:33
Message-ID: 1452966634-25908-5-git-send-email-jprochaz () redhat ! com
[Download RAW message or body]

This patch adds support for both Task and Network part of
PyRecipes.

Tasks are wrapped in class, so Controller iterates over objects
in file with Task and checks if there is a class with parent LnstTask.
If there is one, import it and run task() method. This implementation
is backwards compatible with old Python tasks.

What is completely new, is using Python recipes for network definition.
Processing of PyRecipe network definition is similar to task definition,
again, class with parent class LnstNetwork is looked for. If found,
instance of this class is created, method setup() is run and result is
acquired via get_machines() method. Output of this is dictionary with
machines and it's interfaces, which is 1:1 as what RecipeParser generates.

For more info about how to use PyRecipes, please, see cover letter and
example recipes.

Signed-off-by: Jiri Prochazka <jprochaz@redhat.com>
---
 lnst/Controller/NetTestController.py | 39 ++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/lnst/Controller/NetTestController.py b/lnst/Controller/NetTestController.py
index 04aa473..9436ccc 100644
--- a/lnst/Controller/NetTestController.py
+++ b/lnst/Controller/NetTestController.py
@@ -19,6 +19,7 @@ import cPickle
 import imp
 import copy
 import sys
+import inspect
 from time import sleep
 from lnst.Common.NetUtils import MacPool
 from lnst.Common.Utils import md5sum, dir_md5sum
@@ -28,6 +29,7 @@ from lnst.Controller.RecipeParser import RecipeParser, RecipeError
 from lnst.Controller.SlavePool import SlavePool
 from lnst.Controller.Machine import MachineError, VirtualInterface
 from lnst.Controller.Machine import StaticInterface
+from lnst.Controller.PyRecipe import LnstTask, LnstNetwork
 from lnst.Common.ConnectionHandler import send_data, recv_data
 from lnst.Common.ConnectionHandler import ConnectionHandler
 from lnst.Common.Config import lnst_config
@@ -76,6 +78,31 @@ class NetTestController:
         self._parser.set_machines(self._machines)
         self._parser.set_aliases(defined_aliases, overriden_aliases)
         self._recipe = self._parser.parse()
+        if "network" in self._recipe:
+            root = Path(None, self._recipe_path).get_root()
+            path = Path(root, self._recipe["network"])
+
+            if not path.exists():
+                msg = "PyRecipe file '%s' not found." % self._recipe["network"]
+                raise RecipeError(msg, self._recipe)
+
+            cwd = os.getcwd()
+            name = os.path.basename(path.abs_path()).split(".")[0]
+            sys.path.append(os.path.dirname(path.resolve()))
+            os.chdir(os.path.dirname(path.resolve()))
+
+            module = imp.load_source('PyRecipe', path.resolve())
+            # iterates over all objects in imported module
+            for name, obj in inspect.getmembers(module):
+                # searches for classes with parent class LnstNetwork
+                if inspect.isclass(obj) and LnstNetwork in obj.__bases__:
+                    n = obj()
+                    n.setup()
+                    self._recipe["machines"] = n.get_machines()
+                    self._recipe = self._recipe.to_dict()
+
+            os.chdir(cwd)
+            sys.path.remove(os.path.dirname(path.resolve()))
 
         conf_pools = lnst_config.get_pools()
         pools = {}
@@ -346,7 +373,10 @@ class NetTestController:
             iface.set_netem(iface_xml_data["netem"].to_dict())
 
         if "ovs_conf" in iface_xml_data:
-            iface.set_ovs_conf(iface_xml_data["ovs_conf"].to_dict())
+            if isinstance(iface_xml_data["ovs_conf"], dict):
+                iface.set_ovs_conf(iface_xml_data["ovs_conf"])
+            else:
+                iface.set_ovs_conf(iface_xml_data["ovs_conf"].to_dict())
 
         if iface_xml_data["netns"] != None:
             iface.set_netns(iface_xml_data["netns"])
@@ -704,13 +734,18 @@ class NetTestController:
         sys.path.append(os.path.dirname(task_path.resolve()))
         os.chdir(os.path.dirname(task_path.resolve()))
         module = imp.load_source(name, task_path.resolve())
+        # iterates over all objects in imported module
+        for name, obj in inspect.getmembers(module):
+            # searches for classes with parent class LnstTask
+            if inspect.isclass(obj) and LnstTask in obj.__bases__:
+                obj(Task.ctl).task()
         os.chdir(cwd)
         sys.path.remove(os.path.dirname(task_path.resolve()))
 
         #restore resource table
         self._resource_table = res_table_bkp
 
-        return module.ctl._result
+        return Task.ctl._result
 
     def _run_task(self, task):
         if "python" in task:
-- 
2.4.3
_______________________________________________
LNST-developers mailing list
lnst-developers@lists.fedorahosted.org
https://lists.fedorahosted.org/admin/lists/lnst-developers@lists.fedorahosted.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic