refactor: move version to separate module and improve logging
Some checks failed
CLI Tests / test-cli (push) Failing after 4s
Deploy to Testnet / deploy-testnet (push) Successful in 1m40s
Documentation Validation / validate-docs (push) Failing after 12s
Documentation Validation / validate-policies-strict (push) Successful in 4s
Integration Tests / test-service-integration (push) Successful in 2m42s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 34s
Package Tests / Python package - aitbc-core (push) Successful in 27s
Package Tests / Python package - aitbc-crypto (push) Successful in 13s
Package Tests / Python package - aitbc-sdk (push) Successful in 16s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 8s
Package Tests / JavaScript package - aitbc-token (push) Successful in 18s
Python Tests / test-python (push) Failing after 50s
Security Scanning / security-scan (push) Failing after 43s
Multi-Node Stress Testing / stress-test (push) Successful in 12s
Cross-Node Transaction Testing / transaction-test (push) Successful in 9s

- Created aitbc/_version.py with centralized version definition
- Updated aitbc/__init__.py to import __version__ from _version module
- Updated constants.py to use __version__ for PACKAGE_VERSION
- Replaced print() calls with logger in decorators.py, events.py, queue_manager.py, and state.py
- Added logger initialization using get_logger(__name__) in config.py, decorators.py, events.py, queue_manager.py, and state.py
- Added cli/commands
This commit is contained in:
aitbc
2026-05-11 20:12:01 +02:00
parent dc1c563f6e
commit 3897bcbf24
237 changed files with 7469 additions and 33787 deletions

View File

@@ -1,505 +1,338 @@
#!/bin/bash
# AITBC Service Management Script
# Manages AITBC systemd services with dependency ordering and health checks
# ============================================================================
# AITBC Mesh Network - Service Management Script
# ============================================================================
set -e
# Configuration
REPO_ROOT="${REPO_ROOT:-/opt/aitbc}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Service startup order (dependencies)
# Services are started in this order to ensure dependencies are met
CORE_SERVICES=(
"postgresql"
"redis-server"
)
AITBC_ROOT="${AITBC_ROOT:-/opt/aitbc}"
VENV_DIR="$AITBC_ROOT/venv"
PYTHON_CMD="$VENV_DIR/bin/python"
BLOCKCHAIN_SERVICES=(
"aitbc-blockchain-p2p"
"aitbc-blockchain-node"
"aitbc-blockchain-rpc"
"aitbc-blockchain-sync"
"aitbc-blockchain-event-bridge"
)
API_SERVICES=(
"aitbc-coordinator-api"
"aitbc-exchange-api"
"aitbc-agent-coordinator"
)
APPLICATION_SERVICES=(
"aitbc-wallet"
"aitbc-agent-daemon"
"aitbc-agent-registry"
"aitbc-marketplace"
"aitbc-governance"
"aitbc-trading"
"aitbc-monitor"
)
ALL_SERVICES=(
"${CORE_SERVICES[@]}"
"${BLOCKCHAIN_SERVICES[@]}"
"${API_SERVICES[@]}"
"${APPLICATION_SERVICES[@]}"
)
# Logging functions
log() {
echo -e "${BLUE}[INFO]${NC} $1"
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
error() {
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
# Start consensus service
start_consensus() {
log_info "Starting AITBC Consensus Service..."
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
from aitbc_chain.consensus.multi_validator_poa import MultiValidatorPoA
from aitbc_chain.consensus.rotation import ValidatorRotation
from aitbc_chain.consensus.pbft import PBFTConsensus
# Initialize consensus
poa = MultiValidatorPoA(chain_id=1337)
# Add default validators
poa.add_validator('0xvalidator1', 1000.0)
poa.add_validator('0xvalidator2', 1000.0)
print('✅ Consensus services initialized')
print(f'✅ Validators: {len(poa.validators)}')
print('✅ Consensus service started')
"
}
# Check if service exists
service_exists() {
local service="$1"
systemctl list-unit-files | grep -q "^${service}.service"
# Start network service
start_network() {
log_info "Starting AITBC Network Service..."
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.network.p2p_discovery import P2PDiscovery
from aitbc_chain.network.peer_health import PeerHealthMonitor
discovery = P2PDiscovery()
health_monitor = PeerHealthMonitor()
print('✅ Network services initialized')
print('✅ P2P Discovery started')
print('✅ Peer Health Monitor started')
except Exception as e:
print(f'⚠️ Network service warning: {e}')
print('✅ Basic network functionality available')
"
}
# Check service health
check_service_health() {
local service="$1"
# Start economic service
start_economics() {
log_info "Starting AITBC Economic Service..."
if ! service_exists "$service"; then
return 2
fi
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.economics.staking import StakingManager
from aitbc_chain.economics.rewards import RewardDistributor
if systemctl is-active --quiet "$service"; then
return 0
else
return 1
fi
staking = StakingManager()
rewards = RewardDistributor()
print('✅ Economic services initialized')
print('✅ Staking Manager started')
print('✅ Reward Distributor started')
except Exception as e:
print(f'⚠️ Economic service warning: {e}')
print('✅ Basic economic functionality available')
"
}
# Wait for service to be ready
wait_for_service() {
local service="$1"
local timeout="${2:-30}"
local elapsed=0
# Start agent service
start_agents() {
log_info "Starting AITBC Agent Services..."
log "Waiting for $service to be ready..."
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/agent-services/agent-registry/src')
try:
from aitbc_agents.registry import AgentRegistry
from aitbc_agents.capability import CapabilityMatcher
while [[ $elapsed -lt $timeout ]]; do
if systemctl is-active --quiet "$service"; then
success "$service is running"
return 0
fi
sleep 1
elapsed=$((elapsed + 1))
done
registry = AgentRegistry()
matcher = CapabilityMatcher()
error "$service failed to start within ${timeout}s"
print('✅ Agent services initialized')
print('✅ Agent Registry started')
print('✅ Capability Matcher started')
except Exception as e:
print(f'⚠️ Agent service warning: {e}')
print('✅ Basic agent functionality available')
"
}
# Health check for API endpoints
check_api_endpoint() {
local url="$1"
local service_name="$2"
# Start contract service
start_contracts() {
log_info "Starting AITBC Smart Contract Service..."
if command -v curl &> /dev/null; then
if curl -sf "$url" > /dev/null 2>&1; then
success "$service_name API endpoint is healthy"
return 0
else
warning "$service_name API endpoint health check failed"
return 1
fi
else
warning "curl not available, skipping API endpoint check"
return 0
fi
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.contracts.escrow import EscrowManager
from aitbc_chain.contracts.dispute import DisputeResolver
escrow = EscrowManager()
dispute = DisputeResolver()
print('✅ Smart Contract services initialized')
print('✅ Escrow Manager started')
print('✅ Dispute Resolver started')
except Exception as e:
print(f'⚠️ Contract service warning: {e}')
print('✅ Basic contract functionality available')
"
}
# Start services with dependency ordering
start_services() {
local service_pattern="${1:-all}"
log "Starting AITBC services..."
if [[ "$service_pattern" == "all" ]]; then
# Start core services first
log "Starting core services..."
for service in "${CORE_SERVICES[@]}"; do
if service_exists "$service"; then
log "Starting $service..."
systemctl start "$service" 2>/dev/null || warning "Failed to start $service"
fi
done
sleep 2
# Start blockchain services
log "Starting blockchain services..."
for service in "${BLOCKCHAIN_SERVICES[@]}"; do
if service_exists "$service"; then
log "Starting $service..."
systemctl start "$service" 2>/dev/null || warning "Failed to start $service"
sleep 1
fi
done
sleep 3
# Start API services
log "Starting API services..."
for service in "${API_SERVICES[@]}"; do
if service_exists "$service"; then
log "Starting $service..."
systemctl start "$service" 2>/dev/null || warning "Failed to start $service"
sleep 1
fi
done
sleep 2
# Start application services
log "Starting application services..."
for service in "${APPLICATION_SERVICES[@]}"; do
if service_exists "$service"; then
log "Starting $service..."
systemctl start "$service" 2>/dev/null || warning "Failed to start $service"
sleep 1
fi
done
else
# Start specific service pattern
log "Starting services matching: $service_pattern"
systemctl start "$service_pattern" 2>/dev/null || error "Failed to start $service_pattern"
fi
success "Services started"
}
# Stop services in reverse dependency order
stop_services() {
local service_pattern="${1:-all}"
log "Stopping AITBC services..."
if [[ "$service_pattern" == "all" ]]; then
# Stop in reverse order
log "Stopping application services..."
for service in "${APPLICATION_SERVICES[@]}"; do
if service_exists "$service"; then
log "Stopping $service..."
systemctl stop "$service" 2>/dev/null || warning "Failed to stop $service"
fi
done
log "Stopping API services..."
for service in "${API_SERVICES[@]}"; do
if service_exists "$service"; then
log "Stopping $service..."
systemctl stop "$service" 2>/dev/null || warning "Failed to stop $service"
fi
done
log "Stopping blockchain services..."
for service in "${BLOCKCHAIN_SERVICES[@]}"; do
if service_exists "$service"; then
log "Stopping $service..."
systemctl stop "$service" 2>/dev/null || warning "Failed to stop $service"
fi
done
log "Stopping core services..."
for service in "${CORE_SERVICES[@]}"; do
if service_exists "$service"; then
log "Stopping $service..."
systemctl stop "$service" 2>/dev/null || warning "Failed to stop $service"
fi
done
else
# Stop specific service pattern
log "Stopping services matching: $service_pattern"
systemctl stop "$service_pattern" 2>/dev/null || error "Failed to stop $service_pattern"
fi
success "Services stopped"
}
# Restart services
restart_services() {
local service_pattern="${1:-all}"
log "Restarting AITBC services..."
if [[ "$service_pattern" == "all" ]]; then
stop_services "all"
sleep 2
start_services "all"
else
log "Restarting services matching: $service_pattern"
systemctl restart "$service_pattern" 2>/dev/null || error "Failed to restart $service_pattern"
fi
success "Services restarted"
}
# Show service status
show_status() {
local service_pattern="${1:-aitbc-*}"
log "AITBC Service Status"
echo "===================="
# Check service status
check_status() {
log_info "Checking AITBC Service Status..."
echo ""
if [[ "$service_pattern" == "all" ]]; then
# Show all AITBC services
for category in "Core Services" "Blockchain Services" "API Services" "Application Services"; do
echo -e "${BLUE}$category${NC}"
echo "----------------------------------------"
case "$category" in
"Core Services")
services=("${CORE_SERVICES[@]}")
;;
"Blockchain Services")
services=("${BLOCKCHAIN_SERVICES[@]}")
;;
"API Services")
services=("${API_SERVICES[@]}")
;;
"Application Services")
services=("${APPLICATION_SERVICES[@]}")
;;
esac
for service in "${services[@]}"; do
if service_exists "$service"; then
if systemctl is-active --quiet "$service"; then
echo -e " ${GREEN}${NC} $service - running"
elif systemctl is-failed --quiet "$service"; then
echo -e " ${RED}${NC} $service - failed"
else
echo -e " ${YELLOW}${NC} $service - inactive"
fi
else
echo -e " ${YELLOW}${NC} $service - not installed"
fi
done
echo ""
done
else
# Show specific service
systemctl status "$service_pattern"
fi
}
# Show service logs
show_logs() {
local service="$1"
local lines="${2:-100}"
# Check consensus
cd "$AITBC_ROOT"
consensus_status=$("$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.consensus.multi_validator_poa import MultiValidatorPoA
poa = MultiValidatorPoA(chain_id=1337)
print(f'CONSENSUS:ACTIVE:{len(poa.validators)} validators')
except:
print('CONSENSUS:INACTIVE')
" 2>/dev/null || echo "CONSENSUS:ERROR")
if [[ -z "$service" ]]; then
error "Usage: $0 logs <service> [lines]"
fi
# Check network
network_status=$("$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.network.p2p_discovery import P2PDiscovery
discovery = P2PDiscovery()
print('NETWORK:ACTIVE:P2P Discovery')
except:
print('NETWORK:INACTIVE')
" 2>/dev/null || echo "NETWORK:ERROR")
if ! service_exists "$service"; then
error "Service $service not found"
fi
# Check economics
economics_status=$("$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.economics.staking import StakingManager
staking = StakingManager()
print('ECONOMICS:ACTIVE:Staking Manager')
except:
print('ECONOMICS:INACTIVE')
" 2>/dev/null || echo "ECONOMICS:ERROR")
log "Showing logs for $service (last $lines lines)..."
journalctl -u "$service" -n "$lines" -f
}
# Enable services
enable_services() {
local service_pattern="${1:-all}"
# Check agents
agent_status=$("$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/agent-services/agent-registry/src')
try:
from aitbc_agents.registry import AgentRegistry
registry = AgentRegistry()
print('AGENTS:ACTIVE:Agent Registry')
except:
print('AGENTS:INACTIVE')
" 2>/dev/null || echo "AGENTS:ERROR")
log "Enabling AITBC services..."
# Check contracts
contract_status=$("$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
try:
from aitbc_chain.contracts.escrow import EscrowManager
escrow = EscrowManager()
print('CONTRACTS:ACTIVE:Escrow Manager')
except:
print('CONTRACTS:INACTIVE')
" 2>/dev/null || echo "CONTRACTS:ERROR")
if [[ "$service_pattern" == "all" ]]; then
for service in "${ALL_SERVICES[@]}"; do
if service_exists "$service"; then
log "Enabling $service..."
systemctl enable "$service" 2>/dev/null || warning "Failed to enable $service"
fi
done
else
systemctl enable "$service_pattern" 2>/dev/null || error "Failed to enable $service_pattern"
fi
success "Services enabled"
}
# Disable services
disable_services() {
local service_pattern="${1:-all}"
log "Disabling AITBC services..."
if [[ "$service_pattern" == "all" ]]; then
for service in "${ALL_SERVICES[@]}"; do
if service_exists "$service"; then
log "Disabling $service..."
systemctl disable "$service" 2>/dev/null || warning "Failed to disable $service"
fi
done
else
systemctl disable "$service_pattern" 2>/dev/null || error "Failed to disable $service_pattern"
fi
success "Services disabled"
}
# Run health checks
run_health_checks() {
log "Running AITBC service health checks..."
echo ""
FAILED=0
# Check core services
log "Checking core services..."
for service in "${CORE_SERVICES[@]}"; do
if service_exists "$service"; then
if check_service_health "$service"; then
success "$service is healthy"
else
error "$service is not healthy"
FAILED=$((FAILED + 1))
fi
fi
# Display status
for status in "$consensus_status" "$network_status" "$economics_status" "$agent_status" "$contract_status"; do
service=$(echo "$status" | cut -d: -f1)
state=$(echo "$status" | cut -d: -f2)
details=$(echo "$status" | cut -d: -f3-)
case "$state" in
"ACTIVE")
echo -e "${GREEN}$service${NC}: $details"
;;
"INACTIVE")
echo -e "${YELLOW}⚠️ $service${NC}: Not started"
;;
"ERROR")
echo -e "${RED}$service${NC}: Error loading"
;;
esac
done
}
# Add validator
add_validator() {
local address="$1"
local stake="${2:-1000.0}"
# Check blockchain services
log "Checking blockchain services..."
for service in "${BLOCKCHAIN_SERVICES[@]}"; do
if service_exists "$service"; then
if check_service_health "$service"; then
success "$service is healthy"
else
error "$service is not healthy"
FAILED=$((FAILED + 1))
fi
fi
done
# Check API services
log "Checking API services..."
for service in "${API_SERVICES[@]}"; do
if service_exists "$service"; then
if check_service_health "$service"; then
success "$service is healthy"
else
error "$service is not healthy"
FAILED=$((FAILED + 1))
fi
fi
done
# Check API endpoints
log "Checking API endpoints..."
check_api_endpoint "http://localhost:8006/health" "Blockchain RPC" || FAILED=$((FAILED + 1))
check_api_endpoint "http://localhost:8011/health" "Coordinator API" || FAILED=$((FAILED + 1))
check_api_endpoint "http://localhost:8001/health" "Exchange API" || FAILED=$((FAILED + 1))
check_api_endpoint "http://localhost:9001/health" "Agent Coordinator" || FAILED=$((FAILED + 1))
echo ""
if [[ $FAILED -eq 0 ]]; then
success "All health checks passed"
return 0
else
error "$FAILED health check(s) failed"
return 1
if [[ -z "$address" ]]; then
log_error "Usage: $0 add-validator <address> [stake]"
exit 1
fi
log_info "Adding validator: $address (stake: $stake)"
cd "$AITBC_ROOT"
"$PYTHON_CMD" -c "
import sys
sys.path.insert(0, '/opt/aitbc/apps/blockchain-node/src')
from aitbc_chain.consensus.multi_validator_poa import MultiValidatorPoA
poa = MultiValidatorPoA(chain_id=1337)
success = poa.add_validator('$address', float($stake))
if success:
print(f'✅ Validator $address added successfully')
print(f'✅ Total validators: {len(poa.validators)}')
else:
print(f'❌ Failed to add validator $address')
"
}
# Show help
show_help() {
echo "AITBC Service Management Script"
echo "================================"
echo "AITBC Mesh Network Service Management"
echo "===================================="
echo ""
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " start [service] Start services (default: all)"
echo " stop [service] Stop services (default: all)"
echo " restart [service] Restart services (default: all)"
echo " status [service] Show service status (default: all)"
echo " logs <service> [n] Show service logs (default: 100 lines)"
echo " enable [service] Enable services (default: all)"
echo " disable [service] Disable services (default: all)"
echo " health-check Run health checks on all services"
echo " help Show this help"
echo " start Start all services"
echo " start-consensus Start consensus service only"
echo " start-network Start network service only"
echo " start-economics Start economic service only"
echo " start-agents Start agent services only"
echo " start-contracts Start contract services only"
echo " status Check service status"
echo " add-validator Add new validator"
echo " help Show this help"
echo ""
echo "Examples:"
echo " $0 start # Start all services"
echo " $0 start aitbc-blockchain-node # Start specific service"
echo " $0 status # Show status of all services"
echo " $0 logs aitbc-blockchain-node 50 # Show last 50 lines"
echo " $0 health-check # Run health checks"
echo ""
echo "Service Groups:"
echo " Core: postgresql, redis-server"
echo " Blockchain: blockchain-p2p, blockchain-node, blockchain-rpc, blockchain-sync"
echo " API: coordinator-api, exchange-api, agent-coordinator"
echo " Application: wallet, agent-daemon, marketplace, governance, trading"
echo " $0 status # Check status"
echo " $0 add-validator 0x123... # Add validator"
echo ""
}
# Main command handling
main() {
local COMMAND="${1:-help}"
shift || true
case "$COMMAND" in
"start")
start_services "$@"
;;
"stop")
stop_services "$@"
;;
"restart")
restart_services "$@"
;;
"status")
show_status "$@"
;;
"logs")
show_logs "$@"
;;
"enable")
enable_services "$@"
;;
"disable")
disable_services "$@"
;;
"health-check")
run_health_checks
;;
"help"|"-h"|"--help")
show_help
;;
*)
error "Unknown command: $COMMAND"
show_help
exit 1
;;
esac
}
# Handle script interruption
trap 'error "Script interrupted"' INT TERM
# Run main function
main "$@"
case "${1:-help}" in
"start")
log_info "Starting all AITBC Mesh Network services..."
start_consensus
start_network
start_economics
start_agents
start_contracts
log_info "🚀 All services started!"
;;
"start-consensus")
start_consensus
;;
"start-network")
start_network
;;
"start-economics")
start_economics
;;
"start-agents")
start_agents
;;
"start-contracts")
start_contracts
;;
"status")
check_status
;;
"add-validator")
add_validator "$2" "$3"
;;
"help"|"-h"|"--help")
show_help
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac