package com.educoder.bridge.controller; import com.alibaba.fastjson.JSONObject; import com.educoder.bridge.exception.GameException; import com.educoder.bridge.service.GameService; import com.educoder.bridge.settings.AppConfig; import com.educoder.bridge.utils.Base64Util; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; /** * created by weishao at 2017/8/7 * 数据迁移控制器,用于保证已经发布的实训不受影响 */ @Api(value = "数据迁移控制器", hidden = true) @RestController @RequestMapping("/dataTransfer") public class DataTransferController extends BaseController{ @Autowired private GameService gameService; @Autowired private AppConfig appConfig; private static final Logger logger = LoggerFactory.getLogger(DataTransferController.class); @RequestMapping(path = "/transfer") @ApiOperation(value = "数据迁移,写测试用例,生成模板脚本,tpi评测脚本", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public JSONObject transfer( @ApiParam(name = "gameInfo", required = true, value = "实训的相关配置,base64编码") @RequestParam String gameInfo, @ApiParam(name = "tpiList", required = true, value = "实训实例的信息,包括tpiID与instanceGitURL") @RequestParam String tpiList) { logger.debug("/dataTransfer/transfer: gameInfo: " + gameInfo + ", tpiList: " + tpiList); JSONObject result = new JSONObject(); // // 将字符串解码后转换为JSON对象 // gameInfo = Base64Util.decode(gameInfo); // JSONObject info = JSONObject.parseObject(gameInfo); // // tpiList = Base64Util.decode(tpiList); // JSONArray list = JSONObject.parseArray(tpiList); // // try { // transferTPM(info); // // for (int i = 0; i < list.size(); i++) { // try { // transferTPI(info.getString("tpmID"), list.getJSONObject(i).getString("tpiID"), // list.getJSONObject(i).getString("instanceGitURL")); // } catch (GameException e) { // logger.error("TPI 转换失败,tpiID:{}", list.getJSONObject(i).getString("tpiID")); // continue; // } // } // }catch (GameException e) { // // logger.error("TPM 转换失败,tpmID:{}", info.getString("tpmID")); // result.put("code", -1); // result.put("msg", "转换失败"); // } result.put("code", 0); result.put("msg", "转换成功"); return result; } @RequestMapping(path = "/transferTPI") @ApiOperation(value = "数据迁移,单个tpi迁移", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public JSONObject transferATPI( @ApiParam(name = "tpmID", required = true, value = "tpmID") @RequestParam String tpmID, @ApiParam(name = "tpiID", required = true, value = "tpiID") @RequestParam String tpiID, @ApiParam(name = "instanceGitURL", required = true, value = "TPI版本库地址,base64编码") @RequestParam String instanceGitURL) throws GameException { JSONObject result = new JSONObject(); instanceGitURL = Base64Util.decode(instanceGitURL); transferTPI (tpmID, tpiID, instanceGitURL); result.put("code", 0); result.put("msg", "转换成功"); return result; } /** * 转换tpm * @param gameInfo */ private void transferTPM (JSONObject gameInfo) throws GameException { logger.debug("DataTransferController: transferTPM: gameInfo: " + gameInfo); // 将测试用例写入工作目录下的文件中 // gameService.generateTestCasesForGame(gameInfo.getString("tpmID"), // gameInfo.getJSONArray("testCases")); // // // 生成Tpm评测脚本到工作目录下指定文件 // gameService.generateTpmEvaluateShellScript(gameInfo); } /** * 转换tpi * @param tpmID * @param tpiID * @param instanceGitURL */ private void transferTPI (String tpmID, String tpiID, String instanceGitURL) throws GameException { logger.debug("DataTransferController: transferTPI: tpmID: " + tpmID + ", tpiID: " + tpiID + ", instanceGitURL: " + instanceGitURL); // String repoName = StringUtil.getRepoName(instanceGitURL); // String path = appConfig.getWorkspace() + File.separator + "myshixun_" + tpiID; // // 删除旧数据 // if (!StringUtils.isEmpty(tpiID)) { // ShellUtil.execute("rm -rf " + path); // } // // 克隆版本库 // gameService.gitClone(path, instanceGitURL, "origin", repoName); // // //根据实训模板评测脚本生成Tpi脚本到工作目录 // gameService.generateTpiEvaluateShellScript(appConfig.getWorkspace() + "/shell/" + tpmID + "/evaluate.sh", // appConfig.getWorkspace() + "/myshixun_" + tpiID + "/" + repoName + "/"); } }