// Release releases all address for the given region/client // // TODO(rjeczalik): add logger func (a *Addresses) Release(client *amazon.Client) { if len(a.m) == 0 { return } addresses := a.m[client] fmt.Printf("Releasing %d addresses for region %s\n", len(addresses), client.Region) for _, addr := range addresses { ip := aws.StringValue(addr.PublicIp) assocID := aws.StringValue(addr.AssociationId) if assocID != "" { // EIP is in-use, disassociate it first. err := client.DisassociateAddress(assocID) if err != nil { // Even when it fails, will try to release the EIP. fmt.Printf("[%s] disassociate %s EIP error: %s\n", client.Region, ip, err) } } allocID := aws.StringValue(addr.AllocationId) err := client.ReleaseAddress(allocID) if err != nil { fmt.Printf("[%s] release %s EIP error: %s\n", client.Region, ip, err) } } fmt.Printf("Releasing is done for region %s\n", client.Region) }
// Stop stop all instances func (i Instances) StopAll(client *amazon.Client) { if len(i) == 0 { return } for _, split := range splittedIds(i.Ids(), 500) { _, err := client.StopInstances(split...) if err != nil { fmt.Printf("[%s] stop error: %s\n", client.Region, err) } } }
// Terminate terminates all instances func (i Instances) TerminateAll(client *amazon.Client) { if len(i) == 0 { return } for _, split := range splittedIds(i.Ids(), 500) { _, err := client.TerminateInstances(split...) if err != nil { fmt.Printf("[%s] terminate error: %s\n", client.Region, err) continue } } }
// Terminate terminates the given instance specified with the id func (i Instances) Terminate(client *amazon.Client, db *mongodb.MongoDB, id string) { if id == "" { return } _, err := client.TerminateInstance(id) if err != nil { fmt.Printf("[%s] terminate error: %s\n", client.Region, err) } else { if err := deleteDocument(i[id], db); err != nil { fmt.Printf("[%s] deleting document for %q error: %s\n", client.Region, id, err) } } }
// Terminate terminates the given volume specified with the volume id func (v Volumes) TerminateAll(client *amazon.Client) { if len(v) == 0 { return } var wg sync.WaitGroup for id := range v { wg.Add(1) go func(id string) { client.DeleteVolume(id) wg.Done() }(id) } wg.Wait() }
func awsData(client *amazon.Client, instanceId string) (i *instance, err error) { i = &instance{} i.ec2, err = client.InstanceByID(instanceId) if err != nil { return nil, err } i.status, err = client.InstanceStatusByID(instanceId) if err != nil { return nil, err } if len(i.ec2.BlockDeviceMappings) != 0 { i.volume, err = client.VolumeByID(aws.StringValue(i.ec2.BlockDeviceMappings[0].Ebs.VolumeId)) if err != nil { return nil, err } } return i, nil }