worker添加,自定义镜像worker修改,机器管理接口添加
This commit is contained in:
parent
7e0f4345b6
commit
3ff9040893
@ -0,0 +1,26 @@
|
||||
package cd.casic.ci.common.pipeline.req.machine;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MachineInfoReq {
|
||||
|
||||
/**
|
||||
* 主机ip
|
||||
*/
|
||||
private String machineHost;
|
||||
|
||||
/**
|
||||
* 机器名称
|
||||
*/
|
||||
private String machineName;
|
||||
|
||||
/**
|
||||
* 机器状态 1有效 2无效
|
||||
*/
|
||||
private String machineStatus;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package cd.casic.ci.common.pipeline.resp.machine;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MachineInfoResp {
|
||||
/**
|
||||
* 机器描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 主机ip
|
||||
*/
|
||||
private String machineHost;
|
||||
|
||||
/**
|
||||
* 机器名称
|
||||
*/
|
||||
private String machineName;
|
||||
|
||||
/**
|
||||
* 机器状态 1有效 2无效
|
||||
*/
|
||||
private String machineStatus;
|
||||
|
||||
/**
|
||||
* 机器唯一标识
|
||||
*/
|
||||
private String machineTag;
|
||||
}
|
@ -8,6 +8,6 @@ public class DIYImageExecuteCommandConstant {
|
||||
/**
|
||||
* 人工卡点命令脚本
|
||||
*/
|
||||
public static final String COMMAND_SCRIPT ="commandScript";
|
||||
public static final String COMMAND_SCRIPT ="buildScript";
|
||||
public static final String STATUS_CODE = "statusCode";
|
||||
}
|
||||
|
@ -2,11 +2,140 @@ package cd.casic.ci.process.engine.worker;
|
||||
|
||||
import cd.casic.ci.common.pipeline.annotation.Plugin;
|
||||
import cd.casic.ci.process.engine.runContext.TaskRunContext;
|
||||
import cd.casic.ci.process.process.dataObject.base.PipBaseElement;
|
||||
import cd.casic.ci.process.process.dataObject.pipeline.PipPipeline;
|
||||
import cd.casic.ci.process.process.dataObject.target.TargetVersion;
|
||||
import cd.casic.ci.process.process.dataObject.task.PipTask;
|
||||
import cd.casic.ci.process.process.service.target.TargetVersionService;
|
||||
import cd.casic.framework.commons.exception.ServiceException;
|
||||
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.jcraft.jsch.*;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@Plugin(taskType = "AFL")
|
||||
@Slf4j
|
||||
public class AFLWorker extends SshWorker{
|
||||
@Resource
|
||||
private TargetVersionService targetVersionService;
|
||||
@Override
|
||||
public void execute(TaskRunContext context) {
|
||||
String filePath = "";
|
||||
Map<String, Object> localVariables = context.getLocalVariables();
|
||||
PipBaseElement taskContextDef = context.getContextDef();
|
||||
if (taskContextDef instanceof PipTask pipTask){
|
||||
// 查询并下载目标文件
|
||||
String pipelineId = pipTask.getPipelineId();
|
||||
//根据流水线id查询流水线信息
|
||||
PipPipeline pipeline = (PipPipeline) getContextManager().getContext(pipelineId).getContextDef();
|
||||
//根据目标id查询目标信息
|
||||
if (StringUtils.isEmpty(pipeline.getTargetVersionId())){
|
||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在");
|
||||
}
|
||||
TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId());
|
||||
filePath = targetVersion.getFilePath();
|
||||
File file = new File(filePath);
|
||||
if (!file.exists() || !file.canRead()) {
|
||||
log.error("目标文件不存在或不可读");
|
||||
localVariables.put("statusCode", "-1");
|
||||
append(context,"目标文件不存在或不可读");
|
||||
toBadEnding();
|
||||
}
|
||||
// 上传文件
|
||||
// 执行shell脚本
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String remoteHost = "your-remote-server-ip-or-hostname"; // 远程服务器IP或主机名
|
||||
int remotePort = 22; // SFTP 默认端口通常是 22 (SSH 端口)
|
||||
String username = "your-username"; // 远程服务器用户名
|
||||
String password = "your-password"; // 远程服务器密码 或 SSH Key 路径
|
||||
|
||||
String localFilePath = "path/to/your/local/file.txt"; // 要上传的本地文件路径
|
||||
String remoteDir = "/path/on/remote/server/"; // 远程服务器存放文件的目录
|
||||
String remoteFileName = "uploaded_file.txt"; // 上传到远程服务器的文件名 (可以和本地不同)
|
||||
|
||||
Session session = null;
|
||||
Channel channel = null;
|
||||
ChannelSftp channelSftp = null;
|
||||
FileInputStream fis = null;
|
||||
|
||||
try {
|
||||
JSch jsch = new JSch();
|
||||
|
||||
// 如果使用 SSH Key 认证,可以添加身份文件:
|
||||
// jsch.addIdentity("/path/to/your/private/key"); // 替换为你的私钥文件路径
|
||||
|
||||
session = jsch.getSession(username, remoteHost, remotePort);
|
||||
|
||||
// 如果使用密码认证:
|
||||
session.setPassword(password);
|
||||
|
||||
// 设置连接不进行主机密钥检查 (生产环境不推荐,应该配置known_hosts)
|
||||
java.util.Properties config = new java.util.Properties();
|
||||
config.put("StrictHostKeyChecking", "no");
|
||||
session.setConfig(config);
|
||||
|
||||
session.connect();
|
||||
System.out.println("SFTP Session 连接成功.");
|
||||
|
||||
channel = session.openChannel("sftp");
|
||||
channel.connect();
|
||||
System.out.println("SFTP Channel 打开成功.");
|
||||
|
||||
channelSftp = (ChannelSftp) channel;
|
||||
|
||||
// 切换到远程目录 (如果目录不存在,可能需要先创建)
|
||||
try {
|
||||
channelSftp.cd(remoteDir);
|
||||
} catch (SftpException e) {
|
||||
System.err.println("远程目录不存在,尝试创建: " + remoteDir);
|
||||
try {
|
||||
channelSftp.mkdir(remoteDir);
|
||||
channelSftp.cd(remoteDir);
|
||||
} catch (SftpException e2) {
|
||||
System.err.println("创建远程目录失败: " + remoteDir);
|
||||
throw e2; // 抛出异常终止上传
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 上传文件
|
||||
File localFile = new File(localFilePath);
|
||||
fis = new FileInputStream(localFile);
|
||||
|
||||
channelSftp.put(fis, remoteFileName);
|
||||
System.out.println("文件上传成功到: " + remoteDir + remoteFileName);
|
||||
|
||||
} catch (JSchException | SftpException | IOException e) {
|
||||
System.err.println("SFTP 文件上传过程中发生异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关闭资源
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (channelSftp != null) {
|
||||
channelSftp.disconnect();
|
||||
System.out.println("SFTP Channel 已断开.");
|
||||
}
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
System.out.println("SFTP Session 已断开.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package cd.casic.ci.process.process.converter;
|
||||
|
||||
import cd.casic.ci.common.pipeline.resp.machine.MachineInfoResp;
|
||||
import cd.casic.ci.common.pipeline.resp.pipeline.PipelineFindResp;
|
||||
import cd.casic.ci.process.process.dataObject.machine.MachineInfo;
|
||||
import cd.casic.ci.process.process.dataObject.pipeline.PipPipeline;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author HopeLi
|
||||
* @version v1.0
|
||||
* @ClassName PipelineConverter
|
||||
* @Date: 2025/5/13 14:39
|
||||
* @Description:
|
||||
*/
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface MachineConverter {
|
||||
MachineConverter INSTANCE = Mappers.getMapper(MachineConverter.class);
|
||||
MachineInfoResp toResp(MachineInfo pipPipeline);
|
||||
List<MachineInfoResp> toRespList(List<MachineInfo> pipPipelines);
|
||||
|
||||
}
|
@ -76,7 +76,7 @@ public class MachineInfo extends BaseDO {
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 机器账号
|
||||
* 系统类型
|
||||
*/
|
||||
@TableField("os_system")
|
||||
private String osSystem;
|
||||
|
@ -1,6 +1,8 @@
|
||||
package cd.casic.ci.process.process.service.machine;
|
||||
|
||||
|
||||
import cd.casic.ci.common.pipeline.req.machine.MachineInfoReq;
|
||||
import cd.casic.ci.common.pipeline.resp.machine.MachineInfoResp;
|
||||
import cd.casic.ci.process.process.dataObject.machine.MachineInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
@ -13,5 +15,6 @@ import java.util.List;
|
||||
* @date 2022-09-27 10:25:29
|
||||
*/
|
||||
public interface MachineInfoService extends IService<MachineInfo> {
|
||||
List<MachineInfoResp> list(MachineInfoReq req);
|
||||
|
||||
}
|
||||
|
@ -1,15 +1,21 @@
|
||||
package cd.casic.ci.process.process.service.machine.impl;
|
||||
|
||||
|
||||
import cd.casic.ci.common.pipeline.req.machine.MachineInfoReq;
|
||||
import cd.casic.ci.common.pipeline.resp.machine.MachineInfoResp;
|
||||
import cd.casic.ci.process.process.converter.MachineConverter;
|
||||
import cd.casic.ci.process.process.dal.machine.MachineInfoDao;
|
||||
import cd.casic.ci.process.process.dataObject.machine.MachineInfo;
|
||||
import cd.casic.ci.process.process.service.machine.MachineInfoService;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
@ -20,6 +26,21 @@ import org.springframework.stereotype.Service;
|
||||
@Slf4j
|
||||
@Service
|
||||
public class MachineInfoServiceImpl extends ServiceImpl<MachineInfoDao, MachineInfo> implements MachineInfoService {
|
||||
@Resource
|
||||
private MachineInfoDao machineInfoDao;
|
||||
|
||||
|
||||
@Override
|
||||
public List<MachineInfoResp> list(MachineInfoReq req) {
|
||||
LambdaQueryWrapper<MachineInfo> wrapper = new LambdaQueryWrapper<>();
|
||||
// 根据机器名称 查询
|
||||
if (ObjectUtil.isNotEmpty(req.getMachineName())) {
|
||||
wrapper.like(MachineInfo::getMachineName, req.getMachineName());
|
||||
}
|
||||
// 根据机器状态 1有效 2无效 查询
|
||||
if (ObjectUtil.isNotEmpty(req.getMachineStatus())) {
|
||||
wrapper.eq(MachineInfo::getMachineStatus, req.getMachineStatus());
|
||||
}
|
||||
List<MachineInfo> machineInfos = machineInfoDao.selectList(wrapper);
|
||||
return MachineConverter.INSTANCE.toRespList(machineInfos);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package cd.casic.server.controller;
|
||||
|
||||
import cd.casic.ci.process.process.dataObject.machine.MachineInfo;
|
||||
import cd.casic.ci.process.process.service.machine.MachineInfoService;
|
||||
import cd.casic.framework.commons.pojo.CommonResult;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/machineInfo")
|
||||
public class MachineController {
|
||||
@Resource
|
||||
private MachineInfoService machineInfoService;
|
||||
|
||||
public CommonResult<List<MachineInfo>> list(){
|
||||
return CommonResult.success(machineInfoService.list());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user