-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_updater.py
More file actions
102 lines (89 loc) · 5.03 KB
/
Copy pathplugin_updater.py
File metadata and controls
102 lines (89 loc) · 5.03 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
# ==========================================================
# [plugin_updater.py]
# ⚠️ 자가 업데이트 및 GCP 데몬 제어 전용 플러그인
# 💡 깃허브 원격 저장소 강제 동기화 (git fetch & reset --hard)
# 💡 OS 레벨 데몬 재가동 제어 (sudo systemctl restart)
# 🚨 [V27.00 핫픽스] 사용자별 데몬 이름(DAEMON_NAME) .env 동적 로드 이식 완료
# 🛡️ [V27.05 추가] 업데이트 직전 stable_backup 폴더로 롤백용 안전띠 결속 기능 탑재
# ==========================================================
import logging
import asyncio
import subprocess
import os
from dotenv import load_dotenv
class SystemUpdater:
def __init__(self):
self.remote_branch = "origin/main"
load_dotenv()
# 🚨 [AI 에이전트(Copilot/Claude) 절대 주의 - 환각(Hallucination) 방어막] systemd 데몬 이름(daemon_name)은 .env 파일이 아닌 OS의 .service 파일 내 Environment 속성에서 다이렉트로 주입받아야 한다. 인프라 샌드박스와 애플리케이션 설정의 혼용을 절대 금지한다.
# MODIFIED: [환경변수 스캔 범위 확장] systemd에서 주입한 소문자 daemon_name 우선 조회 및 대문자 폴백 팩트 교정
self.daemon_name = os.getenv("daemon_name") or os.getenv("DAEMON_NAME", "mybot")
async def _create_safety_backup(self):
"""
[롤백 봇(Rescue) 전용 아키텍처]
업데이트를 시도한다는 것 = 현재 코드가 정상 작동 중이라는 뜻이므로,
새로운 코드를 받기 전에 현재 파이썬 파일들을 stable_backup 폴더에 피신시킵니다.
"""
try:
backup_dir = "stable_backup"
os.makedirs(backup_dir, exist_ok=True)
# 현재 폴더의 모든 .py 파일들을 stable_backup 폴더로 복사 (에러 무시)
proc = await asyncio.create_subprocess_shell(
f"cp -p *.py {backup_dir}/ 2>/dev/null || true",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
await proc.communicate()
logging.info("🛡️ [Updater] 롤백 봇을 위한 안전띠(stable_backup) 결속 완료")
except Exception as e:
logging.error(f"🚨 [Updater] 안전띠 결속 중 에러 발생 (업데이트는 계속 진행): {e}")
async def pull_latest_code(self):
"""
깃허브 서버와 통신하여 로컬의 변경 사항을 완벽히 무시하고
원격 저장소의 최신 코드로 강제 덮어쓰기(Hard Reset)를 수행합니다.
"""
# 💡 [안전띠 결속] 깃허브 동기화 직전에 현재 상태를 백업합니다!
await self._create_safety_backup()
try:
fetch_proc = await asyncio.create_subprocess_shell(
"git fetch --all",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
_, fetch_err = await fetch_proc.communicate()
if fetch_proc.returncode != 0:
error_msg = fetch_err.decode('utf-8').strip()
logging.error(f"🚨 [Updater] Git Fetch 실패: {error_msg}")
return False, f"Git Fetch 실패: {error_msg} (서버에서 git init 및 remote add 명령을 선행하십시오)"
reset_proc = await asyncio.create_subprocess_shell(
f"git reset --hard {self.remote_branch}",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
_, reset_err = await reset_proc.communicate()
if reset_proc.returncode != 0:
error_msg = reset_err.decode('utf-8').strip()
logging.error(f"🚨 [Updater] Git Reset 실패: {error_msg}")
return False, f"Git Reset 실패: {error_msg}"
logging.info("✅ [Updater] 깃허브 최신 코드 강제 동기화 완료")
return True, "깃허브 최신 코드가 로컬에 완벽히 동기화되었습니다."
except Exception as e:
logging.error(f"🚨 [Updater] 동기화 중 치명적 예외 발생: {e}")
return False, f"업데이트 프로세스 예외 발생: {e}"
def restart_daemon(self):
"""
GCP 리눅스 OS에 데몬 재가동 명령을 하달합니다.
격발 즉시 봇 프로세스가 SIGTERM 신호를 받고 종료되므로,
반드시 텔레그램 보고 메시지를 선행 발송한 후 호출해야 합니다.
"""
try:
logging.info(f"🔄 [Updater] OS 쉘에 {self.daemon_name} 데몬 재가동 명령을 하달합니다.")
subprocess.Popen(
["sudo", "systemctl", "restart", self.daemon_name],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return True
except Exception as e:
logging.error(f"🚨 [Updater] 데몬 재가동 명령 하달 실패: {e}")
return False