def start_container(container_id, grace_period=None):


    cli = get_docker_client()

    try:
        cli.start(container_id)
        thread_pool.submit(tail_logs, container_id)

    except Exception as e:
        logger.error("Could not start container {0}: {1}".format(container_id, e))
        return False

    if grace_period:

        # Verifies container is UP for at least the grace period time.
        count = 0
        while count < grace_period:
            count += 1
            time.sleep(1)

            active = yield is_stream_active()
            if not active:
                logger.warning("container failed to stay up for {0} seconds".format(grace_period))
                return False

            logger.info("Verifying container stays up, grace period remaining: '{0}'".format(options.grace_period - count))

        return True

    else:
        return True
def stop_stream():
    cli = get_docker_client()

    # Kill and remove existing containers.
    curr_containers = find_containers(curr_container_name, all=True)
    for c in curr_containers:
        i = c["Id"]
        if not c['Status'].startswith("Exited") and not c['Status'].startswith("Created"):
            logger.info("Killing container: '{0}'".format(c["Names"]))
            cli.kill(i)

        logger.info("Removing container: '{0}'".format(c["Names"]))
        cli.remove_container(i)

    options.curr_url = ''
    options.curr_quality = ''
Beispiel #3
0
def stop_stream():
    cli = get_docker_client()

    # Kill and remove existing containers.
    curr_containers = find_containers(curr_container_name, all=True)
    for c in curr_containers:
        i = c["Id"]
        if not c['Status'].startswith("Exited") and not c['Status'].startswith(
                "Created"):
            logger.info("Killing container: '{0}'".format(c["Names"]))
            cli.kill(i)

        logger.info("Removing container: '{0}'".format(c["Names"]))
        cli.remove_container(i)

    options.curr_url = ''
    options.curr_quality = ''
Beispiel #4
0
def start_container(container_id, grace_period=None):

    cli = get_docker_client()

    try:
        cli.start(container_id)
        thread_pool.submit(tail_logs, container_id)

    except Exception as e:
        logger.error("Could not start container {0}: {1}".format(
            container_id, e))
        return False

    if grace_period:

        # Verifies container is UP for at least the grace period time.
        count = 0
        while count < grace_period:
            count += 1
            time.sleep(1)

            active = yield is_stream_active()
            if not active:
                logger.warning(
                    "container failed to stay up for {0} seconds".format(
                        grace_period))
                return False

            logger.info(
                "Verifying container stays up, grace period remaining: '{0}'".
                format(options.grace_period - count))

        return True

    else:
        return True
def create_stream_container(url, quality):
    cli = get_docker_client()

    container = None
    docker_image = None

    docker_image = options.player_docker_image

    if url.startswith("rtsp") or (options.rpi_player and url.startswith("rtmp")):
        # Just pass the url to the player, no checks.
        pass

    else:
        # Attempt to get streamable url via livestreamer
        streams = None
        try:
            streams = livestreamer.streams(url)
        except NoPluginError:
            return False, "Livestreamer is unable to handle the URL '{0}'".format(url)
        except PluginError as err:
            return False, "Plugin error: {0}".format(err)

        if not streams:
            return False, "No streams found on URL '{0}'".format(url)

        # Look for specified stream
        if quality not in streams:
            return False, "Unable to find '{0}' stream on URL '{1}'".format(quality, url)

        # We found the stream
        stream = streams[quality]

        if options.resolve_url:
            logger.info("Stream URL resolved to: '{0}'".format(stream.url))

            try:
                url = stream.url
            except Exception as e:
                return False, "Could not get url from stream, plugin class missing .url method: {0}".format(type(stream))

    container = None
    try:

        devices = []
        if options.rpi_player:
            for d in ["/dev/vchiq", "/dev/fb0", "/dev/snd"]:
                devices.append(d+":"+d+":rwm")

        volumes = []
        binds = []
        if options.rpi_player:
            for v in ["/opt/vc"]:
                binds.append(v+":"+v+":ro")
            volumes.append(v)


        host_config = cli.create_host_config(
            restart_policy={
                "MaximumRetryCount": 0,
                "Name": "always"
            },
            port_bindings={
                8000: 8000
            },
            devices=devices,
            binds=binds
        )

        if options.rpi_player:
            entrypoint = "omxplayer -b -o both"
        else:
            entrypoint = None

        container = cli.create_container(
            image=docker_image,
            entrypoint=entrypoint,
            command=url,
            ports=[8000],
            detach=True,
            name="curr_stream",
            volumes=volumes,
            host_config=host_config
        )
    except Exception as e:
        logger.error("Error creating docker container: {0}".format(e))
        #TODO: revert to previous stream on error

    if container:
        logger.info("Created container with image: '{0}'".format(docker_image))
        return container
    else:
        return None
def tail_logs(container_id):
    # Get container logs and send them to the logger.
    # This is blocking and should be run in a thread.
    cli = get_docker_client()
    for log in cli.attach(container_id, stream=True, stdout=True, stderr=True):
        logger.info(log)
Beispiel #7
0
def create_stream_container(url, quality):
    cli = get_docker_client()

    container = None
    docker_image = None

    docker_image = options.player_docker_image

    if url.startswith("rtsp") or (options.rpi_player
                                  and url.startswith("rtmp")):
        # Just pass the url to the player, no checks.
        pass

    else:
        # Attempt to get streamable url via livestreamer
        streams = None
        try:
            streams = livestreamer.streams(url)
        except NoPluginError:
            return False, "Livestreamer is unable to handle the URL '{0}'".format(
                url)
        except PluginError as err:
            return False, "Plugin error: {0}".format(err)

        if not streams:
            return False, "No streams found on URL '{0}'".format(url)

        # Look for specified stream
        if quality not in streams:
            return False, "Unable to find '{0}' stream on URL '{1}'".format(
                quality, url)

        # We found the stream
        stream = streams[quality]

        if options.resolve_url:
            logger.info("Stream URL resolved to: '{0}'".format(stream.url))

            try:
                url = stream.url
            except Exception as e:
                return False, "Could not get url from stream, plugin class missing .url method: {0}".format(
                    type(stream))

    container = None
    try:

        devices = []
        if options.rpi_player:
            for d in ["/dev/vchiq", "/dev/fb0", "/dev/snd"]:
                devices.append(d + ":" + d + ":rwm")

        volumes = []
        binds = []
        if options.rpi_player:
            for v in ["/opt/vc"]:
                binds.append(v + ":" + v + ":ro")
            volumes.append(v)

        host_config = cli.create_host_config(restart_policy={
            "MaximumRetryCount": 0,
            "Name": "always"
        },
                                             port_bindings={8000: 8000},
                                             devices=devices,
                                             binds=binds)

        if options.rpi_player:
            entrypoint = "omxplayer -b -o both"
        else:
            entrypoint = None

        container = cli.create_container(image=docker_image,
                                         entrypoint=entrypoint,
                                         command=url,
                                         ports=[8000],
                                         detach=True,
                                         name="curr_stream",
                                         volumes=volumes,
                                         host_config=host_config)
    except Exception as e:
        logger.error("Error creating docker container: {0}".format(e))
        #TODO: revert to previous stream on error

    if container:
        logger.info("Created container with image: '{0}'".format(docker_image))
        return container
    else:
        return None
Beispiel #8
0
def tail_logs(container_id):
    # Get container logs and send them to the logger.
    # This is blocking and should be run in a thread.
    cli = get_docker_client()
    for log in cli.attach(container_id, stream=True, stdout=True, stderr=True):
        logger.info(log)