From: Ondrej Lichtner <olichtne(a)redhat.com>
This is a universal class that can be used as a proxy for any
dynamically instantiated object on the slave (that was received from the
Controller).
The commit also adds the init_remote_class method to
lnst.Controller.Machine and init_class to lnst.Controller.Host to expose
this functionality to the tester.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Controller/Host.py | 5 +++++
lnst/Controller/Machine.py | 8 +++++++
lnst/Controller/SlaveObject.py | 41 ++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+)
create mode 100644 lnst/Controller/SlaveObject.py
diff --git a/lnst/Controller/Host.py b/lnst/Controller/Host.py
index 9267081..f455fa9 100644
--- a/lnst/Controller/Host.py
+++ b/lnst/Controller/Host.py
@@ -76,3 +76,8 @@ class Host(Namespace):
return False
else:
return True
+
+ def init_class(self, cls, *args, **kwargs):
+ self._machine.send_class(cls)
+
+ return self._machine.init_remote_class(cls, *args, **kwargs)
diff --git a/lnst/Controller/Machine.py b/lnst/Controller/Machine.py
index e1204e1..2d58e67 100644
--- a/lnst/Controller/Machine.py
+++ b/lnst/Controller/Machine.py
@@ -21,6 +21,7 @@ from lnst.Common.Version import lnst_version
from lnst.Controller.Common import ControllerError
from lnst.Controller.CtlSecSocket import CtlSecSocket
from lnst.Controller.RecipeResults import JobStartResult, JobFinishResult
+from lnst.Controller.SlaveObject import SlaveObject
from lnst.Devices import device_classes
from lnst.Devices.Device import Device
from lnst.Devices.RemoteDevice import RemoteDevice
@@ -506,6 +507,13 @@ class Machine(object):
"file", remote_path, res_name)
return digest
+ def init_remote_class(self, cls, *args, **kwargs):
+ module_name = cls.__module__
+ cls_name = cls.__name__
+ obj_ref = self.rpc_call("init_cls", cls_name, module_name, args,
kwargs)
+
+ return SlaveObject(self, cls, obj_ref)
+
# def enable_nm(self):
# return self._rpc_call("enable_nm")
diff --git a/lnst/Controller/SlaveObject.py b/lnst/Controller/SlaveObject.py
new file mode 100644
index 0000000..7576c04
--- /dev/null
+++ b/lnst/Controller/SlaveObject.py
@@ -0,0 +1,41 @@
+"""
+TODO
+
+Copyright 2018 Red Hat, Inc.
+Licensed under the GNU General Public License, version 2 as
+published by the Free Software Foundation; see COPYING for details.
+"""
+
+__author__ = """
+olichtne(a)redhat.com (Ondrej Lichtner)
+"""
+
+class SlaveObject(object):
+ def __init__(self, machine, cls, obj_ref):
+ self._inited = False
+ self.__cls = cls
+ self.__obj_ref = obj_ref
+ self.__machine = machine
+
+ self._inited = True
+
+ def __getattr__(self, name):
+ if name == "_inited":
+ return super(SlaveObject, self).__getattribute__(name)
+
+ attr = getattr(self.__cls, name)
+
+ if callable(attr):
+ def obj_method(*args, **kwargs):
+ return self.__machine.rpc_call("obj_method", self.__obj_ref,
+ name, args, kwargs)
+ return obj_method
+ else:
+ return self.__machine.rpc_call("obj_getattr", self.__obj_ref,
name)
+
+ def __setattr__(self, name, value):
+ if name == "_inited" or not self._inited:
+ return super(SlaveObject, self).__setattr__(name, value)
+
+ return self._machine.rpc_call("obj_setattr", self.__obj_ref, name,
+ value)
--
2.17.0