|
|
@ -3,6 +3,7 @@ package net.educoder.util;
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import net.educoder.exception.BusinessException;
|
|
|
|
import net.educoder.exception.BusinessException;
|
|
|
@ -10,6 +11,7 @@ import net.educoder.model.param.CreateCloudHostParam;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @Author: youys
|
|
|
|
* @Author: youys
|
|
|
@ -174,6 +176,54 @@ public class JCloudUtil {
|
|
|
|
return StringUtils.isBlank(result);
|
|
|
|
return StringUtils.isBlank(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建浮动id
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String createFloatingIp(String token) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/os-floating-ips";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject body = new JSONObject();
|
|
|
|
|
|
|
|
body.put("pool", "Public Network");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 String serverBindFloatingIp(String token, String serverId, String fixedIp, String floatingIp) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{serverId}/action".replaceAll("\\{serverId}", serverId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String body = "{\n" +
|
|
|
|
|
|
|
|
" \"addFloatingIp\" : {\n" +
|
|
|
|
|
|
|
|
" \"address\": \"" + floatingIp + "\",\n" +
|
|
|
|
|
|
|
|
" \"fixed_address\": \"" + fixedIp + "\"\n" +
|
|
|
|
|
|
|
|
" }\n" +
|
|
|
|
|
|
|
|
"}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(body).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云云主机绑定浮动ip返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取api token
|
|
|
|
* 获取api token
|
|
|
@ -238,23 +288,67 @@ public class JCloudUtil {
|
|
|
|
String apiToken = getApiToken(username, password);
|
|
|
|
String apiToken = getApiToken(username, password);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// createCloudHost(apiToken, defaultCreateCloudHostParam());
|
|
|
|
stepCreate(apiToken);
|
|
|
|
|
|
|
|
// boolean changeStatus = changePassword(apiToken, "d1efeec9-9087-4b83-a8df-557b226f7031", "Educoder123");
|
|
|
|
|
|
|
|
// if (!changeStatus) {
|
|
|
|
|
|
|
|
// System.out.println("修改密码失败");
|
|
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String serverId = "7c91c97b-37a4-4cf2-85be-fdddaef44f2b";
|
|
|
|
private static void stepCreate(String apiToken){
|
|
|
|
|
|
|
|
// 创建云主机
|
|
|
|
/**
|
|
|
|
String createCloudHost = createCloudHost(apiToken, defaultCreateCloudHostParam());
|
|
|
|
* 重置云主机,并且需要修改密码
|
|
|
|
// 获取serverId
|
|
|
|
*/
|
|
|
|
String serverId = JSONObject.parseObject(createCloudHost).getJSONObject("server").getString("id");
|
|
|
|
// rebuildCloud(apiToken, serverId);
|
|
|
|
|
|
|
|
// changePassword(apiToken, serverId, "Educoder123");
|
|
|
|
// 获取云主机详情,判断是否创建成功
|
|
|
|
|
|
|
|
JSONObject cloudDetail = null;
|
|
|
|
|
|
|
|
// 轮询查询创建云主机状态
|
|
|
|
|
|
|
|
int number = 120;
|
|
|
|
|
|
|
|
while (!"ACTIVE".equals(getServerStatus(cloudDetail = getCloudDetail(apiToken, serverId))) && number-- > 0) {
|
|
|
|
|
|
|
|
sleep(1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getCloudDetail(apiToken, serverId);
|
|
|
|
if(number <= 0){
|
|
|
|
|
|
|
|
// 说明创建云主机失败了
|
|
|
|
|
|
|
|
System.out.println("云主机创建失败");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// deleteCloudHost("902d0100-745c-4ef5-af44-edca4b1410d9",apiToken);
|
|
|
|
sleep(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 修改密码
|
|
|
|
|
|
|
|
boolean changeStatus = changePassword(apiToken, serverId, "Educoder123");
|
|
|
|
|
|
|
|
if (!changeStatus) {
|
|
|
|
|
|
|
|
System.out.println("修改密码失败");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建浮动ip
|
|
|
|
|
|
|
|
* {
|
|
|
|
|
|
|
|
* "floating_ip": {
|
|
|
|
|
|
|
|
* "instance_id": null,
|
|
|
|
|
|
|
|
* "ip": "10.119.4.89",
|
|
|
|
|
|
|
|
* "fixed_ip": null,
|
|
|
|
|
|
|
|
* "id": "f7f1e9bb-624b-49b4-ae01-1b09841bf99e",
|
|
|
|
|
|
|
|
* "pool": "Public Network"
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// String fixedIp = getFixedIp(cloudDetail);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// String floatingIpStr = createFloatingIp(apiToken);
|
|
|
|
|
|
|
|
// JSONObject floatingIpObj = JSONObject.parseObject(floatingIpStr).getJSONObject("floating_ip");
|
|
|
|
|
|
|
|
// String floatingIp = floatingIpObj.getString("ip");
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 云主机绑定浮动ip
|
|
|
|
|
|
|
|
// serverBindFloatingIp(apiToken, serverId, fixedIp, floatingIp);
|
|
|
|
|
|
|
|
// System.out.println("------------云主机创建完成------------");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -276,4 +370,52 @@ public class JCloudUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取固定ip
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param cloudDetail
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static String getFixedIp(JSONObject cloudDetail) {
|
|
|
|
|
|
|
|
JSONObject addresses = cloudDetail.getJSONObject("server").getJSONObject("addresses");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AtomicReference<String> fixedIp = 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"));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fixedIp.get();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取云主机状态
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param cloudDetail
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static String getServerStatus(JSONObject cloudDetail) {
|
|
|
|
|
|
|
|
JSONObject server = cloudDetail.getJSONObject("server");
|
|
|
|
|
|
|
|
return server.getString("status");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void sleep(long time){
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(time);
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|