Files
aitbc/cli/aitbc_cli/commands/global_infrastructure.py
oib 15427c96c0 chore: update file permissions to executable across repository
- Change file mode from 644 to 755 for all project files
- Add chain_id parameter to get_balance RPC endpoint with default "ait-devnet"
- Rename Miner.extra_meta_data to extra_metadata for consistency
2026-03-06 22:17:54 +01:00

572 lines
26 KiB
Python

"""
Global Infrastructure CLI Commands for AITBC
Commands for managing global infrastructure deployment and multi-region optimization
"""
import click
import json
import requests
from datetime import datetime
from typing import Dict, Any, List, Optional
@click.group()
def global_infrastructure():
"""Global infrastructure management commands"""
pass
@global_infrastructure.command()
@click.option('--region-id', required=True, help='Region ID (e.g., us-east-1)')
@click.option('--name', required=True, help='Region name')
@click.option('--location', required=True, help='Geographic location')
@click.option('--endpoint', required=True, help='Region endpoint URL')
@click.option('--capacity', type=int, required=True, help='Region capacity')
@click.option('--compliance-level', default='partial', help='Compliance level (full, partial, basic)')
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def deploy_region(region_id, name, location, endpoint, capacity, compliance_level, test_mode):
"""Deploy a new global region"""
try:
region_data = {
"region_id": region_id,
"name": name,
"location": location,
"endpoint": endpoint,
"status": "deploying",
"capacity": capacity,
"current_load": 0,
"latency_ms": 0,
"compliance_level": compliance_level,
"deployed_at": datetime.utcnow().isoformat()
}
if test_mode:
click.echo(f"🌍 Region deployment started (test mode)")
click.echo(f"🆔 Region ID: {region_id}")
click.echo(f"📍 Name: {name}")
click.echo(f"🗺️ Location: {location}")
click.echo(f"🔗 Endpoint: {endpoint}")
click.echo(f"💾 Capacity: {capacity}")
click.echo(f"⚖️ Compliance Level: {compliance_level}")
click.echo(f"✅ Region deployed successfully")
return
# Send to infrastructure service
config = get_config()
response = requests.post(
f"{config.coordinator_url}/api/v1/regions/register",
json=region_data,
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
result = response.json()
click.echo(f"🌍 Region deployment started successfully")
click.echo(f"🆔 Region ID: {result['region_id']}")
click.echo(f"📍 Name: {result['name']}")
click.echo(f"🗺️ Location: {result['location']}")
click.echo(f"🔗 Endpoint: {result['endpoint']}")
click.echo(f"💾 Capacity: {result['capacity']}")
click.echo(f"⚖️ Compliance Level: {result['compliance_level']}")
click.echo(f"📅 Deployed At: {result['created_at']}")
else:
click.echo(f"❌ Region deployment failed: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error deploying region: {str(e)}", err=True)
@global_infrastructure.command()
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def list_regions(test_mode):
"""List all deployed regions"""
try:
if test_mode:
# Mock regions data
mock_regions = [
{
"region_id": "us-east-1",
"name": "US East (N. Virginia)",
"location": "North America",
"endpoint": "https://us-east-1.api.aitbc.dev",
"status": "active",
"capacity": 10000,
"current_load": 3500,
"latency_ms": 45,
"compliance_level": "full",
"deployed_at": "2024-01-15T10:30:00Z"
},
{
"region_id": "eu-west-1",
"name": "EU West (Ireland)",
"location": "Europe",
"endpoint": "https://eu-west-1.api.aitbc.dev",
"status": "active",
"capacity": 8000,
"current_load": 2800,
"latency_ms": 38,
"compliance_level": "full",
"deployed_at": "2024-01-20T14:20:00Z"
},
{
"region_id": "ap-southeast-1",
"name": "AP Southeast (Singapore)",
"location": "Asia Pacific",
"endpoint": "https://ap-southeast-1.api.aitbc.dev",
"status": "active",
"capacity": 6000,
"current_load": 2200,
"latency_ms": 62,
"compliance_level": "partial",
"deployed_at": "2024-02-01T09:15:00Z"
}
]
click.echo("🌍 Global Infrastructure Regions:")
click.echo("=" * 60)
for region in mock_regions:
status_icon = "" if region['status'] == 'active' else ""
load_percentage = (region['current_load'] / region['capacity']) * 100
compliance_icon = "🔒" if region['compliance_level'] == 'full' else "⚠️"
click.echo(f"{status_icon} {region['name']} ({region['region_id']})")
click.echo(f" 🗺️ Location: {region['location']}")
click.echo(f" 🔗 Endpoint: {region['endpoint']}")
click.echo(f" 💾 Load: {region['current_load']}/{region['capacity']} ({load_percentage:.1f}%)")
click.echo(f" ⚡ Latency: {region['latency_ms']}ms")
click.echo(f" {compliance_icon} Compliance: {region['compliance_level']}")
click.echo(f" 📅 Deployed: {region['deployed_at']}")
click.echo("")
return
# Fetch from infrastructure service
config = get_config()
response = requests.get(
f"{config.coordinator_url}/api/v1/regions",
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
result = response.json()
regions = result.get("regions", [])
click.echo("🌍 Global Infrastructure Regions:")
click.echo("=" * 60)
for region in regions:
status_icon = "" if region['status'] == 'active' else ""
load_percentage = (region['current_load'] / region['capacity']) * 100
compliance_icon = "🔒" if region['compliance_level'] == 'full' else "⚠️"
click.echo(f"{status_icon} {region['name']} ({region['region_id']})")
click.echo(f" 🗺️ Location: {region['location']}")
click.echo(f" 🔗 Endpoint: {region['endpoint']}")
click.echo(f" 💾 Load: {region['current_load']}/{region['capacity']} ({load_percentage:.1f}%)")
click.echo(f" ⚡ Latency: {region['latency_ms']}ms")
click.echo(f" {compliance_icon} Compliance: {region['compliance_level']}")
click.echo(f" 📅 Deployed: {region['deployed_at']}")
click.echo("")
else:
click.echo(f"❌ Failed to list regions: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error listing regions: {str(e)}", err=True)
@global_infrastructure.command()
@click.argument('region_id')
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def region_status(region_id, test_mode):
"""Get detailed status of a specific region"""
try:
if test_mode:
# Mock region status
mock_region = {
"region_id": region_id,
"name": "US East (N. Virginia)",
"location": "North America",
"endpoint": "https://us-east-1.api.aitbc.dev",
"status": "active",
"capacity": 10000,
"current_load": 3500,
"latency_ms": 45,
"compliance_level": "full",
"deployed_at": "2024-01-15T10:30:00Z",
"last_health_check": "2024-03-01T14:20:00Z",
"services_deployed": ["exchange-integration", "trading-engine", "plugin-registry"],
"performance_metrics": [
{
"timestamp": "2024-03-01T14:20:00Z",
"cpu_usage": 35.5,
"memory_usage": 62.3,
"network_io": 1024.5,
"response_time_ms": 45.2
}
],
"compliance_data": {
"certifications": ["SOC2", "ISO27001", "GDPR"],
"data_residency": "compliant",
"last_audit": "2024-02-15T10:30:00Z",
"next_audit": "2024-05-15T10:30:00Z"
}
}
click.echo(f"🌍 Region Status: {mock_region['name']}")
click.echo("=" * 60)
click.echo(f"🆔 Region ID: {mock_region['region_id']}")
click.echo(f"🗺️ Location: {mock_region['location']}")
click.echo(f"🔗 Endpoint: {mock_region['endpoint']}")
click.echo(f"📊 Status: {mock_region['status']}")
click.echo(f"💾 Capacity: {mock_region['capacity']}")
click.echo(f"📈 Current Load: {mock_region['current_load']}")
click.echo(f"⚡ Latency: {mock_region['latency_ms']}ms")
click.echo(f"⚖️ Compliance Level: {mock_region['compliance_level']}")
click.echo(f"📅 Deployed At: {mock_region['deployed_at']}")
click.echo(f"🔍 Last Health Check: {mock_region['last_health_check']}")
click.echo("")
click.echo("🔧 Deployed Services:")
for service in mock_region['services_deployed']:
click.echo(f"{service}")
click.echo("")
click.echo("📊 Performance Metrics:")
latest_metric = mock_region['performance_metrics'][-1]
click.echo(f" 💻 CPU Usage: {latest_metric['cpu_usage']}%")
click.echo(f" 🧠 Memory Usage: {latest_metric['memory_usage']}%")
click.echo(f" 🌐 Network I/O: {latest_metric['network_io']} MB/s")
click.echo(f" ⚡ Response Time: {latest_metric['response_time_ms']}ms")
click.echo("")
click.echo("⚖️ Compliance Information:")
compliance = mock_region['compliance_data']
click.echo(f" 📜 Certifications: {', '.join(compliance['certifications'])}")
click.echo(f" 🏠 Data Residency: {compliance['data_residency']}")
click.echo(f" 🔍 Last Audit: {compliance['last_audit']}")
click.echo(f" 📅 Next Audit: {compliance['next_audit']}")
return
# Fetch from infrastructure service
config = get_config()
response = requests.get(
f"{config.coordinator_url}/api/v1/regions/{region_id}",
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
region = response.json()
click.echo(f"🌍 Region Status: {region['name']}")
click.echo("=" * 60)
click.echo(f"🆔 Region ID: {region['region_id']}")
click.echo(f"🗺️ Location: {region['location']}")
click.echo(f"🔗 Endpoint: {region['endpoint']}")
click.echo(f"📊 Status: {region['status']}")
click.echo(f"💾 Capacity: {region['capacity']}")
click.echo(f"📈 Current Load: {region['current_load']}")
click.echo(f"⚡ Latency: {region['latency_ms']}ms")
click.echo(f"⚖️ Compliance Level: {region['compliance_level']}")
click.echo(f"📅 Deployed At: {region['deployed_at']}")
click.echo(f"🔍 Last Health Check: {region.get('last_health_check', 'N/A')}")
else:
click.echo(f"❌ Region not found: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error getting region status: {str(e)}", err=True)
@global_infrastructure.command()
@click.argument('service_name')
@click.option('--target-regions', help='Target regions (comma-separated)')
@click.option('--strategy', default='rolling', help='Deployment strategy (rolling, blue_green, canary)')
@click.option('--configuration', help='Deployment configuration (JSON)')
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def deploy_service(service_name, target_regions, strategy, configuration, test_mode):
"""Deploy a service to multiple regions"""
try:
# Parse target regions
regions = target_regions.split(',') if target_regions else ["us-east-1", "eu-west-1"]
# Parse configuration
config_data = {}
if configuration:
config_data = json.loads(configuration)
deployment_data = {
"service_name": service_name,
"target_regions": regions,
"configuration": config_data,
"deployment_strategy": strategy,
"health_checks": ["/health", "/api/health"],
"created_at": datetime.utcnow().isoformat()
}
if test_mode:
click.echo(f"🚀 Service deployment started (test mode)")
click.echo(f"📦 Service: {service_name}")
click.echo(f"🌍 Target Regions: {', '.join(regions)}")
click.echo(f"📋 Strategy: {strategy}")
click.echo(f"⚙️ Configuration: {config_data or 'Default'}")
click.echo(f"✅ Deployment completed successfully")
return
# Send to infrastructure service
config = get_config()
response = requests.post(
f"{config.coordinator_url}/api/v1/deployments/create",
json=deployment_data,
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
result = response.json()
click.echo(f"🚀 Service deployment started successfully")
click.echo(f"📦 Service: {service_name}")
click.echo(f"🆔 Deployment ID: {result['deployment_id']}")
click.echo(f"🌍 Target Regions: {', '.join(result['target_regions'])}")
click.echo(f"📋 Strategy: {result['deployment_strategy']}")
click.echo(f"📅 Created At: {result['created_at']}")
else:
click.echo(f"❌ Service deployment failed: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error deploying service: {str(e)}", err=True)
@global_infrastructure.command()
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def dashboard(test_mode):
"""View global infrastructure dashboard"""
try:
if test_mode:
# Mock dashboard data
mock_dashboard = {
"infrastructure": {
"total_regions": 3,
"active_regions": 3,
"total_capacity": 24000,
"current_load": 8500,
"utilization_percentage": 35.4,
"average_latency_ms": 48.3
},
"deployments": {
"total": 15,
"pending": 2,
"in_progress": 1,
"completed": 12,
"failed": 0
},
"performance": {
"us-east-1": {
"cpu_usage": 35.5,
"memory_usage": 62.3,
"response_time_ms": 45.2
},
"eu-west-1": {
"cpu_usage": 28.7,
"memory_usage": 55.1,
"response_time_ms": 38.9
},
"ap-southeast-1": {
"cpu_usage": 42.1,
"memory_usage": 68.9,
"response_time_ms": 62.3
}
},
"compliance": {
"compliant_regions": 2,
"partial_compliance": 1,
"total_audits": 6,
"passed_audits": 5,
"failed_audits": 1
}
}
infra = mock_dashboard['infrastructure']
deployments = mock_dashboard['deployments']
performance = mock_dashboard['performance']
compliance = mock_dashboard['compliance']
click.echo("🌍 Global Infrastructure Dashboard")
click.echo("=" * 60)
click.echo("📊 Infrastructure Overview:")
click.echo(f" 🌍 Total Regions: {infra['total_regions']}")
click.echo(f" ✅ Active Regions: {infra['active_regions']}")
click.echo(f" 💾 Total Capacity: {infra['total_capacity']}")
click.echo(f" 📈 Current Load: {infra['current_load']}")
click.echo(f" 📊 Utilization: {infra['utilization_percentage']:.1f}%")
click.echo(f" ⚡ Avg Latency: {infra['average_latency_ms']}ms")
click.echo("")
click.echo("🚀 Deployment Status:")
click.echo(f" 📦 Total Deployments: {deployments['total']}")
click.echo(f" ⏳ Pending: {deployments['pending']}")
click.echo(f" 🔄 In Progress: {deployments['in_progress']}")
click.echo(f" ✅ Completed: {deployments['completed']}")
click.echo(f" ❌ Failed: {deployments['failed']}")
click.echo("")
click.echo("⚡ Performance Metrics:")
for region_id, metrics in performance.items():
click.echo(f" 🌍 {region_id}:")
click.echo(f" 💻 CPU: {metrics['cpu_usage']}%")
click.echo(f" 🧠 Memory: {metrics['memory_usage']}%")
click.echo(f" ⚡ Response: {metrics['response_time_ms']}ms")
click.echo("")
click.echo("⚖️ Compliance Status:")
click.echo(f" 🔒 Fully Compliant: {compliance['compliant_regions']}")
click.echo(f" ⚠️ Partial Compliance: {compliance['partial_compliance']}")
click.echo(f" 🔍 Total Audits: {compliance['total_audits']}")
click.echo(f" ✅ Passed: {compliance['passed_audits']}")
click.echo(f" ❌ Failed: {compliance['failed_audits']}")
return
# Fetch from infrastructure service
config = get_config()
response = requests.get(
f"{config.coordinator_url}/api/v1/global/dashboard",
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
dashboard = response.json()
infra = dashboard['dashboard']['infrastructure']
deployments = dashboard['dashboard']['deployments']
performance = dashboard['dashboard'].get('performance', {})
compliance = dashboard['dashboard'].get('compliance', {})
click.echo("🌍 Global Infrastructure Dashboard")
click.echo("=" * 60)
click.echo("📊 Infrastructure Overview:")
click.echo(f" 🌍 Total Regions: {infra['total_regions']}")
click.echo(f" ✅ Active Regions: {infra['active_regions']}")
click.echo(f" 💾 Total Capacity: {infra['total_capacity']}")
click.echo(f" 📈 Current Load: {infra['current_load']}")
click.echo(f" 📊 Utilization: {infra['utilization_percentage']:.1f}%")
click.echo(f" ⚡ Avg Latency: {infra['average_latency_ms']}ms")
click.echo("")
click.echo("🚀 Deployment Status:")
click.echo(f" 📦 Total Deployments: {deployments['total']}")
click.echo(f" ⏳ Pending: {deployments['pending']}")
click.echo(f" 🔄 In Progress: {deployments['in_progress']}")
click.echo(f" ✅ Completed: {deployments['completed']}")
click.echo(f" ❌ Failed: {deployments['failed']}")
if performance:
click.echo("")
click.echo("⚡ Performance Metrics:")
for region_id, metrics in performance.items():
click.echo(f" 🌍 {region_id}:")
click.echo(f" 💻 CPU: {metrics.get('cpu_usage', 0)}%")
click.echo(f" 🧠 Memory: {metrics.get('memory_usage', 0)}%")
click.echo(f" ⚡ Response: {metrics.get('response_time_ms', 0)}ms")
if compliance:
click.echo("")
click.echo("⚖️ Compliance Status:")
click.echo(f" 🔒 Fully Compliant: {compliance.get('compliant_regions', 0)}")
click.echo(f" ⚠️ Partial Compliance: {compliance.get('partial_compliance', 0)}")
else:
click.echo(f"❌ Failed to get dashboard: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error getting dashboard: {str(e)}", err=True)
@global_infrastructure.command()
@click.argument('deployment_id')
@click.option('--test-mode', is_flag=True, help='Run in test mode')
def deployment_status(deployment_id, test_mode):
"""Get deployment status"""
try:
if test_mode:
# Mock deployment status
mock_deployment = {
"deployment_id": deployment_id,
"service_name": "trading-engine",
"target_regions": ["us-east-1", "eu-west-1"],
"status": "completed",
"deployment_strategy": "rolling",
"created_at": "2024-03-01T10:30:00Z",
"started_at": "2024-03-01T10:31:00Z",
"completed_at": "2024-03-01T10:45:00Z",
"deployment_progress": {
"us-east-1": {
"status": "completed",
"started_at": "2024-03-01T10:31:00Z",
"completed_at": "2024-03-01T10:38:00Z",
"progress": 100
},
"eu-west-1": {
"status": "completed",
"started_at": "2024-03-01T10:38:00Z",
"completed_at": "2024-03-01T10:45:00Z",
"progress": 100
}
}
}
click.echo(f"🚀 Deployment Status: {mock_deployment['deployment_id']}")
click.echo("=" * 60)
click.echo(f"📦 Service: {mock_deployment['service_name']}")
click.echo(f"🌍 Target Regions: {', '.join(mock_deployment['target_regions'])}")
click.echo(f"📋 Strategy: {mock_deployment['deployment_strategy']}")
click.echo(f"📊 Status: {mock_deployment['status']}")
click.echo(f"📅 Created: {mock_deployment['created_at']}")
click.echo(f"🚀 Started: {mock_deployment['started_at']}")
click.echo(f"✅ Completed: {mock_deployment['completed_at']}")
click.echo("")
click.echo("📈 Progress by Region:")
for region_id, progress in mock_deployment['deployment_progress'].items():
status_icon = "" if progress['status'] == 'completed' else "🔄"
click.echo(f" {status_icon} {region_id}: {progress['progress']}% ({progress['status']})")
return
# Fetch from infrastructure service
config = get_config()
response = requests.get(
f"{config.coordinator_url}/api/v1/deployments/{deployment_id}",
headers={"Authorization": f"Bearer {config.api_key}"},
timeout=30
)
if response.status_code == 200:
deployment = response.json()
click.echo(f"🚀 Deployment Status: {deployment['deployment_id']}")
click.echo("=" * 60)
click.echo(f"📦 Service: {deployment['service_name']}")
click.echo(f"🌍 Target Regions: {', '.join(deployment['target_regions'])}")
click.echo(f"📋 Strategy: {deployment['deployment_strategy']}")
click.echo(f"📊 Status: {deployment['status']}")
click.echo(f"📅 Created: {deployment['created_at']}")
if deployment.get('started_at'):
click.echo(f"🚀 Started: {deployment['started_at']}")
if deployment.get('completed_at'):
click.echo(f"✅ Completed: {deployment['completed_at']}")
if deployment.get('deployment_progress'):
click.echo("")
click.echo("📈 Progress by Region:")
for region_id, progress in deployment['deployment_progress'].items():
status_icon = "" if progress['status'] == 'completed' else "🔄"
click.echo(f" {status_icon} {region_id}: {progress['progress']}% ({progress['status']})")
else:
click.echo(f"❌ Deployment not found: {response.text}", err=True)
except Exception as e:
click.echo(f"❌ Error getting deployment status: {str(e)}", err=True)
# Helper function to get config
def get_config():
"""Get CLI configuration"""
try:
from .config import get_config
return get_config()
except ImportError:
# Fallback for testing
from types import SimpleNamespace
return SimpleNamespace(
coordinator_url="http://localhost:8017",
api_key="test-api-key"
)
if __name__ == "__main__":
global_infrastructure()