From: Ondrej Lichtner <olichtne(a)redhat.com>
Splitting off the Recipe.ctl tester API away from the
lnst.Controller.Controller class into it's own dedicated RecipeControl
class. This should be more readable and maintainable hopefully and avoid
accidentaly exposing something we didn't want to.
Currently the API provides two wait* methods, access to the Hosts
iterator and the ability to initialized a connection to a slave during
test execution. This is a new feature.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Controller/Controller.py | 14 ++-----
lnst/Controller/Recipe.py | 2 +-
lnst/Controller/RecipeControl.py | 64 ++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 12 deletions(-)
create mode 100644 lnst/Controller/RecipeControl.py
diff --git a/lnst/Controller/Controller.py b/lnst/Controller/Controller.py
index cdce799..e087ef3 100644
--- a/lnst/Controller/Controller.py
+++ b/lnst/Controller/Controller.py
@@ -30,6 +30,7 @@ from lnst.Controller.MachineMapper import MachineMapper
from lnst.Controller.MachineMapper import format_match_description
from lnst.Controller.Host import Hosts, Host
from lnst.Controller.Recipe import BaseRecipe, RecipeRun
+from lnst.Controller.RecipeControl import RecipeControl
class Controller(object):
"""The LNST Controller class
@@ -106,7 +107,8 @@ class Controller(object):
if not isinstance(recipe, BaseRecipe):
raise ControllerError("recipe argument must be a BaseRecipe
instance.")
- recipe._set_ctl(self)
+ recipe_ctl = RecipeControl(self, recipe)
+ recipe._set_ctl(recipe_ctl)
req = recipe.req
@@ -132,16 +134,6 @@ class Controller(object):
finally:
self._cleanup_slaves()
- def wait(self, sec):
- finish_time = time.time() + sec
- logging.info("Suspending recipe execution for {} seconds, "
- "messages from slaves will still be processed.".
- format(sec))
-
- def condition():
- return time.time() > finish_time
-
- self._msg_dispatcher.wait_for_condition(condition)
def _map_match(self, match, requested, recipe):
self._machines = {}
diff --git a/lnst/Controller/Recipe.py b/lnst/Controller/Recipe.py
index 0f3a54e..ddd8016 100644
--- a/lnst/Controller/Recipe.py
+++ b/lnst/Controller/Recipe.py
@@ -118,7 +118,7 @@ class BaseRecipe(object):
def matched(self):
if self.ctl is None:
return None
- return self.ctl._hosts
+ return self.ctl.hosts
def test(self):
"""Method to be implemented by the Tester"""
diff --git a/lnst/Controller/RecipeControl.py b/lnst/Controller/RecipeControl.py
new file mode 100644
index 0000000..b92b978
--- /dev/null
+++ b/lnst/Controller/RecipeControl.py
@@ -0,0 +1,64 @@
+import time
+import socket
+import logging
+from lnst.Common.Logs import log_exc_traceback
+from lnst.Common.SecureSocket import SecSocketException
+from lnst.Controller.Machine import Machine
+from lnst.Controller.Host import Host
+
+class RecipeControl(object):
+ def __init__(self, controller, recipe):
+ self._controller = controller
+ self._recipe = recipe
+
+ @property
+ def hosts(self):
+ return self._controller._hosts
+
+ def wait(self, sec):
+ finish_time = time.time() + sec
+ logging.info("Suspending recipe execution for {} seconds, "
+ "messages from slaves will still be processed.".
+ format(sec))
+
+ def condition():
+ return time.time() > finish_time
+
+ msg_dispatcher = self._controller._msg_dispatcher
+ msg_dispatcher.wait_for_condition(condition)
+
+ def wait_for_condition(self, condition):
+ #TODO add descriptions to conditions?
+ logging.info("Suspending recipe execution until condition is true")
+
+ msg_dispatcher = self._controller._msg_dispatcher
+ msg_dispatcher.wait_for_condition(condition)
+
+ def connect_host(self, hostname, timeout=60, port=None, machine_id=None,
+ security=None):
+ ctl_config = self._controller._config
+ msg_dispatcher = self._controller._msg_dispatcher
+
+ if security is None:
+ security = {"auth_type": "none"}
+
+ if machine_id is None:
+ machine_id = hostname
+
+ m = Machine(machine_id, hostname, msg_dispatcher,
+ ctl_config, None, port, security)
+
+ def condition():
+ try:
+ m.init_connection(timeout=1)
+ return True
+ except:
+ log_exc_traceback()
+ return False
+
+ msg_dispatcher.wait_for_condition(condition, timeout)
+
+ self._controller._prepare_machine(m)
+ m.start_recipe(self._recipe)
+ host = Host(m)
+ return host
--
2.19.0