master
parent
a882531be6
commit
579c42e620
@ -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<String, String> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue