zioinfo-mail/scripts/setup/extract_stash_files.py
DESKTOP-TKLFCPR\ython 53f34835f1 fix(zioinfo): add /var/www/zioinfo copy step in deploy + restore stash files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 20:42:12 +09:00

102 lines
4.5 KiB
Python

"""stash에서 핵심 파일만 추출 → 빌드 → 배포"""
import paramiko, sys, time, shutil, os
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()
def run(label, cmd, timeout=60):
print(f'\n[{label}]')
_, o, e = c.exec_command(cmd, timeout=timeout)
out = o.read().decode('utf-8','replace').strip()
if out: print(out[:600])
return out
SRC = '/opt/zioinfo/src'
# 1. stash에서 frontend 파일만 직접 추출
run('stash에서 Company.jsx 추출',
f'git -C {SRC} checkout stash -- frontend/src/pages/Company.jsx 2>&1 || echo FAIL')
run('stash에서 Company.css 추출',
f'git -C {SRC} checkout stash -- frontend/src/pages/Company.css 2>&1 || echo FAIL')
run('stash에서 Home.jsx 추출 (변경 있으면)',
f'git -C {SRC} checkout stash -- frontend/src/pages/Home.jsx 2>&1 || echo SKIP')
run('stash에서 java 파일 추출',
f'''git -C {SRC} checkout stash -- \
backend/src/main/java/kr/co/zioinfo/web/config/DataInitializer.java \
backend/src/main/java/kr/co/zioinfo/web/controller/AdminController.java \
backend/src/main/java/kr/co/zioinfo/web/controller/ApiController.java 2>&1 || echo SKIP''')
# 2. 추출된 파일 확인
run('Company.jsx 줄 수', f'wc -l {SRC}/frontend/src/pages/Company.jsx 2>/dev/null')
run('변경된 파일 확인', f'git -C {SRC} diff --stat HEAD 2>/dev/null | head -10')
# 3. npm 빌드 + /var/www/zioinfo 복사
run('npm 빌드',
f'cd {SRC}/frontend && npm run build 2>&1 | tail -5', timeout=120)
run('/var/www/zioinfo 복사',
f'cp -r {SRC}/backend/src/main/resources/static/. /var/www/zioinfo/ && echo "copied $(ls /var/www/zioinfo/assets | wc -l) files"')
# 4. 로컬로 파일 다운로드
print('\n[로컬 workspace 업데이트]')
key_files = [
'frontend/src/pages/Company.jsx',
'frontend/src/pages/Company.css',
'frontend/src/pages/Home.jsx',
'backend/src/main/java/kr/co/zioinfo/web/config/DataInitializer.java',
'backend/src/main/java/kr/co/zioinfo/web/controller/AdminController.java',
'backend/src/main/java/kr/co/zioinfo/web/controller/ApiController.java',
]
downloaded = []
for f in key_files:
remote = f'{SRC}/{f}'
local_ws = f'C:/GUARDiA/workspace/zioinfo-web/{f}'
local_repo = f'C:/GUARDiA/repos/zioinfo-web/{f}'
try:
os.makedirs(os.path.dirname(local_ws), exist_ok=True)
os.makedirs(os.path.dirname(local_repo), exist_ok=True)
sftp.get(remote, local_ws)
shutil.copy2(local_ws, local_repo)
print(f' OK: {f}')
downloaded.append(f)
except Exception as e:
print(f' SKIP {f}: {e}')
# 5. repos commit + Gitea bundle push
if downloaded:
import subprocess
subprocess.run(['git', '-C', 'C:/GUARDiA/repos/zioinfo-web', 'add', '-A'])
r = subprocess.run(['git', '-C', 'C:/GUARDiA/repos/zioinfo-web', 'status', '--short'],
capture_output=True, text=True)
if r.stdout.strip():
subprocess.run(['git', '-C', 'C:/GUARDiA/repos/zioinfo-web', 'commit',
'-m', 'restore: extract Company.jsx and backend from server stash'])
print('\n[repos 커밋 완료]')
# bundle push
bundle = 'C:/GUARDiA/repos/zioinfo-web.bundle'
subprocess.run(['git', '-C', 'C:/GUARDiA/repos/zioinfo-web', 'bundle', 'create', bundle, '--all'])
sftp.put(bundle, '/tmp/zioinfo-web.bundle')
os.remove(bundle)
run('Gitea push',
"rm -rf /tmp/zw_push && git clone /tmp/zioinfo-web.bundle /tmp/zw_push 2>/dev/null && "
"cd /tmp/zw_push && "
"git remote set-url origin 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio/zioinfo-web.git' && "
"git push origin main --force 2>&1 | tail -3 && "
"rm -rf /tmp/zw_push /tmp/zioinfo-web.bundle && echo 'pushed'", timeout=120)
else:
print('\n변경 없음 (이미 최신)')
# 6. Spring Boot 재시작 (mvn build는 시간 오래 걸리므로 스킵, /var/www만 업데이트)
run('zioinfo 서비스 재시작', 'systemctl restart zioinfo && sleep 3 && systemctl is-active zioinfo')
# 7. 최종 확인
run('Company.jsx 최신 (greeting 내용)',
f'grep -c "안녕하십니까\|홍영택" {SRC}/frontend/src/pages/Company.jsx 2>/dev/null')
run('/var/www/zioinfo 최신 파일',
'ls -la /var/www/zioinfo/assets/ | sort -k6,7 -r | head -5')
sftp.close()
c.close()
print('\n=== 완료 ===')