You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.8 KiB

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;
}
}