#!/bin/bash
# Cube Database - Podman Helper Script
set -e
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
print_help() {
echo "Cube Database - Podman Helper"
echo ""
echo "Usage: $0 <command>"
echo ""
echo "Commands:"
echo " build Build Podman image"
echo " start Start 3-node cluster"
echo " stop Stop cluster"
echo " restart Restart cluster"
echo " status Show cluster status"
echo " logs Show logs (all nodes)"
echo " logs-node <N> Show logs for node N"
echo " shell-node <N> Open shell in node N"
echo " clean Stop and remove all containers and volumes"
echo " test Run test queries"
echo " pod Create pod for cluster"
echo ""
}
print_step() {
echo -e "${BLUE}▶ $1${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
case "$1" in
build)
print_step "Building Cube database image..."
podman build -t cube-db:latest .
print_success "Image built successfully"
;;
start)
print_step "Starting 3-node Cube database cluster..."
podman-compose -f podman-compose.yml up -d
echo ""
print_step "Waiting for nodes to be healthy..."
sleep 10
podman-compose ps
echo ""
print_success "Cluster started!"
echo ""
echo "Access nodes at:"
echo " Node 1: http://localhost:8080"
echo " Node 2: http://localhost:8081"
echo " Node 3: http://localhost:8082"
;;
stop)
print_step "Stopping cluster..."
podman-compose -f podman-compose.yml stop
print_success "Cluster stopped"
;;
restart)
print_step "Restarting cluster..."
podman-compose -f podman-compose.yml restart
print_success "Cluster restarted"
;;
status)
print_step "Cluster status:"
podman-compose ps
echo ""
print_step "Node health:"
for port in 8080 8081 8082; do
echo -n "Node on port $port: "
if curl -s -f http://localhost:$port/api/v1/health > /dev/null 2>&1; then
echo -e "${GREEN}✓ Healthy${NC}"
else
echo -e "${YELLOW}✗ Unhealthy${NC}"
fi
done
;;
logs)
podman-compose logs -f
;;
logs-node)
if [ -z "$2" ]; then
echo "Usage: $0 logs-node <node-number>"
exit 1
fi
podman logs -f cube-node-$2
;;
shell-node)
if [ -z "$2" ]; then
echo "Usage: $0 shell-node <node-number>"
exit 1
fi
podman exec -it cube-node-$2 /bin/bash
;;
clean)
print_step "Stopping and removing all containers and volumes..."
podman-compose -f podman-compose.yml down -v
print_success "Cleanup complete"
;;
test)
print_step "Testing cluster..."
echo "1. Writing data to node 1..."
curl -s -X POST http://localhost:8080/api/v1/put \
-H "Content-Type: application/json" \
-d '{"key":"test:podman","value":"Hello from Podman!"}' | jq
echo ""
echo "2. Reading from node 2..."
curl -s http://localhost:8081/api/v1/get/test:podman | jq
echo ""
echo "3. Reading from node 3..."
curl -s http://localhost:8082/api/v1/get/test:podman | jq
echo ""
print_success "Test complete"
;;
pod)
print_step "Creating Cube cluster pod..."
# Create pod with port mappings
podman pod create --name cube-cluster \
-p 8080:8080 \
-p 8081:8081 \
-p 8082:8082
print_success "Pod created"
echo ""
echo "Now run containers in the pod:"
echo " podman run -d --pod cube-cluster --name cube-node-1 -e CUBE_NODE_ID=node-1 cube-db:latest"
echo " podman run -d --pod cube-cluster --name cube-node-2 -e CUBE_NODE_ID=node-2 -e CUBE_PORT=8081 cube-db:latest"
echo " podman run -d --pod cube-cluster --name cube-node-3 -e CUBE_NODE_ID=node-3 -e CUBE_PORT=8082 cube-db:latest"
;;
*)
print_help
exit 1
;;
esac