stock-bot/scripts/docker.sh
2026-11-26 22:23:07 -05:00

228 lines
7.7 KiB
Bash
Executable file

#!/bin/bash
# Trading Bot Docker Management Script (dev / qa / prod)
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
ACTION=""
SERVICE=""
TARGET_ENV="dev"
show_help() {
echo ""
echo -e "${GREEN}🤖 Stock Bot Docker Management${NC}"
echo ""
echo -e "${CYAN}Usage: $0 <action> [options]${NC}"
echo ""
echo -e "${YELLOW}Actions:${NC}"
echo " start Start infrastructure services"
echo " stop Stop infrastructure services"
echo " restart Restart infrastructure services"
echo " status Show service status"
echo " logs Show service logs"
echo " reset Reset dev data only (blocked on qa/prod)"
echo " admin Start QA admin interfaces (pgadmin, redis-insight)"
echo " monitoring Start QA monitoring stack (prometheus, grafana, loki)"
echo " help Show this help"
echo ""
echo -e "${YELLOW}Environments (default: dev):${NC}"
echo " --dev, -d Isolated local dev stack (docker-compose.dev.yml; ports 5433/27018/6380/9100)"
echo " --qa Existing QA stack (docker-compose.yml; ports 5432/27017/6379/9000)"
echo " --prod Production template (docker-compose.prod.yml; requires .env.prod)"
echo " --env <dev|qa|prod> Explicit environment selector"
echo " --service, -s <name> Target a single service"
echo ""
echo -e "${CYAN}Examples:${NC}"
echo " $0 start --dev"
echo " $0 status --qa"
echo " $0 logs --dev --service postgres"
echo " $0 reset --dev"
}
while [[ $# -gt 0 ]]; do
case $1 in
start|stop|restart|status|logs|reset|admin|monitoring|help)
ACTION="$1"
shift
;;
--service|-s)
SERVICE="$2"
shift 2
;;
--dev|-d)
TARGET_ENV="dev"
shift
;;
--qa)
TARGET_ENV="qa"
shift
;;
--prod)
TARGET_ENV="prod"
shift
;;
--env|-e)
TARGET_ENV="$2"
shift 2
;;
--help|-h)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
if [ -z "$ACTION" ]; then
echo "Error: Action is required"
show_help
exit 1
fi
case "$TARGET_ENV" in
dev)
COMPOSE_FILES=(-f docker-compose.dev.yml)
CONTAINER_FILTER="stock-bot-dev"
;;
qa)
COMPOSE_FILES=(-f docker-compose.yml)
CONTAINER_FILTER="trading-bot"
;;
prod)
if [ -f .env.prod ]; then
COMPOSE_FILES=(--env-file .env.prod -f docker-compose.prod.yml)
else
COMPOSE_FILES=(-f docker-compose.prod.yml)
fi
CONTAINER_FILTER="stock-bot-prod"
;;
*)
echo -e "${RED}Invalid environment: $TARGET_ENV (expected dev, qa, or prod)${NC}"
exit 1
;;
esac
case "$ACTION" in
"start")
echo -e "${GREEN}🚀 Starting Stock Bot [${TARGET_ENV}] infrastructure...${NC}"
if [ -n "$SERVICE" ]; then
docker compose "${COMPOSE_FILES[@]}" up -d "$SERVICE"
else
docker compose "${COMPOSE_FILES[@]}" up -d dragonfly postgres questdb mongodb
fi
echo -e "${GREEN}✅ [${TARGET_ENV}] Infrastructure started!${NC}"
if [ "$TARGET_ENV" = "dev" ]; then
echo -e "${CYAN}🔗 Dev Access Points:${NC}"
echo " Dragonfly: localhost:6380"
echo " PostgreSQL: localhost:5433 (trading_bot_dev)"
echo " MongoDB: localhost:27018 (stock_dev - ingestion writes)"
echo " QuestDB: http://localhost:9100"
echo " QA MongoDB: localhost:27017 (read-only via MONGODB_READ_URI for data-pipeline)"
elif [ "$TARGET_ENV" = "qa" ]; then
echo -e "${CYAN}🔗 QA Access Points:${NC}"
echo " Dragonfly: localhost:6379"
echo " PostgreSQL: localhost:5432 (trading_bot)"
echo " MongoDB: localhost:27017 (stock)"
echo " QuestDB: http://localhost:9000"
fi
;;
"stop")
if [ "$TARGET_ENV" = "qa" ] || [ "$TARGET_ENV" = "prod" ]; then
echo -e "${YELLOW}⚠️ Stopping [${TARGET_ENV}] containers (volumes are preserved)...${NC}"
else
echo -e "${YELLOW}🛑 Stopping [${TARGET_ENV}] infrastructure...${NC}"
fi
if [ -n "$SERVICE" ]; then
docker compose "${COMPOSE_FILES[@]}" stop "$SERVICE"
else
docker compose "${COMPOSE_FILES[@]}" stop
fi
echo -e "${GREEN}✅ [${TARGET_ENV}] Infrastructure stopped (volumes preserved).${NC}"
;;
"restart")
echo -e "${YELLOW}🔄 Restarting [${TARGET_ENV}] infrastructure...${NC}"
if [ -n "$SERVICE" ]; then
docker compose "${COMPOSE_FILES[@]}" restart "$SERVICE"
else
docker compose "${COMPOSE_FILES[@]}" restart
fi
echo -e "${GREEN}✅ [${TARGET_ENV}] Infrastructure restarted!${NC}"
;;
"status")
echo -e "${CYAN}📊 Stock Bot [${TARGET_ENV}] Infrastructure Status:${NC}"
docker compose "${COMPOSE_FILES[@]}" ps
echo ""
echo -e "${CYAN}🔍 Containers:${NC}"
docker ps --filter "name=${CONTAINER_FILTER}" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
;;
"logs")
if [ -n "$SERVICE" ]; then
echo -e "${CYAN}📋 Logs for [${TARGET_ENV}] $SERVICE:${NC}"
docker compose "${COMPOSE_FILES[@]}" logs -f "$SERVICE"
else
echo -e "${CYAN}📋 All [${TARGET_ENV}] service logs:${NC}"
docker compose "${COMPOSE_FILES[@]}" logs -f
fi
;;
"reset")
if [ "$TARGET_ENV" != "dev" ]; then
echo -e "${RED}🛑 REFUSED: 'reset' (volume wipe) is strictly forbidden for [${TARGET_ENV}].${NC}"
echo -e "${YELLOW}QA holds the live ingested MongoDB dataset (stock-bot_mongodb_data) and must never be wiped.${NC}"
exit 1
fi
echo -e "${RED}⚠️ Resetting [dev] infrastructure (will delete dev volumes only)...${NC}"
read -p "Are you sure? Type 'yes' to confirm: " confirm
if [ "$confirm" = "yes" ]; then
docker compose "${COMPOSE_FILES[@]}" down -v
echo -e "${YELLOW}🗑️ Dev volumes removed${NC}"
docker compose "${COMPOSE_FILES[@]}" up -d dragonfly postgres questdb mongodb
echo -e "${GREEN}✅ Dev infrastructure reset complete!${NC}"
else
echo -e "${YELLOW}❌ Reset cancelled${NC}"
fi
;;
"admin")
echo -e "${GREEN}🔧 Starting QA admin interfaces...${NC}"
docker compose -f docker-compose.yml up -d redis-insight pgadmin
echo -e "${GREEN}✅ Admin interfaces started!${NC}"
echo -e "${CYAN}🔗 Admin Access:${NC}"
echo " Redis Insight: http://localhost:8001"
echo " PgAdmin: http://localhost:8080"
;;
"monitoring")
echo -e "${GREEN}📊 Starting QA monitoring stack...${NC}"
docker compose -f docker-compose.yml up -d prometheus grafana loki
echo -e "${GREEN}✅ Monitoring started!${NC}"
echo -e "${CYAN}🔗 Monitoring Access:${NC}"
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000"
echo " Loki: http://localhost:3100"
;;
"help")
show_help
;;
*)
echo "Unknown action: $ACTION"
show_help
exit 1
;;
esac