From ae0dc4f46b075b5eace5596a66f6b81e357ae05e Mon Sep 17 00:00:00 2001
From: ludc
Date: 星期二, 03 十二月 2024 17:24:06 +0800
Subject: [PATCH] 码值codeallcode查重改成一句分批次查询(500)一次执行查询改为and拼接的依据sql查询。

---
 Source/UBCS/ubcs-service/ubcs-deploy/src/main/java/com/vci/ubcs/deploy/service/impl/DeployAppsServiceImpl.java |   97 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 72 insertions(+), 25 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-deploy/src/main/java/com/vci/ubcs/deploy/service/impl/DeployAppsServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-deploy/src/main/java/com/vci/ubcs/deploy/service/impl/DeployAppsServiceImpl.java
index a641db4..676068a 100644
--- a/Source/UBCS/ubcs-service/ubcs-deploy/src/main/java/com/vci/ubcs/deploy/service/impl/DeployAppsServiceImpl.java
+++ b/Source/UBCS/ubcs-service/ubcs-deploy/src/main/java/com/vci/ubcs/deploy/service/impl/DeployAppsServiceImpl.java
@@ -8,7 +8,6 @@
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
-import com.vci.ubcs.deploy.entity.CmdConfig;
 import com.vci.ubcs.deploy.entity.DeployApps;
 import com.vci.ubcs.deploy.enumpack.CmdConfigEnum;
 import com.vci.ubcs.deploy.mapper.DeployAppsMapper;
@@ -26,9 +25,7 @@
 import org.springframework.context.EnvironmentAware;
 import org.springframework.core.env.Environment;
 import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -40,12 +37,12 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
-import java.util.stream.Stream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
 
 /**
  * @author ludc
@@ -167,18 +164,18 @@
 		if(deployAppsVO.getName().isEmpty()){
 			throw new ServiceException("缂哄皯蹇呬紶鍙傛暟name");
 		}
-		DeployApps deployApps = deployAppsMapper.selectOne(Wrappers.<DeployApps>query().lambda().eq(DeployApps::getServerName, deployAppsVO.getName()));
+		List<DeployApps> deployApps = deployAppsMapper.selectList(Wrappers.<DeployApps>query().lambda().eq(DeployApps::getServerName, deployAppsVO.getName()));
 		if(Func.isNotEmpty(deployApps)){
-			return deployApps;
+			return deployApps.get(0);
 		}
 		// 鏈粠搴撲腑鏌ヨ鍒帮紝闇�瑕佺敓鎴愭湇鍔′俊鎭繚瀛橀粯璁や俊鎭埌搴撲腑
 		DeployApps defaultDeployApps = new DeployApps();
-		defaultDeployApps.setLogPath(PARENTPATH + "\\" + deployAppsVO.getName().replace("-","_") + LOGPATH);
+		defaultDeployApps.setLogPath(PARENTPATH + SEPARATOR + deployAppsVO.getName().replace("-","_") + LOGPATH);
 		defaultDeployApps.setServerName(deployAppsVO.getName());
 		defaultDeployApps.setStartCmd(CmdConfigEnum.START_CMD.getValue() + deployAppsVO.getName());
 		defaultDeployApps.setStopCmd(CmdConfigEnum.STOP_CMD.getValue() + deployAppsVO.getName());
 		defaultDeployApps.setRestartCmd(CmdConfigEnum.RESTART_CMD.getValue() + deployAppsVO.getName());
-		defaultDeployApps.setServerPath(PARENTPATH + "\\" + deployAppsVO.getName().replace("-","_"));
+		defaultDeployApps.setServerPath(PARENTPATH + SEPARATOR + deployAppsVO.getName().replace("-","_"));
 		int eft = deployAppsMapper.insert(defaultDeployApps);
 		if (!SqlHelper.retBool(eft)) {
 			throw new ServiceException("鐢熸垚榛樿鏈嶅姟淇℃伅鍒板簱涓椂澶辫触锛�");
@@ -193,9 +190,12 @@
 	 * @throws ServiceException
 	 */
 	@Override
-	public boolean saveOrUpdateServiceInfo(DeployAppsVO deployAppsVO) throws ServiceException {
-		boolean b = this.saveOrUpdate(deployAppsVO);
-		return b;
+	public R saveOrUpdateServiceInfo(DeployAppsVO deployAppsVO) throws ServiceException {
+		boolean checkBoolean = checkCmdVer(deployAppsVO.getStartCmd()) && checkCmdVer(deployAppsVO.getStopCmd()) && checkCmdVer(deployAppsVO.getRestartCmd());
+		if(checkBoolean){
+			return R.fail("閰嶇疆鐨勫懡浠や腑鍖呭惈鍗遍櫓鍛戒护锛屽锛歳m銆乵v銆乺m -rf銆乧hmod绛夊懡浠ゅ拰鍏抽敭瀛楋紒 ");
+		}
+		return R.status(this.saveOrUpdate(deployAppsVO));
 	}
 
 	/**
@@ -209,6 +209,10 @@
 		if (Func.isBlank(deployApps.getServerName()) || Func.isBlank(deployApps.getServerPath())) {
 			throw new ServiceException("蹇呬紶鍙傛暟[鏈嶅姟鍚嶇О锛屾湇鍔″瓨鏀捐矾寰刔涓嶈兘涓虹┖");
 		}
+		List<DeployApps> deployAppsList = deployAppsMapper.selectList(Wrappers.<DeployApps>query().lambda().eq(DeployApps::getServerName, deployApps.getServerName()));
+		if (!deployAppsList.isEmpty()) {
+			throw new ServiceException("鏂板鏈嶅姟鐨勬湇鍔″悕閲嶅锛�");
+		}
 		return SqlHelper.retBool(deployAppsMapper.insert(deployApps));
 	}
 
@@ -219,7 +223,7 @@
 	 * @return
 	 */
 	@Override
-	public R importClassify(MultipartFile[] files, String serverName) throws ServiceException {
+	public R importJarUpdate(MultipartFile[] files, String serverName) throws ServiceException {
 		// 鏍规嵁鏈嶅姟鍚嶆煡鐪嬪埌鏈嶅姟鐩稿叧淇℃伅
 		List<DeployApps> deployAppsDB = deployAppsMapper.selectList(Wrappers.<DeployApps>query().lambda().eq(DeployApps::getServerName, serverName));
 		if(deployAppsDB.isEmpty()){
@@ -238,12 +242,12 @@
 					String backName = "";
 					String fileType = "file";
 					// 鏄帇缂╂枃浠�,鍥犱负鍙細瀛樺湪涓ょ鎯呭喌锛屾枃浠跺悕鏄帇缂╂枃浠讹紝鎴栬�呮枃浠讹紙.jar绫诲瀷鐨勬枃浠讹級
+					// 褰撳墠鏃堕棿
+					String currentDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
 					if (file.getContentType().equals("application/zip") || file.getContentType().equals("application/x-zip-compressed")) {
-						backName = file.getOriginalFilename().replace(".zip","_"+Func.formatDate(new Date()));
-						fileType = "zip";
+						backName = file.getOriginalFilename().replace(".zip","_" + currentDateTime);
 					}else{
-						backName = file.getOriginalFilename().replace(".","_"+Func.formatDate(new Date())+".");
-						fileType = "file";
+						backName = file.getOriginalFilename().replace(".","_" + currentDateTime + ".");
 					}
 					File source = new File(deployAppsDB.get(0).getServerPath() + this.SEPARATOR + file.getOriginalFilename().replace(".zip", ""));
 					File destination = new File(deployAppsDB.get(0).getFileBack() + this.SEPARATOR + backName);
@@ -288,7 +292,7 @@
 					file1.delete();
 				}
 			}
-			String output = excute(deployAppsDB.get(0),"UP");
+			String output = execute(deployAppsDB.get(0),"UP");
 			return R.success(output.toString());
 		} catch (IOException e) {
 			e.printStackTrace();
@@ -311,7 +315,7 @@
 			if(deployAppsDB.isEmpty()){
 				return R.fail("鍛戒护鎵ц鍑洪敊锛屽簱涓湭鎵惧埌"+ deployAppsVO.getName() +"鏈嶅姟鐩稿叧閰嶇疆锛�" );
 			}
-			excuteRes = excute(deployAppsDB.get(0),deployAppsVO.getStatus());
+			excuteRes = execute(deployAppsDB.get(0),deployAppsVO.getStatus());
 			return R.success("鍛戒护鎵ц缁撴潫锛�"+excuteRes);
 		}catch (Exception e){
 			throw new ServiceException(e.getMessage());
@@ -324,7 +328,7 @@
 	 * @return
 	 * @throws ServiceException
 	 */
-	private String excute(DeployApps deployApps,String type) throws ServiceException {
+	public String execute(DeployApps deployApps,String type) throws ServiceException {
 		// 澶勭悊涓婁紶鏂囦欢鐨勯�昏緫
 		StringBuilder output = new StringBuilder();
 		try {
@@ -337,17 +341,42 @@
 			if(Func.isEmpty(cmd)){
 				return "The executed command is empty";
 			}
+			if(checkCmdVer(cmd)){
+				return "閰嶇疆鐨勫懡浠や腑鍖呭惈鍗遍櫓鍛戒护锛屽锛歳m銆乵v銆乺m -rf銆乧hmod绛夊懡浠ゅ拰鍏抽敭璇嶏紒 ";
+			}
 			// 鎵цLinux鍛戒护
-			Process process = Runtime.getRuntime().exec(cmd);
+			log.info("寮�濮嬫墽琛屽懡浠わ細"+cmd);
+			// Process process = Runtime.getRuntime().exec(cmd);
+			ProcessBuilder processBuilder = new ProcessBuilder(cmd.split("\\s"));
+			// processBuilder.command(cmd);
+			Process process = processBuilder.start();
+			// 绛夊緟鍛戒护鎵ц瀹屾垚
+			int exitCode = process.waitFor();
+
+			InputStream inputStream = process.getInputStream();
+			OutputStream outputStream = process.getOutputStream();
+			InputStream errorStream = process.getErrorStream();
 			// 璇诲彇鍛戒护鎵ц缁撴灉
-			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
 			String line;
 			while ((line = reader.readLine()) != null) {
 				output.append(line).append("\n");
 			}
-			// 绛夊緟鍛戒护鎵ц瀹屾垚
-			int exitCode = process.waitFor();
-			log.info("鍛戒护鎵ц缁撴灉锛�" + output.toString());
+			reader.close();
+
+			// 璇诲彇鍛戒护鎵ц鐨勯敊璇緭鍑烘祦
+			BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
+			StringBuilder errorOutput = new StringBuilder();
+			String errorLine;
+			while ((errorLine = errorReader.readLine()) != null) {
+				errorOutput.append(errorLine).append("\n");
+			}
+			String errorOutputString = errorOutput.toString();
+			errorReader.close();
+
+			log.info("閿欒杈撳嚭锛�" + errorOutputString);
+			int exitValue = process.exitValue();
+			log.info("鍛戒护鎵ц缁撴灉锛�" + output.toString()+":"+exitCode+","+exitValue);
 			return output.toString();
 		}catch (IOException | InterruptedException e){
 			e.printStackTrace();
@@ -395,4 +424,22 @@
 		return "8080";
 	}
 
+	/**
+	 * 鍚姩銆佸仠姝€�侀噸鍚懡浠ゆ牎楠�
+	 * @param cmd
+	 * @return true: 鍖呭惈楂橀闄╁懡浠わ紝 false锛氫笉鍖呭惈
+	 */
+	private boolean checkCmdVer(String cmd){
+		if(Func.isEmpty(cmd)){
+			return false;
+		}
+		String[] highRiskCommands = {"rm", "rmdir", "mv", "unlink", "rm -rf", "mv -rf", "dd", "chmod", "chown", "mkfs", "shutdown", "reboot", "kill"};
+		for(String highRiskCmd : highRiskCommands){
+			if(cmd.contains(highRiskCmd)){
+				return true;
+			}
+		}
+		return false;
+	}
+
 }

--
Gitblit v1.9.3