|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
package net.educoder.service;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.google.common.cache.Cache;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import net.educoder.constant.CommonConstants;
|
|
|
|
|
import net.educoder.model.ShellResult;
|
|
|
|
|
import net.educoder.model.dto.CreateCloudHostDto;
|
|
|
|
|
import net.educoder.model.param.ResetCloudHostParam;
|
|
|
|
|
import net.educoder.util.JCloudUtil;
|
|
|
|
@ -77,8 +79,20 @@ public class CloudHostService {
|
|
|
|
|
String networkId = nextNetworkId();
|
|
|
|
|
log.info("创建云主机:port:{}, networkId:{}", port, networkId);
|
|
|
|
|
CreateCloudHostDto createCloudHostDto = JCloudUtil.oneStepCreateCloudHost(apiToken, imageId, networkId);
|
|
|
|
|
// 先睡个几秒钟, 不要问,问就是调试过的
|
|
|
|
|
SleepUtil.sleep(10 * 1000);
|
|
|
|
|
|
|
|
|
|
// 内网穿透
|
|
|
|
|
intranetThrough(port, createCloudHostDto.getUsername(), createCloudHostDto.getPassword(), createCloudHostDto.getFloatingIp());
|
|
|
|
|
boolean status = intranetThrough(port, createCloudHostDto.getUsername(), createCloudHostDto.getPassword(), createCloudHostDto.getFloatingIp());
|
|
|
|
|
int retry = 10;
|
|
|
|
|
while(!status && retry-- > 0){
|
|
|
|
|
SleepUtil.sleep(1000);
|
|
|
|
|
status = intranetThrough(port, createCloudHostDto.getUsername(), createCloudHostDto.getPassword(), createCloudHostDto.getFloatingIp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(retry <= 0){
|
|
|
|
|
log.error("创建云主机,内网穿透失败", JSONObject.toJSONString(createCloudHostDto));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createCloudHostDto.setPort(port);
|
|
|
|
|
createCloudHostDto.setIp(frpcHost);
|
|
|
|
@ -103,8 +117,20 @@ public class CloudHostService {
|
|
|
|
|
JCloudUtil.oneStepRebuildCloudHost(apiToken, imageId, resetCloudHostParam);
|
|
|
|
|
log.info("重新构建云主机serverId:{},耗时:{}", resetCloudHostParam.getServerId(), (System.currentTimeMillis() - startTime));
|
|
|
|
|
|
|
|
|
|
// 先睡个几秒钟, 不要问,问就是调试过的
|
|
|
|
|
SleepUtil.sleep(10 * 1000);
|
|
|
|
|
|
|
|
|
|
// 重置之后需要重新配置内网穿透
|
|
|
|
|
intranetThrough(resetCloudHostParam.getPort(), resetCloudHostParam.getUsername(), resetCloudHostParam.getPassword(), resetCloudHostParam.getFloatingIp());
|
|
|
|
|
boolean status = intranetThrough(resetCloudHostParam.getPort(), resetCloudHostParam.getUsername(), resetCloudHostParam.getPassword(), resetCloudHostParam.getFloatingIp());
|
|
|
|
|
int retry = 10;
|
|
|
|
|
while(!status && retry-- > 0){
|
|
|
|
|
SleepUtil.sleep(1000);
|
|
|
|
|
status = intranetThrough(resetCloudHostParam.getPort(), resetCloudHostParam.getUsername(), resetCloudHostParam.getPassword(), resetCloudHostParam.getFloatingIp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(retry <= 0){
|
|
|
|
|
log.error("重置云主机,内网穿透失败", JSONObject.toJSONString(resetCloudHostParam));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -124,7 +150,6 @@ public class CloudHostService {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新token
|
|
|
|
|
*/
|
|
|
|
@ -145,9 +170,8 @@ public class CloudHostService {
|
|
|
|
|
* @param password
|
|
|
|
|
* @param floatingIp
|
|
|
|
|
*/
|
|
|
|
|
private void intranetThrough(int port, String username, String password, String floatingIp) {
|
|
|
|
|
private boolean intranetThrough(int port, String username, String password, String floatingIp) {
|
|
|
|
|
// 休眠一下是有时候出现这个问题 connect to host 10.119.5.5 port 22: No route to host
|
|
|
|
|
SleepUtil.sleep(3 * 1000);
|
|
|
|
|
String template = frpcTemplate.replaceAll("\\{param1}", frpcHost).replaceAll("\\{param2}", String.valueOf(port));
|
|
|
|
|
|
|
|
|
|
// 通过浮动ip+port 连接ssh执行脚本
|
|
|
|
@ -156,8 +180,9 @@ public class CloudHostService {
|
|
|
|
|
"cd /opt/01/02/02; sudo echo -e \"", template, "\" > frpc.ini ; cat frpc.ini; nohup ./frpc >& cataline.log 2>&1 &", "\'");
|
|
|
|
|
String uuid = RandomUtil.randomString(16);
|
|
|
|
|
log.info("id{},配置内网穿透命令:{}", uuid, command);
|
|
|
|
|
String execResult = ShellUtil.execute(command);
|
|
|
|
|
log.info("id{},执行结果:{}", uuid, execResult);
|
|
|
|
|
ShellResult execResult = ShellUtil.executeAndGetExitStatus(command);
|
|
|
|
|
log.info("id{},执行结果:{}", uuid, JSONObject.toJSONString(execResult));
|
|
|
|
|
return !execResult.getOut().contains("No route to host") || !execResult.getOut().contains("Connection refused");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|