2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 from nepi.core import testbed_impl
6 from nepi.util.constants import TIME_NOW
11 class TestbedController(testbed_impl.TestbedController):
12 from nepi.util.tunchannel_impl import TunChannel
14 class HostLock(object):
15 # This class is used as a lock to prevent concurrency issues with more
16 # than one instance of netns running in the same machine. Both in
17 # different processes or different threads.
19 processcond = threading.Condition()
21 def __init__(self, lockfile):
22 processcond = self.__class__.processcond
27 while self.__class__.taken:
29 self.__class__.taken = True
33 self.lockfile = lockfile
34 fcntl.flock(self.lockfile, fcntl.LOCK_EX)
37 processcond = self.__class__.processcond
41 assert self.__class__.taken, "HostLock unlocked without being locked!"
43 fcntl.flock(self.lockfile, fcntl.LOCK_UN)
46 self.__class__.taken = False
51 def __init__(self, testbed_version):
52 super(TestbedController, self).__init__(TESTBED_ID, testbed_version)
54 self._home_directory = None
56 self._netns_lock = open("/tmp/nepi-netns-lock","a")
59 return self.HostLock(self._netns_lock)
62 def home_directory(self):
63 return self._home_directory
70 self._home_directory = self._attributes.\
71 get_attribute_value("homeDirectory")
73 home = os.path.normpath(self.home_directory)
74 if not os.path.exists(home):
75 os.makedirs(home, 0755)
77 self._netns = self._load_netns_module()
78 super(TestbedController, self).do_setup()
82 super(TestbedController, self).do_create()
84 def set(self, guid, name, value, time = TIME_NOW):
85 super(TestbedController, self).set(guid, name, value, time)
86 # TODO: take on account schedule time for the task
87 factory_id = self._create[guid]
88 factory = self._factories[factory_id]
89 if factory.box_attributes.is_attribute_design_only(name) or \
90 factory.box_attributes.is_attribute_invisible(name):
92 element = self._elements.get(guid)
94 setattr(element, name, value)
96 def get(self, guid, name, time = TIME_NOW):
97 value = super(TestbedController, self).get(guid, name, time)
98 # TODO: take on account schedule time for the task
99 factory_id = self._create[guid]
100 factory = self._factories[factory_id]
101 if factory.box_attributes.is_attribute_design_only(name) or \
102 factory.box_attributes.is_attribute_invisible(name):
104 element = self._elements.get(guid)
106 return getattr(element, name)
107 except KeyError, AttributeError:
110 def action(self, time, guid, action):
111 raise NotImplementedError
114 for guid, traces in self._traces.iteritems():
115 for trace_id, (trace, filename) in traces.iteritems():
116 if hasattr(trace, "close"):
118 for guid, element in self._elements.iteritems():
119 if isinstance(element, self.TunChannel):
122 factory_id = self._create[guid]
123 if factory_id == "Node":
125 self._elements.clear()
127 def trace_filepath(self, guid, trace_id, filename = None):
129 (trace, filename) = self._traces[guid][trace_id]
130 return os.path.join(self.home_directory, filename)
132 def follow_trace(self, guid, trace_id, trace, filename):
133 if not guid in self._traces:
134 self._traces[guid] = dict()
135 self._traces[guid][trace_id] = (trace, filename)
137 def _load_netns_module(self):
138 # TODO: Do something with the configuration!!!
141 netns_mod = sys.modules["netns"]
143 enable_debug = self._attributes.get_attribute_value("enableDebug")
145 netns_mod.environ.set_log_level(netns_mod.environ.LOG_DEBUG)