Search in sources :

Example 6 with Feature

use of io.jpom.permission.Feature in project Jpom by dromara.

the class CommandInfoController method del.

/**
 * 删除命令
 *
 * @param id id
 * @return result
 *
 * @api {DELETE} node/ssh_command/del 删除命令
 * @apiGroup node/ssh_command
 * @apiUse defResultJson
 * @apiParam {String} id 日志 id
 */
@RequestMapping(value = "del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String del(String id) {
    File logFileDir = CommandExecLogModel.logFileDir(id);
    boolean fastDel = CommandUtil.systemFastDel(logFileDir);
    Assert.state(!fastDel, "清理日志文件失败");
    // 
    HttpServletRequest request = getRequest();
    commandService.delByKey(id, request);
    commandExecLogService.delByWorkspace(request, entity -> entity.set("commandId", id));
    return JsonMessage.getString(200, "操作成功");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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)

Example 7 with Feature

use of io.jpom.permission.Feature in project Jpom by dromara.

the class SshController method del.

@PostMapping(value = "del.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String del(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id) {
    HttpServletRequest request = getRequest();
    boolean checkSsh = buildInfoService.checkReleaseMethodByLike(id, request, BuildReleaseMethod.Ssh);
    Assert.state(!checkSsh, "当前ssh存在构建项,不能删除");
    // 判断是否绑定节点
    List<NodeModel> nodeBySshId = nodeService.getNodeBySshId(id);
    Assert.state(CollUtil.isEmpty(nodeBySshId), "当前ssh被节点绑定,不能删除");
    sshService.delByKey(id, request);
    // 
    int logCount = sshTerminalExecuteLogService.delByWorkspace(request, entity -> entity.set("sshId", id));
    return JsonMessage.getString(200, "操作成功");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeModel(io.jpom.model.data.NodeModel) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 8 with Feature

use of io.jpom.permission.Feature in project Jpom by dromara.

the class SshController method save.

/**
 * 编辑
 *
 * @param name              名称
 * @param host              端口
 * @param user              用户名
 * @param password          密码
 * @param connectType       连接方式
 * @param privateKey        私钥
 * @param port              端口
 * @param charset           编码格式
 * @param fileDirs          文件夹
 * @param id                ID
 * @param notAllowedCommand 禁止输入的命令
 * @return json
 */
@PostMapping(value = "save.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String save(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "ssh名称不能为空") String name, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "host不能为空") String host, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "user不能为空") String user, String password, SshModel.ConnectType connectType, String privateKey, @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "port错误") int port, String charset, String fileDirs, String id, String notAllowedCommand) {
    SshModel sshModel;
    boolean add = StrUtil.isEmpty(getParameter("id"));
    if (add) {
        // 优先判断参数 如果是 password 在修改时可以不填写
        if (connectType == SshModel.ConnectType.PASS) {
            Assert.hasText(password, "请填写登录密码");
        } else if (connectType == SshModel.ConnectType.PUBKEY) {
        // Assert.hasText(privateKey, "请填写证书内容");
        }
        sshModel = new SshModel();
    } else {
        sshModel = sshService.getByKey(id);
        Assert.notNull(sshModel, "不存在对应ssh");
    }
    // 目录
    if (StrUtil.isEmpty(fileDirs)) {
        sshModel.fileDirs(null);
    } else {
        List<String> list = StrSplitter.splitTrim(fileDirs, StrUtil.LF, true);
        for (String s : list) {
            String normalize = FileUtil.normalize(s + StrUtil.SLASH);
            int count = StrUtil.count(normalize, StrUtil.SLASH);
            Assert.state(count >= 2, "ssh 授权目录不能是根目录");
        }
        // 
        UserModel userModel = getUser();
        Assert.state(!userModel.isDemoUser(), PermissionInterceptor.DEMO_TIP);
        sshModel.fileDirs(list);
    }
    sshModel.setHost(host);
    // 如果密码传递不为空就设置值 因为上面已经判断了只有修改的情况下 password 才可能为空
    if (StrUtil.isNotEmpty(password)) {
        sshModel.setPassword(password);
    }
    if (StrUtil.startWith(privateKey, URLUtil.FILE_URL_PREFIX)) {
        String rsaPath = StrUtil.removePrefix(privateKey, URLUtil.FILE_URL_PREFIX);
        Assert.state(FileUtil.isFile(rsaPath), "配置的私钥文件不存在");
    }
    if (StrUtil.isNotEmpty(privateKey)) {
        sshModel.setPrivateKey(privateKey);
    }
    sshModel.setPort(port);
    sshModel.setUser(user);
    sshModel.setName(name);
    sshModel.setNotAllowedCommand(notAllowedCommand);
    sshModel.setConnectType(connectType.name());
    // 获取允许编辑的后缀
    String allowEditSuffix = getParameter("allowEditSuffix");
    List<String> allowEditSuffixList = AgentWhitelist.parseToList(allowEditSuffix, "允许编辑的文件后缀不能为空");
    sshModel.allowEditSuffix(allowEditSuffixList);
    try {
        Charset.forName(charset);
        sshModel.setCharset(charset);
    } catch (Exception e) {
        return JsonMessage.getString(405, "请填写正确的编码格式");
    }
    // 判断重复
    HttpServletRequest request = getRequest();
    String workspaceId = sshService.getCheckUserWorkspace(request);
    Entity entity = Entity.create();
    entity.set("host", sshModel.getHost());
    entity.set("port", sshModel.getPort());
    entity.set("workspaceId", workspaceId);
    if (StrUtil.isNotEmpty(id)) {
        entity.set("id", StrUtil.format(" <> {}", id));
    }
    boolean exists = sshService.exists(entity);
    Assert.state(!exists, "对应的SSH已经存在啦");
    try {
        SshModel model = sshService.getByKey(id, false);
        if (model != null) {
            sshModel.setPassword(StrUtil.emptyToDefault(sshModel.getPassword(), model.getPassword()));
            sshModel.setPrivateKey(StrUtil.emptyToDefault(sshModel.getPrivateKey(), model.getPrivateKey()));
        }
        Session session = SshService.getSessionByModel(sshModel);
        JschUtil.close(session);
    } catch (Exception e) {
        return JsonMessage.getString(505, "ssh连接失败:" + e.getMessage());
    }
    if (add) {
        sshService.insert(sshModel);
    } else {
        sshService.update(sshModel);
    }
    return JsonMessage.getString(200, "操作成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) Entity(cn.hutool.db.Entity) SshModel(io.jpom.model.data.SshModel) Session(com.jcraft.jsch.Session) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 9 with Feature

use of io.jpom.permission.Feature in project Jpom by dromara.

the class SshController method checkAgent.

/**
 * 检查 ssh 是否安装插件端
 *
 * @param ids ids
 * @return json
 */
@GetMapping(value = "check_agent.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String checkAgent(String ids) {
    List<SshModel> sshModels = sshService.listById(StrUtil.split(ids, StrUtil.COMMA), getRequest());
    Assert.notEmpty(sshModels, "没有任何节点信息");
    JSONObject result = new JSONObject();
    for (SshModel sshModel : sshModels) {
        List<NodeModel> nodeBySshId = nodeService.getNodeBySshId(sshModel.getId());
        JSONObject data = new JSONObject();
        NodeModel nodeModel = CollUtil.getFirst(nodeBySshId);
        SshModel model = sshService.getByKey(sshModel.getId(), false);
        try {
            if (nodeModel == null) {
                Integer pid = sshService.checkSshRunPid(model, Type.Agent.getTag());
                data.put("pid", ObjectUtil.defaultIfNull(pid, 0));
                data.put("ok", true);
            } else {
                data.put("nodeId", nodeModel.getId());
                data.put("nodeName", nodeModel.getName());
            }
            // 
            String javaVersion = sshService.checkCommand(model, "java");
            data.put("javaVersion", javaVersion);
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("检查运行状态异常:{}", e.getMessage());
            data.put("error", e.getMessage());
        }
        result.put(sshModel.getId(), data);
    }
    return JsonMessage.getString(200, "", result);
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) SshModel(io.jpom.model.data.SshModel) GetMapping(org.springframework.web.bind.annotation.GetMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 10 with Feature

use of io.jpom.permission.Feature in project Jpom by dromara.

the class SshInstallAgentController method installAgentSubmit.

@RequestMapping(value = "installAgentSubmit.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
@SystemPermission
public String installAgentSubmit(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "节点数据") String nodeData, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "安装路径") String path) throws Exception {
    // 
    SshModel sshModel = sshService.getByKey(id, false);
    Objects.requireNonNull(sshModel, "没有找到对应ssh");
    // 判断输入的节点信息
    NodeModel nodeModel = this.getNodeModel(nodeData, sshModel);
    // 
    ServerConfigBean instance = ServerConfigBean.getInstance();
    String tempFilePath = instance.getUserTempPath().getAbsolutePath();
    JSONObject agentFile = this.getAgentFile();
    String filePath = agentFile.getString("path");
    // 
    File outFle = FileUtil.file(tempFilePath, Type.Agent.name() + "_" + IdUtil.fastSimpleUUID());
    try {
        String tag = this.unZipGetTag(filePath, outFle);
        // 
        this.readNodeAuthorize(outFle, nodeModel);
        // 查询远程是否运行
        Assert.state(!sshService.checkSshRun(sshModel, tag), "对应服务器中已经存在 Jpom 插件端,不需要再次安装啦");
        // 上传文件到服务器
        sshService.uploadDir(sshModel, path, outFle);
        // 
        String shPtah = FileUtil.normalize(path + StrUtil.SLASH + Type.Agent.name() + ".sh");
        String chmod = getParameter("chmod");
        if (StrUtil.isEmptyOrUndefined(chmod)) {
            chmod = StrUtil.EMPTY;
        } else {
            chmod = StrUtil.format("{} {} && ", chmod, shPtah);
        }
        String command = StrUtil.format("{}bash {} start upgrade", chmod, shPtah);
        String result = sshService.exec(sshModel, command);
        DefaultSystemLog.getLog().debug("ssh install agent node {} {}", command, result);
        // 休眠 5 秒, 尝试 5 次
        int waitCount = getParameterInt("waitCount", 5);
        this.loopCheck(waitCount, nodeModel, sshModel, path, result);
        // 绑定关系
        nodeModel.setSshId(sshModel.getId());
        nodeService.insert(nodeModel);
        // 
        return JsonMessage.getString(200, "操作成功:" + result);
    } finally {
        // 清理资源
        FileUtil.del(outFle);
    }
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) SshModel(io.jpom.model.data.SshModel) ZipFile(java.util.zip.ZipFile) File(java.io.File) ServerConfigBean(io.jpom.system.ServerConfigBean) SystemPermission(io.jpom.permission.SystemPermission) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Feature (io.jpom.permission.Feature)129 MethodFeature (io.jpom.permission.MethodFeature)129 ClassFeature (io.jpom.permission.ClassFeature)128 JSONObject (com.alibaba.fastjson.JSONObject)49 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)48 File (java.io.File)36 PostMapping (org.springframework.web.bind.annotation.PostMapping)25 DockerInfoModel (io.jpom.model.docker.DockerInfoModel)22 HttpServletRequest (javax.servlet.http.HttpServletRequest)22 NodeModel (io.jpom.model.data.NodeModel)21 GetMapping (org.springframework.web.bind.annotation.GetMapping)21 UserModel (io.jpom.model.data.UserModel)14 SystemPermission (io.jpom.permission.SystemPermission)14 BuildInfoModel (io.jpom.model.data.BuildInfoModel)12 SshModel (io.jpom.model.data.SshModel)10 Entity (cn.hutool.db.Entity)8 MultipartFileBuilder (cn.jiangzeyin.controller.multipart.MultipartFileBuilder)8 JSONArray (com.alibaba.fastjson.JSONArray)8 JsonMessage (cn.jiangzeyin.common.JsonMessage)7 List (java.util.List)7