Ejemplo n.º 1
0
 def wait_for_container_exit(
         self, container: docker.models.containers.Container) -> dict:
     logger.info('(%s) container launched, waiting for exit...',
                 container.id)
     for entry in container.logs(timestamps=True, follow=True):
         logger.info("(%s) %s", container.id, entry)
     return container.wait(timeout=self.__docker_wait_timeout)
Ejemplo n.º 2
0
def _collect_container_data(
        container: docker.models.containers.Container) -> dict:
    data = {}
    data['id'] = container.id
    data['name'] = container.name
    if len(container.image.tags) > 0:
        data['image'] = container.image.tags[0]
    data['started_at'] = container.attrs['State']['StartedAt']
    data['finished_at'] = container.attrs['State']['FinishedAt']
    data['finished_at_formatted'] = data['finished_at'].replace('T', ' ')
    data['finished_at_formatted'] = ' '.join(
        data['finished_at_formatted'].split('.')[:-1])
    data['cmd'] = ' '.join(container.attrs['Args'])
    data['base_pid'] = container.attrs['State']['Pid']
    pid, pcpu = _get_ffmpeg_process_data(container)
    data['ffmpeg_pid'] = pid
    data['cpu_usage'] = pcpu
    if container.status == "running":
        now = datetime.datetime.now(pytz.timezone("UTC")).replace(tzinfo=None)
        started = _parse_docker_dt(data['started_at'])
        up = now - started
        hours = up.seconds // 3600
        minutes = (up.seconds % 3600) // 60
        seconds = up.seconds % 60
        data['up'] = '{}d:{}h:{}m:{}s'.format(up.days, hours, minutes, seconds)
    data['status'] = container.status
    if container.status == "running":
        try:
            data['last_log'] = container.logs(tail=LAST_DOCKER_LOGS_COUNT,
                                              timestamps=True)
            data['last_log'] = data['last_log'].decode('utf-8')
        except docker.errors.APIError:
            logger.error(traceback.format_exc())
    data['restart_count'] = container.attrs['RestartCount']
    data['created'] = container.attrs['Created']
    data['is_host_network'] = False
    networks = container.attrs['NetworkSettings'].get('Networks')
    if networks and 'host' in networks:
        data['is_host_network'] = True
    return data