def create_containers(image_name, number):
    containers = [run_container(image_name) for _ in range(number)]

    thread_pool = []
    for container in containers:
        try:
            docker_container = to_docker_container(container)
            docker_container.start()
            docker_container.reload()

            port_mapping = docker_container.attrs["NetworkSettings"]["Ports"]
            log.info("updating port mapping of %s", container.id)

            def get_port(key, pm=port_mapping):
                portlist = pm[key]
                return int(portlist[0][u"HostPort"])

            with db.transaction() as session:
                container.webdriver_port = get_port(PORT_WEBDRIVER)
                container.http_port = get_port(PORT_HTTP)
                container.vnc_port = get_port(PORT_VNC)
                session.merge(container)
        except APIError as exc:
            # No need to cleanup here since normal balancing will take care of it
            log.warning("Error starting %s", container.name)
            log.exception(exc)
            continue

        thread = Thread(target=_watch_selenium, args=(container,))
        thread_pool.append(thread)
        thread.start()
    for thread in thread_pool:
        thread.join()
def create_container(image_name):
    if last_pulled_image_id is None:
        pull()

    create_info = client.create_container(image_name, detach=True, tty=True)
    container_id = _dgci(create_info, 'id')
    container_info = client.inspect_container(container_id)
    name = _name(container_info)

    with db.transaction() as session, lock:
        # Use the db lock to ensure next_available_port doesn't return dupes
        webdriver_port = _next_available_port()
        http_port = webdriver_port + _http_port_offset
        vnc_port = webdriver_port + _vnc_port_offset
        container = db.Container(
            id=container_id,
            image_id=last_pulled_image_id,
            name=name,
            webdriver_port=webdriver_port,
            http_port=http_port,
            vnc_port=vnc_port
        )
        session.add(container)
        session.expire_on_commit = False

    logger.info('Container %s created (id: %s)', name, container_id[:12])
    return container
def create_container(image_name):
    if last_pulled_image_id is None:
        pull()

    create_info = client.create_container(image_name, detach=True, tty=True)
    container_id = _dgci(create_info, 'id')
    container_info = client.inspect_container(container_id)
    name = _name(container_info)

    with db.transaction() as session, lock:
        # Use the db lock to ensure next_available_port doesn't return dupes
        webdriver_port = _next_available_port()
        http_port = webdriver_port + _http_port_offset
        vnc_port = webdriver_port + _vnc_port_offset
        container = db.Container(id=container_id,
                                 image_id=last_pulled_image_id,
                                 name=name,
                                 webdriver_port=webdriver_port,
                                 http_port=http_port,
                                 vnc_port=vnc_port)
        session.add(container)
        session.expire_on_commit = False

    logger.info('Container %s created (id: %s)', name, container_id[:12])
    return container
def containers():
    containers = set()
    # Get all the docker containers that the DB knows about
    for container_id in docker_info():
        container = db.Container.from_id(container_id)
        if container is None:
            logger.debug("Container %s isn't in the DB; ignored", id)
            continue
        containers.add(container)

    # Clean container out of the DB that docker doesn't know about
    with db.transaction() as session:
        for db_container in session.query(db.Container).all():
            if db_container not in containers:
                logger.debug('Container %s (%s) no longer exists, removing from DB',
                    db_container.name, db_container.id)
                session.delete(db_container)
    return containers
def containers():
    containers = set()
    # Get all the docker containers that the DB knows about
    for container_id in docker_info():
        container = db.Container.from_id(container_id)
        if container is None:
            logger.debug("Container %s isn't in the DB; ignored", id)
            continue
        containers.add(container)

    # Clean container out of the DB that docker doesn't know about
    with db.transaction() as session:
        for db_container in session.query(db.Container).all():
            if db_container not in containers:
                logger.debug(
                    'Container %s (%s) no longer exists, removing from DB',
                    db_container.name, db_container.id)
                session.delete(db_container)
    return containers
def run_container(image_name):
    if last_pulled_image_id is None:
        pull()

    container = client.containers.run(
        image_name,
        detach=True,
        tty=True,
        # publish_all_ports=True,
        ports={PORT_VNC: None, PORT_HTTP: None, PORT_WEBDRIVER: None},
        privileged=True,
        remove=True,
    )

    with db.transaction() as session, lock:
        # Use the db lock to ensure next_available_port doesn't return dupes
        container = db.Container(
            id=container.id, image_id=last_pulled_image_id, name=container.name
        )
        session.add(container)
        session.expire_on_commit = False

    log.info("Container %s created (id: %s)", container.name, container.id)
    return container
Beispiel #7
0
def keepalive(container):
    with db.transaction() as session:
        container.checked_out = datetime.utcnow()
        session.merge(container)
Beispiel #8
0
def keepalive(container):
    with db.transaction() as session:
        container.checked_out = datetime.utcnow()
        session.merge(container)