<commit_before>package com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.strategies

import com.netflix.spinnaker.orca.clouddriver.pipeline.cluster.DisableClusterStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.ResizeServerGroupStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.DetermineTargetServerGroupStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup
import com.netflix.spinnaker.orca.kato.pipeline.support.SourceResolver
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.model.SyntheticStageOwner
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.stereotype.Component
import static com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder.newStage

@Slf4j
@Component
class RollingRedBlackStrategy implements Strategy, ApplicationContextAware {
  final String name = "rollingredblack"

  @Autowired
  DisableClusterStage disableClusterStage

  @Autowired
  ResizeServerGroupStage resizeServerGroupStage

  @Autowired
  DetermineTargetServerGroupStage determineTargetServerGroupStage

  @Autowired
  SourceResolver sourceResolver

  @Override
  <T extends Execution<T>> List<Stage<T>> composeFlow(Stage<T> stage) {
    def stages = []
    def stageData = stage.mapTo(RollingRedBlackStageData)
    def cleanupConfig = AbstractDeployStrategyStage.CleanupConfig.fromStage(stage)

    Map baseContext = [
        (cleanupConfig.location.singularType()): cleanupConfig.location.value,
        cluster                                : cleanupConfig.cluster,
        credentials                            : cleanupConfig.account,
        cloudProvider                          : cleanupConfig.cloudProvider,
    ]

    Map originalCapacity = stageData.originalCapacity ?: stageData.capacity
    if (!originalCapacity) {
      originalCapacity = [
          min: stageData.targetSize,
          max: stageData.targetSize,
          desired: stageData.targetSize
      ]
    }

    if (stageData.targetSize) {
      stage.context.targetSize = 0
    }

    stage.context.originalCapacity = originalCapacity
    stage.context.capacity = [
        min: 0,
        max: 0,
        desired: 0
    ]

    def targetPercentages = stageData.getTargetPercentages()
    if (targetPercentages.size() == 0 || targetPercentages[-1] != 100) {
      log.info("Inserting implicit 100% final target percentage...")
      targetPercentages.add(100)
    }

    def findContext = baseContext + [
        target: TargetServerGroup.Params.Target.current_asg_dynamic,
        targetLocation: cleanupConfig.location,
    ]

    stages << newStage(
        stage.execution,
        determineTargetServerGroupStage.type,
        "Determine Deployed Server Group",
        findContext,
        stage,
        SyntheticStageOwner.STAGE_AFTER
    )<commit_msg>fix(core): set use source capacity to false in rolling red black (#1499)<commit_after>package com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.strategies

import com.netflix.spinnaker.orca.clouddriver.pipeline.cluster.DisableClusterStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.ResizeServerGroupStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.DetermineTargetServerGroupStage
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup
import com.netflix.spinnaker.orca.kato.pipeline.support.SourceResolver
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.model.SyntheticStageOwner
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.stereotype.Component
import static com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder.newStage

@Slf4j
@Component
class RollingRedBlackStrategy implements Strategy, ApplicationContextAware {
  final String name = "rollingredblack"

  @Autowired
  DisableClusterStage disableClusterStage

  @Autowired
  ResizeServerGroupStage resizeServerGroupStage

  @Autowired
  DetermineTargetServerGroupStage determineTargetServerGroupStage

  @Autowired
  SourceResolver sourceResolver

  @Override
  <T extends Execution<T>> List<Stage<T>> composeFlow(Stage<T> stage) {
    def stages = []
    def stageData = stage.mapTo(RollingRedBlackStageData)
    def cleanupConfig = AbstractDeployStrategyStage.CleanupConfig.fromStage(stage)

    Map baseContext = [
        (cleanupConfig.location.singularType()): cleanupConfig.location.value,
        cluster                                : cleanupConfig.cluster,
        credentials                            : cleanupConfig.account,
        cloudProvider                          : cleanupConfig.cloudProvider,
    ]

    Map originalCapacity = stageData.originalCapacity ?: stageData.capacity
    if (!originalCapacity) {
      originalCapacity = [
          min: stageData.targetSize,
          max: stageData.targetSize,
          desired: stageData.targetSize
      ]
    }

    if (stageData.targetSize) {
      stage.context.targetSize = 0
    }

    stage.context.originalCapacity = originalCapacity
    stage.context.capacity = [
        min: 0,
        max: 0,
        desired: 0
    ]
    stage.context.useSourceCapacity = false

    def targetPercentages = stageData.getTargetPercentages()
    if (targetPercentages.size() == 0 || targetPercentages[-1] != 100) {
      log.info("Inserting implicit 100% final target percentage...")
      targetPercentages.add(100)
    }

    def findContext = baseContext + [
        target: TargetServerGroup.Params.Target.current_asg_dynamic,
        targetLocation: cleanupConfig.location,
    ]

    stages << newStage(
        stage.execution,
        determineTargetServerGroupStage.type,
        "Determine Deployed Server Group",
        findContext,
        stage,
        SyntheticStageOwner.STAGE_AFTER
    )
{'fraction_non_alphanumeric': 0.07897629141152457, 'fraction_numerical': 0.005966399748783168, 'mean_word_length': 3.632727272727273, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822740', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1752, 'n_words': 289}