feat:存储池和网络管理bug修改,桌面镜像制作脚本
parent
dd2731b661
commit
86c94c93ce
|
|
@ -3,6 +3,7 @@ package com.unisinsight.project.entity.dao;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.*;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,21 +80,28 @@ public class StoragePool {
|
||||||
/**
|
/**
|
||||||
* 总容量
|
* 总容量
|
||||||
*/
|
*/
|
||||||
@TableField(exist = false)
|
@TableField("capacity")
|
||||||
@ApiModelProperty(value = "总容量")
|
@ApiModelProperty(value = "总容量")
|
||||||
private Long capacity;
|
private Long capacity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 已分配容量
|
* 已分配容量
|
||||||
*/
|
*/
|
||||||
@TableField(exist = false)
|
@TableField("allocation")
|
||||||
@ApiModelProperty(value = "已分配容量")
|
@ApiModelProperty(value = "已分配容量")
|
||||||
private Long allocation;
|
private Long allocation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可用容量
|
* 可用容量
|
||||||
*/
|
*/
|
||||||
@TableField(exist = false)
|
@TableField("available")
|
||||||
@ApiModelProperty(value = "可用容量")
|
@ApiModelProperty(value = "可用容量")
|
||||||
private Long available;
|
private Long available;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可用容量
|
||||||
|
*/
|
||||||
|
@TableField("path")
|
||||||
|
@ApiModelProperty(value = "路劲")
|
||||||
|
private String path;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ public class NetworkManageReq {
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "DHCP启用状态:1 启用 0 不启用")
|
@ApiModelProperty(value = "DHCP启用状态:1 启用 0 不启用")
|
||||||
@JsonProperty("dhcp_enabled")
|
@JsonProperty("dhcp_enabled")
|
||||||
private Short dhcpEnabled;
|
private Integer dhcpEnabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DHCP起始地址
|
* DHCP起始地址
|
||||||
|
|
@ -123,6 +123,6 @@ public class NetworkManageReq {
|
||||||
* 开机自启动
|
* 开机自启动
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "开机自启动:1是,0否")
|
@ApiModelProperty(value = "开机自启动:1是,0否")
|
||||||
private Short autostart;
|
private Integer autostart;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -26,8 +26,8 @@ public interface ExternalApiClient {
|
||||||
@DeleteMapping("/api/v1/network/{network_name}")
|
@DeleteMapping("/api/v1/network/{network_name}")
|
||||||
ApiResponse deleteNetwork(@PathVariable("network_name") String networkName);
|
ApiResponse deleteNetwork(@PathVariable("network_name") String networkName);
|
||||||
|
|
||||||
@PostMapping("/api/v1/network/update")
|
@PutMapping("/api/v1/network/update")
|
||||||
ApiResponse updateNetwork(@RequestBody NetworkManageReq networkManageReq);
|
ApiResponse updateNetwork(@RequestBody Network network);
|
||||||
|
|
||||||
@PostMapping("/api/v1/network/start")
|
@PostMapping("/api/v1/network/start")
|
||||||
ApiResponse startNetwork(@RequestBody NetworkManageReq networkManageReq);
|
ApiResponse startNetwork(@RequestBody NetworkManageReq networkManageReq);
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,7 @@ import com.unisinsight.project.util.StringUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -75,7 +73,7 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
Network network = new Network();
|
Network network = new Network();
|
||||||
BeanUtils.copyProperties(networkManageReq, network);
|
BeanUtils.copyProperties(networkManageReq, network);
|
||||||
network.setAutostart(networkManageReq.getAutostart() == 1);
|
network.setAutostart(networkManageReq.getAutostart() == 1);
|
||||||
network.setDhcpEnabled(networkManageReq.getDhcpEnabled()==1);
|
network.setDhcpEnabled(networkManageReq.getDhcpEnabled() == 1);
|
||||||
ApiResponse response = client.createNetwork(network);
|
ApiResponse response = client.createNetwork(network);
|
||||||
if (!"200".equals(response.getCode())) {
|
if (!"200".equals(response.getCode())) {
|
||||||
throw new BusinessException("调用外部接口新建网络失败: " + response.getMessage());
|
throw new BusinessException("调用外部接口新建网络失败: " + response.getMessage());
|
||||||
|
|
@ -114,7 +112,12 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
|
|
||||||
// 调用虚拟机创建存储池
|
// 调用虚拟机创建存储池
|
||||||
try {
|
try {
|
||||||
ApiResponse response = client.updateNetwork(networkManageReq);
|
setNetworkManageReq(existing, networkManageReq);
|
||||||
|
Network network = new Network();
|
||||||
|
BeanUtils.copyProperties(networkManageReq, network);
|
||||||
|
network.setAutostart(networkManageReq.getAutostart() == 1);
|
||||||
|
network.setDhcpEnabled(networkManageReq.getDhcpEnabled() == 1);
|
||||||
|
ApiResponse response = client.updateNetwork(network);
|
||||||
if (!"200".equals(response.getCode())) {
|
if (!"200".equals(response.getCode())) {
|
||||||
throw new BusinessException("调用外部接口失败: " + response.getMessage());
|
throw new BusinessException("调用外部接口失败: " + response.getMessage());
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +128,7 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
// 修改状态
|
// 修改状态
|
||||||
if (networkManageReq.getStatus() != null) {
|
if (networkManageReq.getStatus() != null) {
|
||||||
try {
|
try {
|
||||||
networkManageReq.setNetworkName(existing.getNetworkName());
|
setNetworkManageReq(existing, networkManageReq);
|
||||||
ApiResponse response;
|
ApiResponse response;
|
||||||
if (1 == networkManageReq.getStatus()) {
|
if (1 == networkManageReq.getStatus()) {
|
||||||
response = client.startNetwork(networkManageReq);
|
response = client.startNetwork(networkManageReq);
|
||||||
|
|
@ -143,6 +146,51 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
return this.updateById(existing);
|
return this.updateById(existing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setNetworkManageReq(NetworkManage existing, NetworkManageReq networkManageReq) {
|
||||||
|
if (networkManageReq.getNetworkName() == null) {
|
||||||
|
networkManageReq.setNetworkName(existing.getNetworkName());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getType() == null) {
|
||||||
|
networkManageReq.setType(existing.getType());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getBridgeName() == null) {
|
||||||
|
networkManageReq.setBridgeName(existing.getBridgeName());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getIpRange() == null) {
|
||||||
|
networkManageReq.setIpRange(existing.getIpRange());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getGateway() == null) {
|
||||||
|
networkManageReq.setGateway(existing.getGateway());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getNetmask() == null) {
|
||||||
|
networkManageReq.setNetmask(existing.getNetmask());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getDhcpStart() == null) {
|
||||||
|
networkManageReq.setDhcpStart(existing.getDhcpStart());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getDhcpEnd() == null) {
|
||||||
|
networkManageReq.setDhcpEnd(existing.getDhcpEnd());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getVlanId() == null) {
|
||||||
|
networkManageReq.setVlanId(existing.getVlanId());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getAutostart() == null) {
|
||||||
|
networkManageReq.setAutostart(existing.getAutostart());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getDhcpEnabled() == null) {
|
||||||
|
networkManageReq.setDhcpEnabled(existing.getDhcpEnabled());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getStatus() == null) {
|
||||||
|
networkManageReq.setStatus(existing.getStatus());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getCreateUser() == null) {
|
||||||
|
networkManageReq.setCreateUser(existing.getCreateUser());
|
||||||
|
}
|
||||||
|
if (networkManageReq.getDescription() == null) {
|
||||||
|
networkManageReq.setDescription(existing.getDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID删除网络管理信息
|
* 根据ID删除网络管理信息
|
||||||
*
|
*
|
||||||
|
|
@ -180,9 +228,9 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
/**
|
/**
|
||||||
* 查询网络管理列表
|
* 查询网络管理列表
|
||||||
*
|
*
|
||||||
* @param networkName 网络名称(可选查询条件)
|
* @param networkName 网络名称(可选查询条件)
|
||||||
* @param type 网络类型(可选查询条件)
|
* @param type 网络类型(可选查询条件)
|
||||||
* @param status 状态(可选查询条件)
|
* @param status 状态(可选查询条件)
|
||||||
* @return List<NetworkManage> 网络管理列表
|
* @return List<NetworkManage> 网络管理列表
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -273,10 +321,10 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
|
||||||
for (Network network : networkList) {
|
for (Network network : networkList) {
|
||||||
NetworkManage networkManage = new NetworkManage();
|
NetworkManage networkManage = new NetworkManage();
|
||||||
BeanUtils.copyProperties(network, networkManage);
|
BeanUtils.copyProperties(network, networkManage);
|
||||||
networkManage.setNetworkName(StringUtil.isEmpty(network.getNetworkName())?network.getName(): networkManage.getNetworkName() );
|
networkManage.setNetworkName(StringUtil.isEmpty(network.getNetworkName()) ? network.getName() : networkManage.getNetworkName());
|
||||||
networkManage.setStatus("active".equals(network.getState()) ? 1 : 0);
|
networkManage.setStatus("active".equals(network.getState()) ? 1 : 0);
|
||||||
networkManage.setAutostart(network.getAutostart()? 1 : 0);
|
networkManage.setAutostart(network.getAutostart() ? 1 : 0);
|
||||||
networkManage.setDhcpEnabled(network.getDhcpEnabled()? 1 : 0);
|
networkManage.setDhcpEnabled(network.getDhcpEnabled() ? 1 : 0);
|
||||||
networkManage.setUpdateTime(new Date());
|
networkManage.setUpdateTime(new Date());
|
||||||
|
|
||||||
// 判断是更新还是插入
|
// 判断是更新还是插入
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.unisinsight.torrent.controller;
|
||||||
|
|
||||||
|
import com.unisinsight.torrent.util.BtTorrentUtils;
|
||||||
|
import com.unisinsight.torrent.util.ImageVirtualUtils;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
@RequestMapping("/vdi")
|
||||||
|
@Api("桌面镜像服务")
|
||||||
|
public class DeskImageController {
|
||||||
|
|
||||||
|
@GetMapping("/desk-image")
|
||||||
|
@ApiOperation(value = "制作桌面镜像")
|
||||||
|
public boolean start(@RequestParam("srcFile") String srcFile,
|
||||||
|
@RequestParam("detFile") String detFile,
|
||||||
|
@RequestParam("detFile") String type) {
|
||||||
|
System.out.println("制作桌面镜像");
|
||||||
|
boolean result = ImageVirtualUtils.createImage(srcFile, detFile, type);
|
||||||
|
System.out.println("执行脚本制作桌面镜像: " + (result ? "成功" : "失败"));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/progress")
|
||||||
|
@ApiOperation(value = "制作桌面镜像")
|
||||||
|
public Double start(@RequestParam("pid") String pid) {
|
||||||
|
System.out.println("查询进度");
|
||||||
|
return ImageVirtualUtils.progress(pid);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
package com.unisinsight.torrent.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 种子文件工具类
|
||||||
|
*/
|
||||||
|
public class ImageVirtualUtils {
|
||||||
|
|
||||||
|
private static final String BT_SCRIPT_PATH = "/var/lib/vdi/nodejs/convert_image.sh";
|
||||||
|
private static final String JSON_PATH = "/var/lib/vdi/nodejs/json/";
|
||||||
|
|
||||||
|
private static final Long WAIT_START_TIME = 8000L;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 bttorrent.sh 脚本
|
||||||
|
*/
|
||||||
|
private static boolean executeCommand(String arg1, String arg2, String arg3, String arg4) {
|
||||||
|
try {
|
||||||
|
// 构造命令
|
||||||
|
ProcessBuilder pb = new ProcessBuilder(
|
||||||
|
"./",
|
||||||
|
BT_SCRIPT_PATH,
|
||||||
|
arg1,
|
||||||
|
arg2,
|
||||||
|
JSON_PATH+arg3,
|
||||||
|
arg4
|
||||||
|
).redirectErrorStream(true);
|
||||||
|
// 启动进程
|
||||||
|
Process process = pb.start();
|
||||||
|
// 启动新线程读取输出,避免阻塞
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
logProcessOutput(process);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
// 对于start命令,只要进程启动成功就返回true
|
||||||
|
|
||||||
|
// 稍微等待一下看初始输出是否有错误
|
||||||
|
Thread.sleep(WAIT_START_TIME);
|
||||||
|
return process.isAlive(); // 如果进程还在运行,认为启动成功
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印进程输出
|
||||||
|
*/
|
||||||
|
private static void logProcessOutput(Process process) throws IOException {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
System.out.println("[BT输出] " + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean createImage(String srcFile, String detFile, String name, String type) {
|
||||||
|
// 验证文件是否存在
|
||||||
|
if (!new File(srcFile).exists()) {
|
||||||
|
System.err.println("源文件不存在: " + srcFile);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 确保目标目录存在
|
||||||
|
new File(detFile).getParentFile().mkdirs();
|
||||||
|
return executeCommand(srcFile, detFile, name, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> progress(String name) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 检查文件是否存在
|
||||||
|
File jsonFile = new File(JSON_PATH+name);
|
||||||
|
if (!jsonFile.exists()) {
|
||||||
|
System.err.println("JSON文件不存在: " + JSON_PATH+name);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取文件内容
|
||||||
|
String content = new String(Files.readAllBytes(Paths.get(JSON_PATH+name)));
|
||||||
|
|
||||||
|
// 使用Jackson将JSON转换为Map
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
return objectMapper.readValue(content, Map.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue