-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.py
More file actions
1189 lines (1015 loc) · 37.1 KB
/
Copy pathdeploy.py
File metadata and controls
1189 lines (1015 loc) · 37.1 KB
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
#!/usr/bin/env python3
"""
Complete Deployment Automation Script for Enhanced Deforum Music Generator
Handles the entire deployment pipeline from setup to production deployment.
Usage
python deploy.py --target [local|docker|aws|k8s] --environment [dev|staging|prod]
python deploy.py --setup-project --target local
python deploy.py --build-all --push-registry
"""
import os
import sys
from pathlib import Path
# 🔧 Ensure "src" is added to sys.path so imports work
project_root = Path(__file__).resolve().parent
src_path = project_root / "src"
if str(src_path) not in sys.path:
sys.path.insert(0, str(src_path))
import json
import yaml
import shutil
import subprocess
import tempfile
import argparse
import logging
from typing import Dict, List, Optional, Any
import time
# ✅ Now this will work
from enhanced_deforum_music_generator.config.config_system import Config
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class DeploymentManager:
"""Manages complete deployment pipeline."""
def __init__(self, target: str = "local", environment: str = "dev"):
self.target = target
self.environment = environment
self.project_root = Path.cwd()
self.config = self._load_config()
# Deployment configurations
self.deployment_configs = {
"local": self._get_default_config,
"docker": self._get_docker_compose,
"aws": self._get_k8s_configmap,
"k8s": self._get_k8s_configmap,
"azure": self._get_default_config,
}
def _load_config(self) -> Dict[str, Any]:
"""Load deployment configuration."""
config_file = self.project_root / "config.json"
if config_file.exists():
with open(config_file, "r", encoding="utf-8") as f:
try:
return json.load(f)
except json.JSONDecodeError:
logger.warning("config.json exists but is invalid JSON; regenerating defaults.")
return self._get_default_config()
def _get_default_config(self) -> Dict[str, Any]:
"""Get default configuration."""
return {
"version": "2.0.0",
"app_name": "enhanced_deforum_music_generator",
"docker_registry": "your-registry.com",
"environments": {
"dev": {"replicas": 1, "resources": {"cpu": "1", "memory": "2Gi"}},
"staging": {"replicas": 2, "resources": {"cpu": "2", "memory": "4Gi"}},
"prod": {"replicas": 3, "resources": {"cpu": "4", "memory": "8Gi"}},
},
"features": {
"audio_analysis": True,
"lyrics_transcription": True,
"advanced_nlp": True,
"web_api": True,
},
}
def create_project_structure(self) -> bool:
"""Create complete project structure."""
logger.info("Creating project structure...")
directories = [
"src",
"scripts",
"tests",
"docs",
"deployment/docker",
"deployment/kubernetes",
"deployment/aws",
"deployment/azure",
"deployment/monitoring",
"deployment/nginx/ssl",
"integrations/automatic1111",
"integrations/comfyui",
"integrations/desktop",
"integrations/cloud",
"presets/styles",
"presets/mappings",
"presets/templates",
"data/models",
"data/cache",
"data/logs",
"data/temp",
"output/packages",
"output/analysis",
"output/previews",
"tools",
]
for directory in directories:
dir_path = self.project_root / directory
dir_path.mkdir(parents=True, exist_ok=True)
logger.info(f"Created directory: {directory}")
self._create_essential_files()
return True
def _create_essential_files(self):
"""Create essential configuration files."""
files_to_create = {
".gitignore": self._get_gitignore_content(),
".dockerignore": self._get_dockerignore_content(),
"requirements.txt": self._get_requirements_content(),
"requirements-minimal.txt": self._get_minimal_requirements(),
"requirements-full.txt": self._get_full_requirements(),
"config.json": json.dumps(self.config, indent=2),
".env.template": self._get_env_template(),
"docker-compose.yml": self._get_docker_compose(),
"start.sh": self._get_startup_script("unix"),
"start.bat": self._get_startup_script("windows"),
}
# Ensure custom.yaml exists (either root or config folder)
custom_yaml_root = self.project_root / "src" / "enhanced_deforum_music_generator" / "custom.yaml"
custom_yaml_config = self.project_root / "src" / "enhanced_deforum_music_generator" / "config" / "custom.yaml"
vendored_yaml_root = self.project_root / "studio" / "edmg-studio" / "python_backend" / "enhanced_deforum_music_generator" / "custom.yaml"
vendored_yaml_config = self.project_root / "studio" / "edmg-studio" / "python_backend" / "enhanced_deforum_music_generator" / "config" / "custom.yaml"
if not any(p.exists() for p in (custom_yaml_root, custom_yaml_config, vendored_yaml_root, vendored_yaml_config)):
logger.info("No custom.yaml found. Generating default one...")
default_yaml = """# Default custom.yaml for Enhanced Deforum Music Generator
interface:
server_port: 7860
auto_open_browser: true
enable_sharing: false
show_error_details: true
generation:
fps: 30
duration: 60
resolution: "512x512"
sampler_name: "Euler a"
steps: 20
negative_prompt: ""
advanced:
enable_multitrack: false
enable_style_transfer: false
enable_preview: true
enable_cloud_storage: false
cloud_provider: "local"
"""
# Write into root
custom_yaml_root.write_text(default_yaml)
logger.info(f"Created default custom.yaml at {custom_yaml_root}")
for filename, content in files_to_create.items():
file_path = self.project_root / filename
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)
# Make shell scripts executable
if filename.endswith(".sh"):
try:
os.chmod(file_path, 0o755)
except Exception:
pass
logger.info(f"Created file: {filename}")
def _get_gitignore_content(self) -> str:
return """# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual environments
venv/
env/
ENV/
# IDE
.vscode/
.idea/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Application specific
data/models/
data/cache/
data/temp/
data/logs/
output/
*.wav
*.mp3
*.mp4
*.avi
# Configuration
.env
config.local.json
# Docker
.dockerignore
# Temporary files
*.tmp
*.temp
"""
def _get_dockerignore_content(self) -> str:
return """data/
output/
logs/
.git/
.pytest_cache/
.coverage
*.pyc
*.pyo
*.pyd
__pycache__/
.venv/
venv/
.env
README.md
docs/
tests/
"""
def _get_requirements_content(self) -> str:
return """# Core dependencies for Enhanced Deforum Music Generator
gradio>=4.44.0
numpy>=1.23.0
scipy>=1.10.0
requests>=2.25.0
librosa>=0.10.0
soundfile>=0.12.0
# AI/ML features
openai-whisper>=20230314
torch>=1.13.0
transformers>=4.30.0
spacy>=3.5.0
textblob>=0.17.0
nltk>=3.8.0
# Web API
fastapi>=0.95.0
uvicorn[standard]>=0.22.0
# Utilities
python-multipart>=0.0.5
python-dotenv>=0.21.0
pydantic>=1.10.0
"""
def _get_minimal_requirements(self) -> str:
return """gradio>=4.44.0
numpy>=1.23.0
scipy>=1.10.0
requests>=2.25.0
"""
def _get_full_requirements(self) -> str:
return """gradio>=4.44.0
numpy>=1.23.0
scipy>=1.10.0
requests>=2.25.0
librosa>=0.10.0
soundfile>=0.12.0
openai-whisper>=20230314
torch>=1.13.0
transformers>=4.30.0
spacy>=3.5.0
textblob>=0.17.0
nltk>=3.8.0
fastapi>=0.95.0
uvicorn[standard]>=0.22.0
matplotlib>=3.7.0
opencv-python>=4.8.0
psutil>=5.9.0
boto3>=1.28.0
azure-storage-blob>=12.16.0
"""
def _get_env_template(self) -> str:
return """# Enhanced Deforum Music Generator Configuration
# API Keys
ANTHROPIC_API_KEY=your_anthropic_key_here
OPENAI_API_KEY=your_openai_key_here
# Server Configuration
HOST=0.0.0.0
PORT=7860
WORKERS=4
# Processing Limits
MAX_AUDIO_DURATION=600
MAX_CONCURRENT_JOBS=3
ENABLE_GPU=true
# Cloud Storage (Optional)
AWS_ACCESS_KEY_ID=your_aws_key
AWS_SECRET_ACCESS_KEY=your_aws_secret
AWS_REGION=us-west-2
S3_BUCKET=your-bucket-name
AZURE_STORAGE_CONNECTION_STRING=your_azure_connection
# Feature Flags
ENABLE_CLOUD_STORAGE=false
ENABLE_MULTITRACK=false
ENABLE_STYLE_TRANSFER=false
ENABLE_WEB_API=true
# Development
DEBUG=false
LOG_LEVEL=INFO
ENABLE_PROFILING=false
"""
def _get_docker_compose(self) -> str:
return """version: '3.8'
services:
deforum-music:
build:
context: .
dockerfile: deployment/docker/Dockerfile
ports:
- "7860:7860"
volumes:
- ./data:/app/data:rw
- ./output:/app/output:rw
- ./logs:/app/logs:rw
- ./presets:/app/presets:rw
- ./config.json:/app/config.json:ro
environment:
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
- MAX_AUDIO_DURATION=${MAX_AUDIO_DURATION:-600}
- LOG_LEVEL=${LOG_LEVEL:-INFO}
restart: unless-stopped
healthcheck:
test: ["CMD", "python", "-c", "import requests; requests.get('http://localhost:7860/health', timeout=5)"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
deploy:
resources:
limits:
memory: 4G
cpus: '2.0'
reservations:
memory: 2G
cpus: '1.0'
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./deployment/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./deployment/nginx/ssl:/etc/nginx/ssl:ro
depends_on:
- deforum-music
restart: unless-stopped
profiles:
- production
redis:
image: redis:alpine
restart: unless-stopped
volumes:
- redis_data:/data
profiles:
- scaling
volumes:
redis_data:
"""
def _get_startup_script(self, platform: str) -> str:
if platform == "unix":
# Directly starts the Gradio interface without any extra questions.
return """#!/bin/bash
# Enhanced Deforum Music Generator Startup Script (Gradio UI)
set -e
echo "🎵 Enhanced Deforum Music Generator 🎥"
echo "=================================="
# Check if virtual environment exists
if [ -d "venv" ]; then
echo "📦 Activating virtual environment..."
source venv/bin/activate
fi
# Load environment variables
if [ -f ".env" ]; then
echo "⚙️ Loading environment variables..."
export $(grep -v '^#' .env | xargs)
fi
# Create necessary directories
mkdir -p data/{models,cache,logs,temp} output/{packages,analysis,previews}
echo "🚀 Starting Gradio UI..."
python -c "from enhanced_deforum_music_generator.interface.gradio_interface import create_interface; app=create_interface(); app.launch(server_name='0.0.0.0', server_port=7860)"
echo "👋 Application stopped."
"""
else: # Windows
return r"""@echo off
REM Enhanced Deforum Music Generator Startup Script (Gradio UI)
echo 🎵 Enhanced Deforum Music Generator 🎥
echo ==================================
REM Check if virtual environment exists
if exist "venv\Scripts\activate.bat" (
echo 📦 Activating virtual environment...
call venv\Scripts\activate.bat
)
REM Load environment variables if .env exists (PowerShell users can do this differently)
if exist ".env" (
for /f "usebackq delims=" %%A in (".env") do (
set "%%A"
)
)
REM Create necessary directories
if not exist "data" mkdir data
if not exist "data\models" mkdir data\models
if not exist "data\cache" mkdir data\cache
if not exist "data\logs" mkdir data\logs
if not exist "output" mkdir output
if not exist "output\packages" mkdir output\packages
if not exist "output\analysis" mkdir output\analysis
if not exist "output\previews" mkdir output\previews
echo 🚀 Starting Gradio UI...
python -c "from enhanced_deforum_music_generator.interface.gradio_interface import create_interface; app=create_interface(); app.launch(server_name='0.0.0.0', server_port=7860)"
echo 👋 Application stopped.
pause
"""
# ---------------------------
# Local Deployment (Conda-aware)
# ---------------------------
def deploy_local(self) -> bool:
"""Deploy locally for development (prefers Conda env if available)."""
logger.info("Deploying locally...")
# Prefer Conda environment if environment-cuda.yml or environment.yml exists
env_file = None
for candidate in ["environment-cuda.yml", "environment.yml"]:
if (self.project_root / candidate).exists():
env_file = candidate
break
if env_file:
logger.info(f"Using Conda environment from {env_file}")
# Find env name inside file (fallback to deforum-music)
env_name = "deforum-music"
try:
with open(self.project_root / env_file, "r", encoding="utf-8") as f:
doc = yaml.safe_load(f) or {}
if isinstance(doc, dict) and "name" in doc and doc["name"]:
env_name = doc["name"]
except Exception:
pass
# Create or update env
try:
subprocess.run(
["conda", "env", "update", "-n", env_name, "-f", env_file, "--prune"],
check=True,
)
logger.info(f"Updated existing Conda env: {env_name}")
except subprocess.CalledProcessError:
subprocess.run(
["conda", "env", "create", "-n", env_name, "-f", env_file],
check=True,
)
logger.info(f"Created Conda env: {env_name}")
logger.info(
f"✅ Conda environment {env_name} ready. Activate with: conda activate {env_name}"
)
else:
logger.info("No Conda environment file found, falling back to venv...")
# Old venv method
if not (self.project_root / "venv").exists():
logger.info("Creating virtual environment with venv...")
subprocess.run([sys.executable, "-m", "venv", "venv"], check=True)
pip_path = "venv/bin/pip" if os.name != "nt" else "venv\\Scripts\\pip"
logger.info("Installing requirements into venv...")
subprocess.run([pip_path, "install", "-r", "requirements.txt"], check=True)
logger.info("Local deployment setup complete! Use start script to launch UI.")
return True
def deploy_docker(self) -> bool:
"""Deploy using Docker."""
logger.info("Deploying with Docker...")
# Create Dockerfile (runs Gradio interface directly)
dockerfile_content = """FROM python:3.10-slim
# System dependencies
RUN apt-get update && apt-get install -y \\
ffmpeg \\
libsndfile1-dev \\
gcc \\
g++ \\
curl \\
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy requirements first for better caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Download models (best-effort)
RUN python -c "import spacy; spacy.cli.download('en_core_web_sm')" || echo "spaCy download failed"
# Create non-root user
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
# Copy application
COPY --chown=appuser:appuser src/ ./src/
COPY --chown=appuser:appuser presets/ ./presets/
COPY --chown=appuser:appuser config.json ./
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \\
CMD ["python", "-c", "import requests; requests.get('http://localhost:7860/health', timeout=5)"]
EXPOSE 7860
# Launch Gradio UI directly
CMD ["python", "-c", "from enhanced_deforum_music_generator.interface.gradio_interface import create_interface; app=create_interface(); app.launch(server_name='0.0.0.0', server_port=7860)"]
"""
dockerfile_path = self.project_root / "deployment" / "docker" / "Dockerfile"
dockerfile_path.parent.mkdir(parents=True, exist_ok=True)
with open(dockerfile_path, "w", encoding="utf-8") as f:
f.write(dockerfile_content)
# Build image
image_tag = f"{self.config['app_name']}:{self.config['version']}"
logger.info(f"Building Docker image: {image_tag}")
build_cmd = ["docker", "build", "-t", image_tag, "-f", str(dockerfile_path), "."]
result = subprocess.run(build_cmd, capture_output=True, text=True)
if result.returncode != 0:
logger.error(f"Docker build failed: {result.stderr}")
return False
logger.info("Docker image built successfully!")
logger.info(f"Run with: docker run -p 7860:7860 {image_tag}")
return True
def deploy_kubernetes(self) -> bool:
"""Deploy to Kubernetes."""
logger.info("Deploying to Kubernetes...")
k8s_manifests = {
"configmap.yaml": self._get_k8s_configmap(),
"deployment.yaml": self._get_k8s_deployment(),
"service.yaml": self._get_k8s_service(),
"ingress.yaml": self._get_k8s_ingress(),
}
k8s_dir = self.project_root / "deployment" / "kubernetes"
k8s_dir.mkdir(parents=True, exist_ok=True)
for filename, content in k8s_manifests.items():
with open(k8s_dir / filename, "w", encoding="utf-8") as f:
f.write(content)
logger.info(f"Created {filename}")
# Apply manifests
logger.info("Applying Kubernetes manifests...")
apply_cmd = ["kubectl", "apply", "-f", str(k8s_dir)]
result = subprocess.run(apply_cmd, capture_output=True, text=True)
if result.returncode != 0:
logger.error(f"Kubernetes deployment failed: {result.stderr}")
return False
logger.info("Kubernetes deployment successful!")
return True
def _get_k8s_deployment(self) -> str:
env_config = self.config["environments"][self.environment]
return f"""apiVersion: apps/v1
kind: Deployment
metadata:
name: {self.config['app_name']}
labels:
app: {self.config['app_name']}
version: {self.config['version']}
environment: {self.environment}
spec:
replicas: {env_config['replicas']}
selector:
matchLabels:
app: {self.config['app_name']}
template:
metadata:
labels:
app: {self.config['app_name']}
spec:
containers:
- name: {self.config['app_name']}
image: {self.config['docker_registry']}/{self.config['app_name']}:{self.config['version']}
ports:
- containerPort: 7860
env:
- name: MAX_AUDIO_DURATION
value: "600"
- name: LOG_LEVEL
value: "INFO"
resources:
limits:
cpu: {env_config['resources']['cpu']}
memory: {env_config['resources']['memory']}
requests:
cpu: "{max(1, int(str(env_config['resources']['cpu']).split()[0]))}"
memory: {env_config['resources']['memory']}
livenessProbe:
httpGet:
path: /health
port: 7860
initialDelaySeconds: 60
periodSeconds: 30
readinessProbe:
httpGet:
path: /health
port: 7860
initialDelaySeconds: 30
periodSeconds: 10
"""
def _get_k8s_service(self) -> str:
return f"""apiVersion: v1
kind: Service
metadata:
name: {self.config['app_name']}-service
labels:
app: {self.config['app_name']}
spec:
selector:
app: {self.config['app_name']}
ports:
- protocol: TCP
port: 80
targetPort: 7860
type: ClusterIP
"""
def _get_k8s_ingress(self) -> str:
return f"""apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {self.config['app_name']}-ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/proxy-body-size: "100m"
spec:
tls:
- hosts:
- {self.config['app_name']}.yourdomain.com
secretName: {self.config['app_name']}-tls
rules:
- host: {self.config['app_name']}.yourdomain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {self.config['app_name']}-service
port:
number: 80
"""
def _get_k8s_configmap(self) -> str:
return f"""apiVersion: v1
kind: ConfigMap
metadata:
name: {self.config['app_name']}-config
data:
MAX_AUDIO_DURATION: "600"
LOG_LEVEL: "INFO"
ENABLE_WEB_API: "true"
HOST: "0.0.0.0"
PORT: "7860"
"""
def deploy_aws(self) -> bool:
"""Deploy to AWS using ECS."""
logger.info("Deploying to AWS ECS...")
# Create ECS task definition
task_definition = {
"family": self.config["app_name"],
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "2048",
"memory": "4096",
"executionRoleArn": f"arn:aws:iam::{self._get_aws_account_id()}:role/ecsTaskExecutionRole",
"taskRoleArn": f"arn:aws:iam::{self._get_aws_account_id()}:role/ecsTaskRole",
"containerDefinitions": [
{
"name": self.config["app_name"],
"image": f"{self.config['docker_registry']}/{self.config['app_name']}:{self.config['version']}",
"portMappings": [{"containerPort": 7860, "protocol": "tcp"}],
"environment": [
{"name": "MAX_AUDIO_DURATION", "value": "600"},
{"name": "LOG_LEVEL", "value": "INFO"},
],
"secrets": [
{
"name": "ANTHROPIC_API_KEY",
"valueFrom": f"arn:aws:secretsmanager:us-west-2:{self._get_aws_account_id()}:secret:anthropic-api-key",
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": f"/ecs/{self.config['app_name']}",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs",
},
},
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:7860/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
},
}
],
}
# Save task definition
aws_dir = self.project_root / "deployment" / "aws"
aws_dir.mkdir(parents=True, exist_ok=True)
with open(aws_dir / "ecs-task-definition.json", "w", encoding="utf-8") as f:
json.dump(task_definition, f, indent=2)
logger.info("AWS ECS task definition created")
logger.info(
"Deploy with: aws ecs register-task-definition --cli-input-json file://deployment/aws/ecs-task-definition.json"
)
return True
def _get_aws_account_id(self) -> str:
"""Get AWS account ID."""
try:
result = subprocess.run(
["aws", "sts", "get-caller-identity", "--query", "Account", "--output", "text"],
capture_output=True,
text=True,
check=True,
)
return result.stdout.strip()
except Exception:
return "123456789012" # Placeholder
def build_and_push_docker_image(self, registry: str = None) -> bool:
"""Build and push Docker image to registry."""
registry = registry or self.config.get("docker_registry", "your-registry.com")
image_tag = f"{registry}/{self.config['app_name']}:{self.config['version']}"
latest_tag = f"{registry}/{self.config['app_name']}:latest"
logger.info(f"Building and pushing Docker image: {image_tag}")
# Build image
dockerfile_path = self.project_root / "deployment" / "docker" / "Dockerfile"
build_cmd = ["docker", "build", "-t", image_tag, "-t", latest_tag, "-f", str(dockerfile_path), "."]
result = subprocess.run(build_cmd, capture_output=True, text=True)
if result.returncode != 0:
logger.error(f"Docker build failed: {result.stderr}")
return False
# Push to registry
for tag in [image_tag, latest_tag]:
logger.info(f"Pushing {tag}...")
push_result = subprocess.run(["docker", "push", tag], capture_output=True, text=True)
if push_result.returncode != 0:
logger.error(f"Docker push failed for {tag}: {push_result.stderr}")
return False
logger.info("Docker image pushed successfully!")
return True
def run_health_checks(self) -> bool:
"""Run health checks on deployed service."""
logger.info("Running health checks...")
health_endpoints: List[str] = []
if self.target == "local":
health_endpoints = ["http://localhost:7860/health"]
elif self.target == "docker":
health_endpoints = ["http://localhost:7860/health"]
elif self.target == "k8s":
# Get service endpoint
try:
result = subprocess.run(
[
"kubectl",
"get",
"service",
f"{self.config['app_name']}-service",
"-o",
"jsonpath={.status.loadBalancer.ingress[0].ip}",
],
capture_output=True,
text=True,
check=True,
)
ip = result.stdout.strip()
if ip:
health_endpoints = [f"http://{ip}/health"]
except Exception:
logger.warning("Could not get Kubernetes service IP")
for endpoint in health_endpoints:
try:
import requests
response = requests.get(endpoint, timeout=10)
if response.status_code == 200:
logger.info(f"Health check passed: {endpoint}")
return True
else:
logger.error(f"Health check failed: {endpoint} returned {response.status_code}")
except Exception as e:
logger.error(f"Health check failed: {endpoint} - {e}")
# If no endpoints were available to check, don't fail the pipeline.
return len(health_endpoints) == 0
def setup_monitoring(self) -> bool:
"""Setup monitoring and alerting."""
logger.info("Setting up monitoring...")
monitoring_config = {
"prometheus": {
"scrape_configs": [
{
"job_name": self.config["app_name"],
"static_configs": [{"targets": [f"{self.config['app_name']}-service:7860"]}],
}
]
},
"grafana": {
"dashboards": [
{
"name": f"{self.config['app_name']} Dashboard",
"panels": [
"Request Rate",
"Response Time",
"Error Rate",
"CPU Usage",
"Memory Usage",
"Audio Processing Time",
],
}
]
},
"alerts": [
{"name": "High Error Rate", "condition": "error_rate > 0.05", "action": "notify_slack"},
{"name": "High Memory Usage", "condition": "memory_usage > 0.8", "action": "scale_up"},
],
}
monitoring_dir = self.project_root / "deployment" / "monitoring"
monitoring_dir.mkdir(parents=True, exist_ok=True)
with open(monitoring_dir / "monitoring-config.yaml", "w", encoding="utf-8") as f:
yaml.dump(monitoring_config, f, default_flow_style=False)
logger.info("Monitoring configuration created")
return True
def rollback_deployment(self, version: str = None) -> bool:
"""Rollback to previous deployment."""
logger.info(f"Rolling back deployment to version: {version or 'previous'}")
if self.target == "k8s":
cmd = ["kubectl", "rollout", "undo", f"deployment/{self.config['app_name']}"]
if version:
cmd.extend(["--to-revision", version])
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
logger.info("Kubernetes rollback successful")
return True
else:
logger.error(f"Kubernetes rollback failed: {result.stderr}")
return False
elif self.target == "aws":
logger.info("AWS ECS rollback requires manual intervention via AWS Console or CLI")
return True
else:
logger.warning(f"Rollback not implemented for target: {self.target}")
return True
def cleanup_old_deployments(self) -> bool:
"""Cleanup old deployments and resources."""
logger.info("Cleaning up old deployments...")
if self.target == "docker":
# Remove old images
try:
subprocess.run(
["docker", "image", "prune", "-f"],
capture_output=True,
text=True,
check=False,
)
logger.info("Cleaned up old Docker images")
except Exception:
logger.warning("Failed to cleanup Docker images")
elif self.target == "k8s":
# Cleanup completed jobs/pods
try:
subprocess.run(
["kubectl", "delete", "pods", "--field-selector=status.phase=Succeeded"],
capture_output=True,
text=True,
check=False,
)
logger.info("Cleaned up completed Kubernetes pods")