@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { MonitorKayentaCanaryContext context = stage.mapTo(MonitorKayentaCanaryContext.class); String resolvedStorageAccountName = CredentialsHelper.resolveAccountByNameOrType(context.getStorageAccountName(), AccountCredentials.Type.OBJECT_STORE,
/** * Maps the stage's context to a typed object */ public <O> O mapTo(Class<O> type) { return mapTo(null, type); }
static public Moniker monikerFromStage(Stage stage) { if (stage.getContext().containsKey("moniker")) { return (Moniker) stage.mapTo("/moniker", Moniker.class); } else { return null; } }
@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { EvaluateVariablesStage.EvaluateVariablesStageContext context = stage.mapTo(EvaluateVariablesStage.EvaluateVariablesStageContext.class); Map<String, String> outputs = new HashMap<>(); for (EvaluateVariablesStage.Variable v : context.getVariables()) { outputs.put(v.getKey(), v.getValue()); } return new TaskResult(ExecutionStatus.SUCCEEDED, stage.mapTo(Map.class), outputs); } }
@Override public Collection<ImageDetails> byTags(Stage stage, String packageName, Map<String, String> freeformTags) { StageData stageData = (StageData) stage.mapTo(StageData.class); List<OracleImage> allMatchedImages = oortService .findImage(getCloudProvider(), packageName, null, null, prefixTags(freeformTags)) .stream() .map(imageAsMap -> objectMapper.convertValue(imageAsMap, OracleImage.class)) .sorted() .collect(Collectors.toList()); // For each region, find the most recent matching image for that region. Note: sort order of // images is not defined by the OCI SDK, so we sorted them above return stageData.regions.stream().map(stageDataRegion -> allMatchedImages.stream() .filter(image -> stageDataRegion.equalsIgnoreCase(image.getRegion())).findFirst().orElse(null)) .filter(Objects::nonNull) .collect(Collectors.toList()); }
@Override public TaskResult execute(Stage stage) { String cloudProvider = getCloudProvider(stage); ImageTagger tagger = imageTaggers.stream() .filter(it -> it.getCloudProvider().equalsIgnoreCase(cloudProvider)) .findFirst() .orElseThrow(() -> new IllegalStateException("ImageTagger not found for cloudProvider " + cloudProvider)); StageData stageData = stage.mapTo(StageData.class); return new TaskResult( tagger.areImagesTagged(stageData.targets, stageData.consideredStages, stage) ? ExecutionStatus.SUCCEEDED : ExecutionStatus.RUNNING ); }
@Override public Collection<ImageDetails> byTags(Stage stage, String packageName, Map<String, String> tags) { StageData stageData = (StageData) stage.mapTo(StageData.class); List<Map> result = oortService.findImage(getCloudProvider(), (String) stage.getContext().get("imageLabelOrSha"), null, null, null); List<EcsImage> allMatchedImages = result .stream() .map(image -> objectMapper.convertValue(image, EcsImage.class)) .sorted() .collect(Collectors.toList()); HashSet<ImageDetails> response = Sets.newHashSet(allMatchedImages.get(0).toImageDetails()); return response; }
@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { JiraService.CreateIssueRequest createIssueRequest = stage.mapTo(JiraService.CreateIssueRequest.class); Optional.ofNullable(stage.getExecution().getAuthentication()) .map(Execution.AuthenticationDetails::getUser) .ifPresent(createIssueRequest::setReporter); CreateJiraIssueResponse createJiraIssueResponse = jiraService.createJiraIssue(createIssueRequest); return new TaskResult( ExecutionStatus.SUCCEEDED, ImmutableMap.of("createJiraIssueResponse", createJiraIssueResponse) ); } }
@Override public @Nonnull TaskResult execute(@Nonnull Stage stage) { StageData stageData = stage.mapTo("/context", StageData.class); Map<String, Object> result = contextParameterProcessor.process(singletonMap( "expression", "${" + stageData.expression + '}' ), contextParameterProcessor.buildExecutionContext(stage, true), true); String expression = result.get("expression").toString(); Matcher matcher = Pattern.compile("\\$\\{(.*)}").matcher(expression); if (matcher.matches()) { expression = matcher.group(1); } ensureEvaluationSummaryIncluded(result, stage, expression); ExecutionStatus status = Boolean.valueOf(expression) ? SUCCEEDED : TERMINAL; Map<String, Object> context = (Map<String, Object>) stage.getContext().get("context"); context.put("expressionResult", expression); return new TaskResult(status, singletonMap("context", context)); }
@Override public TaskResult execute(Stage stage) { String cloudProvider = getCloudProvider(stage); ImageFinder imageFinder = imageFinders.stream() .filter(it -> it.getCloudProvider().equals(cloudProvider)) .findFirst() .orElseThrow(() -> new IllegalStateException("ImageFinder not found for cloudProvider " + cloudProvider)); StageData stageData = (StageData) stage.mapTo(StageData.class); Collection<ImageFinder.ImageDetails> imageDetails = imageFinder.byTags(stage, stageData.packageName, stageData.tags); if (imageDetails == null || imageDetails.isEmpty()) { throw new IllegalStateException("Could not find tagged image for package: " + stageData.packageName + " and tags: " + stageData.tags); } List<Artifact> artifacts = new ArrayList<>(); imageDetails.forEach(imageDetail -> artifacts.add(generateArtifactFrom(imageDetail, cloudProvider))); Map<String, Object> stageOutputs = new HashMap<>(); stageOutputs.put("amiDetails", imageDetails); stageOutputs.put("artifacts", artifacts); return new TaskResult( ExecutionStatus.SUCCEEDED, stageOutputs, Collections.singletonMap("deploymentDetails", imageDetails) ); }
@Override public long getDynamicBackoffPeriod(Stage stage, Duration taskDuration) { WaitUntilStage.WaitUntilStageContext context = stage.mapTo(WaitUntilStage.WaitUntilStageContext.class); // Return a backoff time that reflects the requested target time. if (context.getStartTime() != null && context.getEpochMillis() != null) { Instant now = clock.instant(); Instant completion = Instant.ofEpochMilli(context.getEpochMillis()); if (completion.isAfter(now)) { return completion.toEpochMilli() - now.toEpochMilli(); } } return getBackoffPeriod(); }
@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { StageData stageData = stage.mapTo(StageData.class); String account = getCredentials(stage); Map<String, Object> outputs = new HashMap<>(); Manifest manifest = retrySupport.retry(() -> oortService.getManifest(account, stageData.location, stageData.manifestName ), 5, 1000, true); if (manifest != null) { outputs.put("manifest", manifest.getManifest()); outputs.put("artifacts", manifest.getArtifacts()); } else { throw new IllegalArgumentException("Manifest " + stageData.manifestName + " not found in " + stageData.location); } return new TaskResult(ExecutionStatus.SUCCEEDED, outputs, outputs); }
@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { StageData data = stage.mapTo(StageData.class); String autoscalingMode = (String) getTargetGroupForLocation(data, data.getRegion()) .getAutoscalingPolicy() .get("mode"); return AutoscalingMode.valueOf(autoscalingMode) == data.getMode() ? new TaskResult(ExecutionStatus.SUCCEEDED) : new TaskResult(ExecutionStatus.RUNNING); }
@Nonnull @Override public TaskResult execute(@Nonnull Stage stage) { final LockContext lock = stage.mapTo("/lock", LockContext.LockContextBuilder.class).withStage(stage).build(); lockManager.releaseLock(lock.getLockName(), lock.getLockValue(), lock.getLockHolder()); return TaskResult.SUCCEEDED; } }
@Override public void taskGraph( @Nonnull Stage stage, @Nonnull TaskNode.Builder builder) { builder.withTask("suspendExecutionDuringTimeWindow", SuspendExecutionDuringTimeWindowTask.class); try { JitterConfig jitter = stage.mapTo("/restrictedExecutionWindow/jitter", JitterConfig.class); if (jitter.enabled && jitter.maxDelay > 0) { if (jitter.skipManual && stage.getExecution().getTrigger().getType().equals("manual")) { return; } long waitTime = ThreadLocalRandom.current().nextLong(jitter.minDelay, jitter.maxDelay + 1); stage.setContext(contextWithWait(stage.getContext(), waitTime)); builder.withTask("waitForJitter", WaitTask.class); } } catch (IllegalArgumentException e) { // Do nothing } }
@Override public long getDynamicBackoffPeriod(Stage stage, Duration taskDuration) { WaitStage.WaitStageContext context = stage.mapTo(WaitStage.WaitStageContext.class); if (context.isSkipRemainingWait()) { return 0L; } // Return a backoff time that reflects the requested waitTime if (context.getStartTime() != null && context.getWaitDuration() != null) { Instant now = clock.instant(); Instant completion = context.getStartTime().plus(context.getWaitDuration()); if (completion.isAfter(now)) { return completion.toEpochMilli() - now.toEpochMilli(); } } return getBackoffPeriod(); }
@Override public @Nonnull TaskResult execute(@Nonnull Stage stage) { WaitUntilStage.WaitUntilStageContext context = stage.mapTo(WaitUntilStage.WaitUntilStageContext.class); if (context.getEpochMillis() == null) { return new TaskResult(SUCCEEDED); } Instant now = clock.instant(); if (context.getStartTime() == null || context.getStartTime() == Instant.EPOCH) { return new TaskResult(RUNNING, singletonMap("startTime", now)); } else if (context.getEpochMillis() <= now.toEpochMilli()) { return new TaskResult(SUCCEEDED); } else { return new TaskResult(RUNNING); } }
@Override public @Nonnull TaskResult execute(@Nonnull Stage stage) { WaitStage.WaitStageContext context = stage.mapTo(WaitStage.WaitStageContext.class); if (context.getWaitTime() == null) { return new TaskResult(SUCCEEDED); } Instant now = clock.instant(); if (context.isSkipRemainingWait()) { return new TaskResult(SUCCEEDED); } else if (context.getStartTime() == null || context.getStartTime() == Instant.EPOCH) { return new TaskResult(RUNNING, singletonMap("startTime", now)); } else if (context.getStartTime().plus(context.getWaitDuration()).isBefore(now)) { return new TaskResult(SUCCEEDED); } else { return new TaskResult(RUNNING); } }
@Override public final void beforeStages(@Nonnull Stage parent, @Nonnull StageGraphBuilder graph) { if (lockingConfigurationProperties.isEnabled()) { List<Location> locations = locationsFromStage(parent.getContext()); ClusterSelection clusterSelection = parent.mapTo(ClusterSelection.class); for (Location location : locations) { String lockName = ClusterLockHelper.clusterLockName( clusterSelection.getMoniker(), clusterSelection.getCredentials(), location); if (trafficGuard.hasDisableLock(clusterSelection.getMoniker(), clusterSelection.getCredentials(), location)) { graph.add(stage -> { stage.setType(AcquireLockStage.PIPELINE_TYPE); stage.getContext().put("lock", Collections.singletonMap("lockName", lockName)); }); } } } addAdditionalBeforeStages(parent, graph); }
@Override public final void afterStages(@Nonnull Stage parent, @Nonnull StageGraphBuilder graph) { addAdditionalAfterStages(parent, graph); if (lockingConfigurationProperties.isEnabled()) { List<Location> locations = locationsFromStage(parent.getContext()); ClusterSelection clusterSelection = parent.mapTo(ClusterSelection.class); for (Location location : locations) { String lockName = ClusterLockHelper.clusterLockName( clusterSelection.getMoniker(), clusterSelection.getCredentials(), location); if (trafficGuard.hasDisableLock(clusterSelection.getMoniker(), clusterSelection.getCredentials(), location)) { graph.append(stage -> { stage.setType(ReleaseLockStage.PIPELINE_TYPE); stage.getContext().put("lock", Collections.singletonMap("lockName", lockName)); }); } } } }