89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
def run_cmd(cmd, check=True):
|
|
"""执行命令"""
|
|
print(f"执行: {cmd}")
|
|
try:
|
|
result = subprocess.run(cmd, shell=True, check=check, capture_output=True, text=True)
|
|
if result.stdout:
|
|
print(result.stdout)
|
|
return result.returncode == 0
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("=" * 50)
|
|
print("Gerrit 完全重新安装")
|
|
print("=" * 50)
|
|
print()
|
|
|
|
gerrit_home = "/home/renjianbo/gerrit_install"
|
|
java_home = "/usr/local/java/jdk-17.0.12+7"
|
|
|
|
# 1. 停止服务
|
|
print("[1/6] 停止服务...")
|
|
os.chdir(f"{gerrit_home}/review_site")
|
|
run_cmd("bin/gerrit.sh stop", check=False)
|
|
time.sleep(2)
|
|
run_cmd("pkill -9 -f gerrit.war", check=False)
|
|
print("✓ 服务已停止\n")
|
|
|
|
# 2. 删除旧安装
|
|
print("[2/6] 删除旧安装...")
|
|
os.chdir(gerrit_home)
|
|
run_cmd("rm -rf review_site", check=False)
|
|
print("✓ 已删除\n")
|
|
|
|
# 3. 检查 war 文件
|
|
print("[3/6] 检查安装包...")
|
|
if not os.path.exists(f"{gerrit_home}/gerrit-3.9.0.war"):
|
|
print("下载 Gerrit...")
|
|
run_cmd(f"cd {gerrit_home} && wget -q https://gerrit-releases.storage.googleapis.com/gerrit-3.9.0.war")
|
|
print("✓ 安装包就绪\n")
|
|
|
|
# 4. 重新初始化
|
|
print("[4/6] 重新初始化...")
|
|
os.chdir(gerrit_home)
|
|
env = os.environ.copy()
|
|
env['JAVA_HOME'] = java_home
|
|
env['PATH'] = f"{java_home}/bin:{env.get('PATH', '')}"
|
|
|
|
cmd = f"{java_home}/bin/java -jar gerrit-3.9.0.war init -d review_site --batch --no-auto-start -D gerrit.canonicalWebUrl=http://101.43.95.130:8080/ --install-plugin=download-commands --install-plugin=replication --install-plugin=reviewnotes"
|
|
run_cmd(cmd)
|
|
print("✓ 初始化完成\n")
|
|
|
|
# 5. 配置启动脚本
|
|
print("[5/6] 配置启动脚本...")
|
|
os.chdir(f"{gerrit_home}/review_site")
|
|
run_cmd("cp bin/gerrit.sh bin/gerrit.sh.bak", check=False)
|
|
with open("bin/gerrit.sh", "r") as f:
|
|
content = f.read()
|
|
new_content = f"#!/bin/bash\nexport JAVA_HOME={java_home}\nexport PATH=$JAVA_HOME/bin:$PATH\n" + content.split("\n", 1)[1] if "\n" in content else content
|
|
with open("bin/gerrit.sh", "w") as f:
|
|
f.write(new_content)
|
|
print("✓ 配置完成\n")
|
|
|
|
# 6. 启动服务
|
|
print("[6/6] 启动服务...")
|
|
run_cmd("bin/gerrit.sh start")
|
|
time.sleep(5)
|
|
run_cmd("bin/gerrit.sh status")
|
|
print("✓ 服务已启动\n")
|
|
|
|
print("=" * 50)
|
|
print("安装完成!")
|
|
print("=" * 50)
|
|
print("\n访问地址: http://101.43.95.130:8080")
|
|
print("第一个登录的用户将自动成为管理员")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|