forked from spinnaker/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhoook stage): Support for webhoook stage
First take at solving issue spinnaker/spinnaker#1512. Corresponds with Deck PR spinnaker/deck#3447
- Loading branch information
Showing
13 changed files
with
853 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2017 Schibsted ASA. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
dependencies { | ||
compile project(':orca-core') | ||
compile spinnaker.dependency('kork') | ||
compile spinnaker.dependency('bootAutoConfigure') | ||
compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.2.+' | ||
testCompile project(':orca-test') | ||
} |
43 changes: 43 additions & 0 deletions
43
orca-webhook/src/main/groovy/com/netflix/spinnaker/orca/webhook/WebhookService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2017 Schibsted ASA. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.webhook | ||
|
||
import groovy.transform.CompileStatic | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@CompileStatic | ||
@Service | ||
class WebhookService { | ||
|
||
@Autowired | ||
RestTemplate restTemplate | ||
|
||
ResponseEntity<Object> exchange(HttpMethod httpMethod, String url, Object payload) { | ||
HttpEntity<Object> payloadEntity = new HttpEntity<>(payload) | ||
return restTemplate.exchange(url, httpMethod, payloadEntity, Object) | ||
} | ||
|
||
ResponseEntity<Object> getStatus(String url) { | ||
return restTemplate.getForEntity(url, Object) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ook/src/main/groovy/com/netflix/spinnaker/orca/webhook/config/WebhookConfiguration.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2017 Schibsted ASA. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.webhook.config | ||
|
||
import com.netflix.spinnaker.orca.webhook.WebhookService | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.ComponentScan | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(prefix = "webhook.stage", value = "enabled", matchIfMissing = true) | ||
@ComponentScan(basePackageClasses = WebhookService) | ||
class WebhookConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(RestTemplate) | ||
RestTemplate restTemplate() { | ||
new RestTemplate() | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
orca-webhook/src/main/groovy/com/netflix/spinnaker/orca/webhook/pipeline/WebhookStage.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.netflix.spinnaker.orca.webhook.pipeline | ||
|
||
import com.netflix.spinnaker.orca.CancellableStage | ||
import com.netflix.spinnaker.orca.batch.RestartableStage | ||
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder | ||
import com.netflix.spinnaker.orca.pipeline.TaskNode | ||
import com.netflix.spinnaker.orca.pipeline.model.Execution | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage | ||
|
||
import com.netflix.spinnaker.orca.webhook.tasks.CreateWebhookTask | ||
import com.netflix.spinnaker.orca.webhook.tasks.MonitorWebhookTask | ||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import org.springframework.stereotype.Component | ||
|
||
@Slf4j | ||
@Component | ||
@CompileStatic | ||
class WebhookStage implements StageDefinitionBuilder, RestartableStage, CancellableStage { | ||
|
||
@Override | ||
<T extends Execution<T>> void taskGraph(Stage<T> stage, TaskNode.Builder builder) { | ||
String waitForCompletion = stage.context.waitForCompletion | ||
|
||
builder.withTask("createWebhook", CreateWebhookTask) | ||
if (waitForCompletion?.toBoolean()) { | ||
builder | ||
.withTask("monitorWebhook", MonitorWebhookTask) | ||
} | ||
} | ||
|
||
@Override | ||
CancellableStage.Result cancel(Stage stage) { | ||
log.info("Cancelling stage (stageId: ${stage.id}, executionId: ${stage.execution.id}, context: ${stage.context as Map})") | ||
return new CancellableStage.Result(stage, [:]) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...webhook/src/main/groovy/com/netflix/spinnaker/orca/webhook/tasks/CreateWebhookTask.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.netflix.spinnaker.orca.webhook.tasks | ||
|
||
import com.jayway.jsonpath.JsonPath | ||
import com.jayway.jsonpath.PathNotFoundException | ||
import com.netflix.spinnaker.orca.DefaultTaskResult | ||
import com.netflix.spinnaker.orca.ExecutionStatus | ||
import com.netflix.spinnaker.orca.RetryableTask | ||
import com.netflix.spinnaker.orca.TaskResult | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage | ||
import com.netflix.spinnaker.orca.webhook.WebhookService | ||
import groovy.transform.CompileStatic | ||
import org.apache.http.HttpHeaders | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@CompileStatic | ||
class CreateWebhookTask implements RetryableTask { | ||
|
||
long backoffPeriod = 30000 | ||
long timeout = 300000 | ||
|
||
@Autowired | ||
WebhookService webhookService; | ||
|
||
@Override | ||
TaskResult execute(Stage stage) { | ||
def url = stage.context.url as String | ||
def method = stage.context.method ? HttpMethod.valueOf(stage.context.method.toString().toUpperCase()) : HttpMethod.POST | ||
def payload = stage.context.payload | ||
boolean waitForCompletion = (stage.context.waitForCompletion as String)?.toBoolean() | ||
|
||
def response = webhookService.exchange(method, url, payload) | ||
|
||
def outputs = [:] | ||
outputs << [statusCode: response.statusCode] | ||
if (response.body) { | ||
outputs << [buildInfo: response.body] | ||
} | ||
if (response.statusCode.is2xxSuccessful()) { | ||
if (waitForCompletion) { | ||
def statusUrl = null | ||
def statusUrlResolution = stage.context.statusUrlResolution | ||
switch (statusUrlResolution) { | ||
case "getMethod": | ||
statusUrl = url | ||
break | ||
case "locationHeader": | ||
statusUrl = response.headers.getFirst(HttpHeaders.LOCATION) | ||
break | ||
case "webhookResponse": | ||
try { | ||
statusUrl = JsonPath.read(response.body, stage.context.statusUrlJsonPath as String) | ||
} catch (PathNotFoundException e) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
[error: [reason: e.message, response: response.body]]) | ||
} | ||
} | ||
if (!statusUrl || !(statusUrl instanceof String)) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
outputs + [ | ||
error: "The status URL couldn't be resolved, but 'Wait for completion' was checked", | ||
statusUrlValue: statusUrl | ||
]) | ||
} | ||
stage.context.statusEndpoint = statusUrl | ||
return new DefaultTaskResult(ExecutionStatus.SUCCEEDED, outputs + [statusEndpoint: statusUrl]) | ||
} | ||
return new DefaultTaskResult(ExecutionStatus.SUCCEEDED, outputs) | ||
} else { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, outputs + [error: "The request did not return a 2xx status"]) | ||
} | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...ebhook/src/main/groovy/com/netflix/spinnaker/orca/webhook/tasks/MonitorWebhookTask.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.netflix.spinnaker.orca.webhook.tasks | ||
|
||
import com.jayway.jsonpath.JsonPath | ||
import com.jayway.jsonpath.PathNotFoundException | ||
import com.netflix.spinnaker.orca.DefaultTaskResult | ||
import com.netflix.spinnaker.orca.ExecutionStatus | ||
import com.netflix.spinnaker.orca.Task | ||
import com.netflix.spinnaker.orca.TaskResult | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage | ||
import com.netflix.spinnaker.orca.webhook.WebhookService | ||
import groovy.transform.CompileStatic | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@CompileStatic | ||
class MonitorWebhookTask implements Task { | ||
|
||
@Autowired | ||
WebhookService webhookService | ||
|
||
static requiredParameters = ["statusEndpoint", "statusJsonPath", "successStatuses"] | ||
|
||
@Override | ||
TaskResult execute(Stage stage) { | ||
def missing = requiredParameters.find { !stage.context.get(it) } | ||
if (missing) { | ||
throw new IllegalStateException("Missing required parameter '${missing}'") | ||
} | ||
|
||
String statusEndpoint = stage.context.statusEndpoint | ||
String statusJsonPath = stage.context.statusJsonPath | ||
String progressJsonPath = stage.context.progressJsonPath | ||
String successStatuses = stage.context.successStatuses | ||
String canceledStatuses = stage.context.canceledStatuses | ||
String terminalStatuses = stage.context.terminalStatuses | ||
|
||
def response = webhookService.getStatus(statusEndpoint) | ||
def result | ||
try { | ||
result = JsonPath.read(response.body, statusJsonPath) | ||
} catch (PathNotFoundException e) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
[error: [reason: e.message, response: response.body]]) | ||
} | ||
if (!(result instanceof String || result instanceof Number || result instanceof Boolean)) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
[error: [reason: "The json path '${statusJsonPath}' did not resolve to a single value", value: result]]) | ||
} | ||
|
||
def responsePayload = [buildInfo: response.body] | ||
|
||
if (progressJsonPath) { | ||
def progress | ||
try { | ||
progress = JsonPath.read(response.body, progressJsonPath) | ||
} catch (PathNotFoundException e) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
[error: [reason: e.message, response: response.body]]) | ||
} | ||
if (!(progress instanceof String)) { | ||
return new DefaultTaskResult(ExecutionStatus.TERMINAL, | ||
[error: [reason: "The json path '${progressJsonPath}' did not resolve to a String value", value: progress]]) | ||
} | ||
if (progress) { | ||
responsePayload += [progressMessage: progress] | ||
} | ||
} | ||
|
||
Map<String, ExecutionStatus> statusMap = createStatusMap(successStatuses, canceledStatuses, terminalStatuses) | ||
|
||
if (result instanceof Number) { | ||
def status = result == 100 ? ExecutionStatus.SUCCEEDED : ExecutionStatus.RUNNING | ||
return new DefaultTaskResult(status, responsePayload + [percentComplete: result]) | ||
} else if (statusMap.containsKey(result.toString().toUpperCase())) { | ||
return new DefaultTaskResult(statusMap[result.toString().toUpperCase()], responsePayload) | ||
} | ||
|
||
return new DefaultTaskResult(ExecutionStatus.RUNNING, response ? responsePayload : [:]) | ||
} | ||
|
||
private | ||
static Map<String, ExecutionStatus> createStatusMap(String successStatuses, String canceledStatuses, String terminalStatuses) { | ||
Map<String, ExecutionStatus> statusMap = [:] | ||
statusMap << mapStatuses(successStatuses, ExecutionStatus.SUCCEEDED) | ||
if (canceledStatuses) { | ||
statusMap << mapStatuses(canceledStatuses, ExecutionStatus.CANCELED) | ||
} | ||
if (terminalStatuses) { | ||
statusMap << mapStatuses(terminalStatuses, ExecutionStatus.TERMINAL) | ||
} | ||
return statusMap | ||
} | ||
|
||
private static Map mapStatuses(String statuses, ExecutionStatus status) { | ||
statuses.split(",").collectEntries { [(it.trim().toUpperCase()): status] } | ||
} | ||
} |
Oops, something went wrong.