This commit is contained in:
Danilo Reyes
2025-11-02 01:47:25 -06:00
parent 48020b6f42
commit 010df31455
45 changed files with 8045 additions and 720 deletions

205
scripts/dev-services.sh Executable file
View File

@@ -0,0 +1,205 @@
#!/usr/bin/env bash
# Development services manager for local development
# Uses Nix to run PostgreSQL and MinIO
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
# Data directories
POSTGRES_DATA="$PROJECT_ROOT/.dev-data/postgres"
MINIO_DATA="$PROJECT_ROOT/.dev-data/minio"
# Create data directories
mkdir -p "$POSTGRES_DATA" "$MINIO_DATA"
function start_postgres() {
echo -e "${BLUE}🐘 Starting PostgreSQL...${NC}"
if [ ! -d "$POSTGRES_DATA/PG_VERSION" ]; then
echo "Initializing PostgreSQL database..."
initdb -D "$POSTGRES_DATA" -U webref --encoding=UTF8 --locale=C
fi
# Start PostgreSQL
pg_ctl -D "$POSTGRES_DATA" -l "$POSTGRES_DATA/logfile" start
# Wait for PostgreSQL to be ready
until pg_isready -q -h localhost -p 5432; do
echo "Waiting for PostgreSQL..."
sleep 1
done
# Create database if it doesn't exist
createdb -h localhost -U webref webref 2>/dev/null || true
echo -e "${GREEN}✓ PostgreSQL running on localhost:5432${NC}"
echo -e " Database: webref"
echo -e " User: webref (no password)"
}
function stop_postgres() {
echo -e "${BLUE}🐘 Stopping PostgreSQL...${NC}"
pg_ctl -D "$POSTGRES_DATA" stop -m fast || true
echo -e "${GREEN}✓ PostgreSQL stopped${NC}"
}
function start_minio() {
echo -e "${BLUE}📦 Starting MinIO...${NC}"
# Start MinIO in background
MINIO_ROOT_USER=minioadmin \
MINIO_ROOT_PASSWORD=minioadmin \
minio server "$MINIO_DATA" \
--address :9000 \
--console-address :9001 \
> "$MINIO_DATA/minio.log" 2>&1 &
MINIO_PID=$!
echo $MINIO_PID > "$MINIO_DATA/minio.pid"
# Wait for MinIO to be ready
for i in {1..10}; do
if curl -s http://localhost:9000/minio/health/live > /dev/null 2>&1; then
break
fi
echo "Waiting for MinIO..."
sleep 1
done
# Create bucket if it doesn't exist
mc alias set local http://localhost:9000 minioadmin minioadmin 2>/dev/null || true
mc mb local/webref 2>/dev/null || true
echo -e "${GREEN}✓ MinIO running${NC}"
echo -e " API: http://localhost:9000"
echo -e " Console: http://localhost:9001"
echo -e " Credentials: minioadmin / minioadmin"
}
function stop_minio() {
echo -e "${BLUE}📦 Stopping MinIO...${NC}"
if [ -f "$MINIO_DATA/minio.pid" ]; then
PID=$(cat "$MINIO_DATA/minio.pid")
kill $PID 2>/dev/null || true
rm "$MINIO_DATA/minio.pid"
else
# Try to find and kill MinIO process
pkill -f "minio server" || true
fi
echo -e "${GREEN}✓ MinIO stopped${NC}"
}
function status() {
echo -e "${BLUE}📊 Service Status${NC}"
echo ""
# PostgreSQL
if pg_isready -q -h localhost -p 5432 2>/dev/null; then
echo -e "${GREEN}✓ PostgreSQL${NC} - running on localhost:5432"
else
echo -e "${RED}✗ PostgreSQL${NC} - not running"
fi
# MinIO
if curl -s http://localhost:9000/minio/health/live > /dev/null 2>&1; then
echo -e "${GREEN}✓ MinIO${NC} - running on localhost:9000"
else
echo -e "${RED}✗ MinIO${NC} - not running"
fi
}
function logs() {
echo -e "${BLUE}📜 Showing service logs${NC}"
echo ""
if [ -f "$POSTGRES_DATA/logfile" ]; then
echo -e "${YELLOW}=== PostgreSQL ===${NC}"
tail -n 20 "$POSTGRES_DATA/logfile"
echo ""
fi
if [ -f "$MINIO_DATA/minio.log" ]; then
echo -e "${YELLOW}=== MinIO ===${NC}"
tail -n 20 "$MINIO_DATA/minio.log"
fi
}
function reset() {
echo -e "${RED}⚠️ Resetting all data (this will delete everything)${NC}"
read -p "Are you sure? (yes/no): " -r
if [ "$REPLY" = "yes" ]; then
stop_postgres
stop_minio
rm -rf "$POSTGRES_DATA" "$MINIO_DATA"
echo -e "${GREEN}✓ All data deleted${NC}"
else
echo "Aborted"
fi
}
# Main command handler
case "${1:-}" in
start)
echo -e "${BLUE}🚀 Starting development services${NC}"
echo ""
start_postgres
start_minio
echo ""
echo -e "${GREEN}✅ All services started!${NC}"
echo ""
echo "Environment variables:"
echo " export DATABASE_URL='postgresql://webref@localhost:5432/webref'"
echo " export MINIO_ENDPOINT='localhost:9000'"
;;
stop)
echo -e "${BLUE}🛑 Stopping development services${NC}"
echo ""
stop_postgres
stop_minio
echo ""
echo -e "${GREEN}✅ All services stopped${NC}"
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
logs)
logs
;;
reset)
reset
;;
*)
echo "Development Services Manager"
echo ""
echo "Usage: $0 {start|stop|restart|status|logs|reset}"
echo ""
echo "Commands:"
echo " start - Start PostgreSQL and MinIO"
echo " stop - Stop all services"
echo " restart - Restart all services"
echo " status - Show service status"
echo " logs - Show recent logs"
echo " reset - Delete all data and reset services"
exit 1
;;
esac

198
scripts/dev-vm.sh Executable file
View File

@@ -0,0 +1,198 @@
#!/usr/bin/env bash
# Development VM manager using NixOS
# Uses the same service configuration as CI
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
VM_DIR="$PROJECT_ROOT/.dev-vm"
VM_PID_FILE="$VM_DIR/vm.pid"
function build_vm() {
echo -e "${BLUE}🔨 Building development VM...${NC}"
cd "$PROJECT_ROOT"
nix build .#dev-vm -o "$VM_DIR/result"
echo -e "${GREEN}✓ VM built${NC}"
}
function start_vm() {
if [ -f "$VM_PID_FILE" ] && kill -0 $(cat "$VM_PID_FILE") 2>/dev/null; then
echo -e "${YELLOW}⚠️ VM is already running${NC}"
return
fi
if [ ! -f "$VM_DIR/result/bin/run-nixos-vm" ]; then
echo -e "${YELLOW}Building VM first...${NC}"
build_vm
fi
echo -e "${BLUE}🚀 Starting development VM...${NC}"
mkdir -p "$VM_DIR"
# Start VM in background with port forwarding
# PostgreSQL: 5432 -> 5432
# MinIO API: 9000 -> 9000
# MinIO Console: 9001 -> 9001
QEMU_NET_OPTS="hostfwd=tcp::5432-:5432,hostfwd=tcp::9000-:9000,hostfwd=tcp::9001-:9001" \
"$VM_DIR/result/bin/run-nixos-vm" > "$VM_DIR/vm.log" 2>&1 &
VM_PID=$!
echo $VM_PID > "$VM_PID_FILE"
echo -e "${GREEN}✓ VM started (PID: $VM_PID)${NC}"
echo -e " Logs: $VM_DIR/vm.log"
echo ""
echo "Waiting for services to be ready..."
# Wait for PostgreSQL
for i in {1..30}; do
if pg_isready -h localhost -p 5432 -q 2>/dev/null; then
echo -e "${GREEN}✓ PostgreSQL ready${NC}"
break
fi
sleep 1
done
# Wait for MinIO
for i in {1..30}; do
if curl -sf http://localhost:9000/minio/health/live > /dev/null 2>&1; then
echo -e "${GREEN}✓ MinIO ready${NC}"
break
fi
sleep 1
done
echo ""
echo -e "${GREEN}✅ Development VM running!${NC}"
echo ""
echo "Services available at:"
echo " PostgreSQL: localhost:5432"
echo " MinIO API: http://localhost:9000"
echo " MinIO UI: http://localhost:9001"
echo ""
echo "Environment:"
echo " export DATABASE_URL='postgresql://webref@localhost:5432/webref'"
echo " export MINIO_ENDPOINT='localhost:9000'"
}
function stop_vm() {
if [ ! -f "$VM_PID_FILE" ]; then
echo -e "${YELLOW}⚠️ No VM PID file found${NC}"
return
fi
PID=$(cat "$VM_PID_FILE")
if ! kill -0 $PID 2>/dev/null; then
echo -e "${YELLOW}⚠️ VM is not running${NC}"
rm "$VM_PID_FILE"
return
fi
echo -e "${BLUE}🛑 Stopping VM...${NC}"
kill $PID
rm "$VM_PID_FILE"
echo -e "${GREEN}✓ VM stopped${NC}"
}
function status() {
if [ -f "$VM_PID_FILE" ] && kill -0 $(cat "$VM_PID_FILE") 2>/dev/null; then
echo -e "${GREEN}✓ VM is running${NC} (PID: $(cat "$VM_PID_FILE"))"
# Check services
if pg_isready -h localhost -p 5432 -q 2>/dev/null; then
echo -e "${GREEN}✓ PostgreSQL${NC} - responding"
else
echo -e "${RED}✗ PostgreSQL${NC} - not responding"
fi
if curl -sf http://localhost:9000/minio/health/live > /dev/null 2>&1; then
echo -e "${GREEN}✓ MinIO${NC} - responding"
else
echo -e "${RED}✗ MinIO${NC} - not responding"
fi
else
echo -e "${RED}✗ VM is not running${NC}"
fi
}
function logs() {
if [ ! -f "$VM_DIR/vm.log" ]; then
echo -e "${RED}No log file found${NC}"
return
fi
tail -f "$VM_DIR/vm.log"
}
function clean() {
echo -e "${RED}⚠️ Cleaning VM (this will delete the VM image)${NC}"
read -p "Are you sure? (yes/no): " -r
if [ "$REPLY" = "yes" ]; then
stop_vm
rm -rf "$VM_DIR"
echo -e "${GREEN}✓ VM cleaned${NC}"
else
echo "Aborted"
fi
}
case "${1:-}" in
build)
build_vm
;;
start)
start_vm
;;
stop)
stop_vm
;;
restart)
stop_vm
sleep 2
start_vm
;;
status)
status
;;
logs)
logs
;;
clean)
clean
;;
*)
echo "Development VM Manager"
echo ""
echo "Usage: $0 {build|start|stop|restart|status|logs|clean}"
echo ""
echo "Commands:"
echo " build - Build the NixOS VM image"
echo " start - Start the VM with services"
echo " stop - Stop the VM"
echo " restart - Restart the VM"
echo " status - Show VM and service status"
echo " logs - Tail VM logs"
echo " clean - Remove VM image and data"
echo ""
echo "Alternative: Use native services (faster)"
echo " ./scripts/dev-services.sh start"
exit 1
;;
esac

View File

@@ -20,23 +20,12 @@ cat > "$HOOKS_DIR/pre-commit" << 'EOF'
echo "🔍 Running pre-commit linting..."
echo ""
# Try to use nix run if available, otherwise use script directly
if command -v nix &> /dev/null && [ -f "flake.nix" ]; then
# Use nix run for consistent environment
if ! nix run .#lint; then
echo ""
echo "❌ Linting failed. Fix errors or use --no-verify to skip."
echo " Auto-fix: nix run .#lint-fix"
exit 1
fi
else
# Fallback to script
if ! ./scripts/lint.sh; then
echo ""
echo "❌ Linting failed. Fix errors or use --no-verify to skip."
echo " Auto-fix: ./scripts/lint.sh --fix"
exit 1
fi
# Use nix flake linting for consistency
if ! nix run .#lint; then
echo ""
echo "❌ Linting failed. Fix errors or use --no-verify to skip."
echo " Auto-fix: nix run .#lint-fix"
exit 1
fi
echo ""