132 lines
6.4 KiB
Python
132 lines
6.4 KiB
Python
"""zioinfo-mail Gitea repo 올바른 소스로 재설정 + webhook 서버 수정"""
|
|
import paramiko, sys, json, base64, time, os, shutil, subprocess, tempfile
|
|
|
|
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()
|
|
J = 'http://127.0.0.1:9080'; JA = 'admin:Admin@2026!'
|
|
|
|
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])
|
|
|
|
# ── 1. deploy_server.py 문법 오류 확인 ────────────────────────
|
|
run('deploy_server 문법 확인', 'python3 -m py_compile /opt/zioinfo/deploy_server.py && echo OK || echo FAIL')
|
|
run('deploy_server 오류 상세', 'python3 /opt/zioinfo/deploy_server.py --check 2>&1 | head -5 || python3 -c "import ast; ast.parse(open(\'/opt/zioinfo/deploy_server.py\').read()); print(\'syntax ok\')" 2>&1')
|
|
run('포트 9999', 'fuser -k 9999/tcp 2>/dev/null; sleep 1; systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
|
|
|
|
# ── 2. temp dir에서 올바른 repo 생성 ─────────────────────────
|
|
print('\n━━ 2. zioinfo-mail 소스 → temp git repo → bundle ━━')
|
|
|
|
# GUARDiA 바깥 temp 디렉토리 사용
|
|
TMPDIR = tempfile.mkdtemp(prefix='zioinfo_mail_')
|
|
WS = 'C:/GUARDiA/workspace/zioinfo-mail'
|
|
|
|
EXCLUDE_DIRS = {'node_modules', '__pycache__', '.git', '.pytest_cache'}
|
|
EXCLUDE_FILES = {'.pyc', '.db'}
|
|
|
|
def copy_tree(src, dst):
|
|
for root, dirs, files in os.walk(src):
|
|
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS]
|
|
rel = os.path.relpath(root, src)
|
|
dstdir = os.path.join(dst, rel) if rel != '.' else dst
|
|
os.makedirs(dstdir, exist_ok=True)
|
|
for fn in files:
|
|
if any(fn.endswith(e) for e in EXCLUDE_FILES):
|
|
continue
|
|
shutil.copy2(os.path.join(root, fn), os.path.join(dstdir, fn))
|
|
|
|
copy_tree(WS, TMPDIR)
|
|
print(f' 복사 완료: {TMPDIR}')
|
|
|
|
# .gitignore 생성
|
|
with open(os.path.join(TMPDIR, '.gitignore'), 'w') as f:
|
|
f.write('node_modules/\ndist/\n__pycache__/\n*.pyc\n.env\n*.db\n')
|
|
|
|
# git init + commit (GUARDiA repo 바깥)
|
|
subprocess.run(['git', 'init', '-b', 'main', TMPDIR], capture_output=True)
|
|
subprocess.run(['git', '-C', TMPDIR, 'config', 'user.email', 'ci@zioinfo.co.kr'], capture_output=True)
|
|
subprocess.run(['git', '-C', TMPDIR, 'config', 'user.name', 'CI Bot'], capture_output=True)
|
|
subprocess.run(['git', '-C', TMPDIR, 'add', '-A'], capture_output=True)
|
|
r = subprocess.run(['git', '-C', TMPDIR, 'commit', '-m', 'feat: zioinfo-mail webmail system'],
|
|
capture_output=True, text=True, encoding='utf-8', errors='replace')
|
|
print(f' commit: {r.stdout.strip()[:80]}')
|
|
|
|
# bundle
|
|
bundle_path = os.path.join(tempfile.gettempdir(), 'zioinfo_mail.bundle')
|
|
subprocess.run(['git', '-C', TMPDIR, 'bundle', 'create', bundle_path, '--all'],
|
|
capture_output=True, timeout=120)
|
|
size = os.path.getsize(bundle_path) // 1024
|
|
print(f' bundle: {size}KB')
|
|
|
|
# 서버 전송
|
|
sftp.put(bundle_path, '/tmp/mail_correct.bundle')
|
|
os.remove(bundle_path)
|
|
shutil.rmtree(TMPDIR)
|
|
|
|
GITEA_URL = 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git'
|
|
run('Gitea force push',
|
|
f'rm -rf /tmp/zmail_push && '
|
|
f'git clone /tmp/mail_correct.bundle /tmp/zmail_push 2>/dev/null && '
|
|
f'cd /tmp/zmail_push && '
|
|
f'git remote set-url origin "{GITEA_URL}" && '
|
|
f'git push origin main --force 2>&1 | tail -3 && '
|
|
f'rm -rf /tmp/zmail_push /tmp/mail_correct.bundle && echo pushed', timeout=120)
|
|
|
|
# Gitea 파일 확인
|
|
run('Gitea 파일 목록 확인',
|
|
f'curl -sf "http://127.0.0.1:9003/api/v1/repos/zio/zioinfo-mail/contents/" '
|
|
f'-H "Authorization: Basic {G}" 2>/dev/null | '
|
|
'python3 -c "import sys,json; [print(f[\'name\'],f[\'type\']) for f in json.load(sys.stdin)[:8]]" 2>/dev/null')
|
|
|
|
# Jenkinsfile도 별도 업로드 (force push 후 없을 수 있음)
|
|
_, o, _ = c.exec_command(
|
|
f'curl -sf "http://127.0.0.1:9003/api/v1/repos/zio/zioinfo-mail/contents/Jenkinsfile" '
|
|
f'-H "Authorization: Basic {G}" 2>/dev/null | python3 -c "import sys,json; print(json.load(sys.stdin).get(\'sha\',\'\'))" 2>/dev/null', timeout=10)
|
|
sha = o.read().decode('utf-8','replace').strip()
|
|
print(f'\nJenkinsfile SHA: {sha[:8] if sha else "없음"}')
|
|
|
|
if not sha:
|
|
# 새로 업로드
|
|
jf = open('C:/GUARDiA/workspace/zioinfo-mail/Jenkinsfile', encoding='utf-8').read()
|
|
enc = base64.b64encode(jf.encode('utf-8')).decode()
|
|
payload = json.dumps({"message": "ci: add Jenkinsfile", "content": enc, "branch": "main"})
|
|
with sftp.open('/tmp/jf3.json', 'w') as f: f.write(payload)
|
|
run('Jenkinsfile 업로드',
|
|
f'curl -sf -X POST "http://127.0.0.1:9003/api/v1/repos/zio/zioinfo-mail/contents/Jenkinsfile" '
|
|
f'-H "Authorization: Basic {G}" -H "Content-Type: application/json" '
|
|
f'--data @/tmp/jf3.json 2>/dev/null | '
|
|
'python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get(\'content\',{}).get(\'name\',d.get(\'message\',\'?\')))" 2>/dev/null')
|
|
|
|
# ── 3. Jenkins 재빌드 ─────────────────────────────────────────
|
|
print('\n━━ 3. Jenkins 재빌드 ━━')
|
|
_, o, _ = c.exec_command(f'curl -sf -u "{JA}" {J}/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 "{JA}" -H "{CH}" {J}/job/zioinfo-mail/build 2>/dev/null && echo "트리거됨"')
|
|
|
|
print('빌드 대기 (120초)...')
|
|
for _ in range(24):
|
|
time.sleep(5)
|
|
_, o2, _ = c.exec_command(
|
|
f'curl -sf -u "{JA}" {J}/job/zioinfo-mail/lastBuild/api/json 2>/dev/null', timeout=10)
|
|
try:
|
|
d = json.loads(o2.read().decode('utf-8','replace'))
|
|
n = d.get('number','?'); r2 = d.get('result','진행중'); b = d.get('building',True)
|
|
print(f' #{n}: {r2} building={b}')
|
|
if not b: break
|
|
except: pass
|
|
|
|
run('콘솔 로그 (마지막)',
|
|
f'curl -sf -u "{JA}" {J}/job/zioinfo-mail/lastBuild/consoleText 2>/dev/null | tail -20')
|
|
|
|
sftp.close(); c.close()
|
|
print('\n=== 완료 ===')
|