def __prepare(self): self.log.debug("initiating preparation") # create the spool directory self.__spool = self.__createSpool() # create the instance self.__inst = InstanceManager.getInstance().newInstance(self.__spool) self.__inst.task = self from xbe.xbed.task_activities import SetUpActivity, AcquireResourceActivity from xbe.util.activity import ComplexActivity, ThreadedActivity mac_pool = XBEDaemon.getInstance().macAddresses jail_package = XBEDaemon.getInstance().opts.jail_package mac_acquirer = ThreadedActivity( AcquireResourceActivity(mac_pool)) setup_activity = ThreadedActivity( SetUpActivity(self.__spool, jail_package), (self.__jsdl_doc, self.__activity_logs["stage-in"])) complex_activity = ComplexActivity([setup_activity, mac_acquirer]) self.mgr.registerActivity(self, complex_activity, on_finish=self._cb_stage_in_succeed, on_fail=self._eb_stage_in_failed, on_abort=None)
def do_InstanceAlive(self, xml, *a, **kw): msg = message.MessageBuilder.from_xml(xml.getroottree()) inst_id = msg.inst_id() inst = InstanceManager.getInstance().lookupByUUID(msg.inst_id()) if inst is not None: inst.update_alive() else: log.warn("got alive message from suspicious source")
def do_life_sign(self, life_sign): self.log.debug("instance %s is alive", self._instanceID) inst = InstanceManager.getInstance().lookupByUUID(self._instanceID) if inst is not None: inst.life_sign(self, life_sign) else: self.log.warn("unknown instance: %s", self._instanceID) self.requestShutdown(None, None)
def do_InstanceAvailable(self, xml, *args, **kw): inst_avail = message.MessageBuilder.from_xml(xml.getroottree()) inst_id = inst_avail.inst_id() inst = InstanceManager.getInstance().lookupByUUID(inst_id) if inst is not None: inst.available(self) else: log.warn("got available message from suspicious source")
def do_ExecutionFailed(self, xml, *a, **kw): msg = message.MessageBuilder.from_xml(xml.getroottree()) inst_id = msg.inst_id() inst = InstanceManager.getInstance().lookupByUUID(inst_id) if inst is not None: log.debug("execution on instance %s failed: %s" % (inst_id, msg.reason())) try: inst.task.signalExecutionFailed(Exception("execution failed", msg)) except Exception, e: log.warn("could not signal execution failure: %s" % e)
def do_InstanceShuttingDown(self, xml, *a, **kw): msg = message.MessageBuilder.from_xml(xml.getroottree()) inst_id = msg.inst_id() inst = InstanceManager.getInstance().lookupByUUID(inst_id) if inst is not None: log.info("instance is shutting down...") log.debug("execution on instance %s finished" % inst_id) try: inst.task.signalExecutionEnd(0) except Exception, e: log.warn("could not signal execution end: %s" % e)
def do_ExecutionFinished(self, xml, *args, **kw): msg = message.MessageBuilder.from_xml(xml.getroottree()) inst_id = msg.inst_id() exitcode = msg.exitcode() inst = InstanceManager.getInstance().lookupByUUID(inst_id) if inst is not None: log.debug("execution on instance %s finished" % inst_id) try: inst.task.signalExecutionEnd(exitcode) except Exception, e: log.warn("could not signal execution end: %s" % e)
def do_failed(self, failed): self.log.info("execution on instance has failed: %d", failed.reason) # send a failed ack to the instance ack = xbemsg.XbeMessage() ack.header.conversation_id = self._conv_id ack.failed_ack.task = failed.task inst = InstanceManager.getInstance().lookupByUUID(self._instanceID) if inst is not None: inst.task.signalExecutionFailed(failed.reason) return ack
def setUp(self): self.mgr = InstanceManager()
class TestInstanceManager(unittest.TestCase): def setUp(self): self.mgr = InstanceManager() def test_spool_directory(self): """tests the creation of a new instance. Does *not* check the creation of a backend instance. """ log.debug("testing creation of spool directory") inst = self.mgr.newInstance() # check if the spool directory has been created self.assertTrue(os.access(inst.getSpool(), os.F_OK)) inst.cleanUp() def test_lookup_instance(self): """tests the registration of the new instance.""" log.debug("testing instance lookup") # check that the instance has been registered inst = self.mgr.newInstance() self.assertEqual(inst, self.mgr.lookupByUUID(inst.uuid())) inst.cleanUp() def test_remove_instance(self): """tests the removal of an instance.""" log.debug("testing instance removal") inst = self.mgr.newInstance() self.assertEqual(inst, self.mgr.lookupByUUID(inst.uuid())) self.mgr.removeInstance(inst) self.assertEqual(None, self.mgr.lookupByUUID(inst.uuid())) inst.cleanUp() def test_successful_instance_creation(self): """tests the creation of a backend instance. some files are required (will be staged in): files = { "image" : "/srv/xen-images/domains/ttylinux/disk.img", "kernel": "/srv/xen-images/domains/ttylinux/kernel", "initrd": "/srv/xen-images/domains/ttylinux/initrd" } """ log.debug("testing instance creation (tiny)") # create new instance inst = self.mgr.newInstance() src_files = { "root" : "/srv/xen-images/domains/ttylinux/disk.img", "kernel": "/srv/xen-images/domains/ttylinux/kernel", "initrd": "/srv/xen-images/domains/ttylinux/initrd" } for name, path in src_files.iteritems(): self.assertTrue(os.access(path, os.F_OK)) inst.addFile("file://%s" % path, name) inst.start() state = inst.getBackendState() from xbe.xbed.backend import status self.assertTrue(state in (status.BE_INSTANCE_RUNNING, status.BE_INSTANCE_BLOCKED)) # shut the instance down inst.stop() self.assertTrue(inst.getBackendState() in (status.BE_INSTANCE_NOSTATE, status.BE_INSTANCE_SHUTOFF)) inst.cleanUp() def test_successful_instance_creation_2(self): """tests the creation of a backend instance. with networking some files are required (will be staged in): files = { "image" : "/srv/xen-images/domains/xenhobel-1/disk.img", "kernel": "/srv/xen-images/domains/xenhobel-1/kernel", "initrd": "/srv/xen-images/domains/xenhobel-1/initrd" } """ log.debug("testing instance creation (big)") # create new instance inst = self.mgr.newInstance() src_files = { "image" : "file:///srv/xen-images/domains/xenhobel-1/disk.img", "kernel": "file:///srv/xen-images/domains/xenhobel-1/kernel", "initrd": "file:///srv/xen-images/domains/xenhobel-1/initrd" } inst.addFiles(src_files) inst.config.setKernel(inst.getFile("kernel")) inst.config.setInitrd(inst.getFile("initrd")) # create swap image disk.makeSwap(inst.getFile("swap"), 128) inst.config.addDisk(inst.getFile("image"), "sda1") inst.config.addDisk(inst.getFile("swap"), "sda2") inst.config.setMac("00:16:3e:00:00:01") inst.start() state = inst.getBackendState() from xbe.xbed.backend import status self.assertTrue(state in (status.BE_INSTANCE_RUNNING, status.BE_INSTANCE_BLOCKED)) # shut the instance down inst.stop() self.assertTrue(inst.getBackendState() in (status.BE_INSTANCE_NOSTATE, status.BE_INSTANCE_SHUTOFF)) inst.cleanUp()
def __delete_instance(self, *a, **kw): InstanceManager.getInstance().removeInstance(self.__inst) del self.__inst.task del self.__inst
def do_status(self, status): self.log.info("received status information: %s", status) inst = InstanceManager.getInstance().lookupByUUID(self._instanceID) if inst is not None: inst.task.signalStatus(status)