增加日志

master
youys 2 years ago
parent cd22704df8
commit 01e4fb1345

@ -1,5 +1,6 @@
package net.educoder.controller;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.educoder.model.param.ResetCloudHostParam;
import net.educoder.service.CloudHostService;
@ -32,23 +33,12 @@ public class CloudController {
*/
@PostMapping("/createCloudHost")
public ResponseResult<CreateCloudHostDto> createCloudHost(int port) {
log.info("创建云主机请求参数port:{}", port);
CreateCloudHostDto createCloudHostDto = cloudHostService.oneStepCreateCloudHost(port);
return ResponseResult.success(createCloudHostDto);
}
/**
*
*
* @return
*/
@PostMapping("/getCloudHostList")
public ResponseResult<String> getCloudHostList() {
String cloudHost = cloudHostService.getCloudHostList();
return ResponseResult.success(cloudHost);
}
/**
*
*
@ -56,7 +46,7 @@ public class CloudController {
*/
@PostMapping("/resetCloudHost")
public ResponseResult resetCloudHost(ResetCloudHostParam resetCloudHostParam) {
log.info("重置云主机请求参数:{}", JSONObject.toJSONString(resetCloudHostParam));
boolean status = cloudHostService.resetCloudHost(resetCloudHostParam);
if (!status) {
return ResponseResult.failed(-1, "重置云主机失败");
@ -73,7 +63,7 @@ public class CloudController {
@PostMapping("/destroyCloudHost")
public ResponseResult destroyCloudHost(@RequestParam("serverId") String serverId,
@RequestParam("floatingIpId") String floatingIpId) {
log.info("销毁云主机请求参数serverId:{}, floatingIpId:{}", serverId, floatingIpId);
boolean status = cloudHostService.destroyCloudHost(serverId, floatingIpId);
if (!status) {
return ResponseResult.failed(-1, "销毁云主机失败");

@ -74,7 +74,7 @@ public class CloudHostService {
}
// 创建云主机
CreateCloudHostDto createCloudHostDto = JCloudUtil.oneStepCreateCloudHost(apiToken, imageId, nextNetworkId());
// 内网穿透
intranetThrough(port, createCloudHostDto.getUsername(), createCloudHostDto.getPassword(), createCloudHostDto.getFloatingIp());
createCloudHostDto.setPort(port);
@ -84,19 +84,6 @@ public class CloudHostService {
}
/**
*
*
* @return
*/
public String getCloudHostList() {
String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN);
if (StringUtils.isBlank(apiToken)) {
apiToken = refreshApiToken();
}
return JCloudUtil.getCloudListDetail(apiToken);
}
/**
*
*
@ -109,12 +96,13 @@ public class CloudHostService {
apiToken = refreshApiToken();
}
boolean status = JCloudUtil.rebuildCloud(apiToken, resetCloudHostParam.getServerId());
if (status) {
long startTime = System.currentTimeMillis();
JCloudUtil.oneStepRebuildCloudHost(apiToken, imageId, resetCloudHostParam);
log.info("重新构建云主机serverId:{},耗时:{}", resetCloudHostParam.getServerId(), (System.currentTimeMillis() - startTime));
// 重置之后需要重新配置内网穿透
intranetThrough(resetCloudHostParam.getPort(), resetCloudHostParam.getUsername(), resetCloudHostParam.getPassword(), resetCloudHostParam.getFloatingIp());
}
return status;
return true;
}
/**

@ -10,6 +10,7 @@ import net.educoder.constant.CommonConstants;
import net.educoder.exception.BusinessException;
import net.educoder.model.dto.CreateCloudHostDto;
import net.educoder.model.param.CreateCloudHostParam;
import net.educoder.model.param.ResetCloudHostParam;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
@ -218,7 +219,7 @@ public class JCloudUtil {
* @param serverId
* @return
*/
public static boolean rebuildCloud(String token, String serverId) {
public static boolean rebuildCloud(String token, String serverId, String imageId) {
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}/action".replaceAll("\\{server_id}", serverId);
/**
* {
@ -229,7 +230,7 @@ public class JCloudUtil {
* }
*/
JSONObject rebuild = new JSONObject();
rebuild.put("imageRef", "31a54313-bcfe-4c6e-9e19-dcad8360ef56");
rebuild.put("imageRef", imageId);
rebuild.put("adminPass", "Educoder123");
JSONObject requestBody = new JSONObject();
@ -238,7 +239,7 @@ public class JCloudUtil {
long start = System.currentTimeMillis();
String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(requestBody.toJSONString()).execute().body();
long end = System.currentTimeMillis();
log.info("交大云重新构建云主机密码返回结果:{},耗时:{}ms", result, (end - start));
log.info("交大云重新构建云主机返回结果:{},耗时:{}ms", result, (end - start));
return true;
}
@ -375,14 +376,14 @@ public class JCloudUtil {
log.info("开始执行修改云主机:{} 密码", serverId);
boolean changePwdStatus = changePassword(apiToken, serverId, "Educoder123");
if (!changePwdStatus) {
log.error("serverId:{}密码修改失败", serverId);
log.error("oneStepCreateCloudHost serverId:{}密码修改失败", serverId);
}
return createCloudHostDto;
}
/**
*
*
*
* @param apiToken
*/
@ -394,6 +395,38 @@ public class JCloudUtil {
log.info("删除云主机状态:{},删除浮动ip状态:{}", status1, status2);
}
/**
*
* @param apiToken
* @param resetCloudHostParam
*/
public static void oneStepRebuildCloudHost(String apiToken, String imageId, ResetCloudHostParam resetCloudHostParam){
String serverId = resetCloudHostParam.getServerId();
boolean status = rebuildCloud(apiToken, serverId, imageId);
if(status){
// 轮询查询创建云主机状态
int number = 60;
while (!"ACTIVE".equals(getServerStatus(JCloudUtil.getCloudDetail(apiToken, serverId))) && number-- > 0) {
SleepUtil.sleep(2000);
}
if (number <= 0) {
// 说明云主机重新build失败了
log.error("serverId:{}云主机重新构建失败", serverId);
throw new BusinessException("云主机重新构建失败");
}
SleepUtil.sleep(30 * 1000);
log.info("oneStepRebuildCloudHost 开始执行修改云主机:{} 密码", serverId);
boolean changePwdStatus = changePassword(apiToken, serverId, "Educoder123");
if (!changePwdStatus) {
log.error("oneStepRebuildCloudHost serverId:{}密码修改失败", serverId);
}
}
log.info("rebuild与主机完成serverId:{}", serverId);
}
/**
*
@ -464,10 +497,17 @@ public class JCloudUtil {
// String imageId = "8abf7057-95db-4b57-9a64-8249a4c64680";
// oneStepCreateCloudHost(apiToken, imageId);
String serverId = "532773ed-c022-458e-85f3-f45649b08096";
String floatingIpId = "8adc22ff-9cbd-447d-bcdc-2c45014f651a";
oneStepDestroyCloudHost(apiToken, serverId, floatingIpId);
// String serverId = "532773ed-c022-458e-85f3-f45649b08096";
// String floatingIpId = "8adc22ff-9cbd-447d-bcdc-2c45014f651a";
// oneStepDestroyCloudHost(apiToken, serverId, floatingIpId);
// ResetCloudHostParam resetCloudHostParam = new ResetCloudHostParam();
// resetCloudHostParam.setServerId("8cdfc00b-b90a-4e5a-a6ea-1fb558f9fd04");
// oneStepRebuildCloudHost(apiToken,"8abf7057-95db-4b57-9a64-8249a4c64680", resetCloudHostParam);
createCloudHost(apiToken,defaultCreateCloudHostParam("6d3b7f00-0c33-4bad-9a74-ca7d8d7e7b39", "3302992e-0143-4e06-9d11-78abe8881c5f"));
}

@ -7,7 +7,7 @@ server:
jcloud:
username: educoder01
password: Educoder123
imageId: 8abf7057-95db-4b57-9a64-8249a4c64680
imageId: 6d3b7f00-0c33-4bad-9a74-ca7d8d7e7b39
frpc:
host: 39.104.50.181
template: "[common]\nserver_addr = {param1}\nserver_port = 7000\n[ssh{param2}]\ntype = tcp\nlocal_ip = 127.0.0.1\nlocal_port = 22\nremote_port = {param2}"

Loading…
Cancel
Save