From 897093f744c217ede4d7756407beaef33afc87e0 Mon Sep 17 00:00:00 2001 From: sHa Date: Fri, 26 Sep 2025 21:23:45 +0300 Subject: [PATCH] Refactor: Add core.just and fix universal structure - Add core.just with common utilities: - _detect_runtime: Detect Docker/Podman - _detect_compose: Detect compose command - env-check: Environment validation - Fix container.just to be fully universal (remove servass-specific code) - Move _detect_runtime from images.just to core.just - Update container.just to use proper universal operations Universal structure now: - core.just: Common utilities - container.just: Universal container operations - registry.just: Registry authentication - images.just: Universal image operations --- container.just | 374 ++++++++++++------------------------------------- core.just | 100 +++++++++++++ images.just | 12 -- 3 files changed, 190 insertions(+), 296 deletions(-) create mode 100644 core.just diff --git a/container.just b/container.just index 86d7bd1..42c6175 100644 --- a/container.just +++ b/container.just @@ -1,321 +1,127 @@ -# Additional helper commands for service management +# Universal container management operations -# Environment detection -env-check: +# Start service (or all services if no service specified) +start service="": #!/usr/bin/env bash set -euo pipefail - # Colors - RED='\033[0;31m' - GREEN='\033[0;32m' - YELLOW='\033[1;33m' - BLUE='\033[0;34m' - NC='\033[0m' - - echo "================================================" - echo " Container Environment Detection" - echo "================================================" - echo "" - - # Check Docker - if command -v docker >/dev/null 2>&1; then - if docker info >/dev/null 2>&1; then - echo -e "${GREEN}✓ Docker Engine detected and running${NC}" - docker_available=true - else - echo -e "${YELLOW}⚠ Docker is installed but daemon is not running${NC}" - docker_available=false - fi - else - echo -e "${RED}✗ Docker not found${NC}" - docker_available=false - fi - - # Check Podman - if command -v podman >/dev/null 2>&1; then - echo -e "${GREEN}✓ Podman detected${NC}" - podman_available=true - else - echo -e "${RED}✗ Podman not found${NC}" - podman_available=false - fi - - echo "" - echo "================================================" - echo " SUMMARY" - echo "================================================" - - if [ "$docker_available" = true ] || [ "$podman_available" = true ]; then - echo -e "${GREEN}🎉 Container runtime available!${NC}" - echo "" - echo "You can use these commands:" - echo " just build postgres # Build PostgreSQL image" - echo " just build servapp # Build ServApp image" - echo " just login # Login to registry" - else - echo -e "${RED}❌ No container runtime available${NC}" - echo "" - echo "Please install Docker or Podman:" - echo " • Docker: https://docs.docker.com/get-docker/" - echo " • Podman: https://podman.io/getting-started/installation" - fi - -# Start service (helper command) -start service: - #!/usr/bin/env bash - set -euo pipefail - - # Colors - GREEN='\033[0;32m' - BLUE='\033[0;34m' - NC='\033[0m' - - service="{{service}}" - runtime=$(just _detect_runtime) - - if [ ! -d "$service" ]; then - echo "Error: Service directory '$service' not found" >&2 - exit 1 - fi - - echo -e "${BLUE}Starting $service service...${NC}" - cd "$service" - - # Detect compose command based on available and working container runtime - if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then - # Docker daemon is running, check for compose - if docker compose version >/dev/null 2>&1; then - docker compose up -d - elif command -v docker-compose >/dev/null 2>&1; then - docker-compose up -d - else - echo "Error: Docker is running but no compose tool found" >&2 - exit 1 - fi - elif command -v podman >/dev/null 2>&1 && command -v podman-compose >/dev/null 2>&1; then - podman-compose up -d - else - echo "Error: No working container runtime with compose tool found" >&2 - exit 1 - fi - - echo -e "${GREEN}✓ $service service started${NC}" - -# Stop service (helper command) -stop service: - #!/usr/bin/env bash - set -euo pipefail - - # Colors - GREEN='\033[0;32m' - BLUE='\033[0;34m' - NC='\033[0m' - + compose_cmd=$(just _detect_compose) service="{{service}}" - if [ ! -d "$service" ]; then - echo "Error: Service directory '$service' not found" >&2 - exit 1 - fi - - echo -e "${BLUE}Stopping $service service...${NC}" - cd "$service" - - # Detect compose command based on available and working container runtime - if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then - # Docker daemon is running, check for compose - if docker compose version >/dev/null 2>&1; then - docker compose down - elif command -v docker-compose >/dev/null 2>&1; then - docker-compose down - else - echo "Error: Docker is running but no compose tool found" >&2 - exit 1 - fi - elif command -v podman >/dev/null 2>&1 && command -v podman-compose >/dev/null 2>&1; then - podman-compose down + if [ -n "$service" ]; then + echo -e "{{BLUE}}Starting service: $service{{NC}}" + $compose_cmd -f compose.yml up -d "$service" + echo -e "{{GREEN}}✓ Service $service started{{NC}}" else - echo "Error: No working container runtime with compose tool found" >&2 - exit 1 + echo -e "{{BLUE}}Starting all services...{{NC}}" + $compose_cmd -f compose.yml up -d + echo -e "{{GREEN}}✓ All services started{{NC}}" fi - echo -e "${GREEN}✓ $service service stopped${NC}" - -# Show service logs (helper command) -logs service: +# Stop service (or all services if no service specified) +stop service="": #!/usr/bin/env bash set -euo pipefail + compose_cmd=$(just _detect_compose) service="{{service}}" - if [ ! -d "$service" ]; then - echo "Error: Service directory '$service' not found" >&2 - exit 1 - fi - - cd "$service" - - # Detect compose command based on available and working container runtime - if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then - # Docker daemon is running, check for compose - if docker compose version >/dev/null 2>&1; then - docker compose logs -f - elif command -v docker-compose >/dev/null 2>&1; then - docker-compose logs -f - else - echo "Error: Docker is running but no compose tool found" >&2 - exit 1 - fi - elif command -v podman >/dev/null 2>&1 && command -v podman-compose >/dev/null 2>&1; then - podman-compose logs -f + if [ -n "$service" ]; then + echo -e "{{BLUE}}Stopping service: $service{{NC}}" + $compose_cmd -f compose.yml stop "$service" + echo -e "{{GREEN}}✓ Service $service stopped{{NC}}" else - echo "Error: No working container runtime with compose tool found" >&2 - exit 1 + echo -e "{{BLUE}}Stopping all services...{{NC}}" + $compose_cmd -f compose.yml down + echo -e "{{GREEN}}✓ All services stopped{{NC}}" fi -# Open shell in running container (helper command) +# Restart service (or all services if no service specified) +restart service="": + #!/usr/bin/env bash + set -euo pipefail + + compose_cmd=$(just _detect_compose) + service="{{service}}" + + if [ -n "$service" ]; then + echo -e "{{BLUE}}Restarting service: $service{{NC}}" + $compose_cmd -f compose.yml restart "$service" + echo -e "{{GREEN}}✓ Service $service restarted{{NC}}" + else + just stop + just start + fi + +# Show service status (specific service or all) +status service="": + #!/usr/bin/env bash + set -euo pipefail + + compose_cmd=$(just _detect_compose) + service="{{service}}" + + if [ -n "$service" ]; then + echo -e "{{BLUE}}Status for: $service{{NC}}" + $compose_cmd -f compose.yml ps "$service" + else + echo -e "{{BLUE}}Service Status:{{NC}}" + $compose_cmd -f compose.yml ps + fi + +# View logs for specific service or all services +logs service="": + #!/usr/bin/env bash + set -euo pipefail + + compose_cmd=$(just _detect_compose) + service="{{service}}" + + if [ -n "$service" ]; then + echo -e "{{BLUE}}Showing logs for: $service{{NC}}" + $compose_cmd -f compose.yml logs -f "$service" + else + echo -e "{{BLUE}}Showing logs for all services{{NC}}" + $compose_cmd -f compose.yml logs -f + fi + +# Open shell in specific container shell service: #!/usr/bin/env bash set -euo pipefail - # Colors - RED='\033[0;31m' - NC='\033[0m' - + compose_cmd=$(just _detect_compose) service="{{service}}" - runtime=$(just _detect_runtime) - # Get first running container for this service - container_id=$($runtime ps -q --filter "label=com.docker.compose.project=$service" | head -n1) - if [ -n "$container_id" ]; then - $runtime exec -it "$container_id" /bin/bash || \ - $runtime exec -it "$container_id" /bin/sh - else - echo -e "${RED}No running containers found for $service${NC}" - echo "Start the service first with: just start $service" - exit 1 - fi + echo -e "{{BLUE}}Opening shell in: $service{{NC}}" + $compose_cmd -f compose.yml exec "$service" /bin/bash -# Execute command in service container (helper command) +# Execute command in specific service container exec service cmd: #!/usr/bin/env bash set -euo pipefail - # Colors - RED='\033[0;31m' - NC='\033[0m' - + compose_cmd=$(just _detect_compose) service="{{service}}" - command="{{cmd}}" - runtime=$(just _detect_runtime) + cmd="{{cmd}}" - if [ -z "$command" ]; then - echo -e "${RED}No command specified${NC}" >&2 - echo "Usage: just exec $service \"command to execute\"" >&2 - exit 1 - fi + echo -e "{{BLUE}}Executing in $service: $cmd{{NC}}" + $compose_cmd -f compose.yml exec "$service" bash -c "$cmd" - # Get first running container for this service - container_id=$($runtime ps -q --filter "label=com.docker.compose.project=$service" | head -n1) - if [ -n "$container_id" ]; then - $runtime exec -it "$container_id" sh -c "$command" - else - echo -e "${RED}No running containers found for $service${NC}" - echo "Start the service first with: just start $service" - exit 1 - fi - -# Show service status (helper command) -status service: +# Pull latest images for specific service or all services +pull service="": #!/usr/bin/env bash set -euo pipefail + compose_cmd=$(just _detect_compose) service="{{service}}" - if [ ! -d "$service" ]; then - echo "Error: Service directory '$service' not found" >&2 - exit 1 - fi - - cd "$service" - - # Detect compose command based on available and working container runtime - if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then - # Docker daemon is running, check for compose - if docker compose version >/dev/null 2>&1; then - docker compose ps - elif command -v docker-compose >/dev/null 2>&1; then - docker-compose ps - else - echo "Error: Docker is running but no compose tool found" >&2 - exit 1 - fi - elif command -v podman >/dev/null 2>&1 && command -v podman-compose >/dev/null 2>&1; then - podman-compose ps + if [ -n "$service" ]; then + echo -e "{{BLUE}}Pulling image for: $service{{NC}}" + $compose_cmd -f compose.yml pull "$service" + echo -e "{{GREEN}}✓ Image updated for $service{{NC}}" else - echo "Error: No working container runtime with compose tool found" >&2 - exit 1 - fi - - -# Quick development setup -dev-setup: - #!/usr/bin/env bash - set -euo pipefail - - # Colors - GREEN='\033[0;32m' - BLUE='\033[0;34m' - YELLOW='\033[1;33m' - NC='\033[0m' - - echo -e "${BLUE}Setting up development environment...${NC}" - - # Check environment - just env-check - - echo "" - echo -e "${BLUE}Building development images...${NC}" - - # Build development images - just postgres-dev - just servapp-dev - - echo "" - echo -e "${GREEN}✓ Development environment ready!${NC}" - echo "" - echo -e "${YELLOW}Available commands:${NC}" - echo " just start postgres # Start PostgreSQL service" - echo " just start servapp # Start ServApp service" - echo " just logs postgres # View PostgreSQL logs" - echo " just shell postgres # Connect to PostgreSQL container" - -# Clean all development artifacts -dev-clean: - #!/usr/bin/env bash - set -euo pipefail - - # Colors - GREEN='\033[0;32m' - BLUE='\033[0;34m' - YELLOW='\033[1;33m' - NC='\033[0m' - - echo -e "${BLUE}Cleaning development environment...${NC}" - - runtime=$(just _detect_runtime) - - # Stop any running services - echo -e "${YELLOW}Stopping services...${NC}" - just stop postgres 2>/dev/null || true - just stop servapp 2>/dev/null || true - - # Clean images - echo -e "${YELLOW}Cleaning development images...${NC}" - just clean postgres 2>/dev/null || true - just clean servapp 2>/dev/null || true - - # Remove dev tags - $runtime rmi postgres:dev servapp:dev 2>/dev/null || true - - echo -e "${GREEN}✓ Development environment cleaned${NC}" \ No newline at end of file + echo -e "{{BLUE}}Pulling all images...{{NC}}" + $compose_cmd -f compose.yml pull + echo -e "{{GREEN}}✓ All images updated{{NC}}" + fi \ No newline at end of file diff --git a/core.just b/core.just new file mode 100644 index 0000000..452ff1c --- /dev/null +++ b/core.just @@ -0,0 +1,100 @@ +# Core utilities shared across all just-commons modules + +# Detect container runtime (Docker or Podman) +_detect_runtime: + #!/usr/bin/env bash + if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then + echo "docker" + elif command -v podman >/dev/null 2>&1; then + echo "podman" + else + echo "Error: No container runtime available (docker or podman)" >&2 + exit 1 + fi + +# Detect compose command based on available and working container runtime +_detect_compose: + #!/usr/bin/env bash + # Check Docker first - but make sure daemon is actually running + if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then + # Docker daemon is running, check for compose + if docker compose version >/dev/null 2>&1; then + echo "docker compose" + elif command -v docker-compose >/dev/null 2>&1; then + echo "docker-compose" + else + echo "Error: Docker is running but no compose tool found" >&2 + exit 1 + fi + # Check Podman if Docker is not available or not running + elif command -v podman >/dev/null 2>&1; then + if command -v podman-compose >/dev/null 2>&1; then + echo "podman-compose" + else + echo "Error: Podman found but podman-compose not available" >&2 + exit 1 + fi + else + echo "Error: No container runtime (Docker or Podman) found" >&2 + exit 1 + fi + +# Environment detection and validation +env-check: + #!/usr/bin/env bash + set -euo pipefail + + # Colors + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[1;33m' + BLUE='\033[0;34m' + NC='\033[0m' + + echo "================================================" + echo " Container Environment Detection" + echo "================================================" + echo "" + + # Check Docker + if command -v docker >/dev/null 2>&1; then + if docker info >/dev/null 2>&1; then + echo -e "${GREEN}✓ Docker Engine detected and running${NC}" + docker_available=true + else + echo -e "${YELLOW}⚠ Docker is installed but daemon is not running${NC}" + docker_available=false + fi + else + echo -e "${RED}✗ Docker not found${NC}" + docker_available=false + fi + + # Check Podman + if command -v podman >/dev/null 2>&1; then + echo -e "${GREEN}✓ Podman detected${NC}" + podman_available=true + else + echo -e "${RED}✗ Podman not found${NC}" + podman_available=false + fi + + echo "" + echo "================================================" + echo " SUMMARY" + echo "================================================" + + if [ "$docker_available" = true ] || [ "$podman_available" = true ]; then + echo -e "${GREEN}🎉 Container runtime available!${NC}" + echo "" + echo "You can use these universal commands:" + echo " just build # Build any project image" + echo " just start # Start any service" + echo " just registry-login # Login to registry" + else + echo -e "${RED}❌ No container runtime available${NC}" + echo "" + echo "Please install Docker or Podman:" + echo " • Docker: https://docs.docker.com/get-docker/" + echo " • Podman: https://podman.io/getting-started/installation" + fi \ No newline at end of file diff --git a/images.just b/images.just index 8a96d32..f15963d 100644 --- a/images.just +++ b/images.just @@ -1,17 +1,5 @@ # Universal image operations - works for any project -# Detect container runtime -_detect_runtime: - #!/usr/bin/env bash - if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then - echo "docker" - elif command -v podman >/dev/null 2>&1; then - echo "podman" - else - echo "Error: No container runtime available (docker or podman)" >&2 - exit 1 - fi - # Build any project's container image build project *args="": #!/usr/bin/env bash