Search in sources :

Example 1 with ServerConfigBean

use of io.jpom.system.ServerConfigBean in project Jpom by dromara.

the class SshInstallAgentController method getAgentFile.

private JSONObject getAgentFile() throws IOException {
    ServerConfigBean instance = ServerConfigBean.getInstance();
    File agentZipPath = instance.getAgentZipPath();
    Assert.state(FileUtil.exist(agentZipPath), "插件包文件不存在,需要重新上传");
    String tempFilePath = instance.getUserTempPath().getAbsolutePath();
    // 
    File tempAgent = FileUtil.file(tempFilePath, "temp_agent");
    FileUtil.del(tempAgent);
    try {
        // 解析压缩包
        File jarFile = JpomManifest.zipFileFind(FileUtil.getAbsolutePath(agentZipPath), Type.Agent, FileUtil.getAbsolutePath(tempAgent));
        // 获取包内容
        JsonMessage<Tuple> tupleJsonMessage = JpomManifest.checkJpomJar(FileUtil.getAbsolutePath(jarFile), Type.Agent, false);
        Assert.state(tupleJsonMessage.getCode() == 200, tupleJsonMessage::getMsg);
        Tuple data = tupleJsonMessage.getData();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("version", data.get(0));
        jsonObject.put("timeStamp", data.get(1));
        jsonObject.put("path", FileUtil.getAbsolutePath(agentZipPath));
        return jsonObject;
    } finally {
        FileUtil.del(tempAgent);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ZipFile(java.util.zip.ZipFile) File(java.io.File) ServerConfigBean(io.jpom.system.ServerConfigBean) Tuple(cn.hutool.core.lang.Tuple)

Example 2 with ServerConfigBean

use of io.jpom.system.ServerConfigBean 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)

Example 3 with ServerConfigBean

use of io.jpom.system.ServerConfigBean in project Jpom by dromara.

the class SshInstallAgentController method uploadAgent.

@RequestMapping(value = "upload_agent.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
@SystemPermission
public String uploadAgent() throws Exception {
    ServerConfigBean instance = ServerConfigBean.getInstance();
    String tempFilePath = instance.getUserTempPath().getAbsolutePath();
    MultipartFileBuilder multipartFileBuilder = createMultipart().setFileExt("zip").addFieldName("file").setSavePath(tempFilePath);
    String filePath = multipartFileBuilder.save();
    File tempAgent = FileUtil.file(tempFilePath, "temp_agent");
    FileUtil.del(tempAgent);
    // 解析压缩包
    File jarFile = JpomManifest.zipFileFind(filePath, Type.Agent, FileUtil.getAbsolutePath(tempAgent));
    // 验证文件是否正确
    JsonMessage<Tuple> tupleJsonMessage = JpomManifest.checkJpomJar(FileUtil.getAbsolutePath(jarFile), Type.Agent, false);
    Assert.state(tupleJsonMessage.getCode() == 200, tupleJsonMessage::getMsg);
    // 
    File outFle = FileUtil.file(tempFilePath, Type.Agent.name() + "_" + IdUtil.fastSimpleUUID());
    try {
        this.unZipGetTag(filePath, outFle);
        // 保存插件包
        File agentZipPath = instance.getAgentZipPath();
        FileUtil.copy(FileUtil.file(filePath), agentZipPath, true);
        return JsonMessage.getString(200, "上传成功");
    } finally {
        FileUtil.del(filePath);
        FileUtil.del(jarFile);
    }
}
Also used : MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) ZipFile(java.util.zip.ZipFile) File(java.io.File) ServerConfigBean(io.jpom.system.ServerConfigBean) Tuple(cn.hutool.core.lang.Tuple) 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

ServerConfigBean (io.jpom.system.ServerConfigBean)3 File (java.io.File)3 ZipFile (java.util.zip.ZipFile)3 Tuple (cn.hutool.core.lang.Tuple)2 JSONObject (com.alibaba.fastjson.JSONObject)2 ClassFeature (io.jpom.permission.ClassFeature)2 Feature (io.jpom.permission.Feature)2 MethodFeature (io.jpom.permission.MethodFeature)2 SystemPermission (io.jpom.permission.SystemPermission)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MultipartFileBuilder (cn.jiangzeyin.controller.multipart.MultipartFileBuilder)1 NodeModel (io.jpom.model.data.NodeModel)1 SshModel (io.jpom.model.data.SshModel)1