zioinfo-mail/scripts/setup/setup_mail_repo.py
2026-06-01 21:55:48 +09:00

176 lines
7.8 KiB
Python

"""zioinfo-mail repos 생성 + workspace 소스 동기화 + Gitea push + deploy_server 수정"""
import paramiko, subprocess, sys, os, shutil, base64, json, time
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()
G = base64.b64encode(b'zio:Zio@Admin2026!').decode()
def run(label, cmd, timeout=30):
print(f'\n[{label}]')
_, o, _ = c.exec_command(cmd, timeout=timeout)
print(o.read().decode('utf-8','replace').strip()[:500])
REPO_PATH = 'C:/GUARDiA/repos/zioinfo-mail'
WS_PATH = 'C:/GUARDiA/workspace/zioinfo-mail'
# ── 1. repos/zioinfo-mail git 초기화 ────────────────────────
print('\n━━ 1. repos/zioinfo-mail git 초기화 ━━')
os.makedirs(REPO_PATH, exist_ok=True)
# workspace → repos 동기화 (node_modules, dist, __pycache__ 제외)
EXCLUDE = {'node_modules', 'dist', '__pycache__', '.git', '*.pyc', '.pytest_cache'}
def sync_dir(src, dst):
for root, dirs, files in os.walk(src):
dirs[:] = [d for d in dirs if d not in EXCLUDE and not d.endswith('.egg-info')]
rel = os.path.relpath(root, src)
dst_dir = os.path.join(dst, rel) if rel != '.' else dst
os.makedirs(dst_dir, exist_ok=True)
for fn in files:
if fn.endswith(('.pyc',)):
continue
s = os.path.join(root, fn)
d = os.path.join(dst_dir, fn)
shutil.copy2(s, d)
sync_dir(WS_PATH, REPO_PATH)
print(f' 동기화 완료')
# git init + commit
r = subprocess.run(['git', '-C', REPO_PATH, 'rev-parse', '--git-dir'], capture_output=True)
if r.returncode != 0:
subprocess.run(['git', '-C', REPO_PATH, 'init'], capture_output=True)
subprocess.run(['git', '-C', REPO_PATH, 'config', 'user.email', 'ci@zioinfo.co.kr'], capture_output=True)
subprocess.run(['git', '-C', REPO_PATH, 'config', 'user.name', 'CI Bot'], capture_output=True)
# .gitignore 생성
with open(f'{REPO_PATH}/.gitignore', 'w') as f:
f.write('node_modules/\ndist/\n__pycache__/\n*.pyc\n.env\n*.db\n.pytest_cache/\n')
subprocess.run(['git', '-C', REPO_PATH, 'add', '-A'], capture_output=True)
r2 = subprocess.run(['git', '-C', REPO_PATH, 'status', '--short'],
capture_output=True, text=True)
if r2.stdout.strip():
subprocess.run(['git', '-C', REPO_PATH, 'commit', '-m', 'feat: initial zioinfo-mail webmail system'],
capture_output=True)
print(' 커밋 완료')
else:
print(' 변경 없음')
r3 = subprocess.run(['git', '-C', REPO_PATH, 'log', '--oneline', '-2'],
capture_output=True, text=True)
print(f' git log: {r3.stdout.strip()}')
# ── 2. bundle → 서버 → Gitea push ───────────────────────────
print('\n━━ 2. Gitea push ━━')
bundle = f'{REPO_PATH}.bundle'
subprocess.run(['git', '-C', REPO_PATH, 'bundle', 'create', bundle, '--all'],
capture_output=True, timeout=120)
size = os.path.getsize(bundle) // 1024
print(f' bundle: {size}KB → 서버 전송...')
sftp.put(bundle, '/tmp/mail.bundle')
os.remove(bundle)
GITEA_URL = 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git'
run('Gitea push',
f'rm -rf /tmp/mail_push && '
f'git clone /tmp/mail.bundle /tmp/mail_push 2>/dev/null && '
f'cd /tmp/mail_push && '
f'git remote set-url origin "{GITEA_URL}" && '
f'git push origin main --force 2>&1 | tail -3 && '
f'rm -rf /tmp/mail_push /tmp/mail.bundle && echo "pushed"', timeout=120)
# ── 3. deploy_server.py 수정 (직접 편집) ─────────────────────
print('\n━━ 3. deploy_server.py zioinfo-mail 추가 ━━')
# 현재 deploy_server.py 내용 확인
_, o, _ = c.exec_command('cat /opt/zioinfo/deploy_server.py', timeout=15)
content = o.read().decode('utf-8','replace')
if 'zioinfo-mail' in content:
print(' 이미 있음')
else:
# 마지막 elif 블록 찾기 (guardia-docs 또는 guardia-messenger)
mail_block = '''
elif repo == "zioinfo-mail":
SRC = "/opt/mail"
ok = run_steps(repo, [
("git pull", ["bash", "-c",
f"[ -d {SRC}/src/.git ] && git -C {SRC}/src fetch origin main && git -C {SRC}/src reset --hard origin/main"
" || git clone 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git' " + f"{SRC}/src"]),
("npm build", ["bash", "-c",
f"cd {SRC}/src/frontend && npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps && npm run build"]),
("copy dist", ["bash", "-c", f"mkdir -p /var/www/mail && cp -r {SRC}/src/dist/. /var/www/mail/"]),
("pip install", ["bash", "-c",
f"{SRC}/venv/bin/pip install -r {SRC}/src/backend/requirements.txt -q"]),
("rsync", ["bash", "-c",
f"rsync -a --exclude=__pycache__ --exclude=.git --exclude='*.pyc' --exclude='.env' {SRC}/src/backend/ {SRC}/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")
'''
# def notify_itsm 함수 앞에 삽입하거나, guardia-docs 블록 찾기
if 'guardia-docs' in content:
# guardia-docs 블록 찾아서 그 다음에 삽입
import re
# 마지막 elif 블록 끝(다음 elif 또는 else 직전) 찾기
lines = content.split('\n')
insert_idx = None
for i, line in enumerate(lines):
if 'elif repo == "guardia-docs"' in line:
# 이 블록의 끝 찾기
j = i + 1
while j < len(lines) and (lines[j].startswith(' ') or not lines[j].strip()):
j += 1
insert_idx = j
break
if insert_idx:
lines.insert(insert_idx, mail_block)
new_content = '\n'.join(lines)
else:
new_content = content + mail_block
else:
new_content = content + mail_block
with sftp.open('/opt/zioinfo/deploy_server.py', 'w') as f:
f.write(new_content)
print(' 추가 완료')
run('zioinfo-mail 확인',
"grep -n 'zioinfo-mail' /opt/zioinfo/deploy_server.py | head -5")
run('webhook 재시작',
'systemctl restart zioinfo-deploy && sleep 2 && systemctl is-active zioinfo-deploy')
# ── 4. Jenkins 재빌드 ─────────────────────────────────────────
print('\n━━ 4. Jenkins 재빌드 ━━')
J_A = 'admin:Admin@2026!'
_, o, _ = c.exec_command(f'curl -sf -u "{J_A}" http://127.0.0.1:9080/crumbIssuer/api/json 2>/dev/null', timeout=10)
try:
cd = json.loads(o.read().decode('utf-8','replace').strip())
CH = f'{cd["crumbRequestField"]}: {cd["crumb"]}'
except:
CH = 'Jenkins-Crumb: x'
run('재빌드 트리거',
f'curl -sf -X POST -u "{J_A}" -H "{CH}" http://127.0.0.1:9080/job/zioinfo-mail/build 2>/dev/null && echo "트리거됨"')
print('\n빌드 대기 (60초)...')
time.sleep(60)
run('빌드 #2 결과',
f'curl -sf -u "{J_A}" http://127.0.0.1:9080/job/zioinfo-mail/lastBuild/api/json 2>/dev/null | '
'python3 -c "import sys,json; d=json.load(sys.stdin); '
'print(\'build #\'+str(d[\'number\']),d.get(\'result\',\'진행중\'),\'building:\',d.get(\'building\'))" 2>/dev/null')
run('빌드 콘솔 (마지막)',
f'curl -sf -u "{J_A}" http://127.0.0.1:9080/job/zioinfo-mail/lastBuild/consoleText 2>/dev/null | tail -15')
sftp.close(); c.close()
print('\n=== 완료 ===')