From 48e6b932afa3c46df4bcd202f81f2dc263a1ddf3 Mon Sep 17 00:00:00 2001 From: jeremy brisson Date: Fri, 28 Feb 2025 14:27:00 +0100 Subject: [PATCH] debug collect in docker image --- development/Dockerfile | 5 ++- tasks/container_ops.py | 40 ++++++------------ utilities/collect.sh | 92 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 29 deletions(-) create mode 100755 utilities/collect.sh diff --git a/development/Dockerfile b/development/Dockerfile index af56b5197e..e8ca9bde80 100644 --- a/development/Dockerfile +++ b/development/Dockerfile @@ -15,8 +15,11 @@ RUN mkdir /prom_shared /remote RUN apt-get update && \ apt-get upgrade -y && \ - apt-get install --no-install-recommends -y tini curl git pkg-config build-essential ca-certificates && \ + apt-get install --no-install-recommends -y tini curl git pkg-config build-essential ca-certificates docker.io && \ curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.8.5 python3 - && \ + mkdir -p /usr/local/lib/docker/cli-plugins && \ + curl -SL "https://github.com/docker/compose/releases/download/v2.33.1/docker-compose-linux-x86_64" -o /usr/local/lib/docker/cli-plugins/docker-compose && \ + chmod +x /usr/local/lib/docker/cli-plugins/docker-compose && \ apt-get autoremove -y && \ apt-get clean all && \ rm -rf /var/lib/apt/lists/* && \ diff --git a/tasks/container_ops.py b/tasks/container_ops.py index 82161a9044..672abd26ba 100644 --- a/tasks/container_ops.py +++ b/tasks/container_ops.py @@ -515,15 +515,15 @@ def collect_support_data( console.print(f"[red]No logs found for service {service}.[/red]") collect_database_logs( - context=context, database=database, namespace=namespace, logs_dir=logs_dir, include_queries=include_queries + context=context, database=database, namespace=namespace, logs_dir=str(logs_dir), include_queries=include_queries ) - collect_message_queue_status(context=context, database=database, namespace=namespace, logs_dir=logs_dir) - collect_cache_status(context=context, database=database, namespace=namespace, logs_dir=logs_dir) - collect_task_worker_status(context=context, database=database, namespace=namespace, logs_dir=logs_dir) + collect_message_queue_status(context=context, database=database, namespace=namespace, logs_dir=str(logs_dir)) + collect_cache_status(context=context, database=database, namespace=namespace, logs_dir=str(logs_dir)) + collect_task_worker_status(context=context, database=database, namespace=namespace, logs_dir=str(logs_dir)) console.print("[bold yellow]Collecting system metrics[/bold yellow]") sys_metrics = collect_system_metrics() - metrics_file = Path(logs_dir) / f"system_metrics_{timestamp}.json" + metrics_file = logs_dir / f"system_metrics_{timestamp}.json" metrics_file.write_text(json.dumps(sys_metrics, indent=2), encoding="utf-8") console.print("[bold yellow]Collecting container metrics[/bold yellow]") @@ -532,33 +532,17 @@ def collect_support_data( compose_cmd = get_compose_cmd(namespace=namespace) base_cmd = f"{get_env_vars(context, namespace=namespace)} {compose_cmd} {compose_files_cmd} -p {BUILD_NAME}" - stats_result = execute_command(context=context, command=f"{base_cmd} stats --no-stream --no-trunc", hide=True) + stats_result = execute_command(context=context, command=f"{base_cmd} stats --no-stream --no-trunc --format json", hide=True) if stats_result and stats_result.stdout: - containers_metrics = [] - lines = stats_result.stdout.strip().split("\n")[1:] - for line in lines: - stats = parse_docker_stats_line(line) - if stats: - name, cpu, mem_usage, mem_percent, net_io, block_io, pids = stats - containers_metrics.append( - { - "name": name, - "cpu_usage": cpu, - "memory_usage": mem_usage, - "memory_percent": mem_percent, - "network_io": net_io, - "block_io": block_io, - "pids": pids, - } - ) - container_metrics_file = Path(logs_dir) / f"container_metrics_{timestamp}.json" - container_metrics_file.write_text(json.dumps(containers_metrics, indent=2), encoding="utf-8") + container_metrics_file.write_text(stats_result.stdout, encoding="utf-8") - archive_name = f"support_logs_{timestamp}.tar.gz" - shutil.make_archive(base_name=f"support_logs_{timestamp}", format="gztar", root_dir=".", base_dir=logs_dir) + export_dir = Path("exports") + export_dir.mkdir(parents=True, exist_ok=True) + archive_base = export_dir / f"support_logs_{timestamp}" + shutil.make_archive(base_name=str(archive_base), format="gztar", root_dir=".", base_dir=str(logs_dir)) shutil.rmtree(logs_dir) - + archive_name = f"{archive_base}.tar.gz" console.print(f"[green]Archive successfully created: {archive_name}[/green]") console.print("You can now provide this file for support analysis.") diff --git a/utilities/collect.sh b/utilities/collect.sh new file mode 100755 index 0000000000..6cc7aed0ce --- /dev/null +++ b/utilities/collect.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# Collect.sh - A script to run Infrahub's support data collection in Docker mode. +# +# This script runs the following docker command: +# +# docker run --rm \ +# -v /var/run/docker.sock:/var/run/docker.sock \ +# -v "$(pwd)/exports":/source/exports \ +# -e INFRAHUB_BUILD_NAME=infrahub \ +# registry.opsmill.io/opsmill/infrahub invoke dev.collect [additional options] +# +# Usage: +# curl https://infrahub.opsmill.io/collect.sh | sh [--include-queries] +# +# Options: +# --include-queries Pass an extra flag to collect database queries logs +# --version VERSION Specify the version of the Infrahub image to use (default: latest) +# +# Environment Variables: +# INFRAHUB_BUILD_NAME Name of the Infrahub build (default: infrahub) +# VERSION Version of the Infrahub image to use (default: latest) + +set -e + +# Check if docker is available +if ! command -v docker >/dev/null 2>&1; then + echo "Error: Docker is not installed or not in PATH." + exit 1 +fi + +# Use INFRAHUB_BUILD_NAME from the environment or default to "infrahub" +if [ -z "$INFRAHUB_BUILD_NAME" ]; then + export INFRAHUB_BUILD_NAME="infrahub" +fi + +# Use VERSION from the environment or default to "latest" +if [ -z "$VERSION" ]; then + export VERSION="latest" +fi + +# Default: no extra options +EXTRA_OPTS="" + +# Process command-line options +while [ "$#" -gt 0 ]; do + case "$1" in + --include-queries) + EXTRA_OPTS="$EXTRA_OPTS --include-queries" + ;; + --version) + if [ -z "$2" ]; then + echo "Error: --version requires an argument." + exit 1 + fi + export VERSION="$2" + shift + ;; + --help|-h) + echo "Usage: sh collect.sh [--include-queries] [--version VERSION]" + echo "" + echo "Options:" + echo " --include-queries Pass an extra flag to collect database queries logs" + echo " --version VERSION Specify the version of the Infrahub image to use (default: latest)" + exit 0 + ;; + *) + echo "Unknown option: $1" + echo "Usage: sh collect.sh [--include-queries] [--version VERSION]" + exit 1 + ;; + esac + shift +done + +# Create the exports directory if it doesn't exist +if [ ! -d "$(pwd)/exports" ]; then + mkdir -p "$(pwd)/exports" +fi + +# Build the docker run command +DOCKER_CMD="docker run --rm \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v $(pwd)/exports:/source/exports \ + -e INFRAHUB_BUILD_NAME=${INFRAHUB_BUILD_NAME} \ + registry.opsmill.io/opsmill/infrahub:${VERSION} invoke dev.collect $EXTRA_OPTS" + +echo "Running command with image version: ${VERSION}" +echo "$DOCKER_CMD" + +# Execute the command +eval $DOCKER_CMD +echo "Support archive created in the ./exports directory."