def test_periodic_reaper(self, mock_reap):
        """ Test that the we invoke the image reaper periodically """
        image_manager = ImageManager(self.vim_client, self.ds_manager)
        image_manager.monitor_for_cleanup(reap_interval=0.1)

        self.assertFalse(image_manager._image_reaper is None)

        retry = 0
        while mock_reap.call_count < 2 and retry < 10:
            time.sleep(0.1)
            retry += 1
        image_manager.cleanup()
        assert_that(mock_reap.call_count, greater_than(1))
        assert_that(retry, is_not(10), "reaper cleanup not called repeatedly")
Example #2
0
class Hypervisor(object):
    def __init__(self, agent_config):
        self.logger = logging.getLogger(__name__)

        # If VimClient's housekeeping thread failed to update its own cache,
        # call errback to commit suicide. Watchdog will bring up the agent
        # again.
        self.host_client = Hypervisor.create_host_client(errback=lambda: suicide())
        self.host_client.connect_local()
        atexit.register(lambda client: client.disconnect(), self.host_client)

        self.datastore_manager = DatastoreManager(
            self, agent_config.datastores, agent_config.image_datastores)
        # datastore manager needs to update the cache when there is a change.
        self.host_client.add_update_listener(self.datastore_manager)
        self.vm_manager = VmManager(self.host_client, self.datastore_manager)
        self.disk_manager = DiskManager(self.host_client, self.datastore_manager)
        self.image_manager = ImageManager(self.host_client, self.datastore_manager)
        self.network_manager = NetworkManager(self.host_client)
        self.system = System(self.host_client)

        options = PlacementOption(agent_config.memory_overcommit,
                                  agent_config.cpu_overcommit,
                                  agent_config.image_datastores)
        self.placement_manager = PlacementManager(self, options)

        self.image_monitor = ImageMonitor(self.datastore_manager,
                                          self.image_manager,
                                          self.vm_manager)
        self.image_manager.monitor_for_cleanup()
        self.image_transferer = NfcImageTransferer(self.host_client)
        atexit.register(self.image_manager.cleanup)

    @staticmethod
    def create_host_client(auto_sync=True, errback=None):
        try:
            # check whether attache is installed. If not, find_module will throw ImportError.
            from host.hypervisor.esx.attache_client import AttacheClient
            return AttacheClient(auto_sync, errback)
        except ImportError:
            from host.hypervisor.esx.vim_client import VimClient
            return VimClient(auto_sync, errback)

    def check_image(self, image_id, datastore_id):
        return self.image_manager.check_image(
            image_id, self.datastore_manager.datastore_name(datastore_id)
        )

    def get_resources(self):
        return self.vm_manager.get_resources()

    def get_vm_resource(self, vm_id):
        vm = self.vm_manager.get_resource(vm_id)
        resource = Resource(vm=vm)
        return resource

    def acquire_vim_ticket(self):
        return self.host_client.get_vim_ticket()

    def add_update_listener(self, listener):
        self.host_client.add_update_listener(listener)

    def remove_update_listener(self, listener):
        self.host_client.remove_update_listener(listener)

    def transfer_image(self, source_image_id, source_datastore,
                       destination_image_id, destination_datastore,
                       host, port):
        return self.image_transferer.send_image_to_host(
            source_image_id, source_datastore,
            destination_image_id, destination_datastore, host, port)

    @property
    def memory_overcommit(self):
        return self.placement_manager.memory_overcommit

    def set_memory_overcommit(self, value):
        self.placement_manager.memory_overcommit = value

    @property
    def cpu_overcommit(self):
        return self.placement_manager.cpu_overcommit

    def set_cpu_overcommit(self, value):
        self.placement_manager.cpu_overcommit = value
Example #3
0
class Hypervisor(object):
    def __init__(self, agent_config):
        self.logger = logging.getLogger(__name__)

        # If VimClient's housekeeping thread failed to update its own cache,
        # call errback to commit suicide. Watchdog will bring up the agent
        # again.
        self.host_client = Hypervisor.create_host_client(
            errback=lambda: suicide())
        self.host_client.connect_local()
        atexit.register(lambda client: client.disconnect(), self.host_client)

        self.datastore_manager = DatastoreManager(
            self, agent_config.datastores, agent_config.image_datastores)
        # datastore manager needs to update the cache when there is a change.
        self.host_client.add_update_listener(self.datastore_manager)
        self.vm_manager = VmManager(self.host_client, self.datastore_manager)
        self.disk_manager = DiskManager(self.host_client,
                                        self.datastore_manager)
        self.image_manager = ImageManager(self.host_client,
                                          self.datastore_manager)
        self.network_manager = NetworkManager(self.host_client)
        self.system = System(self.host_client)

        options = PlacementOption(agent_config.memory_overcommit,
                                  agent_config.cpu_overcommit,
                                  agent_config.image_datastores)
        self.placement_manager = PlacementManager(self, options)

        self.image_monitor = ImageMonitor(self.datastore_manager,
                                          self.image_manager, self.vm_manager)
        self.image_manager.monitor_for_cleanup()
        self.image_transferer = NfcImageTransferer(self.host_client)
        atexit.register(self.image_manager.cleanup)

    @staticmethod
    def create_host_client(auto_sync=True, errback=None):
        try:
            # check whether attache is installed. If not, find_module will throw ImportError.
            from host.hypervisor.esx.attache_client import AttacheClient
            return AttacheClient(auto_sync, errback)
        except ImportError:
            from host.hypervisor.esx.vim_client import VimClient
            return VimClient(auto_sync, errback)

    def check_image(self, image_id, datastore_id):
        return self.image_manager.check_image(
            image_id, self.datastore_manager.datastore_name(datastore_id))

    def get_resources(self):
        return self.vm_manager.get_resources()

    def get_vm_resource(self, vm_id):
        vm = self.vm_manager.get_resource(vm_id)
        resource = Resource(vm=vm)
        return resource

    def acquire_vim_ticket(self):
        return self.host_client.get_vim_ticket()

    def add_update_listener(self, listener):
        self.host_client.add_update_listener(listener)

    def remove_update_listener(self, listener):
        self.host_client.remove_update_listener(listener)

    def transfer_image(self, source_image_id, source_datastore,
                       destination_image_id, destination_datastore, host,
                       port):
        return self.image_transferer.send_image_to_host(
            source_image_id, source_datastore, destination_image_id,
            destination_datastore, host, port)

    @property
    def memory_overcommit(self):
        return self.placement_manager.memory_overcommit

    def set_memory_overcommit(self, value):
        self.placement_manager.memory_overcommit = value

    @property
    def cpu_overcommit(self):
        return self.placement_manager.cpu_overcommit

    def set_cpu_overcommit(self, value):
        self.placement_manager.cpu_overcommit = value