Skip to content

Commit

Permalink
Changes in AutoScaling.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jul 24, 2020
1 parent ab717f2 commit e9a03c8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
34 changes: 26 additions & 8 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGrou
public abstract autoScalingGroupName: string;
public abstract autoScalingGroupArn: string;
protected albTargetGroup?: elbv2.ApplicationTargetGroup;
public readonly grantPrincipal: iam.IPrincipal = new iam.UnknownPrincipal({ resource: this });
public readonly operatingSystemType?: ec2.OperatingSystemType = undefined;

/**
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
Expand Down Expand Up @@ -490,6 +492,10 @@ abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGrou
public scaleOnMetric(id: string, props: BasicStepScalingPolicyProps): StepScalingPolicy {
return new StepScalingPolicy(this, id, { ...props, autoScalingGroup: this });
}

public addUserData(..._commands: string[]): void {
// do nothing
}
}

/**
Expand All @@ -508,8 +514,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
elb.ILoadBalancerTarget,
ec2.IConnectable,
elbv2.IApplicationLoadBalancerTarget,
elbv2.INetworkLoadBalancerTarget,
iam.IGrantable {
elbv2.INetworkLoadBalancerTarget {

public static fromAutoScalingGroupName(scope: Construct, id: string, autoScalingGroupName: string): IAutoScalingGroup {
class Import extends AutoScalingGroupBase {
Expand All @@ -529,6 +534,8 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
*/
public readonly osType: ec2.OperatingSystemType;

public readonly operatingSystemType?: ec2.OperatingSystemType;

/**
* Allows specify security group connections for instances of this fleet.
*/
Expand Down Expand Up @@ -703,6 +710,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

this.autoScalingGroup = new CfnAutoScalingGroup(this, 'ASG', asgProps);
this.osType = imageConfig.osType;
this.operatingSystemType = imageConfig.osType;
this.autoScalingGroupName = this.getResourceNameAttribute(this.autoScalingGroup.ref),
this.autoScalingGroupArn = Stack.of(this).formatArn({
service: 'autoscaling',
Expand Down Expand Up @@ -760,11 +768,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
return { targetType: elbv2.TargetType.INSTANCE };
}

/**
* Add command to the startup script of fleet instances.
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
*/
public addUserData(...commands: string[]) {
public addUserData(...commands: string[]): void {
this.userData.addCommands(...commands);
}

Expand Down Expand Up @@ -1121,7 +1125,7 @@ function validatePercentage(x?: number): number | undefined {
/**
* An AutoScalingGroup
*/
export interface IAutoScalingGroup extends IResource {
export interface IAutoScalingGroup extends IResource, iam.IGrantable {
/**
* The name of the AutoScalingGroup
* @attribute
Expand All @@ -1134,6 +1138,20 @@ export interface IAutoScalingGroup extends IResource {
*/
readonly autoScalingGroupArn: string;

/**
* The operating system family that the instances in this auto-scaling group belong to.
* Can be undefined for imported groups,
* is never undefined for new ASGs created by the CDK.
*/
readonly operatingSystemType?: ec2.OperatingSystemType;

/**
* Add command to the startup script of fleet instances.
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
* Does nothing for imported ASGs.
*/
addUserData(...commands: string[]): void;

/**
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
*/
Expand Down
66 changes: 32 additions & 34 deletions packages/@aws-cdk/aws-codedeploy/lib/server/deployment-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,40 +342,38 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupBase {
return;
}

if (asg instanceof autoscaling.AutoScalingGroup) {
this.codeDeployBucket.grantRead(asg.role, 'latest/*');

switch (asg.osType) {
case ec2.OperatingSystemType.LINUX:
asg.addUserData(
'PKG_CMD=`which yum 2>/dev/null`',
'if [ -z "$PKG_CMD" ]; then',
'PKG_CMD=apt-get',
'else',
'PKG=CMD=yum',
'fi',
'$PKG_CMD update -y',
'$PKG_CMD install -y ruby2.0',
'if [ $? -ne 0 ]; then',
'$PKG_CMD install -y ruby',
'fi',
'$PKG_CMD install -y awscli',
'TMP_DIR=`mktemp -d`',
'cd $TMP_DIR',
`aws s3 cp s3://aws-codedeploy-${cdk.Stack.of(this).region}/latest/install . --region ${cdk.Stack.of(this).region}`,
'chmod +x ./install',
'./install auto',
'rm -fr $TMP_DIR',
);
break;
case ec2.OperatingSystemType.WINDOWS:
asg.addUserData(
'Set-Variable -Name TEMPDIR -Value (New-TemporaryFile).DirectoryName',
`aws s3 cp s3://aws-codedeploy-${cdk.Stack.of(this).region}/latest/codedeploy-agent.msi $TEMPDIR\\codedeploy-agent.msi`,
'$TEMPDIR\\codedeploy-agent.msi /quiet /l c:\\temp\\host-agent-install-log.txt',
);
break;
}
this.codeDeployBucket.grantRead(asg, 'latest/*');

switch (asg.operatingSystemType) {
case ec2.OperatingSystemType.LINUX:
asg.addUserData(
'PKG_CMD=`which yum 2>/dev/null`',
'if [ -z "$PKG_CMD" ]; then',
'PKG_CMD=apt-get',
'else',
'PKG=CMD=yum',
'fi',
'$PKG_CMD update -y',
'$PKG_CMD install -y ruby2.0',
'if [ $? -ne 0 ]; then',
'$PKG_CMD install -y ruby',
'fi',
'$PKG_CMD install -y awscli',
'TMP_DIR=`mktemp -d`',
'cd $TMP_DIR',
`aws s3 cp s3://aws-codedeploy-${cdk.Stack.of(this).region}/latest/install . --region ${cdk.Stack.of(this).region}`,
'chmod +x ./install',
'./install auto',
'rm -fr $TMP_DIR',
);
break;
case ec2.OperatingSystemType.WINDOWS:
asg.addUserData(
'Set-Variable -Name TEMPDIR -Value (New-TemporaryFile).DirectoryName',
`aws s3 cp s3://aws-codedeploy-${cdk.Stack.of(this).region}/latest/codedeploy-agent.msi $TEMPDIR\\codedeploy-agent.msi`,
'$TEMPDIR\\codedeploy-agent.msi /quiet /l c:\\temp\\host-agent-install-log.txt',
);
break;
}
}

Expand Down

0 comments on commit e9a03c8

Please sign in to comment.