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

112 lines
4.7 KiB
Python

"""deploy_server.py 문법 오류 수정 + zioinfo-mail 블록 올바르게 추가"""
import paramiko, sys, json
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)
print(o.read().decode('utf-8','replace').strip()[:600])
# 1. 현재 문법 오류 위치 확인
run('오류 위치', 'python3 -c "import ast; ast.parse(open(\'/opt/zioinfo/deploy_server.py\').read())" 2>&1')
run('195~210줄', "sed -n '195,210p' /opt/zioinfo/deploy_server.py")
# 2. 현재 파일 다운로드 후 수정
_, o, _ = c.exec_command('cat /opt/zioinfo/deploy_server.py', timeout=15)
content = o.read().decode('utf-8','replace')
# 잘못 삽입된 zioinfo-mail 블록 제거 후 올바르게 다시 삽입
import re
# 잘못된 블록 완전 제거 (여러 패턴)
# 패턴: elif repo == "zioinfo-mail": 이후 다음 elif/else/함수 전까지
content_clean = re.sub(
r'\n\s*elif repo == "zioinfo-mail":.*?(?=\n\s*elif |\n\s*else:|\ndef |\nclass |\n# ──)',
'',
content,
flags=re.DOTALL
)
print(f'\n[제거 후 zioinfo-mail 존재 여부]\n{"있음" if "zioinfo-mail" in content_clean else "없음"}')
# 올바른 위치 찾기: guardia-docs elif 블록 끝 다음에 삽입
# run_steps 패턴의 블록 구조를 따라야 함
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")
'''
# guardia-docs 블록 찾기
if 'elif repo == "guardia-docs"' in content_clean:
lines = content_clean.split('\n')
insert_after = None
in_docs = False
depth = 0
for i, line in enumerate(lines):
if 'elif repo == "guardia-docs"' in line:
in_docs = True
depth = 0
elif in_docs:
if line.strip().startswith('if ok') or line.strip().startswith('notify_itsm'):
insert_after = i
elif line.strip() and not line[0].isspace() and not line.startswith(' '):
# 들여쓰기 끝
if insert_after:
break
in_docs = False
if insert_after is None:
# 마지막 elif 블록 후 if ok/notify_itsm 패턴 찾기
for i in range(len(lines)-1, 0, -1):
if 'notify_itsm' in lines[i] and 'guardia' in '\n'.join(lines[max(0,i-5):i]):
insert_after = i
break
if insert_after:
lines.insert(insert_after + 1, MAIL_BLOCK)
content_new = '\n'.join(lines)
print(f' 삽입 위치: 라인 {insert_after + 1}')
else:
# 그냥 마지막에 추가
content_new = content_clean + MAIL_BLOCK
print(' 마지막에 추가')
else:
content_new = content_clean + MAIL_BLOCK
print(' guardia-docs 없음, 마지막에 추가')
# 저장
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
f.write(content_new)
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 -5")
# webhook 서버 재시작
run('포트 kill + 재시작',
'fuser -k 9999/tcp 2>/dev/null; sleep 1; '
'systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
sftp.close(); c.close()