Search in sources :

Example 11 with SshModel

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);
}
Also used : SshModel(io.jpom.model.data.SshModel) JSONArray(com.alibaba.fastjson.JSONArray) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with SshModel

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);
}
Also used : SshModel(io.jpom.model.data.SshModel) File(java.io.File)

Example 13 with SshModel

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);
    }
}
Also used : SshModel(io.jpom.model.data.SshModel) SshService(io.jpom.service.node.ssh.SshService)

Example 14 with SshModel

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, "上传成功");
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SshModel(io.jpom.model.data.SshModel) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) File(java.io.File) FileInputStream(java.io.FileInputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with SshModel

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, "修改成功");
}
Also used : SshModel(io.jpom.model.data.SshModel) Charset(java.nio.charset.Charset) File(java.io.File) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SshModel (io.jpom.model.data.SshModel)20 ClassFeature (io.jpom.permission.ClassFeature)10 Feature (io.jpom.permission.Feature)10 MethodFeature (io.jpom.permission.MethodFeature)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 JSONObject (com.alibaba.fastjson.JSONObject)5 UserModel (io.jpom.model.data.UserModel)5 File (java.io.File)5 IOException (java.io.IOException)5 StrUtil (cn.hutool.core.util.StrUtil)3 Session (com.jcraft.jsch.Session)3 BaseServerController (io.jpom.common.BaseServerController)3 NodeModel (io.jpom.model.data.NodeModel)3 SshService (io.jpom.service.node.ssh.SshService)3 Charset (java.nio.charset.Charset)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Service (org.springframework.stereotype.Service)3 Entity (cn.hutool.db.Entity)2