106 lines
4.5 KiB
Python
106 lines
4.5 KiB
Python
"""deploy_server.py 구조 파악 후 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[:500])
|
|
return out
|
|
|
|
# 1. webhook 서버 상태 확인 + 포트 fix
|
|
run('webhook 상태', 'fuser -k 9999/tcp 2>/dev/null; systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
|
|
|
|
# 2. deploy_server.py 구조 파악
|
|
run('elif repo 블록 전체',
|
|
"grep -n 'elif repo\|if repo\|run_steps\|notify_itsm' /opt/zioinfo/deploy_server.py | head -30")
|
|
|
|
# 3. 파일 다운로드
|
|
sftp.get('/opt/zioinfo/deploy_server.py', 'C:/GUARDiA/_ds_work.py')
|
|
with open('C:/GUARDiA/_ds_work.py', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
lines = content.split('\n')
|
|
# 들여쓰기 파악
|
|
elif_line = None
|
|
notify_line = None
|
|
for i, line in enumerate(lines):
|
|
if 'elif repo ==' in line and elif_line is None:
|
|
elif_line = i
|
|
print(f'\n첫 elif repo 줄 {i+1}: {repr(line[:60])}')
|
|
if 'notify_itsm' in line and notify_line is None:
|
|
notify_line = i
|
|
print(f'첫 notify_itsm 줄 {i+1}: {repr(line[:60])}')
|
|
|
|
# elif 들여쓰기 계산
|
|
elif_indent = ''
|
|
if elif_line is not None:
|
|
elif_indent = lines[elif_line][:len(lines[elif_line]) - len(lines[elif_line].lstrip())]
|
|
print(f'\nelif 들여쓰기: {len(elif_indent)}칸')
|
|
|
|
# 마지막 notify_itsm 줄 찾기 (삽입 위치)
|
|
last_notify = None
|
|
for i in range(len(lines)-1, 0, -1):
|
|
if 'notify_itsm' in lines[i] and lines[i].strip():
|
|
last_notify = i
|
|
print(f'\n마지막 notify_itsm: 줄 {i+1}: {repr(lines[i][:70])}')
|
|
print(f'다음 줄 {i+2}: {repr(lines[i+1][:70]) if i+1 < len(lines) else "(EOF)"}')
|
|
break
|
|
|
|
# 4. 삽입
|
|
if last_notify is not None:
|
|
# 마지막 notify_itsm 다음 줄에 삽입
|
|
# 단 if __name__ 블록 바깥인지 확인
|
|
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")'''
|
|
|
|
lines.insert(last_notify + 1, MAIL_BLOCK)
|
|
new_content = '\n'.join(lines)
|
|
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}')
|
|
# 오류 컨텍스트
|
|
err_lines = new_content.split('\n')
|
|
for x in range(max(0, e.lineno-3), min(len(err_lines), e.lineno+3)):
|
|
print(f' {x+1}: {repr(err_lines[x][:70])}')
|
|
|
|
run('최종 문법 확인', 'python3 -m py_compile /opt/zioinfo/deploy_server.py && echo "✅" || echo "❌"')
|
|
run('zioinfo-mail 확인', "grep -n 'elif repo.*zioinfo-mail' /opt/zioinfo/deploy_server.py")
|
|
run('webhook 재시작', 'systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
|
|
|
|
import os
|
|
try: os.remove('C:/GUARDiA/_ds_work.py')
|
|
except: pass
|
|
|
|
sftp.close(); c.close()
|