func getPrivateIPs(ec2region *ec2.EC2) []string {
	filter := ec2.NewFilter()

	for _, tag := range *tags {
		parts := strings.SplitN(tag, ":", 2)
		if len(parts) != 2 {
			log.Println("expected TAG:VALUE got", tag)
			break
		}
		filter.Add(fmt.Sprintf("tag:%v", parts[0]), parts[1])
	}

	taggedInstances := []string{}

	resp, err := ec2region.DescribeInstances(nil, filter)
	if err != nil {
		log.Println(err)
		return taggedInstances
	}

	for _, rsv := range resp.Reservations {
		for _, inst := range rsv.Instances {
			taggedInstances = append(taggedInstances, inst.PrivateIPAddress)
		}
	}
	return taggedInstances
}
Пример #2
0
func tagSnapshot(instId, snapId string, tags []ec2.Tag, c *ec2.EC2) {
	defer wg.Done()
	_, err := c.CreateTags([]string{snapId}, tags)
	if err != nil {
		log.Printf("failed to tag snaptshot %s for instance %s, error: %s", snapId, instId, err)
	}
	trimSnapshots(instId, c)
}
Пример #3
0
func terminateInstances(c *gocheck.C, e *ec2.EC2, insts []*ec2.Instance) {
	var ids []string
	for _, inst := range insts {
		if inst != nil {
			ids = append(ids, inst.InstanceId)
		}
	}
	_, err := e.TerminateInstances(ids)
	c.Check(err, gocheck.IsNil, gocheck.Commentf("%d INSTANCES LEFT RUNNING!!!", len(ids)))
}
Пример #4
0
func trimSnapshots(instId string, c *ec2.EC2) {
	filter := ec2.NewFilter()
	val := fmt.Sprintf("%s/%s", instId, *period)
	filter.Add("status", "completed")
	filter.Add("tag:inst_snap", val)
	resp, err := c.Snapshots(nil, filter)
	if err != nil {
		log.Printf("Error getting existing snapshots: ", err)
	}
	if len(resp.Snapshots) > *copies {
		excess := len(resp.Snapshots) - *copies
		extras := resp.Snapshots[:excess]
		log.Printf("Need %d have %d completed snapshots\n", *copies, len(resp.Snapshots))
		for _, extra := range extras {
			log.Printf("Trimming %s for %sn", extra.Id, instId)
			_, err := c.DeleteSnapshots([]string{extra.Id})
			if err != nil {
				log.Println("Failed trimming snapshot:", extra.Id, " Error: ", err)
			}
		}

	}
}