上传镜像到目标主机功能 优化 设置超时时间,不可用时快速失败
This commit is contained in:
parent
5f56977ded
commit
1381d632f0
@ -179,7 +179,7 @@ public class ImageService implements IImageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 先下载后上传到目标主机
|
* 先下载后上传到目标主机 先确定目标主机是否可连接在继续
|
||||||
* @param imageId
|
* @param imageId
|
||||||
* @param machineId
|
* @param machineId
|
||||||
* @return
|
* @return
|
||||||
@ -189,15 +189,7 @@ public class ImageService implements IImageService {
|
|||||||
if (Objects.isNull(imageId) || Objects.isNull(machineId)) {
|
if (Objects.isNull(imageId) || Objects.isNull(machineId)) {
|
||||||
throw new ServerException(MachineErrorCodeConstants.MACHINE_INFO_TAG_NULL);
|
throw new ServerException(MachineErrorCodeConstants.MACHINE_INFO_TAG_NULL);
|
||||||
}
|
}
|
||||||
DockerImageDo imageDo = dockerImageDao.selectById(imageId);
|
// 1 建立连接 区分密码还是秘钥
|
||||||
//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 建立连接 区分密码还是秘钥
|
|
||||||
MachineInfoDO machineInfoDO = machineInfoMapper.selectById(machineId);
|
MachineInfoDO machineInfoDO = machineInfoMapper.selectById(machineId);
|
||||||
FtpConfig targetFtp = FtpConfig.create().setHost(machineInfoDO.getHostIp()).setPort(machineInfoDO.getSshPort()).setUser(machineInfoDO.getUsername()).setPassword(CryptogramUtil.doDecrypt(machineInfoDO.getPassword()));
|
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)) {
|
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.setSystemKey(CryptogramUtil.doDecrypt(secretKeyDO.getPrivateKey()));
|
||||||
targetFtp.setPassword(CryptogramUtil.doDecrypt(secretKeyDO.getPassword()));
|
targetFtp.setPassword(CryptogramUtil.doDecrypt(secretKeyDO.getPassword()));
|
||||||
}
|
}
|
||||||
// 2.1 上传
|
// 2 拿到目标主机连接
|
||||||
Sftp uploadSftp = getSftp(targetFtp);
|
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 suffix = FileUtil.getSuffix(srcPath);
|
||||||
String fileName = imageDo.getName() + "." + suffix;
|
String fileName = imageDo.getName() + "." + suffix;
|
||||||
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteOut.toByteArray()); //转换
|
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteOut.toByteArray()); //转换
|
||||||
@ -225,14 +226,16 @@ public class ImageService implements IImageService {
|
|||||||
*/
|
*/
|
||||||
private Sftp getSftp(FtpConfig config) {
|
private Sftp getSftp(FtpConfig config) {
|
||||||
Session session;
|
Session session;
|
||||||
|
Integer timeOut = 5000;
|
||||||
|
try {
|
||||||
if (Objects.nonNull(config.getSystemKey())) {
|
if (Objects.nonNull(config.getSystemKey())) {
|
||||||
session = JschUtil.getSession(config.getHost(), config.getPort(), config.getUser(), config.getSystemKey().getBytes(StandardCharsets.UTF_8), config.getPassword().getBytes());
|
session = JschUtil.openSession(config.getHost(), config.getPort(), config.getUser(), config.getSystemKey().getBytes(StandardCharsets.UTF_8), config.getPassword().getBytes(), timeOut);
|
||||||
} else {
|
} else {
|
||||||
session = JschUtil.getSession(config.getHost(), config.getPort(), config.getUser(), config.getPassword());
|
session = JschUtil.openSession(config.getHost(), config.getPort(), config.getUser(), config.getPassword(), timeOut);
|
||||||
}
|
}
|
||||||
if (!session.isConnected()) {
|
} catch (Exception e) {
|
||||||
log.error("与主机IP:{} 建立SSH连接失败", config.getHost());
|
log.error("与主机IP:{} 建立SSH连接失败", config.getHost());
|
||||||
throw new ServerException(500, "部署失败,请检测主机是否可用");
|
throw new RuntimeException("部署失败,请检测主机是否可用", e);
|
||||||
}
|
}
|
||||||
return new Sftp(session);
|
return new Sftp(session);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user