Example #1
0
def _configure(reactor, cluster, configuration):
    """
    Configure the cluster with the given deployment configuration.

    :param reactor: The reactor to use.
    :param flocker.provision._common.Cluster cluster: The target cluster.
    :param dict configuration: The deployment configuration.
    :return Deferred: Deferred that fires when the configuration is pushed
                      to the cluster's control agent.
    """
    base_url = b"https://{}:{}/v1".format(cluster.control_node.address,
                                          REST_API_PORT)
    certificates_path = cluster.certificates_path
    cluster_cert = certificates_path.child(b"cluster.crt")
    user_cert = certificates_path.child(b"user.crt")
    user_key = certificates_path.child(b"user.key")
    body = dumps(configuration)
    treq_client = treq_with_authentication(reactor, cluster_cert, user_cert,
                                           user_key)

    def got_all_nodes():
        d = treq_client.get(base_url + b"/state/nodes", persistent=False)
        d.addCallback(check_and_decode_json, OK)
        d.addCallback(lambda nodes: len(nodes) >= len(cluster.agent_nodes))
        d.addErrback(write_failure, logger=None)
        return d

    got_nodes = loop_until(reactor, got_all_nodes, repeat(1, 300))

    def do_configure(_):
        posted = treq_client.post(
            base_url + b"/configuration/_compose",
            data=body,
            headers={b"content-type": b"application/json"},
            persistent=False)

        def got_response(response):
            if response.code != OK:
                d = json_content(response)

                def got_error(error):
                    if isinstance(error, dict):
                        error = error[u"description"] + u"\n"
                    else:
                        error = u"Unknown error: " + unicode(error) + "\n"
                    raise ResponseError(response.code, error)

                d.addCallback(got_error)
                return d

        posted.addCallback(got_response)
        return posted

    configured = got_nodes.addCallback(do_configure)
    return configured
    def _configure(self, configuration):
        """
        Configure the cluster with the given deployment configuration.

        :param dict configuration: dictionary with the configuration
            to deploy.
        :return Deferred: Deferred that fires when the configuration is pushed
                          to the cluster's control agent.
        """
        Message.log(action="Deploying new config")
        base_url = b"https://{}:{}/v1".format(
            self.control_node_address, REST_API_PORT
        )
        cluster_cert = self.cluster_cert
        user_cert = self.user_cert
        user_key = self.user_key
        body = dumps(configuration)
        treq_client = treq_with_authentication(
            self.reactor, cluster_cert, user_cert, user_key)

        def do_configure():
            posted = treq_client.post(
                base_url + b"/configuration/_compose", data=body,
                headers={b"content-type": b"application/json"},
                persistent=False
            )

            def got_response(response):
                if response.code != OK:
                    d = json_content(response)

                    def got_error(error):
                        if isinstance(error, dict):
                            error = error[u"description"] + u"\n"
                        else:
                            error = u"Unknown error: " + unicode(error) + "\n"
                        raise ResponseError(response.code, error)

                    d.addCallback(got_error)
                    return d

            posted.addCallback(got_response)
            return posted

        return do_configure()
Example #3
0
def _configure(reactor, cluster, configuration):
    """
    Configure the cluster with the given deployment configuration.

    :param reactor: The reactor to use.
    :param flocker.provision._common.Cluster cluster: The target cluster.
    :param dict configuration: The deployment configuration.
    :return Deferred: Deferred that fires when the configuration is pushed
                      to the cluster's control agent.
    """
    base_url = b"https://{}:{}/v1".format(
        cluster.control_node.address, REST_API_PORT
    )
    certificates_path = cluster.certificates_path
    cluster_cert = certificates_path.child(b"cluster.crt")
    user_cert = certificates_path.child(b"user.crt")
    user_key = certificates_path.child(b"user.key")
    body = dumps(configuration)
    treq_client = treq_with_authentication(
        reactor, cluster_cert, user_cert, user_key)

    def got_all_nodes():
        d = treq_client.get(
            base_url + b"/state/nodes",
            persistent=False
        )
        d.addCallback(check_and_decode_json, OK)
        d.addCallback(
            lambda nodes: len(nodes) >= len(cluster.agent_nodes)
        )
        d.addErrback(write_failure, logger=None)
        return d

    got_nodes = loop_until(reactor, got_all_nodes, repeat(1, 300))

    def do_configure(_):
        posted = treq_client.post(
            base_url + b"/configuration/_compose", data=body,
            headers={b"content-type": b"application/json"},
            persistent=False
        )

        def got_response(response):
            if response.code != OK:
                d = json_content(response)

                def got_error(error):
                    if isinstance(error, dict):
                        error = error[u"description"] + u"\n"
                    else:
                        error = u"Unknown error: " + unicode(error) + "\n"
                    raise ResponseError(response.code, error)

                d.addCallback(got_error)
                return d

        posted.addCallback(got_response)
        return posted

    configured = got_nodes.addCallback(do_configure)
    return configured