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

77 lines
3.5 KiB
Python

"""Jenkinsfile 확인 + webhook 포트 수정 + 재빌드"""
import paramiko, sys, json, base64, 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()
J = 'http://127.0.0.1:9080'; A = 'admin:Admin@2026!'
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])
return _
# 1. Gitea에 Jenkinsfile 있는지 확인
run('Gitea repo 파일 목록',
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)]" 2>/dev/null')
# Jenkinsfile 없으면 다시 업로드
_, 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()
if not sha:
print('\nJenkinsfile 없음 → Gitea API로 업로드')
jf = open('C:/GUARDiA/workspace/zioinfo-mail/Jenkinsfile', encoding='utf-8').read()
import base64 as b64
encoded = b64.b64encode(jf.encode('utf-8')).decode()
payload = json.dumps({"message": "ci: add Jenkinsfile", "content": encoded, "branch": "main"})
with sftp.open('/tmp/jf2.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/jf2.json 2>/dev/null | '
'python3 -c "import sys,json; d=json.load(sys.stdin); print(\'OK:\', d.get(\'content\',{}).get(\'name\',\'?\'))" 2>/dev/null')
else:
print(f'\nJenkinsfile SHA: {sha[:8]} → 있음 ✅')
# 2. webhook 서버 포트 충돌 수정
run('webhook 서버 상태', 'systemctl status zioinfo-deploy --no-pager | head -5')
run('포트 9999 kill + 재시작',
'fuser -k 9999/tcp 2>/dev/null; sleep 1; '
'systemctl restart zioinfo-deploy && sleep 3 && systemctl is-active zioinfo-deploy')
# 3. crumb + 재빌드
_, o, _ = c.exec_command(f'curl -sf -u "{A}" {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 "{A}" -H "{CH}" {J}/job/zioinfo-mail/build 2>/dev/null && echo "트리거됨"')
print('\n빌드 대기 (90초)...')
for i in range(18):
time.sleep(5)
_, o2, _ = c.exec_command(
f'curl -sf -u "{A}" {J}/job/zioinfo-mail/lastBuild/api/json 2>/dev/null', timeout=10)
try:
d = json.loads(o2.read().decode('utf-8','replace'))
building = d.get('building', True)
result = d.get('result', '진행중')
num = d.get('number', '?')
print(f' #{num}: {result} building={building}')
if not building: break
except: pass
run('콘솔 로그 (마지막 20줄)',
f'curl -sf -u "{A}" {J}/job/zioinfo-mail/lastBuild/consoleText 2>/dev/null | tail -20')
sftp.close(); c.close()