-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terrakube Configuration Language (#79)
Terrakube Configuration Language v1
- Loading branch information
Showing
35 changed files
with
791 additions
and
207 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
2 changes: 1 addition & 1 deletion
2
...builder/api/job/ServerJobApplication.java → ...g/azbuilder/api/ServerJobApplication.java
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
106 changes: 106 additions & 0 deletions
106
api-job/src/main/java/org/azbuilder/api/schedule/ScheduleJobService.java
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,106 @@ | ||
package org.azbuilder.api.schedule; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.azbuilder.api.client.TerrakubeClient; | ||
import org.azbuilder.api.client.model.organization.job.Job; | ||
import org.azbuilder.api.client.model.organization.job.step.Step; | ||
import org.azbuilder.api.client.model.organization.job.step.StepAttributes; | ||
import org.azbuilder.api.client.model.organization.job.step.StepRequest; | ||
import org.azbuilder.api.schedule.yaml.Flow; | ||
import org.azbuilder.api.schedule.yaml.FlowConfig; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.Constructor; | ||
|
||
import java.util.*; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class ScheduleJobService { | ||
|
||
TerrakubeClient terrakubeClient; | ||
JobService jobService; | ||
|
||
@Scheduled(fixedRate = 60000) | ||
public void searchPendingJobs() { | ||
jobService.searchPendingJobs().parallelStream().forEach(job -> { | ||
|
||
job = initialJobSetup(job); | ||
|
||
Optional<Flow> flow = Optional.ofNullable(getNextFlow(job)); | ||
if (flow.isPresent()) { | ||
log.info("Execute command: {} \n {}", flow.get().getType(), flow.get().getCommands()); | ||
String stepId = getCurrentStepId(job); | ||
if(jobService.execute(job, stepId, flow.get())) | ||
log.info("Executing Job {} Step Id {}", job.getId(), stepId); | ||
} else { | ||
jobService.completeJob(job); | ||
} | ||
}); | ||
} | ||
|
||
private Job initialJobSetup(Job job) { | ||
if (job.getRelationships().getStep().getData().isEmpty()) { | ||
|
||
FlowConfig flowConfig = getFlowConfig(job.getAttributes().getTcl()); | ||
log.info("Custom Job Setup: \n {}", flowConfig.toString()); | ||
|
||
flowConfig.getFlow().parallelStream().forEach(flow -> { | ||
log.info("Creating step: {}", flow.toString()); | ||
StepRequest stepRequest = new StepRequest(); | ||
Step newStep = new Step(); | ||
newStep.setType("step"); | ||
StepAttributes stepAttributes = new StepAttributes(); | ||
stepAttributes.setStatus("pending"); | ||
stepAttributes.setStepNumber(String.valueOf(flow.getStep())); | ||
newStep.setAttributes(stepAttributes); | ||
stepRequest.setData(newStep); | ||
terrakubeClient.createStep(stepRequest, job.getRelationships().getOrganization().getData().getId(), job.getId()); | ||
}); | ||
return terrakubeClient.getJobById(job.getRelationships().getOrganization().getData().getId(), job.getId()).getData(); | ||
} else | ||
return job; | ||
} | ||
|
||
|
||
private FlowConfig getFlowConfig(String tcl) { | ||
Yaml yaml = new Yaml(new Constructor(FlowConfig.class)); | ||
FlowConfig temp = yaml.load(new String(Base64.getDecoder().decode(tcl))); | ||
log.info("FlowConfig: \n {}", temp); | ||
return yaml.load(new String(Base64.getDecoder().decode(tcl))); | ||
} | ||
|
||
private Flow getNextFlow(Job job) { | ||
TreeMap<Integer, Step> map = getPendingSteps(job); | ||
|
||
if (!map.isEmpty()) { | ||
log.info("Next Command: {}", map.firstKey()); | ||
Optional<Flow> nextFlow = getFlowConfig(job.getAttributes().getTcl()) | ||
.getFlow() | ||
.stream() | ||
.filter(flow -> flow.getStep() == map.firstKey()) | ||
.findFirst(); | ||
return nextFlow.isPresent()? nextFlow.get(): null; | ||
} | ||
else | ||
return null; | ||
} | ||
|
||
private TreeMap<Integer, Step> getPendingSteps(Job job) { | ||
final TreeMap<Integer, Step> map = new TreeMap<>(); | ||
terrakubeClient.getJobById(job.getRelationships().getOrganization().getData().getId(), job.getId()) | ||
.getIncluded() | ||
.stream() | ||
.filter(step -> step.getAttributes().getStatus().equals("pending")) | ||
.forEach(step -> map.put(Integer.valueOf(step.getAttributes().getStepNumber()), step)); | ||
return map; | ||
} | ||
|
||
public String getCurrentStepId(Job job) { | ||
return getPendingSteps(job).firstEntry().getValue().getId(); | ||
} | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
api-job/src/main/java/org/azbuilder/api/schedule/executor/ExecutorJob.java
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,28 @@ | ||
package org.azbuilder.api.schedule.executor; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.azbuilder.api.schedule.yaml.Command; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
@ToString | ||
@Getter | ||
@Setter | ||
public class ExecutorJob { | ||
private List<Command> commandList; | ||
private String type; | ||
private String organizationId; | ||
private String workspaceId; | ||
private String jobId; | ||
private String stepId; | ||
private String terraformVersion; | ||
private String source; | ||
private String branch; | ||
private String vcsType; | ||
private String accessToken; | ||
private HashMap<String, String> environmentVariables; | ||
private HashMap<String, String> variables; | ||
} |
16 changes: 16 additions & 0 deletions
16
api-job/src/main/java/org/azbuilder/api/schedule/yaml/Command.java
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,16 @@ | ||
package org.azbuilder.api.schedule.yaml; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@ToString | ||
@Getter | ||
@Setter | ||
public class Command { | ||
private String runtime; | ||
private String script; | ||
private int priority; | ||
private boolean before; | ||
private boolean after; | ||
} |
Oops, something went wrong.