From 1381d632f05fb19f58933d111bd17d58ef5faba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=B2=E5=85=88=E7=94=9F?= <821039958@qq.com> Date: Tue, 29 Jul 2025 10:45:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=95=9C=E5=83=8F=E5=88=B0?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=E4=B8=BB=E6=9C=BA=E5=8A=9F=E8=83=BD=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E8=AE=BE=E7=BD=AE=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=8C=E4=B8=8D=E5=8F=AF=E7=94=A8=E6=97=B6?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docker/service/impl/ImageService.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/modules/module-ci-execute/src/main/java/cd/casic/module/execute/docker/service/impl/ImageService.java b/modules/module-ci-execute/src/main/java/cd/casic/module/execute/docker/service/impl/ImageService.java index 7bebb873..88f13a2b 100644 --- a/modules/module-ci-execute/src/main/java/cd/casic/module/execute/docker/service/impl/ImageService.java +++ b/modules/module-ci-execute/src/main/java/cd/casic/module/execute/docker/service/impl/ImageService.java @@ -179,7 +179,7 @@ public class ImageService implements IImageService { } /** - * 先下载后上传到目标主机 + * 先下载后上传到目标主机 先确定目标主机是否可连接在继续 * @param imageId * @param machineId * @return @@ -189,15 +189,7 @@ public class ImageService implements IImageService { if (Objects.isNull(imageId) || Objects.isNull(machineId)) { throw new ServerException(MachineErrorCodeConstants.MACHINE_INFO_TAG_NULL); } - DockerImageDo imageDo = dockerImageDao.selectById(imageId); - //1 远程下载 - // 1.1 建立连接 下载 - Sftp sftp = getSftp(FtpConfig.create().setHost(fileUploadProperties.getRemoteHost()).setPort(fileUploadProperties.getRemotePort()).setUser(fileUploadProperties.getUsername()).setPassword(fileUploadProperties.getPassword())); - String srcPath = imageDo.getPath(); - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - sftp.download(srcPath, byteOut); //远程下载到中间缓存区 - sftp.close(); - // 2 建立连接 区分密码还是秘钥 + // 1 建立连接 区分密码还是秘钥 MachineInfoDO machineInfoDO = machineInfoMapper.selectById(machineId); FtpConfig targetFtp = FtpConfig.create().setHost(machineInfoDO.getHostIp()).setPort(machineInfoDO.getSshPort()).setUser(machineInfoDO.getUsername()).setPassword(CryptogramUtil.doDecrypt(machineInfoDO.getPassword())); if (AuthenticationType.of(machineInfoDO.getAuthenticationType()).equals(AuthenticationType.SECRET_KEY)) { @@ -205,8 +197,17 @@ public class ImageService implements IImageService { targetFtp.setSystemKey(CryptogramUtil.doDecrypt(secretKeyDO.getPrivateKey())); targetFtp.setPassword(CryptogramUtil.doDecrypt(secretKeyDO.getPassword())); } - // 2.1 上传 + // 2 拿到目标主机连接 Sftp uploadSftp = getSftp(targetFtp); + + //远程下载 + DockerImageDo imageDo = dockerImageDao.selectById(imageId); + Sftp sftp = getSftp(FtpConfig.create().setHost(fileUploadProperties.getRemoteHost()).setPort(fileUploadProperties.getRemotePort()).setUser(fileUploadProperties.getUsername()).setPassword(fileUploadProperties.getPassword())); + String srcPath = imageDo.getPath(); + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + sftp.download(srcPath, byteOut); //远程下载到中间缓存区 + sftp.close(); + //远程上传 String suffix = FileUtil.getSuffix(srcPath); String fileName = imageDo.getName() + "." + suffix; ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteOut.toByteArray()); //转换 @@ -225,14 +226,16 @@ public class ImageService implements IImageService { */ private Sftp getSftp(FtpConfig config) { Session session; - if (Objects.nonNull(config.getSystemKey())) { - session = JschUtil.getSession(config.getHost(), config.getPort(), config.getUser(), config.getSystemKey().getBytes(StandardCharsets.UTF_8), config.getPassword().getBytes()); - } else { - session = JschUtil.getSession(config.getHost(), config.getPort(), config.getUser(), config.getPassword()); - } - if (!session.isConnected()) { + Integer timeOut = 5000; + try { + if (Objects.nonNull(config.getSystemKey())) { + session = JschUtil.openSession(config.getHost(), config.getPort(), config.getUser(), config.getSystemKey().getBytes(StandardCharsets.UTF_8), config.getPassword().getBytes(), timeOut); + } else { + session = JschUtil.openSession(config.getHost(), config.getPort(), config.getUser(), config.getPassword(), timeOut); + } + } catch (Exception e) { log.error("与主机IP:{} 建立SSH连接失败", config.getHost()); - throw new ServerException(500, "部署失败,请检测主机是否可用"); + throw new RuntimeException("部署失败,请检测主机是否可用", e); } return new Sftp(session); }