72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""deploy_server.py git pull → fetch+reset 방식으로 변경 + 서버 클론 강제 리셋"""
|
|
import paramiko, sys
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
client.connect('101.79.17.164', username='root', password='1q2w3e!Q', timeout=15)
|
|
|
|
def run(label, cmd, timeout=40):
|
|
print(f'\n[{label}]')
|
|
_, o, e = client.exec_command(cmd, timeout=timeout)
|
|
out = o.read().decode('utf-8','replace').strip()
|
|
if out: print(out[:500])
|
|
return out
|
|
|
|
GITEA = 'http://zio:Zio%40Admin2026%21@127.0.0.1:9003/zio'
|
|
|
|
# 1. 서버 src 디렉토리 강제 리셋 (diverge 해결)
|
|
for path, repo in [('/opt/guardia/src', 'guardia-itsm'), ('/opt/zioinfo/src', 'zioinfo-web')]:
|
|
run(f'{repo} 강제 리셋', f"""
|
|
git config --global --add safe.directory {path} 2>/dev/null
|
|
git -C {path} remote set-url origin '{GITEA}/{repo}.git'
|
|
git -C {path} fetch origin main
|
|
git -C {path} reset --hard origin/main
|
|
echo "리셋 완료: $(git -C {path} log -1 --oneline)"
|
|
""")
|
|
|
|
# 2. deploy_server.py git pull → fetch+reset 방식으로 교체
|
|
run('deploy_server.py 수정 확인', "grep -n 'git pull\|git -C\|pull origin' /opt/zioinfo/deploy_server.py | head -15")
|
|
|
|
run('git pull 명령 교체', r"""
|
|
# ["git", "-C", SRC, "pull", "origin", "main"] → fetch + reset
|
|
python3 -c "
|
|
import re
|
|
with open('/opt/zioinfo/deploy_server.py', 'r') as f:
|
|
content = f.read()
|
|
|
|
# git pull 단순 명령 교체
|
|
content = content.replace(
|
|
'[\"git\", \"-C\", SRC, \"pull\", \"origin\", \"main\"]',
|
|
'[\"bash\", \"-c\", f\"git -C {SRC} fetch origin main && git -C {SRC} reset --hard origin/main\"]'
|
|
)
|
|
|
|
# bash git pull 교체
|
|
content = content.replace(
|
|
'\"[ -d {SRC}/.git ] && git -C {SRC} pull origin main\"',
|
|
'\"[ -d {SRC}/.git ] && git -C {SRC} fetch origin main && git -C {SRC} reset --hard origin/main\"'
|
|
)
|
|
|
|
with open('/opt/zioinfo/deploy_server.py', 'w') as f:
|
|
f.write(content)
|
|
print('수정 완료')
|
|
"
|
|
""")
|
|
|
|
run('수정 결과 확인', "grep -n 'git.*pull\|fetch\|reset' /opt/zioinfo/deploy_server.py | head -10")
|
|
|
|
# 3. 서비스 재시작
|
|
run('서비스 재시작', 'systemctl restart zioinfo-deploy && sleep 2 && systemctl is-active zioinfo-deploy')
|
|
|
|
# 4. 배포 재트리거
|
|
import time
|
|
for repo in ['guardia-itsm', 'zioinfo-web']:
|
|
run(f'{repo} 배포 트리거',
|
|
f'curl -sf -X POST http://localhost:9999 '
|
|
f'-H "Content-Type: application/json" -H "X-Gitea-Event: push" '
|
|
f'-d \'{{"repository":{{"name":"{repo}"}},"ref":"refs/heads/main"}}\' 2>/dev/null')
|
|
|
|
time.sleep(12)
|
|
run('최종 배포 로그', 'tail -20 /var/log/zioinfo/deploy.log 2>/dev/null')
|
|
|
|
client.close()
|