Example #1
0
    def to_thrift(self):
        thrift_host_config = ThriftHostConfig(self.agent_id)
        thrift_host_config.datastores = [ThriftDatastore(ds)
                                         for ds in self.datastores]
        if self.hypervisor:
            thrift_host_config.hypervisor = self.hypervisor
        if self.vm_network:
            thrift_host_config.vm_network = self.vm_network

        return thrift_host_config
Example #2
0
    def to_thrift(self):
        thrift_host_config = ThriftHostConfig(self.agent_id)
        thrift_host_config.datastores = [
            ThriftDatastore(ds) for ds in self.datastores
        ]
        if self.hypervisor:
            thrift_host_config.hypervisor = self.hypervisor
        if self.vm_network:
            thrift_host_config.vm_network = self.vm_network

        return thrift_host_config
Example #3
0
def get_hosts_from_zk(zk_client):
    """This function will read all registered hosts in /hosts and return
    a list of Host objects.
    """
    try:
        hosts_list = []
        if not zk_client.exists(HOSTS_PREFIX):
            log.debug("%s doesn't exist" % (HOSTS_PREFIX, ))
            return []

        hosts = zk_client.get_children(HOSTS_PREFIX)

        for host in hosts:
            try:
                path = "%s/%s" % (HOSTS_PREFIX, host)
                (value, stat) = zk_client.get(path)
                config = HostConfig()
                deserialize(config, value)
                _host = Host(config.agent_id, config.address.host,
                             config.address.port)
                hosts_list.append(_host)
            except NoNodeError:
                log.debug("host %s not found" % (host, ))
                continue
        return hosts_list
    except Exception, e:
        log.exception(e)
Example #4
0
    def get_register_host_request(self, port=8080):
        """
        Generates a random register host request
            which has same datastore and same availability zone
        """
        host_id = str(uuid.uuid4())
        if not hasattr(self, "image_datastore"):
            self.image_datastore = str(uuid.uuid4())

        datastores = [Datastore(self.image_datastore)]
        networks = [Network("nw1", [NetworkType.VM])]
        host_config = HostConfig(agent_id=host_id,
                                 datastores=datastores,
                                 address=ServerAddress("127.0.0.1", port=port),
                                 networks=networks)
        host_config.availability_zone = "foo"
        host_config.image_datastore_ids = set(self.image_datastore)
        return RegisterHostRequest(host_id, host_config)
def get_register_host_request(host, port, agent_id, networks, datastores,
                              image_datastore, availability_zone,
                              management_only=False):
    host_config = HostConfig(agent_id=agent_id, datastores=datastores,
                             address=ServerAddress(host, port=port),
                             networks=networks,
                             availability_zone=availability_zone,
                             image_datastore_ids=set([image_datastore]),
                             management_only=management_only)
    return RegisterHostRequest(agent_id, host_config)
    def setUp(self):
        self.hostname = "localhost"
        self.host_port = 1234
        self.availability_zone_id = "test"
        self.host_addr = ServerAddress(self.hostname, self.host_port)
        self.chairman_list = []
        self.agent_id = "foo"
        self.host_config = HostConfig(self.agent_id, self.availability_zone_id,
                                      [Datastore("bar")], self.host_addr,
                                      [Network("nw1")])
        self.registrant = ChairmanRegistrant(self.chairman_list)
        host_handler = MagicMock()
        host_handler.get_host_config_no_logging.return_value = \
            GetConfigResponse(hostConfig=self.host_config)
        common.services.register(Host.Iface, host_handler)
        self.request = RegisterHostRequest("foo", self.host_config)

        self.state_file = tempfile.mktemp()
        common.services.register(ServiceName.MODE,
                                 Mode(State(self.state_file)))
Example #7
0
    def test_register_host(self):
        """ Register against the chairman and verify it is persisted in zk """
        host_prefix = "/hosts"  # Const in java impl.

        # Register two hosts with the chairman.
        host = []
        h = self.get_register_host_request()
        host.append(h.config)

        retries = 0
        while (retries < 10):
            rc = self.chairman_client.register_host(h)

            if (rc.result == RegisterHostResultCode.NOT_IN_MAJORITY):
                # Possible because the chairman is yet to connect to zk
                retries += 1
                time.sleep(1)
                continue
            break

        self.assertEqual(rc.result, RegisterHostResultCode.OK)

        h = self.get_register_host_request()
        host.append(h.config)
        rc = self.chairman_client.register_host(h)
        self.assertEqual(rc.result, RegisterHostResultCode.OK)

        # Validate the state persisted in zk.
        client = self._get_nonchroot_client()
        client.start()
        self.assertTrue(client.exists(host_prefix))
        read_hosts = client.get_children(host_prefix)
        for h in read_hosts:
            path = host_prefix + "/" + h
            (value, stat) = client.get(path)
            host_config = HostConfig()
            deserialize(host_config, value)
            self.assertTrue(host_config in host)
        client.stop()