Example #1
0
func (clst *amazonCluster) tagSpotRequests(awsIDs []awsID) error {
OuterLoop:
	for region, ids := range groupByRegion(awsIDs) {
		session := clst.getSession(region)
		spotIDs := getSpotIDs(ids)

		var err error
		for i := 0; i < 30; i++ {
			_, err = session.CreateTags(&ec2.CreateTagsInput{
				Tags: []*ec2.Tag{
					{
						Key:   aws.String(clst.namespace),
						Value: aws.String(""),
					},
				},
				Resources: aws.StringSlice(spotIDs),
			})
			if err == nil {
				continue OuterLoop
			}
			time.Sleep(5 * time.Second)
		}

		log.Warn("Failed to tag spot requests: ", err)
		session.CancelSpotInstanceRequests(
			&ec2.CancelSpotInstanceRequestsInput{
				SpotInstanceRequestIds: aws.StringSlice(spotIDs),
			})

		return err
	}

	return nil
}
Example #2
0
func (clst amazonCluster) Stop(machines []Machine) error {
	var awsIDs []awsID
	for _, m := range machines {
		awsIDs = append(awsIDs, awsID{
			region: m.Region,
			spotID: m.ID,
		})
	}
	for region, ids := range groupByRegion(awsIDs) {
		session := clst.getSession(region)
		spotIDs := getSpotIDs(ids)

		spots, err := session.DescribeSpotInstanceRequests(
			&ec2.DescribeSpotInstanceRequestsInput{
				SpotInstanceRequestIds: aws.StringSlice(spotIDs),
			})
		if err != nil {
			return err
		}

		instIds := []string{}
		for _, spot := range spots.SpotInstanceRequests {
			if spot.InstanceId != nil {
				instIds = append(instIds, *spot.InstanceId)
			}
		}

		if len(instIds) > 0 {
			_, err = session.TerminateInstances(&ec2.TerminateInstancesInput{
				InstanceIds: aws.StringSlice(instIds),
			})
			if err != nil {
				return err
			}
		}

		_, err = session.CancelSpotInstanceRequests(
			&ec2.CancelSpotInstanceRequestsInput{
				SpotInstanceRequestIds: aws.StringSlice(spotIDs),
			})
		if err != nil {
			return err
		}

		if err := clst.wait(ids, false); err != nil {
			return err
		}
	}

	return nil
}