feat:存储池和网络管理bug修改,桌面镜像制作脚本

master
汤全昆 2025-09-02 14:25:00 +08:00
parent dd2731b661
commit 86c94c93ce
7 changed files with 216 additions and 18 deletions

View File

@ -3,6 +3,7 @@ package com.unisinsight.project.entity.dao;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;

View File

@ -80,21 +80,28 @@ public class StoragePool {
/**
*
*/
@TableField(exist = false)
@TableField("capacity")
@ApiModelProperty(value = "总容量")
private Long capacity;
/**
*
*/
@TableField(exist = false)
@TableField("allocation")
@ApiModelProperty(value = "已分配容量")
private Long allocation;
/**
*
*/
@TableField(exist = false)
@TableField("available")
@ApiModelProperty(value = "可用容量")
private Long available;
/**
*
*/
@TableField("path")
@ApiModelProperty(value = "路劲")
private String path;
}

View File

@ -71,7 +71,7 @@ public class NetworkManageReq {
*/
@ApiModelProperty(value = "DHCP启用状态1 启用 0 不启用")
@JsonProperty("dhcp_enabled")
private Short dhcpEnabled;
private Integer dhcpEnabled;
/**
* DHCP
@ -123,6 +123,6 @@ public class NetworkManageReq {
*
*/
@ApiModelProperty(value = "开机自启动1是0否")
private Short autostart;
private Integer autostart;
}

View File

@ -26,8 +26,8 @@ public interface ExternalApiClient {
@DeleteMapping("/api/v1/network/{network_name}")
ApiResponse deleteNetwork(@PathVariable("network_name") String networkName);
@PostMapping("/api/v1/network/update")
ApiResponse updateNetwork(@RequestBody NetworkManageReq networkManageReq);
@PutMapping("/api/v1/network/update")
ApiResponse updateNetwork(@RequestBody Network network);
@PostMapping("/api/v1/network/start")
ApiResponse startNetwork(@RequestBody NetworkManageReq networkManageReq);

View File

@ -18,9 +18,7 @@ import com.unisinsight.project.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
@ -75,7 +73,7 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
Network network = new Network();
BeanUtils.copyProperties(networkManageReq, network);
network.setAutostart(networkManageReq.getAutostart() == 1);
network.setDhcpEnabled(networkManageReq.getDhcpEnabled()==1);
network.setDhcpEnabled(networkManageReq.getDhcpEnabled() == 1);
ApiResponse response = client.createNetwork(network);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口新建网络失败: " + response.getMessage());
@ -114,7 +112,12 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
// 调用虚拟机创建存储池
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())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
@ -125,7 +128,7 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
// 修改状态
if (networkManageReq.getStatus() != null) {
try {
networkManageReq.setNetworkName(existing.getNetworkName());
setNetworkManageReq(existing, networkManageReq);
ApiResponse response;
if (1 == networkManageReq.getStatus()) {
response = client.startNetwork(networkManageReq);
@ -143,6 +146,51 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
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
*
@ -180,9 +228,9 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
/**
*
*
* @param networkName
* @param type
* @param status
* @param networkName
* @param type
* @param status
* @return List<NetworkManage>
*/
@Override
@ -273,10 +321,10 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
for (Network network : networkList) {
NetworkManage networkManage = new 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.setAutostart(network.getAutostart()? 1 : 0);
networkManage.setDhcpEnabled(network.getDhcpEnabled()? 1 : 0);
networkManage.setAutostart(network.getAutostart() ? 1 : 0);
networkManage.setDhcpEnabled(network.getDhcpEnabled() ? 1 : 0);
networkManage.setUpdateTime(new Date());
// 判断是更新还是插入

View File

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

View File

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