/
WaitOnJobCompletion.groovy
104 lines (84 loc) · 3.45 KB
/
WaitOnJobCompletion.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2016 Netflix, Inc.
*
* 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.clouddriver.tasks.job
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.frigga.Names
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.clouddriver.KatoRestService
import com.netflix.spinnaker.orca.clouddriver.OortService
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask
import com.netflix.spinnaker.orca.pipeline.model.Stage
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.concurrent.TimeUnit
@Component
public class WaitOnJobCompletion extends AbstractCloudProviderAwareTask implements RetryableTask {
long backoffPeriod = TimeUnit.SECONDS.toMillis(10)
long timeout = TimeUnit.DAYS.toMillis(1)
@Autowired
KatoRestService katoRestService
@Autowired
ObjectMapper objectMapper
static final String REFRESH_TYPE = "Job"
@Override
TaskResult execute(Stage stage) {
String account = getCredentials(stage)
Map<String, List<String>> jobs = stage.context."deploy.jobs"
def status = ExecutionStatus.RUNNING;
if (!jobs) {
throw new IllegalStateException("No jobs in stage context.")
}
Map<String, Object> outputs = [:]
if (jobs.size() > 1) {
throw new IllegalStateException("At most one job location can be specified at a time.")
}
jobs.each { location, names ->
if (!names) {
return
}
if (names.size() > 1) {
throw new IllegalStateException("At most one job can be run and monitored at a time.")
}
def name = names[0]
def parsedName = Names.parseName(name)
Map job = objectMapper.readValue(katoRestService.collectJob(parsedName.app, account, location, name, "delete").body.in(), new TypeReference<Map>() {})
outputs.jobStatus = job
switch ((String) job.jobState) {
case "Succeeded":
status = ExecutionStatus.SUCCEEDED
outputs.completionDetails = job.completionDetails
if (stage.context.propertyFile) {
Map<String, Object> properties = [:]
properties = katoRestService.getFileContents(parsedName.app, account, location, name, stage.context.propertyFile)
if (properties.size() == 0) {
throw new IllegalStateException("expected properties file ${stage.context.propertyFile} but one was not found or was empty")
}
outputs << properties
}
return
case "Failed":
status = ExecutionStatus.TERMINAL
outputs.completionDetails = job.completionDetails
return
}
}
new DefaultTaskResult(status, outputs, outputs)
}
}