mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-12-05 05:18:12 +08:00
ssh 上传文件调整
This commit is contained in:
parent
abdb3e52e7
commit
282ac0704d
@ -24,128 +24,129 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
@Configuration
|
||||
public class ExtConfigBean {
|
||||
static final String FILE_NAME = "extConfig.yml";
|
||||
|
||||
private static Resource resource;
|
||||
/**
|
||||
* 请求日志
|
||||
*/
|
||||
@Value("${consoleLog.reqXss:true}")
|
||||
private boolean consoleLogReqXss;
|
||||
/**
|
||||
* 请求响应
|
||||
*/
|
||||
@Value("${consoleLog.reqResponse:true}")
|
||||
private boolean consoleLogReqResponse;
|
||||
public static final String FILE_NAME = "extConfig.yml";
|
||||
|
||||
/**
|
||||
* 日志文件的编码格式,如果没有指定就自动识别,自动识别可能出现不准确的情况
|
||||
*/
|
||||
@Value("${log.fileCharset:}")
|
||||
private String logFileCharset;
|
||||
/**
|
||||
* 初始读取日志文件行号
|
||||
*/
|
||||
@Value("${log.intiReadLine:10}")
|
||||
private int logInitReadLine;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Charset logFileCharsets;
|
||||
private static Resource resource;
|
||||
/**
|
||||
* 请求日志
|
||||
*/
|
||||
@Value("${consoleLog.reqXss:true}")
|
||||
private boolean consoleLogReqXss;
|
||||
/**
|
||||
* 请求响应
|
||||
*/
|
||||
@Value("${consoleLog.reqResponse:true}")
|
||||
private boolean consoleLogReqResponse;
|
||||
|
||||
public int getLogInitReadLine() {
|
||||
if (logInitReadLine < 0) {
|
||||
return 10;
|
||||
}
|
||||
return logInitReadLine;
|
||||
}
|
||||
/**
|
||||
* 日志文件的编码格式,如果没有指定就自动识别,自动识别可能出现不准确的情况
|
||||
*/
|
||||
@Value("${log.fileCharset:}")
|
||||
private String logFileCharset;
|
||||
/**
|
||||
* 初始读取日志文件行号
|
||||
*/
|
||||
@Value("${log.intiReadLine:10}")
|
||||
private int logInitReadLine;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Charset logFileCharsets;
|
||||
|
||||
public Charset getLogFileCharset() {
|
||||
return logFileCharsets;
|
||||
}
|
||||
public int getLogInitReadLine() {
|
||||
if (logInitReadLine < 0) {
|
||||
return 10;
|
||||
}
|
||||
return logInitReadLine;
|
||||
}
|
||||
|
||||
public boolean isConsoleLogReqResponse() {
|
||||
return consoleLogReqResponse;
|
||||
}
|
||||
public Charset getLogFileCharset() {
|
||||
return logFileCharsets;
|
||||
}
|
||||
|
||||
public boolean isConsoleLogReqXss() {
|
||||
return consoleLogReqXss;
|
||||
}
|
||||
public boolean isConsoleLogReqResponse() {
|
||||
return consoleLogReqResponse;
|
||||
}
|
||||
|
||||
private static ExtConfigBean extConfigBean;
|
||||
public boolean isConsoleLogReqXss() {
|
||||
return consoleLogReqXss;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态获取外部配置文件的 resource
|
||||
*
|
||||
* @return File
|
||||
*/
|
||||
public static Resource getResource() {
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
File file = JpomManifest.getRunPath();
|
||||
if (file.isFile()) {
|
||||
file = file.getParentFile().getParentFile();
|
||||
file = new File(file, FILE_NAME);
|
||||
if (file.exists() && file.isFile()) {
|
||||
resource = new FileSystemResource(file);
|
||||
return ExtConfigBean.resource;
|
||||
}
|
||||
}
|
||||
resource = new ClassPathResource("/bin/" + FILE_NAME);
|
||||
return ExtConfigBean.resource;
|
||||
}
|
||||
private static ExtConfigBean extConfigBean;
|
||||
|
||||
public static File getResourceFile() {
|
||||
File file = JpomManifest.getRunPath();
|
||||
file = file.getParentFile().getParentFile();
|
||||
file = new File(file, FILE_NAME);
|
||||
return file;
|
||||
}
|
||||
/**
|
||||
* 动态获取外部配置文件的 resource
|
||||
*
|
||||
* @return File
|
||||
*/
|
||||
public static Resource getResource() {
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
File file = JpomManifest.getRunPath();
|
||||
if (file.isFile()) {
|
||||
file = file.getParentFile().getParentFile();
|
||||
file = new File(file, FILE_NAME);
|
||||
if (file.exists() && file.isFile()) {
|
||||
resource = new FileSystemResource(file);
|
||||
return ExtConfigBean.resource;
|
||||
}
|
||||
}
|
||||
resource = new ClassPathResource("/bin/" + FILE_NAME);
|
||||
return ExtConfigBean.resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单例
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public static ExtConfigBean getInstance() {
|
||||
if (extConfigBean == null) {
|
||||
extConfigBean = SpringUtil.getBean(ExtConfigBean.class);
|
||||
// 读取配置的编码格式
|
||||
if (StrUtil.isNotBlank(extConfigBean.logFileCharset)) {
|
||||
try {
|
||||
extConfigBean.logFileCharsets = CharsetUtil.charset(extConfigBean.logFileCharset);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return extConfigBean;
|
||||
}
|
||||
public static File getResourceFile() {
|
||||
File file = JpomManifest.getRunPath();
|
||||
file = file.getParentFile().getParentFile();
|
||||
file = new File(file, FILE_NAME);
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目运行存储路径
|
||||
*/
|
||||
@Value("${jpom.path}")
|
||||
private String path;
|
||||
/**
|
||||
* 单例
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public static ExtConfigBean getInstance() {
|
||||
if (extConfigBean == null) {
|
||||
extConfigBean = SpringUtil.getBean(ExtConfigBean.class);
|
||||
// 读取配置的编码格式
|
||||
if (StrUtil.isNotBlank(extConfigBean.logFileCharset)) {
|
||||
try {
|
||||
extConfigBean.logFileCharsets = CharsetUtil.charset(extConfigBean.logFileCharset);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return extConfigBean;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
if (JpomManifest.getInstance().isDebug()) {
|
||||
// 调试模式 为根路径的 jpom文件
|
||||
path = ((SystemUtil.getOsInfo().isMac() ? "~" : "") + "/jpom/" + JpomApplication.getAppType().name() + "/").toLowerCase();
|
||||
} else {
|
||||
// 获取当前项目运行路径的父级
|
||||
File file = JpomManifest.getRunPath();
|
||||
if (!file.exists() && !file.isFile()) {
|
||||
throw new JpomRuntimeException("请配置运行路径属性【jpom.path】");
|
||||
}
|
||||
path = file.getParentFile().getParentFile().getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
/**
|
||||
* 项目运行存储路径
|
||||
*/
|
||||
@Value("${jpom.path}")
|
||||
private String path;
|
||||
|
||||
public String getAbsolutePath() {
|
||||
return FileUtil.getAbsolutePath(FileUtil.file(getPath()));
|
||||
}
|
||||
public String getPath() {
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
if (JpomManifest.getInstance().isDebug()) {
|
||||
// 调试模式 为根路径的 jpom文件
|
||||
path = ((SystemUtil.getOsInfo().isMac() ? "~" : "") + "/jpom/" + JpomApplication.getAppType().name() + "/").toLowerCase();
|
||||
} else {
|
||||
// 获取当前项目运行路径的父级
|
||||
File file = JpomManifest.getRunPath();
|
||||
if (!file.exists() && !file.isFile()) {
|
||||
throw new JpomRuntimeException("请配置运行路径属性【jpom.path】");
|
||||
}
|
||||
path = file.getParentFile().getParentFile().getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public String getAbsolutePath() {
|
||||
return FileUtil.getAbsolutePath(FileUtil.file(getPath()));
|
||||
}
|
||||
}
|
||||
|
@ -34,223 +34,225 @@ import java.util.Objects;
|
||||
*/
|
||||
public class ReleaseManage extends BaseBuild {
|
||||
|
||||
private final UserModel userModel;
|
||||
private final int buildId;
|
||||
private final BaseBuildModule baseBuildModule;
|
||||
private File resultFile;
|
||||
private BaseBuild baseBuild;
|
||||
private final UserModel userModel;
|
||||
private final int buildId;
|
||||
private final BaseBuildModule baseBuildModule;
|
||||
private File resultFile;
|
||||
private BaseBuild baseBuild;
|
||||
|
||||
ReleaseManage(BuildModel buildModel, UserModel userModel, BaseBuild baseBuild) {
|
||||
super(BuildUtil.getLogFile(buildModel.getId(), buildModel.getBuildId()),
|
||||
buildModel.getId());
|
||||
this.baseBuildModule = buildModel;
|
||||
this.buildId = buildModel.getBuildId();
|
||||
this.userModel = userModel;
|
||||
this.baseBuild = baseBuild;
|
||||
this.init();
|
||||
}
|
||||
ReleaseManage(BuildModel buildModel, UserModel userModel, BaseBuild baseBuild) {
|
||||
super(BuildUtil.getLogFile(buildModel.getId(), buildModel.getBuildId()),
|
||||
buildModel.getId());
|
||||
this.baseBuildModule = buildModel;
|
||||
this.buildId = buildModel.getBuildId();
|
||||
this.userModel = userModel;
|
||||
this.baseBuild = baseBuild;
|
||||
this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新发布
|
||||
*
|
||||
* @param buildHistoryLog 构建历史
|
||||
* @param userModel 用户
|
||||
*/
|
||||
public ReleaseManage(BuildHistoryLog buildHistoryLog, UserModel userModel) {
|
||||
super(BuildUtil.getLogFile(buildHistoryLog.getBuildDataId(), buildHistoryLog.getBuildNumberId()),
|
||||
buildHistoryLog.getBuildDataId());
|
||||
this.baseBuildModule = buildHistoryLog;
|
||||
this.buildId = buildHistoryLog.getBuildNumberId();
|
||||
this.userModel = userModel;
|
||||
this.init();
|
||||
}
|
||||
/**
|
||||
* 重新发布
|
||||
*
|
||||
* @param buildHistoryLog 构建历史
|
||||
* @param userModel 用户
|
||||
*/
|
||||
public ReleaseManage(BuildHistoryLog buildHistoryLog, UserModel userModel) {
|
||||
super(BuildUtil.getLogFile(buildHistoryLog.getBuildDataId(), buildHistoryLog.getBuildNumberId()),
|
||||
buildHistoryLog.getBuildDataId());
|
||||
this.baseBuildModule = buildHistoryLog;
|
||||
this.buildId = buildHistoryLog.getBuildNumberId();
|
||||
this.userModel = userModel;
|
||||
this.init();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(BuildModel.Status status) {
|
||||
if (baseBuild == null) {
|
||||
return super.updateStatus(status);
|
||||
} else {
|
||||
return baseBuild.updateStatus(status);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean updateStatus(BuildModel.Status status) {
|
||||
if (baseBuild == null) {
|
||||
return super.updateStatus(status);
|
||||
} else {
|
||||
return baseBuild.updateStatus(status);
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
this.resultFile = BuildUtil.getHistoryPackageFile(this.buildModelId, this.buildId, this.baseBuildModule.getResultDirFile());
|
||||
}
|
||||
private void init() {
|
||||
this.resultFile = BuildUtil.getHistoryPackageFile(this.buildModelId, this.buildId, this.baseBuildModule.getResultDirFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* 不修改为发布中状态
|
||||
*/
|
||||
public void start2() {
|
||||
this.log("start release:" + FileUtil.readableFileSize(FileUtil.size(this.resultFile)));
|
||||
if (!this.resultFile.exists()) {
|
||||
this.log("不存在构建产物");
|
||||
updateStatus(BuildModel.Status.PubError);
|
||||
return;
|
||||
}
|
||||
long time = SystemClock.now();
|
||||
this.log("release method:" + BaseEnum.getDescByCode(BuildModel.ReleaseMethod.class, this.baseBuildModule.getReleaseMethod()));
|
||||
try {
|
||||
if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Outgiving.getCode()) {
|
||||
//
|
||||
this.doOutGiving();
|
||||
} else if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Project.getCode()) {
|
||||
AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, this.baseBuildModule.getAfterOpt());
|
||||
if (afterOpt == null) {
|
||||
afterOpt = AfterOpt.No;
|
||||
}
|
||||
this.doProject(afterOpt, this.baseBuildModule.isClearOld());
|
||||
} else if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Ssh.getCode()) {
|
||||
this.doSsh();
|
||||
} else {
|
||||
this.log(" 没有实现的发布分发");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.pubLog("发布异常", e);
|
||||
return;
|
||||
}
|
||||
this.log("release complete : " + DateUtil.formatBetween(SystemClock.now() - time, BetweenFormatter.Level.MILLISECOND));
|
||||
updateStatus(BuildModel.Status.PubSuccess);
|
||||
}
|
||||
/**
|
||||
* 不修改为发布中状态
|
||||
*/
|
||||
public void start2() {
|
||||
this.log("start release:" + FileUtil.readableFileSize(FileUtil.size(this.resultFile)));
|
||||
if (!this.resultFile.exists()) {
|
||||
this.log("不存在构建产物");
|
||||
updateStatus(BuildModel.Status.PubError);
|
||||
return;
|
||||
}
|
||||
long time = SystemClock.now();
|
||||
this.log("release method:" + BaseEnum.getDescByCode(BuildModel.ReleaseMethod.class, this.baseBuildModule.getReleaseMethod()));
|
||||
try {
|
||||
if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Outgiving.getCode()) {
|
||||
//
|
||||
this.doOutGiving();
|
||||
} else if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Project.getCode()) {
|
||||
AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, this.baseBuildModule.getAfterOpt());
|
||||
if (afterOpt == null) {
|
||||
afterOpt = AfterOpt.No;
|
||||
}
|
||||
this.doProject(afterOpt, this.baseBuildModule.isClearOld());
|
||||
} else if (this.baseBuildModule.getReleaseMethod() == BuildModel.ReleaseMethod.Ssh.getCode()) {
|
||||
this.doSsh();
|
||||
} else {
|
||||
this.log(" 没有实现的发布分发");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.pubLog("发布异常", e);
|
||||
return;
|
||||
}
|
||||
this.log("release complete : " + DateUtil.formatBetween(SystemClock.now() - time, BetweenFormatter.Level.MILLISECOND));
|
||||
updateStatus(BuildModel.Status.PubSuccess);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改为发布中状态
|
||||
*/
|
||||
public void start() {
|
||||
updateStatus(BuildModel.Status.PubIng);
|
||||
this.start2();
|
||||
}
|
||||
/**
|
||||
* 修改为发布中状态
|
||||
*/
|
||||
public void start() {
|
||||
updateStatus(BuildModel.Status.PubIng);
|
||||
this.start2();
|
||||
}
|
||||
|
||||
private void doSsh() {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
SshService sshService = SpringUtil.getBean(SshService.class);
|
||||
SshModel item = sshService.getItem(releaseMethodDataId);
|
||||
if (item == null) {
|
||||
this.log("没有找到对应的ssh项:" + releaseMethodDataId);
|
||||
return;
|
||||
}
|
||||
Session session = SshService.getSession(item);
|
||||
try (Sftp sftp = new Sftp(session, item.getCharsetT())) {
|
||||
if (this.baseBuildModule.isClearOld() && StrUtil.isNotEmpty(this.baseBuildModule.getReleasePath())) {
|
||||
try {
|
||||
sftp.delDir(this.baseBuildModule.getReleasePath());
|
||||
} catch (Exception e) {
|
||||
this.pubLog("清楚构建产物失败", e);
|
||||
}
|
||||
}
|
||||
String prefix = "";
|
||||
if (!StrUtil.startWith(this.baseBuildModule.getReleasePath(), StrUtil.SLASH)) {
|
||||
prefix = sftp.pwd();
|
||||
}
|
||||
if (this.resultFile.isFile()) {
|
||||
// 文件
|
||||
String normalizePath = FileUtil.normalize(prefix + "/" + this.baseBuildModule.getReleasePath());
|
||||
try {
|
||||
sftp.mkDirs(normalizePath);
|
||||
} catch (Exception e) {
|
||||
this.pubLog(" 切换目录失败:" + normalizePath, e);
|
||||
}
|
||||
sftp.cd(normalizePath);
|
||||
sftp.put(this.resultFile.getAbsolutePath(), this.resultFile.getName());
|
||||
} else if (this.resultFile.isDirectory()) {
|
||||
// 文件夹
|
||||
List<File> files = FileUtil.loopFiles(this.resultFile, pathname -> !pathname.isHidden());
|
||||
String absolutePath = FileUtil.getAbsolutePath(this.resultFile);
|
||||
//
|
||||
for (File file : files) {
|
||||
String itemAbsPath = FileUtil.getAbsolutePath(file);
|
||||
String remoteItemAbsPath = StrUtil.removePrefix(itemAbsPath, absolutePath);
|
||||
remoteItemAbsPath = FileUtil.normalize(prefix + "/" + this.baseBuildModule.getReleasePath() + "/" + remoteItemAbsPath);
|
||||
String parent = StrUtil.subBefore(remoteItemAbsPath, StrUtil.SLASH, true);
|
||||
try {
|
||||
sftp.mkDirs(parent);
|
||||
} catch (Exception e) {
|
||||
this.pubLog(" 切换目录失败:" + parent, e);
|
||||
}
|
||||
sftp.cd(parent);
|
||||
sftp.put(itemAbsPath, file.getName());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.pubLog("执行ssh发布异常", e);
|
||||
}
|
||||
this.log("");
|
||||
this.log(DateUtil.now() + " start exec");
|
||||
// 执行命令
|
||||
String[] commands = StrUtil.splitToArray(this.baseBuildModule.getReleaseCommand(), StrUtil.LF);
|
||||
if (commands == null || commands.length <= 0) {
|
||||
this.log("没有需要执行的ssh命令");
|
||||
return;
|
||||
}
|
||||
for (String commandItem : commands) {
|
||||
try {
|
||||
this.log(commandItem);
|
||||
String s = sshService.exec(item, commandItem);
|
||||
this.log(s);
|
||||
} catch (Exception e) {
|
||||
this.pubLog(item + " 执行异常", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void doSsh() {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
SshService sshService = SpringUtil.getBean(SshService.class);
|
||||
SshModel item = sshService.getItem(releaseMethodDataId);
|
||||
if (item == null) {
|
||||
this.log("没有找到对应的ssh项:" + releaseMethodDataId);
|
||||
return;
|
||||
}
|
||||
Session session = SshService.getSession(item);
|
||||
try (Sftp sftp = new Sftp(session, item.getCharsetT())) {
|
||||
if (this.baseBuildModule.isClearOld() && StrUtil.isNotEmpty(this.baseBuildModule.getReleasePath())) {
|
||||
try {
|
||||
sftp.delDir(this.baseBuildModule.getReleasePath());
|
||||
} catch (Exception e) {
|
||||
this.pubLog("清除构建产物失败", e);
|
||||
}
|
||||
}
|
||||
String prefix = "";
|
||||
if (!StrUtil.startWith(this.baseBuildModule.getReleasePath(), StrUtil.SLASH)) {
|
||||
prefix = sftp.pwd();
|
||||
}
|
||||
String normalizePath = FileUtil.normalize(prefix + "/" + this.baseBuildModule.getReleasePath());
|
||||
sftp.syncUpload(this.resultFile, normalizePath);
|
||||
// if (this.resultFile.isFile()) {
|
||||
// // 文件
|
||||
// String normalizePath = FileUtil.normalize(prefix + "/" + this.baseBuildModule.getReleasePath());
|
||||
// try {
|
||||
// sftp.mkDirs(normalizePath);
|
||||
// } catch (Exception e) {
|
||||
// this.pubLog(" 切换目录失败:" + normalizePath, e);
|
||||
// }
|
||||
// sftp.cd(normalizePath);
|
||||
// sftp.put(this.resultFile.getAbsolutePath(), this.resultFile.getName());
|
||||
// } else if (this.resultFile.isDirectory()) {
|
||||
// // 文件夹
|
||||
// List<File> files = FileUtil.loopFiles(this.resultFile, pathname -> !pathname.isHidden());
|
||||
// String absolutePath = FileUtil.getAbsolutePath(this.resultFile);
|
||||
// //
|
||||
// for (File file : files) {
|
||||
// String itemAbsPath = FileUtil.getAbsolutePath(file);
|
||||
// String remoteItemAbsPath = StrUtil.removePrefix(itemAbsPath, absolutePath);
|
||||
// remoteItemAbsPath = FileUtil.normalize(prefix + "/" + this.baseBuildModule.getReleasePath() + "/" + remoteItemAbsPath);
|
||||
// String parent = StrUtil.subBefore(remoteItemAbsPath, StrUtil.SLASH, true);
|
||||
// try {
|
||||
// sftp.mkDirs(parent);
|
||||
// } catch (Exception e) {
|
||||
// this.pubLog(" 切换目录失败:" + parent, e);
|
||||
// }
|
||||
// sftp.cd(parent);
|
||||
// sftp.put(itemAbsPath, file.getName());
|
||||
// }
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
this.pubLog("执行ssh发布异常", e);
|
||||
}
|
||||
this.log("");
|
||||
this.log(DateUtil.now() + " start exec");
|
||||
// 执行命令
|
||||
String[] commands = StrUtil.splitToArray(this.baseBuildModule.getReleaseCommand(), StrUtil.LF);
|
||||
if (commands == null || commands.length <= 0) {
|
||||
this.log("没有需要执行的ssh命令");
|
||||
return;
|
||||
}
|
||||
for (String commandItem : commands) {
|
||||
try {
|
||||
this.log(commandItem);
|
||||
String s = sshService.exec(item, commandItem);
|
||||
this.log(s);
|
||||
} catch (Exception e) {
|
||||
this.pubLog(item + " 执行异常", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布项目
|
||||
*
|
||||
* @param afterOpt 后续操作
|
||||
*/
|
||||
private void doProject(AfterOpt afterOpt, boolean clearOld) {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
String[] strings = StrUtil.splitToArray(releaseMethodDataId, ":");
|
||||
if (strings == null || strings.length != 2) {
|
||||
throw new JpomRuntimeException(releaseMethodDataId + " error");
|
||||
}
|
||||
NodeService nodeService = SpringUtil.getBean(NodeService.class);
|
||||
NodeModel nodeModel = nodeService.getItem(strings[0]);
|
||||
Objects.requireNonNull(nodeModel, "节点不存在");
|
||||
/**
|
||||
* 发布项目
|
||||
*
|
||||
* @param afterOpt 后续操作
|
||||
*/
|
||||
private void doProject(AfterOpt afterOpt, boolean clearOld) {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
String[] strings = StrUtil.splitToArray(releaseMethodDataId, ":");
|
||||
if (strings == null || strings.length != 2) {
|
||||
throw new JpomRuntimeException(releaseMethodDataId + " error");
|
||||
}
|
||||
NodeService nodeService = SpringUtil.getBean(NodeService.class);
|
||||
NodeModel nodeModel = nodeService.getItem(strings[0]);
|
||||
Objects.requireNonNull(nodeModel, "节点不存在");
|
||||
|
||||
File zipFile = BuildUtil.isDirPackage(this.resultFile);
|
||||
boolean unZip = true;
|
||||
if (zipFile == null) {
|
||||
zipFile = this.resultFile;
|
||||
unZip = false;
|
||||
}
|
||||
JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(zipFile,
|
||||
strings[1],
|
||||
unZip,
|
||||
afterOpt,
|
||||
nodeModel, this.userModel, clearOld);
|
||||
if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
|
||||
this.log("发布项目包成功:" + jsonMessage.toString());
|
||||
} else {
|
||||
throw new JpomRuntimeException("发布项目包失败:" + jsonMessage.toString());
|
||||
}
|
||||
}
|
||||
File zipFile = BuildUtil.isDirPackage(this.resultFile);
|
||||
boolean unZip = true;
|
||||
if (zipFile == null) {
|
||||
zipFile = this.resultFile;
|
||||
unZip = false;
|
||||
}
|
||||
JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(zipFile,
|
||||
strings[1],
|
||||
unZip,
|
||||
afterOpt,
|
||||
nodeModel, this.userModel, clearOld);
|
||||
if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
|
||||
this.log("发布项目包成功:" + jsonMessage.toString());
|
||||
} else {
|
||||
throw new JpomRuntimeException("发布项目包失败:" + jsonMessage.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分发包
|
||||
*/
|
||||
private void doOutGiving() {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
File zipFile = BuildUtil.isDirPackage(this.resultFile);
|
||||
boolean unZip = true;
|
||||
if (zipFile == null) {
|
||||
zipFile = this.resultFile;
|
||||
unZip = false;
|
||||
}
|
||||
OutGivingRun.startRun(releaseMethodDataId, zipFile, userModel, unZip);
|
||||
this.log("开始执行分发包啦,请到分发中查看当前状态");
|
||||
}
|
||||
/**
|
||||
* 分发包
|
||||
*/
|
||||
private void doOutGiving() {
|
||||
String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
|
||||
File zipFile = BuildUtil.isDirPackage(this.resultFile);
|
||||
boolean unZip = true;
|
||||
if (zipFile == null) {
|
||||
zipFile = this.resultFile;
|
||||
unZip = false;
|
||||
}
|
||||
OutGivingRun.startRun(releaseMethodDataId, zipFile, userModel, unZip);
|
||||
this.log("开始执行分发包啦,请到分发中查看当前状态");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发布异常日志
|
||||
*
|
||||
* @param title 描述
|
||||
* @param throwable 异常
|
||||
*/
|
||||
private void pubLog(String title, Throwable throwable) {
|
||||
log(title, throwable, BuildModel.Status.PubError);
|
||||
}
|
||||
/**
|
||||
* 发布异常日志
|
||||
*
|
||||
* @param title 描述
|
||||
* @param throwable 异常
|
||||
*/
|
||||
private void pubLog(String title, Throwable throwable) {
|
||||
log(title, throwable, BuildModel.Status.PubError);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import io.jpom.plugin.Feature;
|
||||
import io.jpom.plugin.MethodFeature;
|
||||
import io.jpom.service.node.ssh.SshService;
|
||||
import io.jpom.system.ConfigBean;
|
||||
import io.jpom.system.ExtConfigBean;
|
||||
import io.jpom.system.ServerConfigBean;
|
||||
import org.springframework.boot.env.YmlUtil;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -107,7 +108,7 @@ public class SshInstallAgentController extends BaseServerController {
|
||||
return JsonMessage.getString(405, "管理命令中不存在tag");
|
||||
}
|
||||
// 读取授权信息
|
||||
File configFile = FileUtil.file(outFle, "extConfig.yml");
|
||||
File configFile = FileUtil.file(outFle, ExtConfigBean.FILE_NAME);
|
||||
if (configFile.exists()) {
|
||||
List<Map<String, Object>> load = YmlUtil.load(configFile);
|
||||
Map<String, Object> map = load.get(0);
|
||||
|
@ -202,56 +202,56 @@ public class SshService extends BaseOperService<SshModel> implements BaseDynamic
|
||||
* @param sshModel ssh
|
||||
* @param remotePath 远程路径
|
||||
* @param desc 文件夹或者文件
|
||||
* @throws FileNotFoundException 文件异常
|
||||
* @throws SftpException ftp
|
||||
*/
|
||||
public void uploadDir(SshModel sshModel, String remotePath, File desc) throws FileNotFoundException, SftpException {
|
||||
public void uploadDir(SshModel sshModel, String remotePath, File desc) {
|
||||
Session session = null;
|
||||
ChannelSftp channel = null;
|
||||
try {
|
||||
session = getSession(sshModel);
|
||||
channel = (ChannelSftp) JschUtil.openChannel(session, ChannelType.SFTP);
|
||||
uploadDir(channel, remotePath, desc, sshModel.getCharsetT());
|
||||
Sftp sftp = new Sftp(channel, sshModel.getCharsetT());
|
||||
sftp.syncUpload(desc, remotePath);
|
||||
//uploadDir(channel, remotePath, desc, sshModel.getCharsetT());
|
||||
} finally {
|
||||
JschUtil.close(channel);
|
||||
JschUtil.close(session);
|
||||
}
|
||||
}
|
||||
|
||||
private void uploadDir(ChannelSftp channel, String remotePath, File file, Charset charset) throws FileNotFoundException, SftpException {
|
||||
if (file.isDirectory()) {
|
||||
File[] files = file.listFiles();
|
||||
if (files == null || files.length <= 0) {
|
||||
return;
|
||||
}
|
||||
for (File f : files) {
|
||||
if (f.isDirectory()) {
|
||||
String mkdir = FileUtil.normalize(remotePath + "/" + f.getName());
|
||||
this.uploadDir(channel, mkdir, f, charset);
|
||||
} else {
|
||||
this.uploadDir(channel, remotePath, f, charset);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mkdir(channel, remotePath, charset);
|
||||
String name = file.getName();
|
||||
channel.put(new FileInputStream(file), name);
|
||||
}
|
||||
}
|
||||
|
||||
private void mkdir(ChannelSftp channel, String remotePath, Charset charset) {
|
||||
Sftp sftp = new Sftp(channel, charset);
|
||||
sftp.mkDirs(remotePath);
|
||||
// try {
|
||||
// channel.mkdir(remotePath);
|
||||
// } catch (SftpException ignored) {
|
||||
// }
|
||||
try {
|
||||
channel.cd(remotePath);
|
||||
} catch (SftpException e) {
|
||||
throw new RuntimeException("切换目录失败:" + remotePath, e);
|
||||
}
|
||||
}
|
||||
// private void uploadDir(ChannelSftp channel, String remotePath, File file, Charset charset) throws FileNotFoundException, SftpException {
|
||||
// if (file.isDirectory()) {
|
||||
// File[] files = file.listFiles();
|
||||
// if (files == null || files.length <= 0) {
|
||||
// return;
|
||||
// }
|
||||
// for (File f : files) {
|
||||
// if (f.isDirectory()) {
|
||||
// String mkdir = FileUtil.normalize(remotePath + "/" + f.getName());
|
||||
// this.uploadDir(channel, mkdir, f, charset);
|
||||
// } else {
|
||||
// this.uploadDir(channel, remotePath, f, charset);
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// mkdir(channel, remotePath, charset);
|
||||
// String name = file.getName();
|
||||
// channel.put(new FileInputStream(file), name);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void mkdir(ChannelSftp channel, String remotePath, Charset charset) {
|
||||
// Sftp sftp = new Sftp(channel, charset);
|
||||
// sftp.mkDirs(remotePath);
|
||||
//// try {
|
||||
//// channel.mkdir(remotePath);
|
||||
//// } catch (SftpException ignored) {
|
||||
//// }
|
||||
// try {
|
||||
// channel.cd(remotePath);
|
||||
// } catch (SftpException e) {
|
||||
// throw new RuntimeException("切换目录失败:" + remotePath, e);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
|
Loading…
Reference in New Issue
Block a user