Spaces:
Sleeping
Sleeping
File size: 52,759 Bytes
4e3c158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 | #!/usr/bin/env python3
"""
Production Deployment Configuration
Advanced Workflow Automation - Production Readiness
This script implements:
- Production configuration management
- Environment setup and validation
- Database configuration and migration
- Security configuration for production
- Monitoring and logging setup
- Deployment automation
"""
from dataclasses import dataclass, field
from datetime import datetime
import json
import logging
import os
from pathlib import Path
import sys
from typing import Any, Dict, List, Optional
import uuid
import yaml
# Add backend directory to Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
logger = logging.getLogger(__name__)
@dataclass
class ProductionConfig:
"""Production configuration settings"""
environment: str = "production"
debug: bool = False
log_level: str = "INFO"
# Database Configuration
database_url: str = ""
database_pool_size: int = 20
database_max_overflow: int = 30
database_pool_timeout: int = 30
database_pool_recycle: int = 3600
# Redis Configuration (for caching and sessions)
redis_url: str = ""
redis_db: int = 0
redis_password: Optional[str] = None
redis_max_connections: int = 100
# WebSocket Configuration
websocket_host: str = "0.0.0.0"
websocket_port: int = 8765
websocket_ssl_enabled: bool = True
websocket_cert_file: str = ""
websocket_key_file: str = ""
# Security Configuration
secret_key: str = ""
jwt_secret_key: str = ""
jwt_expiration_hours: int = 24
session_timeout_minutes: int = 30
cors_origins: List[str] = field(default_factory=list)
rate_limit_enabled: bool = True
rate_limit_requests: int = 1000
rate_limit_window_minutes: int = 60
# Monitoring Configuration
prometheus_enabled: bool = True
prometheus_port: int = 9090
health_check_enabled: bool = True
health_check_port: int = 8080
metrics_collection_enabled: bool = True
log_analytics_enabled: bool = True
# Performance Configuration
max_concurrent_workflows: int = 1000
workflow_timeout_minutes: int = 60
task_queue_max_size: int = 10000
cache_ttl_seconds: int = 3600
# External Services Configuration
gmail_api_key: str = ""
slack_api_key: str = ""
github_api_key: str = ""
asana_api_key: str = ""
trello_api_key: str = ""
# Backup and Recovery
backup_enabled: bool = True
backup_schedule_hours: int = 24
backup_retention_days: int = 30
auto_recovery_enabled: bool = True
# Email Configuration
smtp_server: str = ""
smtp_port: int = 587
smtp_username: str = ""
smtp_password: str = ""
smtp_use_tls: bool = True
class ProductionDeploymentManager:
"""Manages production deployment and configuration"""
def __init__(self):
self.config = ProductionConfig()
self.deployment_path = Path("/opt/atom/production")
self.config_path = self.deployment_path / "config"
self.logs_path = self.deployment_path / "logs"
self.backups_path = self.deployment_path / "backups"
def setup_production_environment(self) -> Dict[str, Any]:
"""Setup production environment"""
try:
print("🚀 Setting Up Production Environment")
print("=" * 60)
# Create directory structure
self._create_directory_structure()
# Generate configuration files
self._generate_configuration_files()
# Setup security configuration
self._setup_security_configuration()
# Configure monitoring and logging
self._setup_monitoring_configuration()
# Setup database configuration
self._setup_database_configuration()
# Create deployment scripts
self._create_deployment_scripts()
# Setup health checks
self._setup_health_checks()
print("✅ Production environment setup completed")
return {"success": True, "message": "Production environment configured successfully"}
except Exception as e:
logger.error(f"Error setting up production environment: {str(e)}")
return {"success": False, "error": str(e)}
def _create_directory_structure(self):
"""Create production directory structure"""
print("\n📁 Creating Directory Structure...")
directories = [
self.deployment_path,
self.config_path,
self.logs_path,
self.backups_path,
self.deployment_path / "scripts",
self.deployment_path / "ssl",
self.deployment_path / "data",
self.deployment_path / "temp"
]
for directory in directories:
directory.mkdir(parents=True, exist_ok=True)
print(f" ✅ Created: {directory}")
def _generate_configuration_files(self):
"""Generate production configuration files"""
print("\n⚙️ Generating Configuration Files...")
# Main production config
config_data = {
"environment": self.config.environment,
"debug": self.config.debug,
"log_level": self.config.log_level,
"database": {
"url": self.config.database_url,
"pool_size": self.config.database_pool_size,
"max_overflow": self.config.database_max_overflow,
"pool_timeout": self.config.database_pool_timeout,
"pool_recycle": self.config.database_pool_recycle
},
"redis": {
"url": self.config.redis_url,
"db": self.config.redis_db,
"password": self.config.redis_password,
"max_connections": self.config.redis_max_connections
},
"websocket": {
"host": self.config.websocket_host,
"port": self.config.websocket_port,
"ssl_enabled": self.config.websocket_ssl_enabled,
"cert_file": self.config.websocket_cert_file,
"key_file": self.config.websocket_key_file
},
"security": {
"secret_key": self.config.secret_key or str(uuid.uuid4()),
"jwt_secret_key": self.config.jwt_secret_key or str(uuid.uuid4()),
"jwt_expiration_hours": self.config.jwt_expiration_hours,
"session_timeout_minutes": self.config.session_timeout_minutes,
"cors_origins": self.config.cors_origins,
"rate_limit_enabled": self.config.rate_limit_enabled,
"rate_limit_requests": self.config.rate_limit_requests,
"rate_limit_window_minutes": self.config.rate_limit_window_minutes
},
"monitoring": {
"prometheus_enabled": self.config.prometheus_enabled,
"prometheus_port": self.config.prometheus_port,
"health_check_enabled": self.config.health_check_enabled,
"health_check_port": self.config.health_check_port,
"metrics_collection_enabled": self.config.metrics_collection_enabled,
"log_analytics_enabled": self.config.log_analytics_enabled
},
"performance": {
"max_concurrent_workflows": self.config.max_concurrent_workflows,
"workflow_timeout_minutes": self.config.workflow_timeout_minutes,
"task_queue_max_size": self.config.task_queue_max_size,
"cache_ttl_seconds": self.config.cache_ttl_seconds
},
"backup": {
"enabled": self.config.backup_enabled,
"schedule_hours": self.config.backup_schedule_hours,
"retention_days": self.config.backup_retention_days,
"auto_recovery_enabled": self.config.auto_recovery_enabled
}
}
# Write YAML configuration
config_file = self.config_path / "production.yaml"
with open(config_file, 'w') as f:
yaml.dump(config_data, f, default_flow_style=False)
print(f" ✅ Created: {config_file}")
# Write JSON configuration (for Node.js services)
json_config_file = self.config_path / "production.json"
with open(json_config_file, 'w') as f:
json.dump(config_data, f, indent=2)
print(f" ✅ Created: {json_config_file}")
# Environment variables file
env_file = self.config_path / ".env"
env_content = f"""
# Production Environment Variables
ATOM_ENV={self.config.environment}
ATOM_DEBUG={self.config.debug}
ATOM_LOG_LEVEL={self.config.log_level}
# Database
DATABASE_URL={self.config.database_url}
DATABASE_POOL_SIZE={self.config.database_pool_size}
# Redis
REDIS_URL={self.config.redis_url}
REDIS_DB={self.config.redis_db}
# WebSocket
WEBSOCKET_HOST={self.config.websocket_host}
WEBSOCKET_PORT={self.config.websocket_port}
WEBSOCKET_SSL_ENABLED={self.config.websocket_ssl_enabled}
# Security
SECRET_KEY={config_data['security']['secret_key']}
JWT_SECRET_KEY={config_data['security']['jwt_secret_key']}
JWT_EXPIRATION_HOURS={self.config.jwt_expiration_hours}
# Monitoring
PROMETHEUS_ENABLED={self.config.prometheus_enabled}
PROMETHEUS_PORT={self.config.prometheus_port}
HEALTH_CHECK_ENABLED={self.config.health_check_enabled}
HEALTH_CHECK_PORT={self.config.health_check_port}
# Performance
MAX_CONCURRENT_WORKFLOWS={self.config.max_concurrent_workflows}
WORKFLOW_TIMEOUT_MINUTES={self.config.workflow_timeout_minutes}
# Backup
BACKUP_ENABLED={self.config.backup_enabled}
BACKUP_SCHEDULE_HOURS={self.config.backup_schedule_hours}
BACKUP_RETENTION_DAYS={self.config.backup_retention_days}
"""
with open(env_file, 'w') as f:
f.write(env_content.strip())
print(f" ✅ Created: {env_file}")
def _setup_security_configuration(self):
"""Setup security configuration"""
print("\n🔒 Setting Up Security Configuration...")
# Generate SSL certificate (self-signed for development)
ssl_config = {
"country": "US",
"state": "California",
"locality": "San Francisco",
"organization": "Atom Workflow Automation",
"common_name": "localhost",
"email": "noreply@atom.com"
}
ssl_config_file = self.config_path / "ssl_config.json"
with open(ssl_config_file, 'w') as f:
json.dump(ssl_config, f, indent=2)
print(f" ✅ Created: {ssl_config_file}")
# Nginx configuration for reverse proxy
nginx_config = """
server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name localhost;
ssl_certificate /opt/atom/production/ssl/cert.pem;
ssl_certificate_key /opt/atom/production/ssl/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# WebSocket proxy
location /ws {
proxy_pass http://localhost:8765;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket specific headers
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
# API proxy
location /api {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Health check
location /health {
proxy_pass http://localhost:8080;
access_log off;
}
# Static files
location /static {
alias /opt/atom/production/static;
expires 1y;
add_header Cache-Control "public, immutable";
}
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
}
"""
nginx_config_file = self.config_path / "nginx.conf"
with open(nginx_config_file, 'w') as f:
f.write(nginx_config.strip())
print(f" ✅ Created: {nginx_config_file}")
# Security policies configuration
security_policies = {
"password_policy": {
"min_length": 12,
"require_uppercase": True,
"require_lowercase": True,
"require_numbers": True,
"require_symbols": True,
"max_age_days": 90
},
"session_policy": {
"timeout_minutes": 30,
"max_concurrent_sessions": 3,
"require_reauth_minutes": 60
},
"api_policy": {
"rate_limit_per_minute": 100,
"rate_limit_per_hour": 1000,
"max_request_size_mb": 10,
"allowed_methods": ["GET", "POST", "PUT", "DELETE", "PATCH"],
"cors_policy": {
"allowed_origins": ["https://localhost"],
"allowed_methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
"allowed_headers": ["Authorization", "Content-Type", "X-Requested-With"],
"max_age_seconds": 3600
}
}
}
security_policies_file = self.config_path / "security_policies.json"
with open(security_policies_file, 'w') as f:
json.dump(security_policies, f, indent=2)
print(f" ✅ Created: {security_policies_file}")
def _setup_monitoring_configuration(self):
"""Setup monitoring and logging configuration"""
print("\n📊 Setting Up Monitoring Configuration...")
# Prometheus configuration
prometheus_config = """
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
- "workflow_alerts.yml"
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
scrape_configs:
- job_name: 'atom-workflow-api'
static_configs:
- targets: ['localhost:8000']
metrics_path: '/metrics'
scrape_interval: 30s
- job_name: 'atom-websocket-server'
static_configs:
- targets: ['localhost:8765']
metrics_path: '/metrics'
scrape_interval: 30s
- job_name: 'atom-health-checks'
static_configs:
- targets: ['localhost:8080']
metrics_path: '/metrics'
scrape_interval: 60s
- job_name: 'node-exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'redis-exporter'
static_configs:
- targets: ['localhost:9121']
- job_name: 'postgres-exporter'
static_configs:
- targets: ['localhost:9187']
"""
prometheus_config_file = self.config_path / "prometheus.yml"
with open(prometheus_config_file, 'w') as f:
f.write(prometheus_config.strip())
print(f" ✅ Created: {prometheus_config_file}")
# Workflow alerts configuration
workflow_alerts = """
groups:
- name: workflow_alerts
rules:
- alert: WorkflowExecutionFailure
expr: workflow_execution_failures_total > 0
for: 5m
labels:
severity: warning
annotations:
summary: "Workflow execution failed"
description: "Workflow {{ $labels.workflow_id }} has failed {{ $value }} times in the last 5 minutes"
- alert: HighWorkflowExecutionTime
expr: workflow_execution_duration_seconds > 300
for: 10m
labels:
severity: warning
annotations:
summary: "High workflow execution time"
description: "Workflow {{ $labels.workflow_id }} has been running for {{ $value }} seconds"
- alert: WebSocketConnectionFailure
expr: websocket_connection_errors_total > 10
for: 2m
labels:
severity: critical
annotations:
summary: "High WebSocket connection errors"
description: "{{ $value }} WebSocket connection errors in the last 2 minutes"
- alert: DatabaseConnectionFailure
expr: up{job="postgres-exporter"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Database connection failed"
description: "Database is down for more than 1 minute"
- alert: RedisConnectionFailure
expr: up{job="redis-exporter"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Redis connection failed"
description: "Redis is down for more than 1 minute"
- alert: HighMemoryUsage
expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes > 0.8
for: 5m
labels:
severity: warning
annotations:
summary: "High memory usage"
description: "Memory usage is {{ $value | humanizePercentage }}"
- alert: HighCPUUsage
expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 10m
labels:
severity: warning
annotations:
summary: "High CPU usage"
description: "CPU usage is {{ $value | humanizePercentage }}"
"""
workflow_alerts_file = self.config_path / "workflow_alerts.yml"
with open(workflow_alerts_file, 'w') as f:
f.write(workflow_alerts.strip())
print(f" ✅ Created: {workflow_alerts_file}")
# Grafana dashboard configuration
grafana_dashboard = {
"dashboard": {
"id": None,
"title": "Atom Workflow Automation Dashboard",
"tags": ["atom", "workflow", "automation"],
"timezone": "browser",
"panels": [
{
"id": 1,
"title": "Workflow Executions",
"type": "graph",
"targets": [
{
"expr": "rate(workflow_executions_total[5m])",
"legendFormat": "Executions/sec"
},
{
"expr": "rate(workflow_execution_failures_total[5m])",
"legendFormat": "Failures/sec"
}
],
"yAxes": [
{"label": "Rate per second"}
]
},
{
"id": 2,
"title": "WebSocket Connections",
"type": "stat",
"targets": [
{
"expr": "websocket_connections_active",
"legendFormat": "Active Connections"
}
]
},
{
"id": 3,
"title": "Workflow Execution Duration",
"type": "heatmap",
"targets": [
{
"expr": "workflow_execution_duration_seconds",
"legendFormat": "{{ workflow_id }}"
}
]
},
{
"id": 4,
"title": "System Resources",
"type": "graph",
"targets": [
{
"expr": "100 - (avg by(instance) (irate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
"legendFormat": "CPU %"
},
{
"expr": "(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100",
"legendFormat": "Memory %"
}
]
}
],
"time": {
"from": "now-1h",
"to": "now"
},
"refresh": "30s"
}
}
grafana_dashboard_file = self.config_path / "grafana_dashboard.json"
with open(grafana_dashboard_file, 'w') as f:
json.dump(grafana_dashboard, f, indent=2)
print(f" ✅ Created: {grafana_dashboard_file}")
# Logging configuration
logging_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"detailed": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
},
"json": {
"()": "pythonjsonlogger.jsonlogger.JsonFormatter",
"format": "%(asctime)s %(name)s %(levelname)s %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "detailed",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "json",
"filename": "/opt/atom/production/logs/atom.log",
"maxBytes": 10485760, # 10MB
"backupCount": 5
},
"workflow_file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "json",
"filename": "/opt/atom/production/logs/workflows.log",
"maxBytes": 10485760, # 10MB
"backupCount": 10
},
"websocket_file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "json",
"filename": "/opt/atom/production/logs/websocket.log",
"maxBytes": 10485760, # 10MB
"backupCount": 10
}
},
"loggers": {
"": {
"level": "INFO",
"handlers": ["console", "file"]
},
"atom.workflows": {
"level": "INFO",
"handlers": ["workflow_file"],
"propagate": False
},
"atom.websocket": {
"level": "INFO",
"handlers": ["websocket_file"],
"propagate": False
}
}
}
logging_config_file = self.config_path / "logging.yaml"
with open(logging_config_file, 'w') as f:
yaml.dump(logging_config, f)
print(f" ✅ Created: {logging_config_file}")
def _setup_database_configuration(self):
"""Setup database configuration"""
print("\n🗄️ Setting Up Database Configuration...")
# PostgreSQL configuration
postgres_config = """
# PostgreSQL Configuration for Atom Workflow Automation
# Connection Settings
listen_addresses = 'localhost'
port = 5432
max_connections = 200
# Memory Settings
shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 4MB
maintenance_work_mem = 64MB
# WAL Settings
wal_level = replica
max_wal_size = 1GB
min_wal_size = 80MB
checkpoint_completion_target = 0.9
# Query Performance
random_page_cost = 1.1
effective_io_concurrency = 200
# Logging Settings
log_statement = 'all'
log_min_duration_statement = 1000
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
# Security Settings
ssl = on
password_encryption = scram-sha-256
"""
postgres_config_file = self.config_path / "postgresql.conf"
with open(postgres_config_file, 'w') as f:
f.write(postgres_config.strip())
print(f" ✅ Created: {postgres_config_file}")
# Database migration script
migration_script = """
#!/bin/bash
# Database Migration Script for Atom Workflow Automation
set -e
echo "🗄️ Starting Database Migration..."
# Database connection parameters
DB_HOST="localhost"
DB_PORT="5432"
DB_NAME="atom_production"
DB_USER="atom_user"
DB_PASSWORD="CHANGE_THIS_PASSWORD"
# Create database if it doesn't exist
echo "📝 Creating database..."
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U postgres -c "CREATE DATABASE IF NOT EXISTS $DB_NAME;"
# Create user if it doesn't exist
echo "👤 Creating user..."
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U postgres -c "DO $$\\nBEGIN;\\nIF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '$DB_USER') THEN\\n CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';\\nEND IF;\\n$$;"
# Grant privileges
echo "🔐 Granting privileges..."
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
# Run migration files
echo "🔄 Running migrations..."
export PGPASSWORD=$DB_PASSWORD
# Create tables
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME << 'EOF'
-- Workflows table
CREATE TABLE IF NOT EXISTS workflows (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
description TEXT,
category VARCHAR(100),
user_id UUID NOT NULL,
parameters JSONB DEFAULT '{}',
template_id UUID,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
version INTEGER DEFAULT 1
);
-- Workflow executions table
CREATE TABLE IF NOT EXISTS workflow_executions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
workflow_id UUID NOT NULL REFERENCES workflows(id),
status VARCHAR(50) NOT NULL,
input_data JSONB DEFAULT '{}',
output_data JSONB DEFAULT '{}',
error_message TEXT,
execution_time_seconds DECIMAL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
started_at TIMESTAMP WITH TIME ZONE,
completed_at TIMESTAMP WITH TIME ZONE,
user_id UUID NOT NULL
);
-- Workflow steps table
CREATE TABLE IF NOT EXISTS workflow_steps (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
execution_id UUID NOT NULL REFERENCES workflow_executions(id),
step_order INTEGER NOT NULL,
service VARCHAR(100) NOT NULL,
action VARCHAR(100) NOT NULL,
parameters JSONB DEFAULT '{}',
status VARCHAR(50) NOT NULL,
result JSONB,
error_message TEXT,
execution_time_seconds DECIMAL,
started_at TIMESTAMP WITH TIME ZONE,
completed_at TIMESTAMP WITH TIME ZONE
);
-- Templates table
CREATE TABLE IF NOT EXISTS workflow_templates (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
description TEXT,
category VARCHAR(100),
author VARCHAR(100) NOT NULL,
version VARCHAR(50) NOT NULL,
parameters JSONB DEFAULT '{}',
steps JSONB NOT NULL,
tags TEXT[],
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Users table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
is_admin BOOLEAN DEFAULT FALSE,
last_login TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Sessions table
CREATE TABLE IF NOT EXISTS sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id),
session_token VARCHAR(255) UNIQUE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Audit log table
CREATE TABLE IF NOT EXISTS audit_log (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id),
action VARCHAR(100) NOT NULL,
resource_type VARCHAR(100),
resource_id UUID,
old_values JSONB,
new_values JSONB,
ip_address INET,
user_agent TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Integration catalog table
CREATE TABLE IF NOT EXISTS integration_catalog (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
category TEXT NOT NULL,
icon TEXT,
color TEXT DEFAULT '#6366F1',
auth_type TEXT DEFAULT 'none',
native_id TEXT,
triggers JSONB DEFAULT '[]',
actions JSONB DEFAULT '[]',
popular BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_workflows_user_id ON workflows(user_id);
CREATE INDEX IF NOT EXISTS idx_workflows_template_id ON workflows(template_id);
CREATE INDEX IF NOT EXISTS idx_workflow_executions_workflow_id ON workflow_executions(workflow_id);
CREATE INDEX IF NOT EXISTS idx_workflow_executions_user_id ON workflow_executions(user_id);
CREATE INDEX IF NOT EXISTS idx_workflow_executions_status ON workflow_executions(status);
CREATE INDEX IF NOT EXISTS idx_workflow_steps_execution_id ON workflow_steps(execution_id);
CREATE INDEX IF NOT EXISTS idx_workflow_steps_status ON workflow_steps(status);
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(session_token);
CREATE INDEX IF NOT EXISTS idx_audit_log_user_id ON audit_log(user_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_created_at ON audit_log(created_at);
CREATE INDEX IF NOT EXISTS idx_integration_catalog_category ON integration_catalog(category);
CREATE INDEX IF NOT EXISTS idx_integration_catalog_popular ON integration_catalog(popular);
EOF
echo "✅ Database migration completed successfully"
"""
migration_script_file = self.config_path / "migrate_database.sh"
with open(migration_script_file, 'w') as f:
f.write(migration_script.strip())
# Make script executable
os.chmod(migration_script_file, 0o755)
print(f" ✅ Created: {migration_script_file}")
# Redis configuration
redis_config = """
# Redis Configuration for Atom Workflow Automation
# Network
bind 127.0.0.1
port 6379
protected-mode yes
requirepass CHANGE_THIS_REDIS_PASSWORD
# Memory
maxmemory 512mb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
# Security
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG ""
# Performance
tcp-keepalive 300
timeout 0
# Logging
loglevel notice
logfile /var/log/redis/redis-server.log
# Clients
maxclients 10000
"""
redis_config_file = self.config_path / "redis.conf"
with open(redis_config_file, 'w') as f:
f.write(redis_config.strip())
print(f" ✅ Created: {redis_config_file}")
def _create_deployment_scripts(self):
"""Create deployment and management scripts"""
print("\n🚀 Creating Deployment Scripts...")
# Main deployment script
deploy_script = """#!/bin/bash
# Main Deployment Script for Atom Workflow Automation
set -e
DEPLOYMENT_PATH="/opt/atom/production"
BACKUP_PATH="/opt/atom/production/backups"
LOG_FILE="/opt/atom/production/logs/deploy.log"
echo "🚀 Starting Atom Workflow Automation Deployment..."
echo "$(date): Deployment started" >> $LOG_FILE
# Function to log messages
log() {
echo "$1"
echo "$(date): $1" >> $LOG_FILE
}
# Check if running as root
if [ "$EUID" -ne 0 ]; then
log "❌ This script must be run as root"
exit 1
fi
# Create backup if this is not a fresh deployment
if [ -d "$DEPLOYMENT_PATH" ] && [ "$(ls -A $DEPLOYMENT_PATH)" ]; then
log "📦 Creating backup..."
BACKUP_NAME="backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_PATH/$BACKUP_NAME"
cp -r $DEPLOYMENT_PATH/* "$BACKUP_PATH/$BACKUP_NAME/" 2>/dev/null || true
log "✅ Backup created: $BACKUP_NAME"
fi
# Stop existing services
log "🛑 Stopping existing services..."
systemctl stop atom-workflow-api || true
systemctl stop atom-websocket-server || true
systemctl stop atom-scheduler || true
# Update application code
log "📥 Updating application code..."
cd $DEPLOYMENT_PATH
if [ -d "git" ]; then
cd git
git pull origin main
cd ..
rsync -av --exclude '.git' git/ $DEPLOYMENT_PATH/
fi
# Install dependencies
log "📦 Installing Python dependencies..."
python3 -m pip install -r requirements.txt --upgrade
# Run database migrations
log "🗄️ Running database migrations..."
$DEPLOYMENT_PATH/config/migrate_database.sh
# Update configuration
log "⚙️ Updating configuration..."
if [ ! -f "$DEPLOYMENT_PATH/config/.env" ]; then
cp $DEPLOYMENT_PATH/config/.env.example $DEPLOYMENT_PATH/config/.env
log "⚠️ Please configure environment variables in $DEPLOYMENT_PATH/config/.env"
fi
# Build static assets
log "🎨 Building static assets..."
npm run build || echo "⚠️ npm build failed, continuing..."
# Set permissions
log "🔒 Setting permissions..."
chown -R atom:atom $DEPLOYMENT_PATH
chmod +x $DEPLOYMENT_PATH/scripts/*.sh
# Start services
log "🚀 Starting services..."
systemctl daemon-reload
systemctl enable atom-workflow-api
systemctl enable atom-websocket-server
systemctl enable atom-scheduler
systemctl start atom-workflow-api
systemctl start atom-websocket-server
systemctl start atom-scheduler
# Health check
log "🏥 Running health checks..."
sleep 10
if curl -f http://localhost:8080/health > /dev/null 2>&1; then
log "✅ Health check passed"
else
log "❌ Health check failed"
echo "$(date): Health check failed" >> $LOG_FILE
exit 1
fi
log "🎉 Deployment completed successfully!"
echo "$(date): Deployment completed" >> $LOG_FILE
# Display status
systemctl status atom-workflow-api --no-pager -l
systemctl status atom-websocket-server --no-pager -l
systemctl status atom-scheduler --no-pager -l
"""
deploy_script_file = self.deployment_path / "scripts" / "deploy.sh"
with open(deploy_script_file, 'w') as f:
f.write(deploy_script.strip())
os.chmod(deploy_script_file, 0o755)
print(f" ✅ Created: {deploy_script_file}")
# Systemd service files
workflow_api_service = """[Unit]
Description=Atom Workflow API
After=network.target postgresql.service redis.service
[Service]
Type=exec
User=atom
Group=atom
WorkingDirectory=/opt/atom/production
Environment=PATH=/opt/atom/production/venv/bin
ExecStart=/opt/atom/production/venv/bin/python -m uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
"""
workflow_api_service_file = self.deployment_path / "scripts" / "atom-workflow-api.service"
with open(workflow_api_service_file, 'w') as f:
f.write(workflow_api_service.strip())
print(f" ✅ Created: {workflow_api_service_file}")
websocket_server_service = """[Unit]
Description=Atom WebSocket Server
After=network.target postgresql.service redis.service
[Service]
Type=exec
User=atom
Group=atom
WorkingDirectory=/opt/atom/production
Environment=PATH=/opt/atom/production/venv/bin
ExecStart=/opt/atom/production/venv/bin/python websocket_server.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
"""
websocket_server_service_file = self.deployment_path / "scripts" / "atom-websocket-server.service"
with open(websocket_server_service_file, 'w') as f:
f.write(websocket_server_service.strip())
print(f" ✅ Created: {websocket_server_service_file}")
# Monitoring script
monitoring_script = """#!/bin/bash
# Monitoring Script for Atom Workflow Automation
DEPLOYMENT_PATH="/opt/atom/production"
LOG_FILE="/opt/atom/production/logs/monitoring.log"
log() {
echo "$1"
echo "$(date): $1" >> $LOG_FILE
}
# Check service status
check_service() {
local service=$1
if systemctl is-active --quiet $service; then
log "✅ $service is running"
else
log "❌ $service is not running"
systemctl restart $service
fi
}
# Check system resources
check_resources() {
# CPU usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\\([0-9.]*\\)%* id.*/\\1/" | awk '{print 100 - $1}')
if (( $(echo "$CPU_USAGE > 80" | bc -l) )); then
log "⚠️ High CPU usage: ${CPU_USAGE}%"
fi
# Memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{printf("%.2f", $3/$2 * 100.0)}')
if (( $(echo "$MEMORY_USAGE > 80" | bc -l) )); then
log "⚠️ High memory usage: ${MEMORY_USAGE}%"
fi
# Disk usage
DISK_USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $DISK_USAGE -gt 80 ]; then
log "⚠️ High disk usage: ${DISK_USAGE}%"
fi
}
# Check connectivity
check_connectivity() {
# Database
if PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "SELECT 1;" > /dev/null 2>&1; then
log "✅ Database connection is OK"
else
log "❌ Database connection failed"
fi
# Redis
if redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD ping > /dev/null 2>&1; then
log "✅ Redis connection is OK"
else
log "❌ Redis connection failed"
fi
# WebSocket
if curl -f http://localhost:8765/health > /dev/null 2>&1; then
log "✅ WebSocket server is responding"
else
log "❌ WebSocket server is not responding"
fi
}
log "🔍 Starting system monitoring..."
# Check services
check_service "atom-workflow-api"
check_service "atom-websocket-server"
check_service "atom-scheduler"
# Check resources
check_resources
# Check connectivity
check_connectivity
log "✅ Monitoring completed"
"""
monitoring_script_file = self.deployment_path / "scripts" / "monitor.sh"
with open(monitoring_script_file, 'w') as f:
f.write(monitoring_script.strip())
os.chmod(monitoring_script_file, 0o755)
print(f" ✅ Created: {monitoring_script_file}")
# Backup script
backup_script = """#!/bin/bash
# Backup Script for Atom Workflow Automation
BACKUP_PATH="/opt/atom/production/backups"
DB_BACKUP_PATH="$BACKUP_PATH/database"
CONFIG_BACKUP_PATH="$BACKUP_PATH/config"
LOG_FILE="/opt/atom/production/logs/backup.log"
log() {
echo "$1"
echo "$(date): $1" >> $LOG_FILE
}
# Create backup directories
mkdir -p $DB_BACKUP_PATH
mkdir -p $CONFIG_BACKUP_PATH
log "📦 Starting backup process..."
# Database backup
log "🗄️ Creating database backup..."
DB_NAME="atom_production"
DB_USER="atom_user"
DB_PASSWORD="CHANGE_THIS_PASSWORD"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PGPASSWORD=$DB_PASSWORD pg_dump -h localhost -U $DB_USER -d $DB_NAME | gzip > "$DB_BACKUP_PATH/db_backup_$TIMESTAMP.sql.gz"
# Configuration backup
log "⚙️ Creating configuration backup..."
tar -czf "$CONFIG_BACKUP_PATH/config_backup_$TIMESTAMP.tar.gz" /opt/atom/production/config/
# Application backup
log "📱 Creating application backup..."
tar -czf "$BACKUP_PATH/app_backup_$TIMESTAMP.tar.gz" /opt/atom/production/ --exclude=/opt/atom/production/logs --exclude=/opt/atom/production/backups --exclude=/opt/atom/production/temp
# Cleanup old backups (keep last 30 days)
log "🧹 Cleaning up old backups..."
find $BACKUP_PATH -name "*.gz" -mtime +30 -delete
log "✅ Backup completed successfully"
log "📊 Backup size: $(du -sh $BACKUP_PATH | cut -f1)"
"""
backup_script_file = self.deployment_path / "scripts" / "backup.sh"
with open(backup_script_file, 'w') as f:
f.write(backup_script.strip())
os.chmod(backup_script_file, 0o755)
print(f" ✅ Created: {backup_script_file}")
def _setup_health_checks(self):
"""Setup health check endpoints"""
print("\n🏥 Setting Up Health Checks...")
health_check_server = """
#!/usr/bin/env python3
"""
Health Check Server for Atom Workflow Automation
"""
import os
import sys
import json
import asyncio
import aiohttp
try:
import psycopg2
PSYCOPG2_AVAILABLE = True
except ImportError:
PSYCOPG2_AVAILABLE = False
try:
import redis
REDIS_AVAILABLE = True
except ImportError:
REDIS_AVAILABLE = False
from datetime import datetime
from pathlib import Path
# Add deployment path to Python path
sys.path.append('/opt/atom/production')
class HealthCheckServer:
def __init__(self):
self.port = 8080
self.db_url = os.getenv('DATABASE_URL', '')
self.redis_url = os.getenv('REDIS_URL', '')
async def health_check(self, request):
"""Main health check endpoint"""
status = {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"version": "1.0.0",
"checks": {}
}
overall_healthy = True
# Database health check
if PSYCOPG2_AVAILABLE and self.db_url:
try:
conn = psycopg2.connect(self.db_url)
cursor = conn.cursor()
cursor.execute("SELECT 1")
cursor.close()
conn.close()
status["checks"]["database"] = {"status": "healthy", "message": "Database connection successful"}
except Exception as e:
status["checks"]["database"] = {"status": "unhealthy", "message": str(e)}
overall_healthy = False
else:
status["checks"]["database"] = {"status": "unknown", "message": "psycopg2 not installed or database URL missing"}
# Redis health check
if REDIS_AVAILABLE and self.redis_url:
try:
r = redis.from_url(self.redis_url)
r.ping()
status["checks"]["redis"] = {"status": "healthy", "message": "Redis connection successful"}
except Exception as e:
status["checks"]["redis"] = {"status": "unhealthy", "message": str(e)}
overall_healthy = False
else:
status["checks"]["redis"] = {"status": "unknown", "message": "redis-py not installed or Redis URL missing"}
# WebSocket server health check
try:
async with aiohttp.ClientSession() as session:
async with session.get('http://localhost:8765/health', timeout=5) as response:
if response.status == 200:
status["checks"]["websocket"] = {"status": "healthy", "message": "WebSocket server responding"}
else:
raise Exception(f"WebSocket server returned status {response.status}")
except Exception as e:
status["checks"]["websocket"] = {"status": "unhealthy", "message": str(e)}
overall_healthy = False
# API server health check
try:
async with aiohttp.ClientSession() as session:
async with session.get('http://localhost:8000/health', timeout=5) as response:
if response.status == 200:
status["checks"]["api"] = {"status": "healthy", "message": "API server responding"}
else:
raise Exception(f"API server returned status {response.status}")
except Exception as e:
status["checks"]["api"] = {"status": "unhealthy", "message": str(e)}
overall_healthy = False
# System resources check
try:
import psutil
cpu_percent = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
disk = psutil.disk_usage('/')
resources = {
"cpu_percent": cpu_percent,
"memory_percent": memory.percent,
"disk_percent": (disk.used / disk.total) * 100
}
# Check if resources are within acceptable limits
if cpu_percent < 80 and memory.percent < 80 and resources["disk_percent"] < 80:
status["checks"]["resources"] = {"status": "healthy", "data": resources}
else:
status["checks"]["resources"] = {"status": "warning", "data": resources}
except Exception as e:
status["checks"]["resources"] = {"status": "unhealthy", "message": str(e)}
overall_healthy = False
# Set overall status
if not overall_healthy:
status["status"] = "unhealthy"
# Return appropriate HTTP status
http_status = 200 if overall_healthy else 503
return web.json_response(status, status=http_status)
async def ready_check(self, request):
"""Readiness check endpoint"""
return web.json_response({
"status": "ready",
"timestamp": datetime.now().isoformat()
})
async def live_check(self, request):
"""Liveness check endpoint"""
return web.json_response({
"status": "alive",
"timestamp": datetime.now().isoformat()
})
async def start_server(self):
"""Start the health check server"""
app = web.Application()
app.router.add_get('/health', self.health_check)
app.router.add_get('/ready', self.ready_check)
app.router.add_get('/live', self.live_check)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', self.port)
await site.start()
print(f"🏥 Health check server started on port {self.port}")
if __name__ == '__main__':
health_server = HealthCheckServer()
asyncio.run(health_server.start_server())
"""
health_check_file = self.deployment_path / "health_check_server.py"
with open(health_check_file, 'w') as f:
f.write(health_check_server.strip())
os.chmod(health_check_file, 0o755)
print(f" ✅ Created: {health_check_file}")
def create_cron_jobs(self):
"""Create cron jobs for maintenance tasks"""
print("\n⏰ Creating Cron Jobs...")
crontab_content = """
# Cron Jobs for Atom Workflow Automation
# Edit with: crontab -e -u atom
# Backup every day at 2 AM
0 2 * * * /opt/atom/production/scripts/backup.sh >> /opt/atom/production/logs/backup.log 2>&1
# Monitoring every 5 minutes
*/5 * * * * /opt/atom/production/scripts/monitor.sh >> /opt/atom/production/logs/monitoring.log 2>&1
# Log rotation every day at 3 AM
0 3 * * * /usr/sbin/logrotate /opt/atom/production/config/logrotate.conf
# Database maintenance every Sunday at 4 AM
0 4 * * 0 psql -h localhost -U atom_user -d atom_production -c "VACUUM ANALYZE;" >> /opt/atom/production/logs/maintenance.log 2>&1
# Clean up temp files every hour
0 * * * * find /opt/atom/production/temp -type f -mtime +1 -delete
"""
crontab_file = self.config_path / "crontab.txt"
with open(crontab_file, 'w') as f:
f.write(crontab_content.strip())
print(f" ✅ Created: {crontab_file}")
# Log rotation configuration
logrotate_config = """
/opt/atom/production/logs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 644 atom atom
postrotate
systemctl reload atom-workflow-api || true
systemctl reload atom-websocket-server || true
endscript
}
/var/log/postgresql/*.log {
weekly
missingok
rotate 8
compress
delaycompress
notifempty
create 644 postgres postgres
postrotate
systemctl reload postgresql || true
endscript
}
/var/log/redis/redis-server.log {
weekly
missingok
rotate 8
compress
delaycompress
notifempty
create 644 redis redis
postrotate
systemctl reload redis || true
endscript
}
"""
logrotate_file = self.config_path / "logrotate.conf"
with open(logrotate_file, 'w') as f:
f.write(logrotate_config.strip())
print(f" ✅ Created: {logrotate_file}")
def main():
"""Main deployment setup"""
print("🚀 PRODUCTION DEPLOYMENT SETUP")
print("=" * 80)
print("Setting up production environment for Atom Workflow Automation")
print("=" * 80)
try:
# Check running user
if os.geteuid() != 0:
print("❌ This script must be run as root (use sudo)")
return {"success": False, "error": "Root privileges required"}
# Create deployment manager
deployment_manager = ProductionDeploymentManager()
# Setup production environment
result = deployment_manager.setup_production_environment()
if result.get("success"):
print("\n" + "=" * 80)
print("🎉 PRODUCTION SETUP COMPLETED SUCCESSFULLY!")
print("=" * 80)
print("\n📋 Next Steps:")
print("1. Configure environment variables in /opt/atom/production/config/.env")
print("2. Run database migration: /opt/atom/production/config/migrate_database.sh")
print("3. Install systemd services: cp /opt/atom/production/scripts/*.service /etc/systemd/system/")
print("4. Reload systemd: systemctl daemon-reload")
print("5. Deploy application: /opt/atom/production/scripts/deploy.sh")
print("6. Setup monitoring: cp /opt/atom/production/config/*.yml /etc/prometheus/")
print("7. Setup cron jobs: crontab -e -u atom (paste content from /opt/atom/production/config/crontab.txt)")
print("\n🔍 Verification Commands:")
print(" curl http://localhost:8080/health")
print(" curl http://localhost:8000/health")
print(" curl http://localhost:8765/health")
print("\n📊 Monitoring URLs:")
print(" Prometheus: http://localhost:9090")
print(" Grafana: http://localhost:3000")
print("\n🔧 Management Commands:")
print(" Deploy: /opt/atom/production/scripts/deploy.sh")
print(" Monitor: /opt/atom/production/scripts/monitor.sh")
print(" Backup: /opt/atom/production/scripts/backup.sh")
else:
print(f"\n❌ Production setup failed: {result.get('error')}")
return result
except Exception as e:
print(f"\n❌ Setup failed with exception: {str(e)}")
logger.error(f"Production setup failed: {str(e)}")
return {"success": False, "error": str(e)}
if __name__ == "__main__":
result = main()
sys.exit(0 if result.get("success") else 1) |