post-start.shโข4.56 kB
#!/bin/bash
# NetBox MCP Server DevContainer Post-Start Script
# This script runs every time the container starts
set -e
echo "๐ Starting NetBox MCP Server development environment..."
# Check if we're in a Codespace
if [ -n "$CODESPACES" ]; then
echo "๐ Running in GitHub Codespaces"
export CODESPACE_NAME="$CODESPACE_NAME"
else
echo "๐ป Running in local devcontainer"
fi
# Ensure Python path is set
export PYTHONPATH="/workspaces/mcp-dc/src:$PYTHONPATH"
# Check if development services are running
echo "๐ Checking development services..."
# Function to check if a service is running
check_service() {
local service_name=$1
local port=$2
local url=$3
if curl -s --connect-timeout 5 "$url" > /dev/null 2>&1; then
echo "โ
$service_name is running on port $port"
return 0
else
echo "โ $service_name is not running on port $port"
return 1
fi
}
# Check services
netbox_running=false
vault_running=false
postgres_running=false
if check_service "NetBox Mock" "8000" "http://localhost:8000/health"; then
netbox_running=true
fi
if check_service "Vault Mock" "8200" "http://localhost:8200/v1/sys/health"; then
vault_running=true
fi
if docker-compose -f docker-compose.dev.yml ps postgres | grep -q "Up"; then
echo "โ
PostgreSQL is running"
postgres_running=true
else
echo "โ PostgreSQL is not running"
fi
# Start services if they're not running
if [ "$netbox_running" = false ] || [ "$vault_running" = false ] || [ "$postgres_running" = false ]; then
echo "๐ Starting development services..."
./scripts/start-dev-services.sh
# Wait a bit for services to start
echo "โณ Waiting for services to be ready..."
sleep 10
# Verify services are now running
echo "๐ Verifying services..."
check_service "NetBox Mock" "8000" "http://localhost:8000/health"
check_service "Vault Mock" "8200" "http://localhost:8200/v1/sys/health"
if docker-compose -f docker-compose.dev.yml ps postgres | grep -q "Up"; then
echo "โ
PostgreSQL is now running"
else
echo "โ PostgreSQL failed to start"
fi
fi
# Set up environment for development
echo "โ๏ธ Setting up development environment..."
# Create a symlink to the development environment file
if [ ! -f .env ]; then
ln -sf .env.dev .env
echo "๐ Created .env symlink to .env.dev"
fi
# Ensure pre-commit hooks are installed
if [ -f .pre-commit-config.yaml ]; then
pre-commit install --install-hooks
echo "๐ช Pre-commit hooks installed"
fi
# Run a quick health check
echo "๐ฅ Running health check..."
# Test Python imports
python -c "
import sys
sys.path.insert(0, 'src')
try:
from src.server import list_tools, call_tool
from src.vault_client import VaultClient
from src.netbox_client import NetBoxClient
from src.state_confidence import StateConfidenceClient
print('โ
All Python imports successful')
except ImportError as e:
print(f'โ Import error: {e}')
sys.exit(1)
"
# Test database connection
echo "๐๏ธ Testing database connection..."
python -c "
import psycopg2
import os
try:
conn = psycopg2.connect(
host=os.getenv('POSTGRES_HOST', 'localhost'),
port=os.getenv('POSTGRES_PORT', '5432'),
database=os.getenv('POSTGRES_DB', 'mcp_state'),
user=os.getenv('POSTGRES_USER', 'mcp_user'),
password=os.getenv('POSTGRES_PASSWORD', 'mcp_password')
)
with conn.cursor() as cur:
cur.execute('SELECT COUNT(*) FROM state.certainty_scores')
count = cur.fetchone()[0]
print(f'โ
Database connection successful, {count} records in certainty_scores')
conn.close()
except Exception as e:
print(f'โ Database connection failed: {e}')
"
# Display status
echo ""
echo "๐ Development environment is ready!"
echo ""
echo "๐ Service Status:"
echo " - NetBox Mock: http://localhost:8000"
echo " - Vault Mock: http://localhost:8200"
echo " - PostgreSQL: localhost:5432"
echo " - Redis: localhost:6379"
echo ""
echo "๐ Quick Commands:"
echo " - Run tests: ./scripts/test-mcp-server.sh"
echo " - Start services: ./scripts/start-dev-services.sh"
echo " - Stop services: ./scripts/stop-dev-services.sh"
echo ""
echo "๐ Documentation: .devcontainer/README.md"
echo ""
# If running in Codespaces, show the Codespace URL
if [ -n "$CODESPACES" ]; then
echo "๐ Codespace URL: https://$CODESPACE_NAME-8000.app.github.dev"
echo " (NetBox Mock will be available at this URL)"
fi