harbor run -t openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0060harbor download openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0060solution file
package io.jenkins.plugins.alibabacloud.pkg.deployment;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.model.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.oos.model.v20190601.ListExecutionsRequest;
import com.aliyuncs.oos.model.v20190601.StartExecutionRequest;
import com.aliyuncs.oos.model.v20190601.StartExecutionResponse;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.*;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.alibabacloud.pkg.deployment.utils.AliyunClientFactory;
import io.jenkins.plugins.alibabacloud.pkg.deployment.utils.ResourceType;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.interceptor.RequirePOST;
import java.io.*;
import java.util.*;
/**
* This class is a jenkins execute step:
* apply to freestyle job post bulid or pipeline step
*/
public class AliyunEcsOpsByOssFilePublisher extends Publisher implements SimpleBuildStep {
/**
* ESS_TEMPLATE_NAME and ECS_TEMPLATE_NAME are OOS template name.
*/
private final static String ESS_TEMPLATE_NAME = "ACS-ESS-RollingUpdateByDownloadOSSFileAndRunCommand";
private final static String ECS_TEMPLATE_NAME = "ACS-ECS-BulkyDownloadOSSFileAndRunCommand";
//ess or ecs resource location
private final String region;
//resource type: ESS or ECS
private final String resourceType;
/**
* resource id:
* ESS:scalingGroupId
* ECS: instanceIds by comma separated
*/
private final String resourceId;
// oss bucket name.
private final String bucket;
// bulit project upload OSS bucket specific path.
private final String objectName;
//package project path:must be directory.
private final String localPath;
//OOS template download OSS bucket File to ECS specific file path
private final String destinationDir;
//OOS template number of execution batches.
private final int batchNumber;
//OOS template execute script on ECS after download OSS bucket File to ECS specific file path.
private final String invokeScript;
// bind data
@DataBoundConstructor
public AliyunEcsOpsByOssFilePublisher(String region, String resourceType, String resourceId, String bucket, String objectName, String localPath, String destinationDir, int batchNumber, String invokeScript) {
this.region = region;
this.resourceType = resourceType;
this.resourceId = resourceId;
this.bucket = bucket;
this.objectName = objectName;
this.localPath = localPath;
this.destinationDir = destinationDir;
this.batchNumber = batchNumber;
this.invokeScript = invokeScript;
}
// getXXX functions are obtain data.
public int getBatchNumber() {
return batchNumber;
}
public String getRegion() {
return region;
}
public String getResourceType() {
return resourceType;
}
public String getResourceId() {
return resourceId;
}
public String getBucket() {
return bucket;
}
public String getObjectName() {
return objectName;
}
public String getLocalPath() {
return localPath;
}
public String getDestinationDir() {
return destinationDir;
}
public String getInvokeScript() {
return invokeScript;
}
/**
* perform is core function: it can be automatically executed based on the project built.
*/
@Override
public void perform(@NonNull Run<?, ?> build, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException {
/**
* get and check localPath
* */
final Map<String, String> envVars = build.getEnvironment(listener);
String localPathFromEnv = getLocalPathFromEnv(envVars);
String localPath = StringUtils.trimToEmpty(localPathFromEnv);
if (!localPath.isEmpty() && !localPath.startsWith("/")) {
localPath = "/" + localPath;
}
FilePath sourcePath = workspace.withSuffix(localPath).absolutize();
if (!sourcePath.isDirectory() || !isSubDirectory(workspace, sourcePath)) {
throw new IllegalArgumentException("Provided path (resolved as '" + sourcePath
+ "') is not a subdirectory of the workspace (resolved as '" + workspace + "')");
}
final FilePath sourceDirectory = sourcePath;
//acquired build name
final String projectName = build.getDisplayName();
//log util
final PrintStream logger = listener.getLogger();
listener.getLogger().println("resourceType:" + resourceType);
AliyunClientFactory instance = new AliyunClientFactory();
instance.build(this.region);
// Compress and upload the specific path of the built project.
zipAndUpload(instance, projectName, sourceDirectory, logger);
//batch automatic execution oos template according to resource type.
String executionId = null;
StartExecutionRequest request = new StartExecutionRequest();
//query execute status until execution end.
ListExecutionsRequest executionsRequest = new ListExecutionsRequest();
if ("ESS".equals(resourceType)) {
// ess resource execute template.
executionId = essResourceExec(request, instance, listener);
executionsRequest.setExecutionId(executionId);
executionsRequest.setTemplateName(ESS_TEMPLATE_NAME);
} else {
// ecs resource execute template.
executionId = ecsResourceExec(request, instance, listener);
executionsRequest.setExecutionId(executionId);
executionsRequest.setTemplateName(ECS_TEMPLATE_NAME);
}
String status = null;
while (!"Success".equalsIgnoreCase(status) && !"Failed".equalsIgnoreCase(status) && !"Cancelled".equalsIgnoreCase(status)) {
try {
Thread.sleep(500);
status = instance.getOosClient().getAcsResponse(executionsRequest).getExecutions().get(0).getStatus();
listener.getLogger().println("ExecutionId Status:" + status);
} catch (ClientException e) {
e.printStackTrace();
}
}
}
// ess resource execute oos Template : ESS_TEMPLATE_NAME
private String essResourceExec(StartExecutionRequest request, AliyunClientFactory instance, TaskListener listener) {
request.setTemplateName(ESS_TEMPLATE_NAME);
String parameter =
"{\"invokeDestinationDir\":\"" +
"" + destinationDir + "" +
"\"," +
"\"scalingGroupId\":\"" +
"" + resourceId + "" +
"\"," +
"\"invokeScript\":\"" +
invokeScript +
"\"," +
"\"OSSRegion\":\"" +
"" + region + "" +
"\"," +
"\"whetherSaveToFile\":true," +
"\"URLExpirationTime\":6000," +
"\"batchNumber\":" +
"" +
this.batchNumber +
"" +
"," +
"\"rollbackBucketName\":\"" +
"" + bucket + "" +
"\"," +
"\"rollbackObjectName\":\"" +
"" + objectName + "" +
"\"," +
"\"rollbackDestinationDir\":\"" +
"" + destinationDir + "" +
"\"," +
"\"invokeBucketName\":\"" +
"" + bucket + "" +
"\"," +
"\"invokeObjectName\":\"" +
"" + objectName + "" +
"\"," +
"\"invokeType\":\"" +
"" + "invoke" + "" +
"\"}";
request.setParameters(parameter);
StartExecutionResponse response = null;
String executionId = null;
try {
response =
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py