Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pipeline executions/gate) : Added code to save multiple pipelines at once to sql database. #1385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,53 @@ public Map createAndWaitForCompletion(Map body, int maxPolls, int intervalMs) {
return task;
}

public Map bulkCreateAndWaitForCompletion(Map body, int maxPolls, int intervalMs) {
log.info("Bulk Creating and waiting for completion: " + body);

if (body.containsKey("application")) {
AuthenticatedRequest.setApplication(body.get("application").toString());
}

Map createResult = create(body);
if (createResult.get("ref") == null) {
log.warn("No ref field found in create result, returning entire result: " + createResult);
return createResult;
}

String taskId = ((String) createResult.get("ref")).split("/")[2];
log.info("Create succeeded; polling task for completion: " + taskId);

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(1);
map.put("id", taskId);
Map task = map;
for (int i = 0; i < maxPolls; i++) {
try {
Thread.sleep(intervalMs);
} catch (InterruptedException ignored) {
}

task = getTask(taskId);
if (new ArrayList<>(Arrays.asList("SUCCEEDED", "TERMINAL"))
.contains((String) task.get("status"))) {
List<Map<String, String>> bulksaveTasks = (List<Map<String, String>>) task.get("steps");
long count = 0;
if (bulksaveTasks != null && !bulksaveTasks.isEmpty()) {
count =
bulksaveTasks.stream()
.filter(
hashmap ->
("SUCCEEDED".equals((String) hashmap.get("status"))
|| "TERMINAL".equals((String) hashmap.get("status"))))
.count();
}
if (count == 2) {
return task;
}
}
}
return task;
}

public Map createAndWaitForCompletion(Map body, int maxPolls) {
return createAndWaitForCompletion(body, maxPolls, 1000);
}
Expand All @@ -127,6 +174,10 @@ public Map createAndWaitForCompletion(Map body) {
return createAndWaitForCompletion(body, 32, 1000);
}

public Map bulkCreateAndWaitForCompletion(Map body) {
return bulkCreateAndWaitForCompletion(body, 300, 1000);
}

/** @deprecated This pipeline operation does not belong here. */
@Deprecated
public Map cancelPipeline(final String id, final String reason) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,43 @@ class PipelineController {
}
}

@CompileDynamic
@ApiOperation(value = "Save an array of pipeline definition")
@PostMapping('/bulksave')
Map bulksavePipeline(
@RequestBody List<Map> pipelineList,
@RequestParam(value = "staleCheck", required = false, defaultValue = "false")
Boolean staleCheck) {

def retData = []
def operation = [
description: "bulk save pipeline",
application: "bulk save application",
job : [
[
type : "savePipeline",
pipeline : (String) Base64.encoder.encodeToString(objectMapper.writeValueAsString(pipelineList).getBytes("UTF-8")),
user : AuthenticatedRequest.spinnakerUser.orElse("anonymous"),
staleCheck: staleCheck,
bulksave : true
]
]
]

def result = taskService.bulkCreateAndWaitForCompletion(operation)
String resultStatus = result.get("status")

if (!"SUCCEEDED".equalsIgnoreCase(resultStatus)) {
String exception = result.variables.find { it.key == "exception" }?.value?.details?.errors?.getAt(0)
throw new PipelineException(
exception ?: "Pipeline bulk save operation did not succeed: ${result.get("id", "unknown task id")} (status: ${resultStatus})"
)
} else {
retData = result.variables.find { it.key == "bulksave"}?.value
}
return retData
}

@ApiOperation(value = "Rename a pipeline definition")
@PostMapping('move')
void renamePipeline(@RequestBody Map renameCommand) {
Expand Down