From 579c42e620a31daa5f38ef67f5cab7d7615c08d1 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Thu, 18 Aug 2022 13:48:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/educoder/CloudHostService.java | 132 ++++++++ .../educoder/constant/CommonConstants.java | 22 +- .../educoder/controller/CloudController.java | 72 ++--- .../handler/GlobalExceptionHandler.java | 21 +- .../model/dto/CreateCloudHostDto.java | 50 +++ .../java/net/educoder/util/JCloudUtil.java | 296 ++++++++++-------- .../java/net/educoder/util/SleepUtil.java | 20 ++ src/main/resources/application.yml | 11 + 8 files changed, 438 insertions(+), 186 deletions(-) create mode 100644 src/main/java/net/educoder/CloudHostService.java create mode 100644 src/main/java/net/educoder/model/dto/CreateCloudHostDto.java create mode 100644 src/main/java/net/educoder/util/SleepUtil.java diff --git a/src/main/java/net/educoder/CloudHostService.java b/src/main/java/net/educoder/CloudHostService.java new file mode 100644 index 0000000..09cab9b --- /dev/null +++ b/src/main/java/net/educoder/CloudHostService.java @@ -0,0 +1,132 @@ +package net.educoder; + +import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.RuntimeUtil; +import com.google.common.cache.Cache; +import lombok.extern.slf4j.Slf4j; +import net.educoder.constant.CommonConstants; +import net.educoder.model.dto.CreateCloudHostDto; +import net.educoder.util.JCloudUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + + +/** + * @Author: youys + * @Date: 2022/8/18 + * @Description: + */ +@Slf4j +@Service +public class CloudHostService { + + @Value("${jcloud.username}") + private String username; + + @Value("${jcloud.password}") + private String password; + + @Value("${jcloud.imageId}") + private String imageId; + + @Value("${jcloud.frpc.template}") + private String frpcTemplate; + + @Value("${jcloud.frpc.host}") + private String frpcHost; + + @Autowired + @Qualifier("guavaCache") + private Cache guavaCache; + + /** + * 创建云主机 + * 1、创建云主机 + * 2、创建浮动ip + * 3、绑定浮动ip + * + * @param port 穿透的端口 + * @return + */ + public CreateCloudHostDto oneStepCreateCloudHost(int port) { + String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); + if (StringUtils.isBlank(apiToken)) { + apiToken = refreshApiToken(); + } + // 创建云主机 + CreateCloudHostDto createCloudHostDto = JCloudUtil.oneStepCreateCloudHost(apiToken, imageId); + + String template = frpcTemplate.replaceAll("\\{param1}", frpcHost).replaceAll("\\{param2}", String.valueOf(port)); + + // 通过浮动ip+port 连接ssh执行脚本 + String command = StringUtils.join("sshpass -p ", createCloudHostDto.getPassword(), " ssh -o UserKnownHostsFile=/dev/null ", + createCloudHostDto.getUsername(), "@", createCloudHostDto.getFloatingIp(), " \"", "cd /root/frp_0.44.0_linux_amd64; echo \'", template, "\' > frpc.ini", "\""); + String uuid = RandomUtil.randomString(16); + log.info("id{},配置内网穿透命令:{}", uuid, command); + + String execResult = RuntimeUtil.execForStr(command); + log.info("id{},执行结果:{}", uuid, execResult); + + return createCloudHostDto; + } + + + /** + * 获取云主机列表 + * + * @return + */ + public String getCloudHostList() { + String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); + if (StringUtils.isBlank(apiToken)) { + apiToken = refreshApiToken(); + } + return JCloudUtil.getCloudListDetail(apiToken); + } + + /** + * 重置云主机 + * + * @param serverId + * @return + */ + public boolean resetCloudHost(String serverId) { + String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); + if (StringUtils.isBlank(apiToken)) { + apiToken = refreshApiToken(); + } + return JCloudUtil.rebuildCloud(apiToken, serverId); + } + + /** + * 重置云主机 + * + * @param serverId + * @return + */ + public boolean destroyCloudHost(String serverId) { + String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); + if (StringUtils.isBlank(apiToken)) { + apiToken = refreshApiToken(); + } + JCloudUtil.oneStepDestroyCloudHost(apiToken, serverId); + return true; + } + + + /** + * 刷新token + */ + private String refreshApiToken() { + String apiToken = JCloudUtil.getApiToken(username, password); + log.info("token refresh--------{}", apiToken); + guavaCache.put(CommonConstants.API_TOKEN, apiToken); + + return apiToken; + } + + +} diff --git a/src/main/java/net/educoder/constant/CommonConstants.java b/src/main/java/net/educoder/constant/CommonConstants.java index c15ee32..88b8a33 100644 --- a/src/main/java/net/educoder/constant/CommonConstants.java +++ b/src/main/java/net/educoder/constant/CommonConstants.java @@ -7,12 +7,24 @@ package net.educoder.constant; */ public class CommonConstants { - public static final String SUCCESS = "success"; - - public static final String FAILED = "failed"; - public static final String API_TOKEN = "api_token"; + /** + * 云主机默认用户 + */ + public static final String DEFAULT_ROOT = "root"; + + /** + * 云主机默认密码 + */ + public static final String DEFAULT_PASSWORD = "Educoder123"; - public static final String DEFAULT_PASSWORD = "Educoder_123123"; + /** + * 固定id类型 + */ + public static final String FIXED_IP_TYPE = "fixed"; + /** + * 浮动ip类型 + */ + public static final String FLOATING_IP_TYPE = "floating"; } diff --git a/src/main/java/net/educoder/controller/CloudController.java b/src/main/java/net/educoder/controller/CloudController.java index ee07ec0..9b7bb77 100644 --- a/src/main/java/net/educoder/controller/CloudController.java +++ b/src/main/java/net/educoder/controller/CloudController.java @@ -1,14 +1,10 @@ package net.educoder.controller; -import com.google.common.cache.Cache; import lombok.extern.slf4j.Slf4j; -import net.educoder.constant.CommonConstants; -import net.educoder.util.JCloudUtil; +import net.educoder.CloudHostService; +import net.educoder.model.dto.CreateCloudHostDto; import net.educoder.util.ResponseResult; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -23,16 +19,8 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/jcloud") public class CloudController { - - @Value("${jcloud.username}") - private String username; - - @Value("${jcloud.password}") - private String password; - @Autowired - @Qualifier("guavaCache") - private Cache guavaCache; + private CloudHostService cloudHostService; /** @@ -41,14 +29,9 @@ public class CloudController { * @return */ @PostMapping("/createCloudHost") - public ResponseResult createCloudHost() { - - String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); - if (StringUtils.isBlank(apiToken)) { - apiToken = refreshApiToken(); - } - JCloudUtil.createCloudHost(apiToken, JCloudUtil.defaultCreateCloudHostParam()); - return ResponseResult.success(); + public ResponseResult createCloudHost(int port) { + CreateCloudHostDto createCloudHostDto = cloudHostService.oneStepCreateCloudHost(port); + return ResponseResult.success(createCloudHostDto); } @@ -59,13 +42,7 @@ public class CloudController { */ @PostMapping("/getCloudHostList") public ResponseResult getCloudHostList() { - - String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); - if (StringUtils.isBlank(apiToken)) { - apiToken = refreshApiToken(); - } - - String cloudHost = JCloudUtil.createCloudHost(apiToken, JCloudUtil.defaultCreateCloudHostParam()); + String cloudHost = cloudHostService.getCloudHostList(); return ResponseResult.success(cloudHost); } @@ -78,38 +55,27 @@ public class CloudController { @PostMapping("/resetCloudHost") public ResponseResult resetCloudHost(String serverId) { - String apiToken = guavaCache.getIfPresent(CommonConstants.API_TOKEN); - if (StringUtils.isBlank(apiToken)) { - apiToken = refreshApiToken(); - } - - boolean status = JCloudUtil.rebuildCloud(apiToken, serverId); + boolean status = cloudHostService.resetCloudHost(serverId); if (!status) { return ResponseResult.failed(-1, "重置云主机失败"); } - // 等待30秒再重置密码 - try { - Thread.sleep(30); - } catch (InterruptedException e) { - e.printStackTrace(); - } - // 重置之后需要再更新密码 - boolean changePwdStatus = JCloudUtil.changePassword(apiToken, serverId, CommonConstants.DEFAULT_PASSWORD); - - return changePwdStatus ? ResponseResult.success() : ResponseResult.failed(-1,"重置密码失败"); + return ResponseResult.success(); } /** - * 刷新token + * 销毁云主机 + * + * @return */ - private String refreshApiToken() { - String apiToken = JCloudUtil.getApiToken(username, password); - log.info("token refresh--------{}", apiToken); - guavaCache.put(CommonConstants.API_TOKEN, apiToken); + @PostMapping("/destroyCloudHost") + public ResponseResult destroyCloudHost(String serverId) { - return apiToken; + boolean status = cloudHostService.destroyCloudHost(serverId); + if (!status) { + return ResponseResult.failed(-1, "销毁云主机失败"); + } + return ResponseResult.success(); } - } diff --git a/src/main/java/net/educoder/handler/GlobalExceptionHandler.java b/src/main/java/net/educoder/handler/GlobalExceptionHandler.java index 5970b5b..836a97f 100644 --- a/src/main/java/net/educoder/handler/GlobalExceptionHandler.java +++ b/src/main/java/net/educoder/handler/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import net.educoder.exception.BusinessException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @@ -21,7 +22,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(BusinessException.class) @ResponseBody - public ResponseEntity handleBridgeExceptions(BusinessException e) { + public ResponseEntity handleBusinessException(BusinessException e) { log.error("捕获到BusinessException", e); JSONObject jsonObj = new JSONObject(); @@ -31,6 +32,18 @@ public class GlobalExceptionHandler { return new ResponseEntity<>(jsonObj, HttpStatus.OK); } + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + @ResponseBody + public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { + log.error("捕获到HttpRequestMethodNotSupportedException", e); + + JSONObject jsonObj = new JSONObject(); + jsonObj.put("code", -1); + jsonObj.put("msg", "请求方法错误"); + + return new ResponseEntity<>(jsonObj, HttpStatus.OK); + } + @ExceptionHandler(Exception.class) @ResponseBody public ResponseEntity handleOtherExceptions(Exception e) { @@ -38,10 +51,8 @@ public class GlobalExceptionHandler { JSONObject jsonObj = new JSONObject(); - if (e.getMessage().contains("Maximum upload size exceeded")) { - jsonObj.put("code", -1); - jsonObj.put("msg", "未知错误!" + e.getMessage()); - } + jsonObj.put("code", -1); + jsonObj.put("msg", "系统错误!"); return new ResponseEntity<>(jsonObj, HttpStatus.OK); } diff --git a/src/main/java/net/educoder/model/dto/CreateCloudHostDto.java b/src/main/java/net/educoder/model/dto/CreateCloudHostDto.java new file mode 100644 index 0000000..1a5d771 --- /dev/null +++ b/src/main/java/net/educoder/model/dto/CreateCloudHostDto.java @@ -0,0 +1,50 @@ +package net.educoder.model.dto; + +import lombok.Data; + +/** + * @Author: youys + * @Date: 2022/8/18 + * @Description: + */ +@Data +public class CreateCloudHostDto { + + /** + * 云主机id + */ + private String serverId; + + /** + * 固定ip + */ + private String fixedIp; + + /** + * 浮动id + */ + private String floatingIp; + + /** + * 浮动ip id + */ + private String floatingIpId; + + /** + * ip + */ + private String ip; + + /** + * 用户名 + */ + private String username; + /** + * 密码 + */ + private String password; + /** + * 端口 + */ + private int port; +} diff --git a/src/main/java/net/educoder/util/JCloudUtil.java b/src/main/java/net/educoder/util/JCloudUtil.java index 17759ac..dd10f57 100644 --- a/src/main/java/net/educoder/util/JCloudUtil.java +++ b/src/main/java/net/educoder/util/JCloudUtil.java @@ -6,7 +6,9 @@ import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; +import net.educoder.constant.CommonConstants; import net.educoder.exception.BusinessException; +import net.educoder.model.dto.CreateCloudHostDto; import net.educoder.model.param.CreateCloudHostParam; import org.apache.commons.lang3.StringUtils; @@ -21,6 +23,65 @@ import java.util.concurrent.atomic.AtomicReference; @Slf4j public class JCloudUtil { + /** + * 获取api token + * + * @param username + * @param password + * @return + */ + public static String getApiToken(String username, String password) { + /** + * { + * "auth": { + * "identity": { + * "methods": [ + * "password" + * ], + * "password": { + * "user": { + * "name": "admin", + * "domain": { + * "name": "Default" + * }, + * "password": "devstacker" + * } + * } + * } + * } + * } + */ + JSONObject domain = new JSONObject(); + domain.put("name", "Default"); + + JSONObject user = new JSONObject(); + user.put("name", username); + user.put("password", password); + user.put("domain", domain); + + JSONObject passwordJson = new JSONObject(); + passwordJson.put("user", user); + + JSONObject identity = new JSONObject(); + identity.put("methods", Arrays.asList("password")); + identity.put("password", passwordJson); + + JSONObject auth = new JSONObject(); + auth.put("identity", identity); + + JSONObject requestBody = new JSONObject(); + requestBody.put("auth", auth); + + HttpResponse response = HttpRequest.post("https://home.jcloud.sjtu.edu.cn:5000/v3/auth/tokens").header("Content-Type", "application/json").body(requestBody.toJSONString()).execute(); + if (response.isOk()) { + String token = response.header("X-Subject-Token"); + log.info("交我算平台获取token成功,token: {}", token); + return token; + } + log.info("交我算平台获取token失败,body:{}", response.body()); + return ""; + } + /** * 获取云主机列表详情 @@ -97,6 +158,29 @@ public class JCloudUtil { return result; } + /** + * 删除云主机 + * + * @param token + * @param serverId + * @return + */ + public static boolean deleteCloudHost(String token, String serverId) { + String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}".replaceAll("\\{server_id}", serverId); + + long start = System.currentTimeMillis(); + try { + String result = HttpRequest.delete(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body(); + long end = System.currentTimeMillis(); + log.info("交大云删除云主机返回结果:{},耗时:{}ms", result, (end - start)); + } catch (Exception e) { + log.error("交大云删除云主机异常", e); + return false; + } + return true; + } + + /** * 更改密码 * @@ -124,7 +208,7 @@ public class JCloudUtil { 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)); - return StringUtils.isNotBlank(result); + return StringUtils.isBlank(result); } /** @@ -158,26 +242,9 @@ public class JCloudUtil { return true; } - /** - * 删除云主机 - * - * @param serverId - * @param token - * @return - */ - public static boolean deleteCloudHost(String serverId, String token) { - String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}".replaceAll("\\{server_id}", serverId); - - long start = System.currentTimeMillis(); - String result = HttpRequest.delete(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body(); - long end = System.currentTimeMillis(); - log.info("交大云删除云主机返回结果:{},耗时:{}ms", result, (end - start)); - - return StringUtils.isBlank(result); - } /** - * 创建浮动id + * 创建浮动ip * * @param token * @return @@ -193,10 +260,30 @@ public class JCloudUtil { String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(body.toJSONString()).execute().body(); long end = System.currentTimeMillis(); log.info("交大云创建浮动id返回结果:{},耗时:{}ms", result, (end - start)); - return result; } + /** + * 删除浮动ip + * + * @param token + * @return + */ + public static boolean deleteFloatingIp(String token, String floatingIpId) { + String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/os-floating-ips/{floating_ip_id}".replaceAll("\\{floating_ip_id}", floatingIpId); + + long start = System.currentTimeMillis(); + + try { + String result = HttpRequest.delete(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body(); + long end = System.currentTimeMillis(); + log.info("交大云创删除浮动ip返回结果:{},耗时:{}ms", result, (end - start)); + } catch (Exception e) { + return false; + } + return true; + } + /** * 云主机绑定浮动ip @@ -226,80 +313,13 @@ public class JCloudUtil { /** - * 获取api token + * 一步创建云主机 * - * @return token + * @param apiToken */ - public static String getApiToken(String username, String password) { - /** - * { - * "auth": { - * "identity": { - * "methods": [ - * "password" - * ], - * "password": { - * "user": { - * "name": "admin", - * "domain": { - * "name": "Default" - * }, - * "password": "devstacker" - * } - * } - * } - * } - * } - */ - JSONObject domain = new JSONObject(); - domain.put("name", "Default"); - - JSONObject user = new JSONObject(); - user.put("name", username); - user.put("password", password); - user.put("domain", domain); - - JSONObject passwordJson = new JSONObject(); - passwordJson.put("user", user); - - JSONObject identity = new JSONObject(); - identity.put("methods", Arrays.asList("password")); - identity.put("password", passwordJson); - - JSONObject auth = new JSONObject(); - auth.put("identity", identity); - - JSONObject requestBody = new JSONObject(); - requestBody.put("auth", auth); - - HttpResponse response = HttpRequest.post("https://home.jcloud.sjtu.edu.cn:5000/v3/auth/tokens").header("Content-Type", "application/json").body(requestBody.toJSONString()).execute(); - if (response.isOk()) { - String token = response.header("X-Subject-Token"); - log.info("交我算平台获取token成功,token: {}", token); - return token; - } - log.info("交我算平台获取token失败,body:{}", response.body()); - return ""; - } - - public static void main(String[] args) { - String username = "educoder01"; - String password = "Educoder123"; - String apiToken = getApiToken(username, password); - - - stepCreate(apiToken); -// boolean changeStatus = changePassword(apiToken, "d1efeec9-9087-4b83-a8df-557b226f7031", "Educoder123"); -// if (!changeStatus) { -// System.out.println("修改密码失败"); -// return; -// } - - } - - private static void stepCreate(String apiToken){ + public static CreateCloudHostDto oneStepCreateCloudHost(String apiToken, String imageId) { // 创建云主机 - String createCloudHost = createCloudHost(apiToken, defaultCreateCloudHostParam()); + String createCloudHost = createCloudHost(apiToken, defaultCreateCloudHostParam(imageId)); // 获取serverId String serverId = JSONObject.parseObject(createCloudHost).getJSONObject("server").getString("id"); @@ -308,23 +328,13 @@ public class JCloudUtil { // 轮询查询创建云主机状态 int number = 120; while (!"ACTIVE".equals(getServerStatus(cloudDetail = getCloudDetail(apiToken, serverId))) && number-- > 0) { - sleep(1000); + SleepUtil.sleep(1000); } - if(number <= 0){ + if (number <= 0) { // 说明创建云主机失败了 - System.out.println("云主机创建失败"); - return; - } - - - sleep(1000); - - // 修改密码 - boolean changeStatus = changePassword(apiToken, serverId, "Educoder123"); - if (!changeStatus) { - System.out.println("修改密码失败"); - return; + log.error("serverId:{}云主机创建失败", serverId); + throw new BusinessException("云主机创建失败"); } /** @@ -339,16 +349,52 @@ public class JCloudUtil { * } * } */ -// String fixedIp = getFixedIp(cloudDetail); - +// String fixedIp = getCloudHostIp(cloudDetail, CommonConstants.FIXED_IP_TYPE); +// // String floatingIpStr = createFloatingIp(apiToken); // JSONObject floatingIpObj = JSONObject.parseObject(floatingIpStr).getJSONObject("floating_ip"); // String floatingIp = floatingIpObj.getString("ip"); -// +// String floatingIpId = floatingIpObj.getString("id"); // // // 云主机绑定浮动ip // serverBindFloatingIp(apiToken, serverId, fixedIp, floatingIp); -// System.out.println("------------云主机创建完成------------"); + log.info("serverId:{}------------云主机创建完成------------", serverId); + + CreateCloudHostDto createCloudHostDto = new CreateCloudHostDto(); + createCloudHostDto.setServerId(serverId); +// createCloudHostDto.setFixedIp(fixedIp); +// createCloudHostDto.setFloatingIp(floatingIp); +// createCloudHostDto.setFloatingIpId(floatingIpId); + + createCloudHostDto.setUsername(CommonConstants.DEFAULT_ROOT); + createCloudHostDto.setPassword(CommonConstants.DEFAULT_PASSWORD); + + return createCloudHostDto; + } + + /** + * 一步创建云主机 + * + * @param apiToken + */ + public static void oneStepDestroyCloudHost(String apiToken, String serverId) { + // 获取云主机详情 + JSONObject cloudDetail = getCloudDetail(apiToken, serverId); + + String floatingIp = getCloudHostIp(cloudDetail, CommonConstants.FLOATING_IP_TYPE); + + // 删除云主机 + deleteCloudHost(apiToken, serverId); + +// if(StringUtils.isNotBlank(floatingIp)){ +// +// +// // 删除浮动ip +// deleteFloatingIp(apiToken, floatingIpId); +// +// } + + } @@ -360,41 +406,42 @@ public class JCloudUtil { * * @return */ - public static CreateCloudHostParam defaultCreateCloudHostParam() { + public static CreateCloudHostParam defaultCreateCloudHostParam(String imageId) { CreateCloudHostParam createCloudHost = new CreateCloudHostParam(); createCloudHost.setName(RandomUtil.randomString(10)); - createCloudHost.setImageRef("31a54313-bcfe-4c6e-9e19-dcad8360ef56"); + // 镜像id + createCloudHost.setImageRef(imageId); createCloudHost.setFlavorRef("02723f5f-5518-4031-bd5e-dfecae675606"); + // 网络id createCloudHost.setNetworkId("f41ece4b-ff14-4444-8d1d-c730779a0df2"); return createCloudHost; } - /** * 获取固定ip * * @param cloudDetail * @return */ - private static String getFixedIp(JSONObject cloudDetail) { + private static String getCloudHostIp(JSONObject cloudDetail, String type) { JSONObject addresses = cloudDetail.getJSONObject("server").getJSONObject("addresses"); - AtomicReference fixedIp = new AtomicReference<>(); + AtomicReference ip = new AtomicReference<>(); addresses.forEach((key, value) -> { JSONArray networks = (JSONArray) value; for (Object o : networks) { JSONObject addressDetail = (JSONObject) o; - if ("fixed".equals(addressDetail.getString("OS-EXT-IPS:type"))) { - fixedIp.set(addressDetail.getString("addr")); + if (type.equals(addressDetail.getString("OS-EXT-IPS:type"))) { + ip.set(addressDetail.getString("addr")); break; } } }); - return fixedIp.get(); + return ip.get(); } @@ -409,12 +456,15 @@ public class JCloudUtil { return server.getString("status"); } - private static void sleep(long time){ - try { - Thread.sleep(time); - } catch (InterruptedException e) { - e.printStackTrace(); - } + public static void main(String[] args) { + String username = "educoder01"; + String password = "Educoder123"; + String apiToken = getApiToken(username, password); + + + String imageId = "8abf7057-95db-4b57-9a64-8249a4c64680"; + oneStepCreateCloudHost(apiToken, imageId); + } diff --git a/src/main/java/net/educoder/util/SleepUtil.java b/src/main/java/net/educoder/util/SleepUtil.java new file mode 100644 index 0000000..690a706 --- /dev/null +++ b/src/main/java/net/educoder/util/SleepUtil.java @@ -0,0 +1,20 @@ +package net.educoder.util; + +/** + * @Author: youys + * @Date: 2022/8/18 + * @Description: 线程休眠工具类 + */ +public class SleepUtil { + + /** + * 休眠 + * @param time 毫秒 + */ + public static void sleep(long time) { + try { + Thread.sleep(time); + } catch (InterruptedException e) { + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d60ef2d..3f4c12f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,3 +7,14 @@ server: jcloud: username: educoder01 password: Educoder123 + imageId: 8abf7057-95db-4b57-9a64-8249a4c64680 + frpc: + host: 39.104.50.181 + template: "[common] + server_addr = {param1} + server_port = 7000 + [ssh{param2}] + type = tcp + local_ip = 127.0.0.1 + local_port = 22 + remote_port = {param2}"