Following patch set allows user to use aliases from a python task. This was
not possible before.
Example:
<lnstrecipe>
<define>
<alias name="virtual_ip" value="192.168.111.10/24"/>
</define>
<network>
<host id="m1">
<interfaces>
<eth id="nic1" label="testnet"/>
</interfaces>
</host>
<host id="m2">
<interfaces>
<eth id="nic1" label="testnet"/>
</interfaces>
</host>
</network>
<task python="my_task.py"/>
</lnstrecipe>
And the content of my_task.py:
from lnst.Controller.Task import ctl
virtual_ip = ctl.get_alias("virtual_ip")
m1 = ctl.get_host("m1")
m1.run("ip a add dev %s %s" % (m1.get_devname("nic1"), virtual_ip))
I've run regression check for the patch series without errors.
Jan Tluka (3):
XmlTemplates: do not drop toplevel aliases so that python task can see
them
NetTestController: adding get_alias method
ControllerAPI: adding get_alias method to ControllerAPI
lnst/Controller/NetTestController.py | 12 ++++++++----
lnst/Controller/Task.py | 12 ++++++++++++
lnst/Controller/XmlTemplates.py | 5 ++++-
3 files changed, 24 insertions(+), 5 deletions(-)
--
1.8.1.4
Show replies by date
Current implementation uses XmlTemplates to process aliases in the recipe XML
during parsing phase. This works fine for tasks written in XML itself or
xincluded. For the Python tasks this currently does not work since the task
is not being processed for aliases. This patch prevents removal of available
top-level aliases from XmlTemplates processor so that python task can access
them via ControllerAPI during the task execution.
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
lnst/Controller/XmlTemplates.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lnst/Controller/XmlTemplates.py b/lnst/Controller/XmlTemplates.py
index 1a65c8b..7572b16 100644
--- a/lnst/Controller/XmlTemplates.py
+++ b/lnst/Controller/XmlTemplates.py
@@ -336,7 +336,10 @@ class XmlTemplates:
for child in element.getchildren():
self.process_aliases(child)
- self.drop_namespace_level()
+ # do not drop alias definitions when at top-level so that python
+ # tasks are able to access them
+ if element.tag != "lnstrecipe":
+ self.drop_namespace_level()
def expand_aliases(self, string):
while True:
--
1.8.1.4
Adding _get_alias method to the NetTestController.
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
lnst/Controller/NetTestController.py | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/lnst/Controller/NetTestController.py b/lnst/Controller/NetTestController.py
index ad5a2df..ff40033 100644
--- a/lnst/Controller/NetTestController.py
+++ b/lnst/Controller/NetTestController.py
@@ -58,6 +58,7 @@ class NetTestController:
self._recipe_path = recipe_path
self._msg_dispatcher = MessageDispatcher(log_ctl)
self._reduce_sync = reduce_sync
+ self._parser = RecipeParser(recipe_path)
self.remove_saved_machine_config()
@@ -72,10 +73,9 @@ class NetTestController:
mac_pool_range = lnst_config.get_option('environment',
'mac_pool_range')
self._mac_pool = MacPool(mac_pool_range[0], mac_pool_range[1])
- parser = RecipeParser(recipe_path)
- parser.set_machines(self._machines)
- parser.set_aliases(defined_aliases, overriden_aliases)
- self._recipe = parser.parse()
+ self._parser.set_machines(self._machines)
+ self._parser.set_aliases(defined_aliases, overriden_aliases)
+ self._recipe = self._parser.parse()
modules_dirs = lnst_config.get_option('environment',
'module_dirs')
tools_dirs = lnst_config.get_option('environment', 'tool_dirs')
@@ -772,6 +772,10 @@ class NetTestController:
"hash": pkg_hash}
return packages
+ def _get_alias(self, alias):
+ templates = self._parser._template_proc
+ return templates._find_definition(alias)
+
class MessageDispatcher(ConnectionHandler):
def __init__(self, log_ctl):
super(MessageDispatcher, self).__init__()
--
1.8.1.4
ControllerAPI now provides get_alias() method to get alias defined in the
recipe XML.
Example:
my_value = ctl.get_alias("my_alias")
The code above gets the value of the alias named "my_alias" previously defined
in <lnstrecipe> namespace, e.g:
<lnstrecipe>
<define>
<alias name="my_alias" value="my_value"/>
</define>
...
</lnstrecipe>
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
lnst/Controller/Task.py | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/lnst/Controller/Task.py b/lnst/Controller/Task.py
index b028231..10c9f02 100644
--- a/lnst/Controller/Task.py
+++ b/lnst/Controller/Task.py
@@ -78,6 +78,18 @@ class ControllerAPI(object):
cmd = {"type": "ctl_wait", "seconds":
int(seconds)}
return self._ctl._run_command(cmd)
+ def get_alias(self, alias):
+ """
+ Get the value of user defined alias.
+
+ :param alias: name of user defined alias
+ :type alias: string
+
+ :return: value of a user defined alias
+ :rtype: string
+ """
+ return self._ctl._get_alias(alias)
+
class HostAPI(object):
""" An API class representing a host machine. """
--
1.8.1.4