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_metadata(name):
91 element = self._elements.get(guid)
93 setattr(element, name, value)
95 def get(self, guid, name, time = TIME_NOW):
96 value = super(TestbedController, self).get(guid, name, time)
97 # TODO: take on account schedule time for the task
98 factory_id = self._create[guid]
99 factory = self._factories[factory_id]
100 if factory.box_attributes.is_attribute_metadata(name):
102 element = self._elements.get(guid)
104 return getattr(element, name)
105 except (KeyError, AttributeError):
108 def action(self, time, guid, action):
109 raise NotImplementedError
112 for guid, traces in self._traces.iteritems():
113 for trace_id, (trace, filename) in traces.iteritems():
114 if hasattr(trace, "close"):
116 for guid, element in self._elements.iteritems():
117 if isinstance(element, self.TunChannel):
120 factory_id = self._create[guid]
121 if factory_id == "Node":
123 self._elements.clear()
125 def trace_filepath(self, guid, trace_id, filename = None):
127 (trace, filename) = self._traces[guid][trace_id]
128 return os.path.join(self.home_directory, filename)
130 def follow_trace(self, guid, trace_id, trace, filename):
131 if not guid in self._traces:
132 self._traces[guid] = dict()
133 self._traces[guid][trace_id] = (trace, filename)
135 def _load_netns_module(self):
136 # TODO: Do something with the configuration!!!
139 netns_mod = sys.modules["netns"]
141 enable_debug = self._attributes.get_attribute_value("enableDebug")
143 netns_mod.environ.set_log_level(netns_mod.environ.LOG_DEBUG)