예제 #1
0
    def __init__(self, agent_config):
        self._logger = logging.getLogger(__name__)
        self._config = agent_config

        if self._config.hypervisor == "esx":
            from esx.hypervisor import EsxHypervisor
            # This will throw an error if it can't connect to the local vim.
            self.hypervisor = EsxHypervisor(agent_config)
        elif self._config.hypervisor == "fake":
            from fake.hypervisor import FakeHypervisor
            self.hypervisor = FakeHypervisor(agent_config)
        else:
            raise ValueError("Invalid hypervisor")
        """
        The creation of the Hypervisors above translates datastore names
        into datastore ids. Methods that access datastores through this
        class should use datastore ids.
        """

        self.datastore_manager = self.hypervisor.datastore_manager
        self.disk_manager = self.hypervisor.disk_manager
        self.image_manager = self.hypervisor.image_manager
        self.vm_manager = self.hypervisor.vm_manager
        self.network_manager = self.hypervisor.network_manager
        self.system = self.hypervisor.system

        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)
    def build(self):
        hypervisor = MagicMock()

        hypervisor.datastore_manager = MagicMock()
        hypervisor.datastore_manager.vm_datastores.return_value = \
            [ds for ds in self.ds_map.keys() if ds !=
             self.image_ds]
        hypervisor.datastore_manager.image_datastore.return_value = \
            self.image_ds
        hypervisor.datastore_manager.get_datastore_ids.return_value = \
            self.ds_map.keys()
        hypervisor.datastore_manager.datastore_info = self.datastore_info
        hypervisor.datastore_manager.normalize.side_effect = self.normalize
        hypervisor.datastore_manager.get_datastores.return_value = \
            [Datastore(id=ds_id, tags=self.ds_map[ds_id][1])
             for ds_id in self.ds_map.keys()]

        hypervisor.network_manager.get_vm_networks.return_value = \
            self.vm_networks

        hypervisor.system = MagicMock()
        hypervisor.system.total_vmusable_memory_mb.return_value = \
            self.total_mem
        hypervisor.system.host_version.return_value = \
            self.host_version
        hypervisor.system.num_physical_cpus.return_value = 1

        hypervisor.image_manager = MagicMock()
        hypervisor.image_manager.get_image_id_from_disks.return_value = \
            self.image_id
        hypervisor.image_manager.check_image = self.check_image
        hypervisor.image_manager.image_size.return_value = self.image_size

        hypervisor.vm_manager = MagicMock()
        hypervisor.vm_manager.get_used_memory_mb.return_value = 0

        placement_option = PlacementOption(self.mem_overcommit,
                                           self.cpu_overcommit,
                                           self.image_datastores)
        return PlacementManager(hypervisor, placement_option)