146 lines
5.5 KiB
Python
146 lines
5.5 KiB
Python
"""deploy_server.py 로컬에서 직접 수정"""
|
|
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()
|
|
|
|
# 파일 다운로드
|
|
sftp.get('/opt/zioinfo/deploy_server.py', 'C:/GUARDiA/_deploy_server_backup.py')
|
|
print('다운로드 완료')
|
|
|
|
with open('C:/GUARDiA/_deploy_server_backup.py', encoding='utf-8', errors='replace') as f:
|
|
lines = f.readlines()
|
|
|
|
print(f'총 {len(lines)}줄')
|
|
|
|
# 구조 파악: elif repo == 패턴 찾기
|
|
for i, line in enumerate(lines):
|
|
if 'elif repo ==' in line or 'if repo ==' in line:
|
|
print(f' {i+1}: {repr(line.rstrip()[:60])}')
|
|
|
|
print()
|
|
|
|
# zioinfo-mail 관련 모든 줄 범위 찾기
|
|
mail_ranges = []
|
|
i = 0
|
|
while i < len(lines):
|
|
if 'elif repo == "zioinfo-mail"' in lines[i] or 'zioinfo-mail' in lines[i] and 'elif' in lines[i]:
|
|
start = i
|
|
# 다음 elif 또는 if __name__ 또는 def 까지
|
|
j = i + 1
|
|
while j < len(lines):
|
|
stripped = lines[j].strip()
|
|
# 같은 들여쓰기 수준의 elif/else/if __name__/def
|
|
indent = len(lines[i]) - len(lines[i].lstrip())
|
|
line_indent = len(lines[j]) - len(lines[j].lstrip())
|
|
if stripped and line_indent <= indent and (
|
|
stripped.startswith('elif ') or
|
|
stripped.startswith('else:') or
|
|
stripped.startswith('if __name__') or
|
|
(stripped.startswith('def ') and line_indent == 0)
|
|
):
|
|
break
|
|
j += 1
|
|
mail_ranges.append((start, j))
|
|
i = j
|
|
else:
|
|
i += 1
|
|
|
|
print(f'zioinfo-mail 블록 범위: {mail_ranges}')
|
|
|
|
# 모든 zioinfo-mail 블록 제거
|
|
to_remove = set()
|
|
for start, end in mail_ranges:
|
|
for x in range(start, end):
|
|
to_remove.add(x)
|
|
|
|
clean_lines = [l for i, l in enumerate(lines) if i not in to_remove]
|
|
print(f'제거 후: {len(clean_lines)}줄 (제거: {len(to_remove)}줄)')
|
|
|
|
# 마지막 elif repo 블록의 마지막 줄 찾기 (삽입 위치)
|
|
insert_idx = None
|
|
for i in range(len(clean_lines)-1, 0, -1):
|
|
line = clean_lines[i]
|
|
if 'notify_itsm' in line and line.startswith(' '): # 12칸 들여쓰기
|
|
insert_idx = i + 1
|
|
break
|
|
|
|
# 삽입 전 컨텍스트
|
|
if insert_idx:
|
|
print(f'\n삽입 위치: {insert_idx}')
|
|
for x in range(max(0, insert_idx-3), min(len(clean_lines), insert_idx+2)):
|
|
print(f' {x+1}: {repr(clean_lines[x].rstrip()[:70])}')
|
|
|
|
# 현재 들여쓰기 스타일 파악 (elif repo 줄의 들여쓰기)
|
|
elif_indent = ' ' # 기본 8칸
|
|
for line in clean_lines:
|
|
if 'elif repo ==' in line:
|
|
elif_indent = line[:len(line) - len(line.lstrip())]
|
|
break
|
|
|
|
print(f'\n들여쓰기: {repr(elif_indent)}')
|
|
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 insert_idx:
|
|
clean_lines.insert(insert_idx, MAIL_BLOCK)
|
|
else:
|
|
clean_lines.append(MAIL_BLOCK)
|
|
|
|
# 파일 저장 (로컬)
|
|
new_content = ''.join(clean_lines)
|
|
|
|
# 문법 검사
|
|
import ast
|
|
try:
|
|
ast.parse(new_content)
|
|
print('\n✅ 문법 OK')
|
|
except SyntaxError as e:
|
|
print(f'\n❌ 문법 오류: {e}')
|
|
# 오류 위치 주변 표시
|
|
error_lines = new_content.split('\n')
|
|
for x in range(max(0, e.lineno-3), min(len(error_lines), e.lineno+2)):
|
|
print(f' {x+1}: {error_lines[x][:70]}')
|
|
import sys; sys.exit(1)
|
|
|
|
# 서버에 업로드
|
|
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
|
|
f.write(new_content)
|
|
print('업로드 완료')
|
|
|
|
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()[:400])
|
|
|
|
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('포트 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()
|