97 lines
4.3 KiB
Python
97 lines
4.3 KiB
Python
"""zioinfo-mail 소스만 올바르게 Gitea push (GUARDiA 바깥 temp 사용)"""
|
|
import paramiko, subprocess, sys, os, shutil, json, base64, stat
|
|
|
|
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=60):
|
|
print(f'\n[{label}]')
|
|
_, o, _ = c.exec_command(cmd, timeout=timeout)
|
|
print(o.read().decode('utf-8','replace').strip()[:400])
|
|
|
|
# C 드라이브 루트 temp 사용 (GUARDiA 바깥)
|
|
TMPDIR = 'C:\\tmp_mail_repo'
|
|
WS = 'C:\\GUARDiA\\workspace\\zioinfo-mail'
|
|
|
|
# 기존 temp 제거
|
|
def rm_readonly(func, path, _):
|
|
os.chmod(path, stat.S_IWRITE); func(path)
|
|
if os.path.exists(TMPDIR):
|
|
shutil.rmtree(TMPDIR, onerror=rm_readonly)
|
|
os.makedirs(TMPDIR)
|
|
|
|
# 파일 복사
|
|
EXCLUDE = {'node_modules', '__pycache__', '.git', '.pytest_cache', 'dist'}
|
|
for root, dirs, files in os.walk(WS):
|
|
dirs[:] = [d for d in dirs if d not in EXCLUDE]
|
|
rel = os.path.relpath(root, WS)
|
|
dst = os.path.join(TMPDIR, rel) if rel != '.' else TMPDIR
|
|
os.makedirs(dst, exist_ok=True)
|
|
for fn in files:
|
|
if not fn.endswith(('.pyc', '.db')):
|
|
shutil.copy2(os.path.join(root, fn), os.path.join(dst, fn))
|
|
|
|
# .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 (독립 repo)
|
|
GIT = lambda *args: subprocess.run(['git', '-C', TMPDIR, '--git-dir', f'{TMPDIR}\\.git'] + list(args),
|
|
capture_output=True, text=True, encoding='utf-8', errors='replace')
|
|
subprocess.run(['git', 'init', '-b', 'main', TMPDIR], capture_output=True)
|
|
GIT('config', 'user.email', 'ci@zioinfo.co.kr')
|
|
GIT('config', 'user.name', 'CI Bot')
|
|
GIT('add', '-A')
|
|
r = GIT('commit', '-m', 'feat: zioinfo-mail webmail system')
|
|
print(f'commit: {r.stdout.strip()[:60]}')
|
|
print(f'files: {len([f for r2,d,fs in os.walk(TMPDIR) for f in fs if ".git" not in r2])}')
|
|
|
|
# bundle (cwd 방식으로)
|
|
BUNDLE = os.path.join(os.path.dirname(TMPDIR), 'tmp_mail.bundle')
|
|
r2 = subprocess.run(['git', 'bundle', 'create', BUNDLE, '--all'],
|
|
cwd=TMPDIR, capture_output=True)
|
|
size = os.path.getsize(BUNDLE) // 1024
|
|
print(f'bundle: {size}KB')
|
|
|
|
# 서버 전송
|
|
sftp.put(BUNDLE, '/tmp/mail_ok.bundle')
|
|
os.remove(BUNDLE)
|
|
shutil.rmtree(TMPDIR, onerror=rm_readonly)
|
|
print('전송 완료')
|
|
|
|
GITEA_URL = 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-mail.git'
|
|
run('Gitea force push',
|
|
f'rm -rf /tmp/mp && git clone /tmp/mail_ok.bundle /tmp/mp 2>/dev/null && '
|
|
f'cd /tmp/mp && git remote set-url origin "{GITEA_URL}" && '
|
|
f'git push origin main --force 2>&1 | tail -3 && '
|
|
f'rm -rf /tmp/mp /tmp/mail_ok.bundle && echo pushed', timeout=120)
|
|
|
|
# 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:
|
|
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/jf_ok.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/jf_ok.json 2>/dev/null | python3 -c "import sys,json; print(json.load(sys.stdin).get(\'content\',{{}}).get(\'name\',\'OK\'))" 2>/dev/null')
|
|
else:
|
|
print(f'\nJenkinsfile 이미 있음 sha={sha[:8]}')
|
|
|
|
# 확인
|
|
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)[:12]]" 2>/dev/null')
|
|
|
|
sftp.close(); c.close()
|
|
print('\n=== 완료 ===')
|