use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method rootFileList.
/**
* 根据 id 获取 fileDirs 目录集合
*
* @param id ssh id
* @return json
* @author Hotstrip
* @since for dev 3.x
*/
@RequestMapping(value = "root_file_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String rootFileList(String id) {
SshModel sshModel = sshService.getByKey(id, false);
Assert.notNull(sshModel, "不存在对应ssh");
List<String> fileDirs = sshModel.fileDirs();
Assert.notEmpty(fileDirs, "未设置授权目录");
JSONArray jsonArray = this.listDir(sshModel, fileDirs);
return JsonMessage.getString(200, "ok", jsonArray);
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class BuildInfoController method formatSsh.
/**
* 验证构建信息
* 当发布方式为【SSH】的时候
*
* @param jsonObject 配置信息
*/
private void formatSsh(JSONObject jsonObject) {
// 发布方式
String releaseMethodDataId = jsonObject.getString("releaseMethodDataId_3");
Assert.hasText(releaseMethodDataId, "请选择分发SSH项");
String releasePath = jsonObject.getString("releasePath");
Assert.hasText(releasePath, "请输入发布到ssh中的目录");
releasePath = FileUtil.normalize(releasePath);
String releaseCommand = jsonObject.getString("releaseCommand");
List<String> strings = StrUtil.splitTrim(releaseMethodDataId, StrUtil.COMMA);
for (String releaseMethodDataIdItem : strings) {
SshModel sshServiceItem = sshService.getByKey(releaseMethodDataIdItem, getRequest());
Assert.notNull(sshServiceItem, "没有对应的ssh项");
//
if (releasePath.startsWith(StrUtil.SLASH)) {
// 以根路径开始
List<String> fileDirs = sshServiceItem.fileDirs();
Assert.notEmpty(fileDirs, sshServiceItem.getName() + "此ssh未授权操作此目录");
boolean find = false;
for (String fileDir : fileDirs) {
if (FileUtil.isSub(new File(fileDir), new File(releasePath))) {
find = true;
}
}
Assert.state(find, sshServiceItem.getName() + "此ssh未授权操作此目录");
}
// 发布命令
if (StrUtil.isNotEmpty(releaseCommand)) {
int length = releaseCommand.length();
Assert.state(length <= 4000, "发布命令长度限制在4000字符");
// return JsonMessage.getString(405, "请输入发布命令");
String[] commands = StrUtil.splitToArray(releaseCommand, StrUtil.LF);
for (String commandItem : commands) {
boolean checkInputItem = SshModel.checkInputItem(sshServiceItem, commandItem);
Assert.state(checkInputItem, sshServiceItem.getName() + "发布命令中包含禁止执行的命令");
}
}
}
jsonObject.put("releaseMethodDataId", releaseMethodDataId);
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class ReleaseManage method doSsh.
/**
* ssh 发布
*/
private void doSsh() {
String releaseMethodDataId = this.buildExtraModule.getReleaseMethodDataId();
SshService sshService = SpringUtil.getBean(SshService.class);
List<String> strings = StrUtil.splitTrim(releaseMethodDataId, StrUtil.COMMA);
for (String releaseMethodDataIdItem : strings) {
SshModel item = sshService.getByKey(releaseMethodDataIdItem, false);
if (item == null) {
logRecorder.info("没有找到对应的ssh项:" + releaseMethodDataIdItem);
continue;
}
this.doSsh(item, sshService);
}
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method upload.
// /**
// * 删除文件或者文件夹
// *
// * @param channel channel
// * @param path 文件路径
// * @throws SftpException SftpException
// */
// private void deleteFile(ChannelSftp channel, String path) throws SftpException {
// Vector<ChannelSftp.LsEntry> vector = channel.ls(path);
// if (null == vector) {
// return;
// }
// int size = vector.size();
// if (size == 1) {
// // 文件,直接删除
// channel.rm(path);
// } else if (size == 2) {
// // 空文件夹,直接删除
// channel.rmdir(path);
// } else {
// // 删除文件夹下所有文件
// String fileName;
// for (ChannelSftp.LsEntry en : vector) {
// fileName = en.getFilename();
// if (!".".equals(fileName) && !"..".equals(fileName)) {
// deleteFile(channel, path + "/" + fileName);
// }
// }
// channel.rmdir(path);
// }
// }
@RequestMapping(value = "upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.UPLOAD)
public String upload(String id, String path, String name, String unzip) {
SshModel sshModel = sshService.getByKey(id, false);
Assert.notNull(sshModel, "ssh error");
List<String> fileDirs = sshModel.fileDirs();
Assert.state(CollUtil.contains(fileDirs, path), "没有配置此文件夹");
String remotePath = FileUtil.normalize(path + StrUtil.SLASH + name);
Session session = null;
ChannelSftp channel = null;
String localPath = null;
try {
session = SshService.getSessionByModel(sshModel);
channel = (ChannelSftp) JschUtil.openChannel(session, ChannelType.SFTP);
MultipartFileBuilder multipart = createMultipart();
// 保存路径
File tempPath = ServerConfigBean.getInstance().getUserTempPath();
File savePath = FileUtil.file(tempPath, "ssh", sshModel.getId());
multipart.setSavePath(FileUtil.getAbsolutePath(savePath));
multipart.addFieldName("file").setUseOriginalFilename(true);
//
if (Convert.toBool(unzip, false)) {
multipart.setFileExt(StringUtil.PACKAGE_EXT);
localPath = multipart.save();
// 解压
File file = new File(localPath);
File tempUnzipPath = FileUtil.file(savePath, IdUtil.fastSimpleUUID());
try {
CompressionFileUtil.unCompress(file, tempUnzipPath);
// 同步上传文件
sshService.uploadDir(sshModel, remotePath, tempUnzipPath);
} finally {
// 删除临时文件
CommandUtil.systemFastDel(file);
CommandUtil.systemFastDel(tempUnzipPath);
}
} else {
localPath = multipart.save();
File file = FileUtil.file(localPath);
channel.cd(remotePath);
try (FileInputStream src = IoUtil.toStream(file)) {
channel.put(src, file.getName());
}
}
} catch (Exception e) {
DefaultSystemLog.getLog().error("ssh上传文件异常", e);
return JsonMessage.getString(400, "上传失败:" + e.getMessage());
} finally {
JschUtil.close(channel);
JschUtil.close(session);
FileUtil.del(localPath);
}
return JsonMessage.getString(200, "上传成功");
}
use of io.jpom.model.data.SshModel in project Jpom by dromara.
the class SshFileController method updateFileData.
@RequestMapping(value = "update_file_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String updateFileData(String id, String path, String children, String content) {
SshModel sshModel = this.check(id, path, children);
//
List<String> allowEditSuffix = sshModel.allowEditSuffix();
Charset charset = AgentWhitelist.checkFileSuffix(allowEditSuffix, children);
// 缓存到本地
File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), sshModel.getId(), children);
FileUtil.writeString(content, file, charset);
// 上传
this.syncFile(sshModel, path, children, file);
//
FileUtil.del(file);
return JsonMessage.getString(200, "修改成功");
}
Aggregations