61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""deploy_server.py trigger_jenkins에 token 추가"""
|
|
import paramiko, sys, json
|
|
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)
|
|
|
|
def run(label, cmd, timeout=20):
|
|
print(f'\n[{label}]')
|
|
_, o, _ = c.exec_command(cmd, timeout=timeout)
|
|
out = o.read().decode('utf-8','replace').strip()
|
|
if out: print(out[:600])
|
|
return out
|
|
|
|
# 현재 trigger_jenkins 함수 확인
|
|
run('trigger_jenkins 현재 코드',
|
|
"sed -n '35,60p' /opt/zioinfo/deploy_server.py")
|
|
|
|
# token 추가: /job/{job}/build → /job/{job}/build?token=gitea-build-2026
|
|
run('token 추가 수정', r"""
|
|
python3 -c "
|
|
with open('/opt/zioinfo/deploy_server.py', 'r') as f:
|
|
content = f.read()
|
|
|
|
# token 없으면 추가
|
|
if 'token=gitea-build-2026' not in content:
|
|
content = content.replace(
|
|
'f\"{JENKINS_URL}/job/{job}/build\"',
|
|
'f\"{JENKINS_URL}/job/{job}/build?token=gitea-build-2026\"'
|
|
)
|
|
with open('/opt/zioinfo/deploy_server.py', 'w') as f:
|
|
f.write(content)
|
|
print('수정 완료')
|
|
else:
|
|
print('이미 token 있음')
|
|
"
|
|
""")
|
|
|
|
run('수정 후 확인',
|
|
"grep -n 'trigger_jenkins\|/build\|token' /opt/zioinfo/deploy_server.py | head -10")
|
|
|
|
run('서비스 재시작',
|
|
'systemctl restart zioinfo-deploy && sleep 2 && systemctl is-active zioinfo-deploy')
|
|
|
|
# 테스트
|
|
import time
|
|
run('배포 트리거',
|
|
"curl -sf -X POST http://localhost:9999 "
|
|
"-H 'Content-Type: application/json' "
|
|
"-H 'X-Gitea-Event: push' "
|
|
"-d '{\"repository\":{\"name\":\"guardia-itsm\"},\"ref\":\"refs/heads/main\"}' 2>/dev/null")
|
|
|
|
time.sleep(20)
|
|
run('Jenkins 새 빌드 확인',
|
|
'curl -sf -u "admin:Admin@2026!" http://127.0.0.1:9080/job/guardia-itsm/api/json 2>/dev/null | '
|
|
"python3 -c \"import sys,json; d=json.load(sys.stdin); "
|
|
"print('lastBuild #'+str(d['lastBuild']['number']), "
|
|
"'result:', d.get('lastBuild',{}).get('result','?'), "
|
|
"'nextBuild:', d['nextBuildNumber'])\" 2>/dev/null")
|
|
|
|
c.close()
|