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

91 lines
4.3 KiB
Python

"""deploy_server.py 완전 수정: 올바른 위치에 zioinfo-mail 삽입"""
import paramiko, sys
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()[:500])
# 전체 파일 다운로드
_, 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/def/if __name__ 전까지 전부 제거
content = re.sub(
r'\n[ \t]*elif repo == "zioinfo-mail":.*?(?=\n[ \t]*elif |\n[ \t]*else:|\nif __name__|$)',
'',
content,
flags=re.DOTALL
)
print(f'[정리 후 zioinfo-mail 잔존]\n{"있음 (추가 정리 필요)" if "zioinfo-mail" in content else "없음 ✅"}')
# 올바른 삽입 위치: WebhookHandler.do_POST 내부 if/elif 체인
# 패턴: "elif repo == "guardia-messenger":" 또는 마지막 notify_itsm 블록 다음
# deploy_server.py 구조상 WebhookHandler 클래스 내부에 있어야 함
# "guardia-messenger" 블록의 끝(notify_itsm 호출 직후) 찾기
lines = content.split('\n')
insert_idx = None
for i in range(len(lines)-1, 0, -1):
line = lines[i].strip()
# guardia-messenger 또는 guardia-docs notify_itsm 이후 위치 찾기
if 'notify_itsm' in line and i > 100:
# 이 줄이 클래스/함수 내부인지 확인 (들여쓰기 확인)
if lines[i].startswith(' '): # 8칸 들여쓰기 = 클래스 메서드 내부
insert_idx = i + 1
break
print(f'[삽입 위치]\n라인 {insert_idx}: {lines[insert_idx].strip()[:60] if insert_idx and insert_idx < len(lines) else "없음"}')
# 적절한 들여쓰기로 zioinfo-mail 블록 생성
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:
# 찾지 못한 경우: 마지막 elif repo 블록 후에 직접 삽입
new_content = content.rstrip() + '\n' + MAIL_BLOCK + '\n'
# 파일 저장
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\\|run_steps.*mail' /opt/zioinfo/deploy_server.py | head -5")
# webhook 재시작
run('포트 9999 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()