Skip to content

Commit

Permalink
Remove instances from ASG and terminate them when deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Aug 18, 2017
1 parent d77c840 commit e46d77b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion provisioner/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net"
"sort"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -146,12 +147,27 @@ func (d *windows) InstanceDelete(session *types.Session, instance *types.Instanc
if err != nil {
return err
}

_, err = asgService.DetachInstances(&autoscaling.DetachInstancesInput{
AutoScalingGroupName: aws.String("pwd-windows"),
InstanceIds: []*string{aws.String(instance.WindowsId)},
ShouldDecrementDesiredCapacity: aws.Bool(false),
})

if err != nil {
return err
}

// return error and don't do anything else
if _, err := ec2Service.TerminateInstances(&ec2.TerminateInstancesInput{InstanceIds: []*string{aws.String(instance.WindowsId)}}); err != nil {
return err
}

err = dockerClient.DeleteContainer(instance.Name)
if err != nil && !strings.Contains(err.Error(), "No such container") {
return err
}

// TODO trigger deletion in AWS
return d.releaseInstance(session.Id, instance.WindowsId)
}

Expand Down Expand Up @@ -198,6 +214,9 @@ func (d *windows) getWindowsInstanceInfo(sessionId string) (*instanceInfo, error
instances := out.AutoScalingGroups[0].Instances
availInstances := make([]string, len(instances))

// reverse order so older instances are first served
sort.Sort(sort.Reverse(sort.StringSlice(availInstances)))

for i, inst := range instances {
if *inst.LifecycleState == "InService" {
availInstances[i] = *inst.InstanceId
Expand Down

0 comments on commit e46d77b

Please sign in to comment.