This commit is contained in:
jiangzhongxiang 2019-09-27 08:56:11 +08:00
commit ac0b99e8e5
2 changed files with 92 additions and 89 deletions

View File

@ -147,6 +147,7 @@ public class GameController {
String evaluateStartTime = requestTime.toString();
JSONObject cost = new JSONObject();
cost.put("evaluateStartTime", evaluateStartTime);
JedisUtil.set("timeCost:" + tpiID + ":" + buildID, cost.toJSONString());
Boolean reuseFlag = reusePod == null ? Boolean.FALSE : reusePod;
needPortMapping = needPortMapping == null ? 0 : needPortMapping;

View File

@ -156,96 +156,98 @@ public class GameService {
* @param tpiGitURL
* @throws GameException
*/
public void gitPull(String path, String tpiGitURL, Integer contentModified) throws Exception {
logger.debug("git pull, path: {}, tpiGitURL: {}", path, tpiGitURL);
String tpiRepoName = GameHelper.getRepoName(tpiGitURL);
String tpiRepoPath = path + File.separator + tpiRepoName;
String tpiID = path.substring(path.lastIndexOf("_") + 1);
// pull 操作之前如果tpi本地版本库不存在则克隆主机名为origin
File file = FileUtils.getFile(tpiRepoPath);
if (!file.exists()) {
logger.warn("版本库不存在,先克隆! tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
}
// pull之前先判断版本库的origin主机是否存在若不存在则先创建
String cmd = "cd " + tpiRepoPath + " && git remote | grep ^origin$";
JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd);
if (remoteExist.getInteger("exitStatus") != 0) {
if (remoteExist.getString("out").contains("fatal")) {
logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL: {}, 检查cmd: {}", tpiID, tpiGitURL, cmd);
ShellUtil.execute("cd " + tpiRepoPath + " && git init");
}
logger.warn("origin主机名不存在先添加主机名及其git地址remoteName: {}, tpiGitURL: {}, tpiID: {}", "origin", tpiGitURL,
tpiID);
cmd = "cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL;
JSONObject addRemote = ShellUtil.executeAndGetExitStatus(cmd, 3);
if (addRemote.getInteger("exitStatus") != 0) {
logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}, cmd: {}", tpiGitURL, tpiID, cmd);
throw new GameException("origin主机名不存在无法完成pull操作,tpiID:" + tpiID);
} else {
logger.debug("origin主机名不存在,添加主机名成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
// 执行pull操作
long currentPullStart = System.currentTimeMillis();
logger.debug("##----> gitpull 开始包括git config ; set remote当前时间为" + currentPullStart);
String pullCommand = "cd " + tpiRepoPath + " && git config user.email educoder@163.com"
+ " && git config user.name educoder " + "&& git config core.fileMode false" + // 忽略文件权限
"&& git remote set-url origin " + tpiGitURL + " && git pull origin master"; // 本地直接清除所有改动
JSONObject result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
logger.debug("result: {}, out: {}", result.getInteger("exitStatus"), result.getString("out"));
if (result.getString("out").contains("commit your changes or stash them before you can merge")) {
String stashCmd = "cd " + tpiRepoPath + " && git stash save "
+ TimeHelper.getCurrentTime("yyyyMMddHHmmss");
logger.info("tpi {} git pull与未commit代码冲突,先保存本地代码, cmd:{}", tpiID, stashCmd);
result = ShellUtil.executeAndGetExitStatus(stashCmd, 3);
if (result.getInteger("exitStatus") != 0) {
logger.error("tpi {} git pull与未commit代码冲突,保存本地代码失败, cmd:{}, error: {}", tpiID, stashCmd, result.getString("out"));
throw new GameException("git pull与未commit代码冲突,保存本地代码失败,tpiID:" + tpiID);
}
logger.info("tpi {} git pull与未commit代码冲突, 保存本地代码代码后, 再pull", tpiID);
result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
} else if(result.getString("out").contains("Merge conflict")
|| result.getString("out").contains("Pull is not possible because you have unmerged files")) {
String resetCmd = "cd " + tpiRepoPath + " && git reset --hard FETCH_HEAD ";
logger.info("tpi {} git pull与本地commit冲突, 使用远程版本, cmd:{}", tpiID, resetCmd);
JSONObject resetResult = ShellUtil.executeAndGetExitStatus(resetCmd, 3);
if (resetResult.getInteger("exitStatus") != 0) {
logger.error("tpi {}git pull与本地commit冲突, 使用远程版本失败, cmd:{}, error: {}", tpiID, resetCmd, resetResult.getString("out"));
throw new GameException("git pull与本地commit冲突, 使用远程版本失败,tpiID:" + tpiID);
} else {
return;
}
public void gitPull(String path, String tpiGitURL, Integer contentModified) throws Exception {
logger.debug("git pull, path: {}, tpiGitURL: {}", path, tpiGitURL);
String tpiRepoName = GameHelper.getRepoName(tpiGitURL);
String tpiRepoPath = path + File.separator + tpiRepoName;
String tpiID = path.substring(path.lastIndexOf("_") + 1);
// pull 操作之前如果tpi本地版本库不存在则克隆主机名为origin
File file = FileUtils.getFile(tpiRepoPath);
if (!file.exists()) {
logger.warn("版本库不存在,先克隆! tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
}
// pull之前先判断版本库的origin主机是否存在若不存在则先创建
String cmd = "cd " + tpiRepoPath + " && git remote | grep ^origin$";
JSONObject remoteExist = ShellUtil.executeAndGetExitStatus(cmd);
if (remoteExist.getInteger("exitStatus") != 0) {
if (remoteExist.getString("out").contains("fatal")) {
logger.warn("版本库不完整先进行init操作, tpiID: {}, tpiGitURL: {}, 检查cmd: {}", tpiID, tpiGitURL, cmd);
ShellUtil.execute("cd " + tpiRepoPath + " && git init");
}
logger.warn("origin主机名不存在先添加主机名及其git地址remoteName: {}, tpiGitURL: {}, tpiID: {}", "origin", tpiGitURL,
tpiID);
cmd = "cd " + tpiRepoPath + " && git remote add origin " + tpiGitURL;
JSONObject addRemote = ShellUtil.executeAndGetExitStatus(cmd, 3);
if (addRemote.getInteger("exitStatus") != 0) {
logger.error("origin主机名不存在,添加主机名失败, tpiGitURL: {}, tpiID: {}, cmd: {}", tpiGitURL, tpiID, cmd);
throw new GameException("origin主机名不存在无法完成pull操作,tpiID:" + tpiID);
} else {
logger.debug("origin主机名不存在,添加主机名成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
// 执行pull操作
long currentPullStart = System.currentTimeMillis();
logger.debug("##----> gitpull 开始包括git config ; set remote当前时间为" + currentPullStart);
String pullCommand = "cd " + tpiRepoPath + " && git config user.email educoder@163.com"
+ " && git config user.name educoder " + "&& git config core.fileMode false" + // 忽略文件权限
"&& git remote set-url origin " + tpiGitURL + " && git pull origin master"; // 本地直接清除所有改动
JSONObject result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
logger.debug("{} git pull result: {}, out: {}", tpiID, result.getInteger("exitStatus"), result.getString("out"));
if (result.getString("out").contains("commit your changes or stash")
|| result.getString("out").contains("would be overwritten by merge")) {
String stashCmd = "cd " + tpiRepoPath + " && git stash save "
+ TimeHelper.getCurrentTime("yyyyMMddHHmmss");
logger.info("tpi {} git pull与未commit代码冲突,先保存本地代码, cmd:{}", tpiID, stashCmd);
result = ShellUtil.executeAndGetExitStatus(stashCmd, 3);
if (result.getInteger("exitStatus") != 0) {
logger.error("tpi {} git pull与未commit代码冲突,保存本地代码失败, cmd:{}, error: {}", tpiID, stashCmd, result.getString("out"));
throw new GameException("git pull与未commit代码冲突,保存本地代码失败,tpiID:" + tpiID);
}
logger.info("tpi {} git pull与未commit代码冲突, 保存本地代码代码后, 再pull", tpiID);
result = ShellUtil.executeAndGetExitStatus(pullCommand, 3);
}
if(result.getString("out").contains("Merge conflict")
|| result.getString("out").contains("have unmerged files")) {
String resetCmd = "cd " + tpiRepoPath + " && git reset --hard FETCH_HEAD ";
logger.info("tpi {} git pull与本地commit冲突, 使用远程版本, cmd:{}", tpiID, resetCmd);
JSONObject resetResult = ShellUtil.executeAndGetExitStatus(resetCmd, 3);
if (resetResult.getInteger("exitStatus") != 0) {
logger.error("tpi {}git pull与本地commit冲突, 使用远程版本失败, cmd:{}, error: {}", tpiID, resetCmd, resetResult.getString("out"));
throw new GameException("git pull与本地commit冲突, 使用远程版本失败,tpiID:" + tpiID);
} else {
return;
}
}
long currentPullEnd = System.currentTimeMillis();
logger.debug("##----> gitpull 结束,总耗时间为:" + (currentPullEnd - currentPullStart));
if (result.getInteger("exitStatus") != 0) {
logger.info("pullCommand: {}, tpiID: {}", pullCommand, tpiID);
logger.info("output: {}, tpiID: {}", result.getString("out"), tpiID);
// 如果pull失败先check一下看是不是远端版本库HEAD丢失如果是进行修复
check(tpiID, tpiGitURL);
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
logger.debug("gitPull冲突重新clone结束, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
// 判段是否成功拉取到更新
int rePullTimes = 10;
while (contentModified == 1 && rePullTimes > 0 && "Already up-to-date.".equals(result.getString("out"))) {
try {
Thread.sleep(200);
} catch (Exception e) {
}
result = ShellUtil.executeAndGetExitStatus(pullCommand, 1);
rePullTimes--;
}
if (rePullTimes == 0) {
logger.error("版本库有更新2s之内拉取版本库更新失败, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
logger.debug("pull成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
}
long currentPullEnd = System.currentTimeMillis();
logger.debug("##----> gitpull 结束,总耗时间为:" + (currentPullEnd - currentPullStart));
if (result.getInteger("exitStatus") != 0) {
logger.info("pullCommand: {}, tpiID: {}", pullCommand, tpiID);
logger.info("output: {}, tpiID: {}", result.getString("out"), tpiID);
// 如果pull失败先check一下看是不是远端版本库HEAD丢失如果是进行修复
check(tpiID, tpiGitURL);
backupPullError(path, tpiRepoPath, tpiID);
// 重新clone
gitClone(path, tpiGitURL, "origin", tpiRepoName, "--depth=1");
logger.debug("gitPull冲突重新clone结束, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
// 判段是否成功拉取到更新
int rePullTimes = 10;
while (contentModified == 1 && rePullTimes > 0 && "Already up-to-date.".equals(result.getString("out"))) {
try {
Thread.sleep(200);
} catch (Exception e) {
}
result = ShellUtil.executeAndGetExitStatus(pullCommand, 1);
rePullTimes--;
}
if (rePullTimes == 0) {
logger.error("版本库有更新2s之内拉取版本库更新失败, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
} else {
logger.debug("pull成功, tpiGitURL: {}, tpiID: {}", tpiGitURL, tpiID);
}
}
}
private void backupPullError(String path, String tpiRepoPath, String tpiID) {
try {