zioinfo-mail/scripts/setup/restore_deploy_server.py
DESKTOP-TKLFCPR\ython 371f77e7ab
Some checks are pending
GUARDiA CI / Python Lint & Import Test (push) Waiting to run
GUARDiA CI / Validate Install Scripts (push) Waiting to run
GUARDiA CI / PR Validation Summary (push) Blocked by required conditions
fix(enhance-v4): APK QR 버그 수정 + 웹메일 라우터 수정
2026-06-02 20:23:55 +09:00

138 lines
5.8 KiB
Python

"""deploy_server.py 서버에서 git 히스토리로 복원 + 올바른 zioinfo-mail 추가"""
import paramiko, sys, re, ast
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
c = paramiko.SSHClient(); c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('101.79.17.164', username='root', password='1q2w3e!Q', timeout=15)
sftp = c.open_sftp()
def run(label, cmd, timeout=20):
print(f'\n[{label}]')
_, o, _ = c.exec_command(cmd, timeout=timeout)
out = o.read().decode('utf-8','replace').strip()
if out: print(out[:400])
return out
# 1. 서버 git 히스토리에서 원본 복원
run('git log deploy_server.py',
'git -C /opt/zioinfo/src log --oneline -- deploy_server.py 2>/dev/null | head -5')
# 마지막 정상 커밋에서 복원
result = run('git show 복원 시도',
'git -C /opt/zioinfo/src show HEAD:deploy_server.py 2>/dev/null | '
'python3 -m py_compile /dev/stdin 2>/dev/null && echo OK || echo FAIL')
if 'OK' in result or 'FAIL' not in result:
run('서버 deploy_server.py 복원 (git HEAD)',
'git -C /opt/zioinfo/src show HEAD:deploy_server.py > /opt/zioinfo/deploy_server.py 2>/dev/null && echo "복원 완료"')
else:
# HEAD에 없으면 이전 커밋에서 찾기
for ref in ['HEAD~1', 'HEAD~2']:
ok = run(f'{ref} 확인',
f'git -C /opt/zioinfo/src show {ref}:deploy_server.py 2>/dev/null | '
'python3 -m py_compile /dev/stdin 2>/dev/null && echo OK || echo FAIL')
if 'OK' in ok:
run(f'{ref}에서 복원',
f'git -C /opt/zioinfo/src show {ref}:deploy_server.py > /opt/zioinfo/deploy_server.py && echo "복원 완료"')
break
run('복원 후 문법 확인', 'python3 -m py_compile /opt/zioinfo/deploy_server.py && echo "✅ OK"')
# 2. 파일 다운로드 후 로컬에서 수정
sftp.get('/opt/zioinfo/deploy_server.py', 'C:/GUARDiA/_ds_clean.py')
with open('C:/GUARDiA/_ds_clean.py', encoding='utf-8', errors='replace') as f:
content = f.read()
# 문법 확인
try:
ast.parse(content)
print('\n✅ 복원된 파일 문법 OK')
except SyntaxError as e:
print(f'\n❌ 복원 파일도 문법 오류: {e}')
# 마지막 수단: 백업 파일 사용
with open('C:/GUARDiA/_deploy_server_backup.py', encoding='utf-8', errors='replace') as f:
content = f.read()
# 백업에서 zioinfo-mail 제거
content = re.sub(
r'\n elif repo == "zioinfo-mail":.*?(?=\n elif |\n if __name__|$)',
'', content, flags=re.DOTALL)
try:
ast.parse(content)
print('백업에서 복원 ✅')
except SyntaxError as e2:
print(f'백업도 오류: {e2}')
import sys; sys.exit(1)
if 'zioinfo-mail' in content:
print('⚠️ 이미 있음, 제거 후 재삽입')
content = re.sub(
r'\n elif repo == "zioinfo-mail":.*?(?=\n elif |\n if __name__|$)',
'', content, flags=re.DOTALL)
# 3. 올바른 위치에 삽입
# WebhookHandler 내부 마지막 notify_itsm 다음
lines = content.split('\n')
insert_idx = None
for i in range(len(lines)-1, 0, -1):
# 12칸 들여쓰기 + notify_itsm → 클래스 메서드 내부
if 'notify_itsm' in lines[i] and lines[i].startswith(' '):
# 이 다음 줄
insert_idx = i + 1
break
print(f'\n삽입 위치: {insert_idx}')
if insert_idx and insert_idx < len(lines):
print(f' 이전: {repr(lines[insert_idx-1].rstrip()[:60])}')
print(f' 이후: {repr(lines[insert_idx].rstrip()[:60])}')
MAIL_BLOCK = '''
elif repo == "zioinfo-mail":
SRC = "/opt/mail"
ok = run_steps(repo, [
("git pull", ["bash", "-c",
"[ -d /opt/mail/src/.git ] && git -C /opt/mail/src fetch origin main && git -C /opt/mail/src reset --hard origin/main"
" || git clone 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git' /opt/mail/src"]),
("npm build", ["bash", "-c",
"cd /opt/mail/src/frontend && npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps && npm run build"]),
("copy dist", ["bash", "-c",
"mkdir -p /var/www/mail && cp -r /opt/mail/src/dist/. /var/www/mail/"]),
("pip install", ["bash", "-c",
"/opt/mail/venv/bin/pip install -r /opt/mail/src/backend/requirements.txt -q"]),
("rsync", ["bash", "-c",
"rsync -a --exclude=__pycache__ --exclude=.git --exclude='*.pyc' --exclude='.env' /opt/mail/src/backend/ /opt/mail/backend/"]),
("restart", ["systemctl", "restart", "zioinfo-mail"]),
("health check", ["bash", "-c", "sleep 4 && curl -sf http://localhost:8026/health"]),
])
if ok:
notify_itsm(True, "\\u2705 zioinfo-mail \\ubc30\\ud3ec \\uc644\\ub8cc")
else:
notify_itsm(False, "\\u274c zioinfo-mail \\ube4c\\ub4dc \\uc2e4\\ud328")'''
if insert_idx:
lines.insert(insert_idx, MAIL_BLOCK)
new_content = '\n'.join(lines)
else:
new_content = content + MAIL_BLOCK
try:
ast.parse(new_content)
print('\n✅ 최종 문법 OK')
except SyntaxError as e:
print(f'\n❌ 최종 오류: {e}')
# 그냥 삽입 없이 원본만 복원
new_content = content
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
f.write(new_content)
run('최종 확인', 'python3 -m py_compile /opt/zioinfo/deploy_server.py && echo "✅ OK" || echo "❌ FAIL"')
run('zioinfo-mail 블록', "grep -n 'elif repo.*zioinfo-mail' /opt/zioinfo/deploy_server.py")
run('webhook 재시작',
'fuser -k 9999/tcp 2>/dev/null; sleep 1; '
'systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
import os
try: os.remove('C:/GUARDiA/_ds_clean.py')
except: pass
sftp.close(); c.close()