Esempio n. 1
0
def get_connection_info(master_ip, master_port, token, callback_token=None, cluster_type="etcd"):
    """
    Contact the master and get all connection information

    :param master_ip: the master IP
    :param master_port: the master port
    :param token: the token to contact the master with
    :param cluster_type: the type of cluster we want to join, etcd or dqlite

    :return: the json response of the master
    """
    cluster_agent_port = get_cluster_agent_port()

    if cluster_type == "dqlite":
        req_data = {
            "token": token,
            "hostname": socket.gethostname(),
            "port": cluster_agent_port,
        }

        # TODO: enable ssl verification
        try:
            connection_info = requests.post(
                "https://{}:{}/{}/join".format(master_ip, master_port, CLUSTER_API_V2),
                json=req_data,
                verify=False,
            )  # type: requests.models.Response
        except requests.exceptions.ConnectionError:
            print("Please ensure the master node is reachable.")
            usage()
            exit(1)
    else:
        req_data = {
            "token": token,
            "hostname": socket.gethostname(),
            "port": cluster_agent_port,
            "callback": callback_token,
        }

        # TODO: enable ssl verification
        try:
            connection_info = requests.post(
                "https://{}:{}/{}/join".format(master_ip, master_port, CLUSTER_API),
                json=req_data,
                verify=False,
            )
        except requests.exceptions.ConnectionError:
            print("Please ensure the master node is reachable.")
            usage()
            exit(1)

    if connection_info.status_code != 200:
        message = "Error code {}.".format(connection_info.status_code)  # type: str
        if connection_info.headers.get('content-type') == 'application/json':
            res_data = connection_info.json()  # type: Dict[str, str]
            if 'error' in res_data:
                message = "{} {}".format(message, res_data["error"])
        print("Failed to join cluster. {}".format(message))
        exit(1)
    return connection_info.json()
Esempio n. 2
0
def get_connection_info(
    master_ip,
    master_port,
    token,
    callback_token=None,
    cluster_type="etcd",
    verify_peer=False,
    fingerprint=None,
    worker=False,
):
    """
    Contact the master and get all connection information

    :param master_ip: the master IP
    :param master_port: the master port
    :param token: the token to contact the master with
    :param callback_token: callback token for etcd based clusters
    :param cluster_type: the type of cluster we want to join, etcd or dqlite
    :param verify_peer: flag indicating if we should verify peers certificate
    :param fingerprint: the certificate fingerprint we expect from the peer
    :param worker: this is a worker only node

    :return: the json response of the master
    """
    cluster_agent_port = get_cluster_agent_port()
    try:
        context = ssl._create_unverified_context()
        conn = http.client.HTTPSConnection("{}:{}".format(
            master_ip, master_port),
                                           context=context)
        conn.connect()
        if cluster_type == "dqlite":
            req_data = {
                "token": token,
                "hostname": socket.gethostname(),
                "port": cluster_agent_port,
                "worker": worker,
            }

            return join_request(conn, CLUSTER_API_V2, req_data, master_ip,
                                verify_peer, fingerprint)
        else:
            req_data = {
                "token": token,
                "hostname": socket.gethostname(),
                "port": cluster_agent_port,
                "callback": callback_token,
            }
            return join_request(conn,
                                CLUSTER_API,
                                req_data,
                                master_ip,
                                verify_peer=False,
                                fingerprint=None)
    except http.client.HTTPException as e:
        print("Connecting to cluster failed with {}.".format(e))
        exit(5)
    except ssl.SSLError as e:
        print("Peer node verification failed with {}.".format(e))
        exit(4)