zioinfo-mail/scripts/setup/restore_and_fix_final.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

141 lines
5.8 KiB
Python

"""deploy_server.py 완전 복원 + zioinfo-mail 추가 + webhook 재기동"""
import paramiko, sys, 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[:500])
return out
# 1. git에서 복원 (올바른 경로)
run('파일 크기 확인', 'ls -la /opt/zioinfo/deploy_server.py')
run('git log 확인', 'git -C /opt/zioinfo/src log --oneline -3 2>/dev/null')
run('git show deploy_server 크기',
'git -C /opt/zioinfo/src show HEAD:deploy_server.py 2>/dev/null | wc -l || '
'git -C /opt/zioinfo/src show HEAD:deploy_server.py 2>&1 | head -3')
# 실제 복원
run('deploy_server.py 복원',
'git -C /opt/zioinfo/src show HEAD:deploy_server.py > /opt/zioinfo/deploy_server.py && '
'ls -la /opt/zioinfo/deploy_server.py')
run('복원 후 크기', 'wc -l /opt/zioinfo/deploy_server.py')
# 파일이 여전히 비어있으면 GitHub commit history 확인
_, o, _ = c.exec_command('wc -c /opt/zioinfo/deploy_server.py', timeout=10)
size = o.read().decode().strip()
print(f'\n파일 크기: {size}')
if '0 ' in size or size.startswith('0'):
print('여전히 비어있음 - git에서 실제 파일 찾기')
run('git 모든 브랜치 파일',
'git -C /opt/zioinfo/src log --all --oneline -- deploy_server.py | head -5')
run('git show origin/main',
'git -C /opt/zioinfo/src show origin/main:deploy_server.py 2>/dev/null | wc -l')
# 2. 파일 다운로드
sftp.get('/opt/zioinfo/deploy_server.py', 'C:/GUARDiA/_ds_restored.py')
with open('C:/GUARDiA/_ds_restored.py', encoding='utf-8') as f:
content = f.read()
print(f'\n복원된 파일: {len(content)} bytes, {content.count(chr(10))}')
if len(content) < 100:
print('파일이 너무 작음 - 백업 사용')
with open('C:/GUARDiA/_deploy_server_backup.py', encoding='utf-8', errors='replace') as f:
content = f.read()
# 3. zioinfo-mail 블록 추가
print('\n=== repo 구조 파악 ===')
lines = content.split('\n')
for i, line in enumerate(lines):
if 'elif repo' in line or 'if repo' in line:
print(f' {i+1}: {line[:70]}')
# 마지막 repo elif 블록 끝 위치
last_repo_end = None
for i in range(len(lines)-1, 0, -1):
if ('if ok:' in lines[i] or 'notify_itsm' in lines[i]) and i > 10:
# 같은 들여쓰기 수준인지 확인
indent = len(lines[i]) - len(lines[i].lstrip())
if indent >= 8: # 8칸 이상 들여쓰기 = if/elif 내부
last_repo_end = i
print(f'\n마지막 적합한 위치: 줄 {i+1}: {lines[i][:60]}')
break
# elif 들여쓰기
elif_indent = ' '
for line in lines:
if 'elif repo ==' in line:
elif_indent = ' ' * (len(line) - len(line.lstrip()))
break
print(f'elif 들여쓰기: {len(elif_indent)}')
# zioinfo-mail 이미 있으면 제거
import re
if 'zioinfo-mail' in content:
content = re.sub(
r'\n' + elif_indent + r'elif repo == "zioinfo-mail":.*?(?=\n' + elif_indent + r'elif |\n' + r'if __name__|$)',
'', content, flags=re.DOTALL)
lines = content.split('\n')
INNER = elif_indent + ' '
MAIL_BLOCK = f'''
{elif_indent}elif repo == "zioinfo-mail":
{INNER}SRC = "/opt/mail"
{INNER}ok = run_steps(repo, [
{INNER} ("git pull", ["bash", "-c",
{INNER} "[ -d /opt/mail/src/.git ] && git -C /opt/mail/src fetch origin main && git -C /opt/mail/src reset --hard origin/main"
{INNER} " || git clone 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git' /opt/mail/src"]),
{INNER} ("npm build", ["bash", "-c",
{INNER} "cd /opt/mail/src/frontend && npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps && npm run build"]),
{INNER} ("copy dist", ["bash", "-c",
{INNER} "mkdir -p /var/www/mail && cp -r /opt/mail/src/dist/. /var/www/mail/"]),
{INNER} ("pip install", ["bash", "-c",
{INNER} "/opt/mail/venv/bin/pip install -r /opt/mail/src/backend/requirements.txt -q"]),
{INNER} ("rsync", ["bash", "-c",
{INNER} "rsync -a --exclude=__pycache__ --exclude=.git --exclude='*.pyc' --exclude='.env' /opt/mail/src/backend/ /opt/mail/backend/"]),
{INNER} ("restart", ["systemctl", "restart", "zioinfo-mail"]),
{INNER} ("health check", ["bash", "-c", "sleep 4 && curl -sf http://localhost:8026/health"]),
{INNER}])
{INNER}if ok:
{INNER} notify_itsm(True, "\\u2705 zioinfo-mail \\ubc30\\ud3ec \\uc644\\ub8cc")
{INNER}else:
{INNER} notify_itsm(False, "\\u274c zioinfo-mail \\ube4c\\ub4dc \\uc2e4\\ud328")'''
if last_repo_end:
lines = content.split('\n')
lines.insert(last_repo_end + 1, MAIL_BLOCK)
new_content = '\n'.join(lines)
else:
new_content = content.rstrip() + '\n' + MAIL_BLOCK + '\n'
try:
ast.parse(new_content)
print('\n✅ 문법 OK')
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
f.write(new_content)
print('업로드 완료')
except SyntaxError as e:
print(f'\n❌ 문법 오류 {e} — 원본만 복원')
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
f.write(content)
run('최종 확인', 'python3 -m py_compile /opt/zioinfo/deploy_server.py && echo "✅ OK" || echo "❌ FAIL"')
run('zioinfo-mail 줄', "grep -n 'zioinfo-mail' /opt/zioinfo/deploy_server.py | head -3")
run('webhook 재기동',
'fuser -k 9999/tcp 2>/dev/null; sleep 1; '
'systemctl restart zioinfo-deploy && sleep 4 && systemctl is-active zioinfo-deploy')
import os
for f in ['C:/GUARDiA/_ds_restored.py', 'C:/GUARDiA/_deploy_server_backup.py']:
try: os.remove(f)
except: pass
sftp.close(); c.close()
print('\n=== 완료 ===')