예제 #1
0
def setup_container_host(host_type, worker_api, timeout=5):
    """
    Setup a container host for deploying cluster on it
    :param host_type: Docker host type
    :param worker_api: Docker daemon url
    :param timeout: timeout to wait
    :return: True or False
    """
    if not worker_api or not worker_api.startswith("tcp://"):
        logger.error("Invalid worker_api={}".format(worker_api))
        return False
    if host_type not in WORKER_TYPES:
        logger.error("Invalid host_type={}".format(host_type))
        return False
    try:
        client = Client(base_url=worker_api, version="auto", timeout=timeout)
        net_names = [x["Name"] for x in client.networks()]
        for cs_type in CONSENSUS_PLUGINS_FABRIC_V1:
            net_name = CLUSTER_NETWORK + "_{}".format(cs_type)
            if net_name in net_names:
                logger.warning(
                    "Network {} already exists, use it!".format(net_name))
            else:
                if host_type == WORKER_TYPES[0]:  # single
                    client.create_network(net_name, driver='bridge')
                elif host_type == WORKER_TYPES[1]:  # swarm
                    client.create_network(net_name, driver='overlay')
                else:
                    logger.error("No-supported host_type={}".format(host_type))
                    return False
    except Exception as e:
        logger.error("Exception happens!")
        logger.error(e)
        return False
    return True
예제 #2
0
def setup_container_host(host_type, worker_api, timeout=5):
    """
    Setup a container host for deploying cluster on it

    :param host_type: Docker host type
    :param worker_api: Docker daemon url
    :param timeout: timeout to wait
    :return: True or False
    """
    if not worker_api or not worker_api.startswith("tcp://"):
        logger.error("Invalid worker_api={}".format(worker_api))
        return False
    if host_type not in WORKER_TYPES:
        logger.error("Invalid host_type={}".format(host_type))
        return False
    try:
        client = Client(base_url=worker_api, version="auto", timeout=timeout)
        net_names = [x["Name"] for x in client.networks()]
        for cs_type in CONSENSUS_PLUGINS_FABRIC_V1:
            net_name = CLUSTER_NETWORK + "_{}".format(cs_type)
            if net_name in net_names:
                logger.warning("Network {} already exists, use it!".format(
                    net_name))
            else:
                if host_type == WORKER_TYPES[0]:  # single
                    client.create_network(net_name, driver='bridge')
                elif host_type == WORKER_TYPES[1]:  # swarm
                    client.create_network(net_name, driver='overlay')
                else:
                    logger.error("No-supported host_type={}".format(host_type))
                    return False
    except Exception as e:
        logger.error("Exception happens!")
        logger.error(e)
        return False
    return True
예제 #3
0
    def __add_node():
        docker_client = APIClient(base_url=TwinHub.DOCKER_CLIENT_URI)

        responses = docker_client.build(
            dockerfile=TwinHub.DOCKERFILE_PATH,
            path=TwinHub.TWIN_DOCKER_SOURCES_DIR,
            encoding=TwinHub.DEFAULT_DOCKERFILE_ENCODING,
            rm=True,
            tag=TwinHub.NODE_IMAGE_TAG)

        for msg in responses:
            print(msg)

        # Creating network if not exists.
        network = docker_client.networks(names=[TwinHub.NETWORK_NAME])
        if not network:
            docker_client.create_network(TwinHub.NETWORK_NAME, driver="bridge")

        # Creating new container
        TwinHub.NODE_COUNTER += 1
        node_name = 'twin_node_{}'.format(TwinHub.NODE_COUNTER)

        container = docker_client.create_container(TwinHub.NODE_IMAGE_TAG,
                                                   name=node_name,
                                                   ports=[5000],
                                                   tty=True,
                                                   stdin_open=True,
                                                   detach=True,
                                                   hostname=node_name)

        docker_client.start(container['Id'])

        docker_client.connect_container_to_network(container=node_name,
                                                   net_id=TwinHub.NETWORK_NAME)

        return "<html><h1>Hello world</h1></html>"