From f427a61272ac0668be0a46e99b1bd3c6bab36d49 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Thu, 8 Sep 2022 15:01:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E7=BD=91=E7=A9=BF?= =?UTF-8?q?=E9=80=8F=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../educoder/service/CloudHostService.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/educoder/service/CloudHostService.java b/src/main/java/net/educoder/service/CloudHostService.java index 5db418f..0247920 100644 --- a/src/main/java/net/educoder/service/CloudHostService.java +++ b/src/main/java/net/educoder/service/CloudHostService.java @@ -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"); } /**