worker common 模块
This commit is contained in:
parent
a100eae6c1
commit
12ccae1f2a
@ -0,0 +1,149 @@
|
||||
package cd.casic.devops.common.worker.task;
|
||||
|
||||
import cd.casic.ci.common.pipeline.enums.ErrorCode;
|
||||
import cd.casic.ci.common.pipeline.pojo.BuildParameters;
|
||||
import cd.casic.ci.common.pipeline.pojo.ErrorType;
|
||||
import cd.casic.ci.process.api.process.pojo.BuildTask;
|
||||
import cd.casic.ci.process.api.process.pojo.BuildVariables;
|
||||
import cd.casic.devops.common.worker.env.BuildEnv;
|
||||
import cd.casic.devops.common.worker.env.BuildType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings({"NestedMethodCall", "MethodCount"})
|
||||
public abstract class ITask {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ITask.class);
|
||||
|
||||
private final Map<String, String> environment = new HashMap<>();
|
||||
|
||||
private final Map<String, Object> monitorData = new HashMap<>();
|
||||
|
||||
private String platformCode;
|
||||
|
||||
private Integer platformErrorCode;
|
||||
|
||||
private Boolean finishKillFlag;
|
||||
|
||||
/* 存储常量的key */
|
||||
private List<String> constVar;
|
||||
|
||||
public void run(BuildTask buildTask, BuildVariables buildVariables, File workspace) {
|
||||
// 过滤只读变量并收集键名到列表
|
||||
constVar = buildVariables.getVariablesWithType().stream()
|
||||
.filter(variable -> variable.isReadOnly())
|
||||
.map(variable -> variable.getKey())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
execute(buildTask, buildVariables, workspace);
|
||||
}
|
||||
|
||||
private BuildVariables combineVariables(BuildTask buildTask, BuildVariables buildVariables) {
|
||||
|
||||
Map<String, String> buildVariable = buildTask.getBuildVariable();
|
||||
if (buildVariable == null) {
|
||||
return buildVariables;
|
||||
}
|
||||
|
||||
Map<String, String> newVariables = buildVariables.getVariables();
|
||||
newVariables.putAll(buildVariable);
|
||||
|
||||
List<BuildParameters> buildParameters = buildVariable.entrySet().stream().map(entry -> new BuildParameters(entry.getKey(), entry.getValue())
|
||||
).collect(Collectors.toList());
|
||||
|
||||
Map<String, BuildParameters> newBuildParameters =
|
||||
buildVariables.getVariablesWithType().stream()
|
||||
.collect(Collectors.toMap(
|
||||
BuildParameters::getKey,
|
||||
param -> param,
|
||||
(a, b) -> b // 合并函数(新值优先)
|
||||
));
|
||||
buildParameters.forEach(param -> newBuildParameters.put(param.getKey(), param));
|
||||
|
||||
BuildVariables restBuildVariables = new BuildVariables();
|
||||
restBuildVariables.setVariables(newVariables);
|
||||
restBuildVariables.setVariablesWithType(newBuildParameters.values().stream().collect(Collectors.toList()));
|
||||
return restBuildVariables;
|
||||
}
|
||||
|
||||
abstract void execute(BuildTask buildTask, BuildVariables buildVariables, File workspace);
|
||||
|
||||
protected void addEnv(Map<String, String> env) {
|
||||
if (!constVar.isEmpty()) {
|
||||
boolean errFlag = false;
|
||||
for (Map.Entry<String, String> entry : env.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (constVar.contains(key)) {
|
||||
LoggerService.addErrorLine("Variable " + key + " is read-only and cannot be modified.");
|
||||
errFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (errFlag) {
|
||||
throw new TaskExecuteException(
|
||||
"[Finish task] status: false, errorType: " + ErrorType.USER.getNum() +
|
||||
", errorCode: " + ErrorCode.USER_INPUT_INVAILD +
|
||||
", message: read-only cannot be modified.",
|
||||
ErrorType.USER,
|
||||
ErrorCode.USER_INPUT_INVAILD
|
||||
);
|
||||
}
|
||||
environment.putAll(env);
|
||||
}
|
||||
}
|
||||
|
||||
protected void addEnv(String key, String value) {
|
||||
environment.put(key, value);
|
||||
}
|
||||
|
||||
protected String getEnv(String key) {
|
||||
return environment.getOrDefault(key, "");
|
||||
}
|
||||
|
||||
public Map<String, String> getAllEnv() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
protected void addMonitorData(Map<String, Object> monitorDataMap) {
|
||||
monitorData.putAll(monitorDataMap);
|
||||
}
|
||||
|
||||
public Map<String, Object> getMonitorData() {
|
||||
return new HashMap<>(monitorData);
|
||||
}
|
||||
|
||||
protected void addPlatformCode(String taskPlatformCode) {
|
||||
this.platformCode = taskPlatformCode;
|
||||
}
|
||||
|
||||
public String getPlatformCode() {
|
||||
return platformCode;
|
||||
}
|
||||
|
||||
protected void addPlatformErrorCode(int taskPlatformErrorCode) {
|
||||
this.platformErrorCode = taskPlatformErrorCode;
|
||||
}
|
||||
|
||||
public Integer getPlatformErrorCode() {
|
||||
return platformErrorCode;
|
||||
}
|
||||
|
||||
protected void addFinishKillFlag(boolean taskFinishKillFlag) {
|
||||
this.finishKillFlag = taskFinishKillFlag;
|
||||
}
|
||||
|
||||
public Boolean getFinishKillFlag() {
|
||||
return finishKillFlag;
|
||||
}
|
||||
|
||||
protected boolean isThirdAgent() {
|
||||
return BuildEnv.getBuildType() == BuildType.AGENT;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user