Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Docker ๋ฐฐํฌ ํ ์คํธ ์คํฌ๋ฆฝํธ | |
| Docker ์ปจํ ์ด๋๊ฐ ์ ์์ ์ผ๋ก ์คํ๋๊ณ ์๋์ง ํ์ธ | |
| """ | |
| import requests | |
| import time | |
| import subprocess | |
| import json | |
| import sys | |
| import os | |
| def check_docker_installation(): | |
| """Docker ์ค์น ํ์ธ""" | |
| try: | |
| result = subprocess.run(['docker', '--version'], | |
| capture_output=True, text=True, check=True) | |
| print(f"โ Docker ์ค์น๋จ: {result.stdout.strip()}") | |
| return True | |
| except subprocess.CalledProcessError: | |
| print("โ Docker๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.") | |
| return False | |
| except FileNotFoundError: | |
| print("โ Docker๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.") | |
| return False | |
| def check_docker_compose_installation(): | |
| """Docker Compose ์ค์น ํ์ธ""" | |
| try: | |
| result = subprocess.run(['docker-compose', '--version'], | |
| capture_output=True, text=True, check=True) | |
| print(f"โ Docker Compose ์ค์น๋จ: {result.stdout.strip()}") | |
| return True | |
| except subprocess.CalledProcessError: | |
| print("โ Docker Compose๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.") | |
| return False | |
| except FileNotFoundError: | |
| print("โ Docker Compose๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.") | |
| return False | |
| def check_dockerfile(): | |
| """Dockerfile ์กด์ฌ ํ์ธ""" | |
| if os.path.exists('Dockerfile'): | |
| print("โ Dockerfile ์กด์ฌ") | |
| return True | |
| else: | |
| print("โ Dockerfile์ด ์กด์ฌํ์ง ์์ต๋๋ค.") | |
| return False | |
| def check_docker_compose_file(): | |
| """docker-compose.yml ์กด์ฌ ํ์ธ""" | |
| if os.path.exists('docker-compose.yml'): | |
| print("โ docker-compose.yml ์กด์ฌ") | |
| return True | |
| else: | |
| print("โ docker-compose.yml์ด ์กด์ฌํ์ง ์์ต๋๋ค.") | |
| return False | |
| def build_docker_image(): | |
| """Docker ์ด๋ฏธ์ง ๋น๋""" | |
| try: | |
| print("๐จ Docker ์ด๋ฏธ์ง ๋น๋ ์ค...") | |
| result = subprocess.run(['docker-compose', 'build'], | |
| capture_output=True, text=True, check=True) | |
| print("โ Docker ์ด๋ฏธ์ง ๋น๋ ์ฑ๊ณต") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"โ Docker ์ด๋ฏธ์ง ๋น๋ ์คํจ: {e.stderr}") | |
| return False | |
| def start_services(): | |
| """์๋น์ค ์์""" | |
| try: | |
| print("๐ ์๋น์ค ์์ ์ค...") | |
| result = subprocess.run(['docker-compose', 'up', '-d'], | |
| capture_output=True, text=True, check=True) | |
| print("โ ์๋น์ค ์์ ์ฑ๊ณต") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"โ ์๋น์ค ์์ ์คํจ: {e.stderr}") | |
| return False | |
| def check_service_status(): | |
| """์๋น์ค ์ํ ํ์ธ""" | |
| try: | |
| result = subprocess.run(['docker-compose', 'ps'], | |
| capture_output=True, text=True, check=True) | |
| print("๐ ์๋น์ค ์ํ:") | |
| print(result.stdout) | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"โ ์๋น์ค ์ํ ํ์ธ ์คํจ: {e.stderr}") | |
| return False | |
| def wait_for_api_ready(max_wait=60): | |
| """API ์ค๋น ๋๊ธฐ""" | |
| print("โณ API ์ค๋น ๋๊ธฐ ์ค...") | |
| for i in range(max_wait): | |
| try: | |
| response = requests.get('http://localhost:8001/health', timeout=5) | |
| if response.status_code == 200: | |
| print(f"โ API ์ค๋น ์๋ฃ ({i+1}์ด)") | |
| return True | |
| except requests.exceptions.RequestException: | |
| pass | |
| time.sleep(1) | |
| print("โ API ์ค๋น ์๊ฐ ์ด๊ณผ") | |
| return False | |
| def test_api_endpoints(): | |
| """API ์๋ํฌ์ธํธ ํ ์คํธ""" | |
| endpoints = [ | |
| ('/health', 'GET'), | |
| ('/models', 'GET'), | |
| ('/generate', 'POST') | |
| ] | |
| print("๐งช API ์๋ํฌ์ธํธ ํ ์คํธ:") | |
| for endpoint, method in endpoints: | |
| try: | |
| if method == 'GET': | |
| response = requests.get(f'http://localhost:8001{endpoint}', timeout=10) | |
| elif method == 'POST' and endpoint == '/generate': | |
| # ๊ฐ๋จํ ์์ฑ ํ ์คํธ | |
| data = { | |
| 'prompt': '์๋ ํ์ธ์', | |
| 'model_id': 'polyglot-ko-1.3b-chat', | |
| 'max_length': 50, | |
| 'temperature': 0.7 | |
| } | |
| response = requests.post(f'http://localhost:8001{endpoint}', | |
| data=data, timeout=30) | |
| else: | |
| continue | |
| if response.status_code in [200, 201]: | |
| print(f"โ {endpoint} - {response.status_code}") | |
| else: | |
| print(f"โ ๏ธ {endpoint} - {response.status_code}") | |
| except requests.exceptions.RequestException as e: | |
| print(f"โ {endpoint} - ์ค๋ฅ: {e}") | |
| def test_redis_connection(): | |
| """Redis ์ฐ๊ฒฐ ํ ์คํธ""" | |
| try: | |
| result = subprocess.run(['docker-compose', 'exec', '-T', 'redis', 'redis-cli', 'ping'], | |
| capture_output=True, text=True, check=True) | |
| if 'PONG' in result.stdout: | |
| print("โ Redis ์ฐ๊ฒฐ ์ฑ๊ณต") | |
| return True | |
| else: | |
| print("โ Redis ์ฐ๊ฒฐ ์คํจ") | |
| return False | |
| except subprocess.CalledProcessError: | |
| print("โ Redis ์ฐ๊ฒฐ ์คํจ") | |
| return False | |
| def test_celery_worker(): | |
| """Celery Worker ํ ์คํธ""" | |
| try: | |
| result = subprocess.run(['docker-compose', 'exec', '-T', 'celery-worker', 'celery', '-A', 'lily_llm_core.celery_app', 'inspect', 'active'], | |
| capture_output=True, text=True, check=True) | |
| print("โ Celery Worker ์ ์") | |
| return True | |
| except subprocess.CalledProcessError: | |
| print("โ ๏ธ Celery Worker ํ์ธ ์คํจ (์ ์์ผ ์ ์์)") | |
| return True | |
| def check_flower_monitoring(): | |
| """Flower ๋ชจ๋ํฐ๋ง ํ์ธ""" | |
| try: | |
| response = requests.get('http://localhost:5555', timeout=5) | |
| if response.status_code == 200: | |
| print("โ Flower ๋ชจ๋ํฐ๋ง ์ ๊ทผ ๊ฐ๋ฅ") | |
| return True | |
| else: | |
| print("โ ๏ธ Flower ๋ชจ๋ํฐ๋ง ์ ๊ทผ ๋ถ๊ฐ") | |
| return False | |
| except requests.exceptions.RequestException: | |
| print("โ ๏ธ Flower ๋ชจ๋ํฐ๋ง ์ ๊ทผ ๋ถ๊ฐ") | |
| return False | |
| def stop_services(): | |
| """์๋น์ค ์ค์ง""" | |
| try: | |
| print("๐ ์๋น์ค ์ค์ง ์ค...") | |
| subprocess.run(['docker-compose', 'down'], | |
| capture_output=True, text=True, check=True) | |
| print("โ ์๋น์ค ์ค์ง ์๋ฃ") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"โ ์๋น์ค ์ค์ง ์คํจ: {e.stderr}") | |
| return False | |
| def cleanup(): | |
| """์ ๋ฆฌ""" | |
| try: | |
| print("๐งน ์ ๋ฆฌ ์ค...") | |
| subprocess.run(['docker-compose', 'down', '-v'], | |
| capture_output=True, text=True, check=True) | |
| subprocess.run(['docker', 'system', 'prune', '-f'], | |
| capture_output=True, text=True, check=True) | |
| print("โ ์ ๋ฆฌ ์๋ฃ") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| print(f"โ ์ ๋ฆฌ ์คํจ: {e.stderr}") | |
| return False | |
| def main(): | |
| """๋ฉ์ธ ํ ์คํธ ํจ์""" | |
| print("๐ณ Docker ๋ฐฐํฌ ํ ์คํธ ์์") | |
| print("=" * 50) | |
| # 1. ์ฌ์ ์๊ตฌ์ฌํญ ํ์ธ | |
| print("\n๐ ์ฌ์ ์๊ตฌ์ฌํญ ํ์ธ") | |
| print("-" * 30) | |
| if not check_docker_installation(): | |
| return False | |
| if not check_docker_compose_installation(): | |
| return False | |
| if not check_dockerfile(): | |
| return False | |
| if not check_docker_compose_file(): | |
| return False | |
| # 2. Docker ์ด๋ฏธ์ง ๋น๋ | |
| print("\n๐จ Docker ์ด๋ฏธ์ง ๋น๋") | |
| print("-" * 30) | |
| if not build_docker_image(): | |
| return False | |
| # 3. ์๋น์ค ์์ | |
| print("\n๐ ์๋น์ค ์์") | |
| print("-" * 30) | |
| if not start_services(): | |
| return False | |
| # 4. ์๋น์ค ์ํ ํ์ธ | |
| print("\n๐ ์๋น์ค ์ํ ํ์ธ") | |
| print("-" * 30) | |
| check_service_status() | |
| # 5. API ์ค๋น ๋๊ธฐ | |
| print("\nโณ API ์ค๋น ๋๊ธฐ") | |
| print("-" * 30) | |
| if not wait_for_api_ready(): | |
| print("โ API ์ค๋น ์คํจ") | |
| stop_services() | |
| return False | |
| # 6. API ์๋ํฌ์ธํธ ํ ์คํธ | |
| print("\n๐งช API ์๋ํฌ์ธํธ ํ ์คํธ") | |
| print("-" * 30) | |
| test_api_endpoints() | |
| # 7. Redis ์ฐ๊ฒฐ ํ ์คํธ | |
| print("\n๐ด Redis ์ฐ๊ฒฐ ํ ์คํธ") | |
| print("-" * 30) | |
| test_redis_connection() | |
| # 8. Celery Worker ํ ์คํธ | |
| print("\n๐ฟ Celery Worker ํ ์คํธ") | |
| print("-" * 30) | |
| test_celery_worker() | |
| # 9. Flower ๋ชจ๋ํฐ๋ง ํ์ธ | |
| print("\n๐ธ Flower ๋ชจ๋ํฐ๋ง ํ์ธ") | |
| print("-" * 30) | |
| check_flower_monitoring() | |
| # 10. ๊ฒฐ๊ณผ ์์ฝ | |
| print("\n๐ ํ ์คํธ ๊ฒฐ๊ณผ ์์ฝ") | |
| print("-" * 30) | |
| print("โ Docker ๋ฐฐํฌ ํ ์คํธ ์๋ฃ!") | |
| print("\n๐ ์ ๊ทผ ๊ฐ๋ฅํ ์๋น์ค:") | |
| print(" - API Server: http://localhost:8001") | |
| print(" - Health Check: http://localhost:8001/health") | |
| print(" - Flower Monitor: http://localhost:5555") | |
| print("\n๐ ์ ์ฉํ ๋ช ๋ น์ด:") | |
| print(" - ๋ก๊ทธ ํ์ธ: docker-compose logs") | |
| print(" - ์๋น์ค ์ํ: docker-compose ps") | |
| print(" - ์๋น์ค ์ค์ง: docker-compose down") | |
| print(" - ์ ๋ฆฌ: docker-compose down -v && docker system prune -f") | |
| return True | |
| if __name__ == "__main__": | |
| try: | |
| success = main() | |
| if not success: | |
| print("\nโ Docker ๋ฐฐํฌ ํ ์คํธ ์คํจ") | |
| sys.exit(1) | |
| else: | |
| print("\n๐ Docker ๋ฐฐํฌ ํ ์คํธ ์ฑ๊ณต!") | |
| except KeyboardInterrupt: | |
| print("\nโ ๏ธ ์ฌ์ฉ์์ ์ํด ์ค๋จ๋จ") | |
| stop_services() | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"\nโ ์์์น ๋ชปํ ์ค๋ฅ: {e}") | |
| stop_services() | |
| sys.exit(1) |