|
|
@@ -0,0 +1,153 @@ |
|
|
|
package com.imitate.web.module.game.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.imitate.common.annotation.PublicUrl; |
|
|
|
import com.imitate.common.k8s.mgr.ClusterManager; |
|
|
|
import com.imitate.common.k8s.pojo.RunPod; |
|
|
|
import com.imitate.common.k8s.service.DiskService; |
|
|
|
import com.imitate.common.k8s.service.RunPodService; |
|
|
|
import com.imitate.common.util.Base64Util; |
|
|
|
import com.imitate.common.util.R; |
|
|
|
import com.imitate.common.util.TpUtils; |
|
|
|
import com.imitate.web.module.game.service.GameService; |
|
|
|
import com.imitate.web.module.game.service.GitService; |
|
|
|
import com.imitate.web.module.game.service.WebService; |
|
|
|
import com.imitate.web.module.game.thread.code.LocalCheckJob; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import io.swagger.annotations.ApiParam; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @author 悟空 |
|
|
|
* @date 2022/04/12 |
|
|
|
*/ |
|
|
|
@Api(value = "web控制器", hidden = true) |
|
|
|
@RestController |
|
|
|
@RequestMapping("/web") |
|
|
|
public class WebController { |
|
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WebService webService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ClusterManager clusterManager; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private GitService gitService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private GameService gameService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RunPodService runPodService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DiskService diskService; |
|
|
|
|
|
|
|
@Qualifier("gitTaskExecutor") |
|
|
|
@Autowired |
|
|
|
private ThreadPoolTaskExecutor gitTaskExecutor; |
|
|
|
|
|
|
|
|
|
|
|
@PublicUrl(signValidate = true) |
|
|
|
@RequestMapping(path = "/getWebsvc") |
|
|
|
@ApiOperation(value = "getWebsvc", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
|
|
|
public R getWebsvc( |
|
|
|
@ApiParam(name = "tpiID", required = true, value = "tpiID") @RequestParam String tpiID, |
|
|
|
@ApiParam(name = "podType", required = true, value = "pod类型,0为evaluate, 1为webssh,2为evassh") @RequestParam Integer podType, |
|
|
|
@ApiParam(name = "tpiGitURL", required = false, value = "tpi版本库地址") String tpiGitURL, |
|
|
|
@ApiParam(name = "containers", required = true, value = "需要使用的容器,base64编码") @RequestParam String containers, |
|
|
|
@ApiParam(name = "router", required = false, value = "容器内部服务启动的路由地址") @RequestParam(defaultValue = "") String router, |
|
|
|
@ApiParam(name = "needPortMapping", required = false, value = "容器中需要被映射的端口") Integer needPortMapping, |
|
|
|
@ApiParam(name = "bigDataFile", required = false, value = "大数据集文件") String bigDataFile, |
|
|
|
@ApiParam(name = "createImage", required = false, value = "是否为创建镜像") Boolean createImage, |
|
|
|
@ApiParam(name = "survivalSecond", required = false, value = "存活秒数") Integer survivalSecond) throws Exception { |
|
|
|
logger.info("[start]获取web service连接信息,tpiID: {}, podType: {},containers: {}, tpiGitURL: {},needPortMapping: {}, bigDataFile: {}, createImage: {}, survivalSecond: {},router:{}", |
|
|
|
tpiID, podType, containers,tpiGitURL, needPortMapping, bigDataFile, createImage, survivalSecond,router); |
|
|
|
|
|
|
|
//若容器为数组 |
|
|
|
if("W10=".equals(containers)){ |
|
|
|
return R.error(-3,"容器为空数组"); |
|
|
|
} |
|
|
|
|
|
|
|
containers = Base64Util.decode(containers); |
|
|
|
|
|
|
|
|
|
|
|
createImage = createImage != null && createImage; |
|
|
|
JSONObject response = new JSONObject(); |
|
|
|
|
|
|
|
|
|
|
|
//指定默认集群 |
|
|
|
String cluster = "local"; |
|
|
|
|
|
|
|
// 检查本地版本库 |
|
|
|
if (StringUtils.isNotEmpty(tpiGitURL)) { |
|
|
|
if (clusterManager.isLocalCluster(cluster)) { |
|
|
|
String tpiWorkspace = diskService.getTpiWorkspaceHostPath(tpiID); |
|
|
|
boolean localCodeExist = gameService.checkLocalCodeExist(tpiID, tpiGitURL, tpiWorkspace); |
|
|
|
tpiGitURL = Base64Util.decode(tpiGitURL); |
|
|
|
if (!localCodeExist) { |
|
|
|
// 下载本地版本库 |
|
|
|
LocalCheckJob localCheckJob = gameService.getLocalCheckJob(tpiID, tpiGitURL, tpiWorkspace); |
|
|
|
gitTaskExecutor.execute(localCheckJob); |
|
|
|
} |
|
|
|
} else { |
|
|
|
String finalTpiGitURL = tpiGitURL; |
|
|
|
gitTaskExecutor.execute(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
gitService.remoteClusterCheckCode(cluster, Boolean.TRUE, tpiID, finalTpiGitURL); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//返回web服务路径地址 |
|
|
|
String targetUrl = webService.getWebService(cluster,tpiID,podType, containers, bigDataFile, survivalSecond, createImage, needPortMapping,router); |
|
|
|
|
|
|
|
// 环境存在问题则重新获取创建一次 |
|
|
|
if (targetUrl == null) { |
|
|
|
targetUrl = webService.getWebService(cluster,tpiID,podType,containers, bigDataFile, survivalSecond, createImage, needPortMapping,router); |
|
|
|
} |
|
|
|
|
|
|
|
if(targetUrl != null){ |
|
|
|
//测试能否访问 |
|
|
|
int timeout = 120; |
|
|
|
while (webService.testWsdlConnection(targetUrl) != 200 && webService.testWsdlConnection(targetUrl) != 302 && timeout > 0){ |
|
|
|
Thread.sleep(1000); |
|
|
|
timeout--; |
|
|
|
} |
|
|
|
|
|
|
|
if(webService.testWsdlConnection(targetUrl) != 200 && webService.testWsdlConnection(targetUrl) != 302 && timeout <= 0){ |
|
|
|
return R.error(-4,"服务链接: " + targetUrl +" 无法正常访问。"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(targetUrl == null) { |
|
|
|
return R.error(-5,"当前实验使用的用户较多,系统正在智能化为您调度更优质的资源,预计一分钟内完成,请稍后重试!"); |
|
|
|
}else{ |
|
|
|
String podName = TpUtils.buildWebPodName(tpiID, podType); |
|
|
|
RunPod runPod = runPodService.getRunPodByName(podName); |
|
|
|
//存活秒数 |
|
|
|
response.put("remainingTime",webService.getRemainingTime(runPod)); |
|
|
|
response.put("url",targetUrl); |
|
|
|
} |
|
|
|
return R.ok().setData(response); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |