#!/bin/bash # Development Workflow Script for Bakery-IA # # This script provides common development workflows with database management set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color show_usage() { echo "Development Workflow Script for Bakery-IA" echo "" echo "Usage: $0 [COMMAND] [OPTIONS]" echo "" echo "Commands:" echo " start Start development environment" echo " reset Reset database(s) and restart" echo " clean Clean start (drop all data)" echo " migrate Run migrations only" echo " logs Show service logs" echo " status Show deployment status" echo "" echo "Options:" echo " --service NAME Target specific service (default: all)" echo " --profile NAME Use specific Skaffold profile (minimal, full, dev)" echo " --clean-slate Force recreate all tables" echo " --help Show this help" echo "" echo "Examples:" echo " $0 start --profile minimal # Start with minimal services" echo " $0 reset --service auth # Reset auth service only" echo " $0 clean --profile dev # Clean start with dev profile" } start_development() { local profile="${1:-dev}" local clean_slate="${2:-false}" echo -e "${BLUE}Starting development environment with profile: $profile${NC}" if [[ "$clean_slate" == "true" ]]; then echo -e "${YELLOW}Enabling clean slate mode...${NC}" kubectl create configmap development-config --dry-run=client -o yaml \ --from-literal=DB_FORCE_RECREATE=true \ --from-literal=DEVELOPMENT_MODE=true \ --from-literal=DEBUG_LOGGING=true | \ kubectl apply -f - fi # Start with Skaffold echo -e "${BLUE}Starting Skaffold with profile: $profile${NC}" skaffold dev --profile="$profile" --port-forward } reset_service_and_restart() { local service="$1" local profile="${2:-dev}" echo -e "${BLUE}Resetting service: $service${NC}" # Reset the database ./scripts/dev-reset-database.sh --service "$service" --yes # Restart the deployment kubectl rollout restart deployment "${service}-service" -n bakery-ia 2>/dev/null || \ kubectl rollout restart deployment "$service" -n bakery-ia 2>/dev/null || true echo -e "${GREEN}Service $service reset and restarted${NC}" } clean_start() { local profile="${1:-dev}" echo -e "${YELLOW}Performing clean start...${NC}" # Stop existing Skaffold process pkill -f "skaffold" || true # Clean up all deployments kubectl delete jobs -l app.kubernetes.io/component=migration -n bakery-ia 2>/dev/null || true # Wait a moment sleep 2 # Start with clean slate start_development "$profile" "true" } run_migrations() { local service="$1" if [[ -n "$service" ]]; then echo -e "${BLUE}Running migration for service: $service${NC}" kubectl delete job "${service}-migration" -n bakery-ia 2>/dev/null || true kubectl apply -f "infrastructure/kubernetes/base/migrations/${service}-migration-job.yaml" kubectl wait --for=condition=complete job/"${service}-migration" -n bakery-ia --timeout=300s else echo -e "${BLUE}Running migrations for all services${NC}" kubectl delete jobs -l app.kubernetes.io/component=migration -n bakery-ia 2>/dev/null || true kubectl apply -f infrastructure/kubernetes/base/migrations/ # Wait for all migration jobs for job in $(kubectl get jobs -l app.kubernetes.io/component=migration -n bakery-ia -o name); do kubectl wait --for=condition=complete "$job" -n bakery-ia --timeout=300s done fi echo -e "${GREEN}Migrations completed${NC}" } show_logs() { local service="$1" if [[ -n "$service" ]]; then echo -e "${BLUE}Showing logs for service: $service${NC}" kubectl logs -l app.kubernetes.io/name="${service}" -n bakery-ia --tail=100 -f else echo -e "${BLUE}Available services for logs:${NC}" kubectl get deployments -n bakery-ia -o custom-columns="NAME:.metadata.name" fi } show_status() { echo -e "${BLUE}Deployment Status:${NC}" echo "" echo -e "${YELLOW}Pods:${NC}" kubectl get pods -n bakery-ia echo "" echo -e "${YELLOW}Services:${NC}" kubectl get services -n bakery-ia echo "" echo -e "${YELLOW}Jobs:${NC}" kubectl get jobs -n bakery-ia echo "" echo -e "${YELLOW}ConfigMaps:${NC}" kubectl get configmaps -n bakery-ia } main() { local command="" local service="" local profile="dev" local clean_slate="false" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in start|reset|clean|migrate|logs|status) command="$1" shift ;; --service) service="$2" shift 2 ;; --profile) profile="$2" shift 2 ;; --clean-slate) clean_slate="true" shift ;; --help) show_usage exit 0 ;; *) if [[ -z "$command" ]]; then command="$1" fi shift ;; esac done if [[ -z "$command" ]]; then show_usage exit 1 fi case "$command" in "start") start_development "$profile" "$clean_slate" ;; "reset") if [[ -n "$service" ]]; then reset_service_and_restart "$service" "$profile" else echo -e "${RED}Error: --service required for reset command${NC}" exit 1 fi ;; "clean") clean_start "$profile" ;; "migrate") run_migrations "$service" ;; "logs") show_logs "$service" ;; "status") show_status ;; *) echo -e "${RED}Error: Unknown command: $command${NC}" show_usage exit 1 ;; esac } # Check if kubectl and skaffold are available if ! command -v kubectl &> /dev/null; then echo -e "${RED}Error: kubectl is not installed or not in PATH${NC}" exit 1 fi if ! command -v skaffold &> /dev/null; then echo -e "${RED}Error: skaffold is not installed or not in PATH${NC}" exit 1 fi # Run main function main "$@"