in magenta-lib/src/main/scala/magenta/tasks/AWS.scala [312:401]
def withAsgClient[T](
keyRing: KeyRing,
region: Region,
resources: DeploymentResources
)(block: AutoScalingClient => T): T =
withResource(
AutoScalingClient
.builder()
.credentialsProvider(AWS.provider(keyRing, resources))
.overrideConfiguration(AWS.clientConfiguration)
.region(region.awsRegion)
.build()
)(block)
def desiredCapacity(name: String, capacity: Int, client: AutoScalingClient) =
client.setDesiredCapacity(
SetDesiredCapacityRequest
.builder()
.autoScalingGroupName(name)
.desiredCapacity(capacity)
.build()
)
def maxCapacity(name: String, capacity: Int, client: AutoScalingClient) =
client.updateAutoScalingGroup(
UpdateAutoScalingGroupRequest
.builder()
.autoScalingGroupName(name)
.maxSize(capacity)
.build()
)
/** Check status of all ELBs, or all instance states if there are no ELBs
*/
def isStabilized(
asg: AutoScalingGroup,
elbClient: ELB.Client
): Either[String, Unit] = {
def matchCapacityAndState(
states: List[String],
desiredState: String,
checkDescription: Option[String]
): Either[String, Unit] = {
val descriptionWithPrecedingSpace =
checkDescription.map(d => s" $d").getOrElse("")
if (states.size != asg.desiredCapacity)
Left(
s"Number of$descriptionWithPrecedingSpace instances (${states.size}) and ASG desired capacity (${asg.desiredCapacity}) don't match"
)
else if (!states.forall(_ == desiredState))
Left(
s"Only ${states.count(_ == desiredState)} of ${states.size}$descriptionWithPrecedingSpace instances $desiredState"
)
else
Right(())
}
def checkClassicELB(name: String): Either[String, Unit] = {
val elbHealth = ELB.instanceHealth(name, elbClient.classic)
matchCapacityAndState(elbHealth, "InService", Some("Classic ELB"))
}
def checkTargetGroup(arn: String): Either[String, Unit] = {
val elbHealth = ELB.targetInstancesHealth(arn, elbClient.application)
matchCapacityAndState(
elbHealth,
TargetHealthStateEnum.HEALTHY.toString,
Some("V2 ELB")
)
}
val classicElbNames = elbNames(asg)
val targetGroupArns = elbTargetArns(asg)
if (classicElbNames.nonEmpty || targetGroupArns.nonEmpty) {
for {
_ <- classicElbNames.map(checkClassicELB).sequence
_ <- targetGroupArns.map(checkTargetGroup).sequence
} yield ()
} else {
val instanceStates =
asg.instances.asScala.toList.map(_.lifecycleStateAsString)
matchCapacityAndState(
instanceStates,
LifecycleState.IN_SERVICE.toString,
None
)
}
}