-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Close Terraform AWS Provider binary (#116)
* Fix: Shutdown provider binary when done The binary process kept running in the background after the awsweeper process terminated. Other features/fixes in this commit are included by bumping up the version of terradozer to v0.1.2: 1. Ignore non-existing resources (of type cty.Nil): This means, AWS instances that are in state TERMINATED, NAT gateways that are not available anymore, etc. are filtered out and not shown 2. Fix panic of nil pointer dereference when trying to destroy a resource that doesn't exist anymore (however, nil pointer shouldn't happen anymore because of 1.) 3. Offline mode: don't re-download Terraform provider binary if binary exists already in ~/.awsweeper/terraform-provider-aws_v2.68.0_x4 * Remove aws-sdk-go dependency Only using v2 now * Fix failing tests
- Loading branch information
Showing
23 changed files
with
433 additions
and
624 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
package main | ||
|
||
//go:generate mockgen -package mocks -destination pkg/resource/mocks/autoscaling.go -source=$GOPATH/pkg/mod/github.com/aws/[email protected]/service/autoscaling/autoscalingiface/interface.go | ||
//go:generate mockgen -package mocks -destination pkg/resource/mocks/ec2.go -source=$GOPATH/pkg/mod/github.com/aws/[email protected]/service/ec2/ec2iface/interface.go | ||
//go:generate mockgen -package mocks -destination pkg/resource/mocks/sts.go -source=$GOPATH/pkg/mod/github.com/aws/[email protected]/service/sts/stsiface/interface.go | ||
//go:generate mockgen -package mocks -destination pkg/resource/mocks/rds.go -source=$GOPATH/pkg/mod/github.com/aws/[email protected]/service/rds/rdsiface/interface.go | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
stdlog "log" | ||
|
@@ -16,14 +10,12 @@ import ( | |
|
||
"github.com/apex/log" | ||
"github.com/apex/log/handlers/cli" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/fatih/color" | ||
awsls "github.com/jckuester/awsls/aws" | ||
"github.com/jckuester/awsls/util" | ||
"github.com/jckuester/awsweeper/internal" | ||
"github.com/jckuester/awsweeper/pkg/resource" | ||
"github.com/jckuester/terradozer/pkg/provider" | ||
terradozerRes "github.com/jckuester/terradozer/pkg/resource" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
func main() { | ||
|
@@ -50,8 +42,8 @@ func mainExitCode() int { | |
flags.StringVar(&outputType, "output", "string", "The type of output result (String, JSON or YAML)") | ||
flags.BoolVar(&dryRun, "dry-run", false, "Don't delete anything, just show what would be deleted") | ||
flags.BoolVar(&logDebug, "debug", false, "Enable debug logging") | ||
flags.StringVar(&profile, "profile", "", "The AWS named profile to use as credential") | ||
flags.StringVar(®ion, "region", "", "The region to delete resources in") | ||
flags.StringVarP(&profile, "profile", "p", "", "The AWS profile for the account to delete resources in") | ||
flags.StringVarP(®ion, "region", "r", "", "The region to delete resources in") | ||
flags.IntVar(¶llel, "parallel", 10, "Limit the number of concurrent delete operations") | ||
flags.BoolVar(&version, "version", false, "Show application version") | ||
flags.BoolVar(&force, "force", false, "Delete without asking for confirmation") | ||
|
@@ -112,17 +104,20 @@ func mainExitCode() int { | |
return 1 | ||
} | ||
|
||
var profiles []string | ||
var regions []string | ||
|
||
if profile != "" { | ||
err := os.Setenv("AWS_PROFILE", profile) | ||
if err != nil { | ||
log.WithError(err).Error("failed to set AWS profile") | ||
profiles = []string{profile} | ||
} else { | ||
env, ok := os.LookupEnv("AWS_PROFILE") | ||
if ok { | ||
profiles = []string{env} | ||
} | ||
} | ||
|
||
if region != "" { | ||
err := os.Setenv("AWS_DEFAULT_REGION", region) | ||
if err != nil { | ||
log.WithError(err).Error("failed to set AWS region") | ||
} | ||
regions = []string{region} | ||
} | ||
|
||
timeoutDuration, err := time.ParseDuration(timeout) | ||
|
@@ -131,30 +126,34 @@ func mainExitCode() int { | |
return 1 | ||
} | ||
|
||
provider, err := provider.Init("aws", "~/.awsweeper", timeoutDuration) | ||
clients, err := util.NewAWSClientPool(profiles, regions) | ||
if err != nil { | ||
log.WithError(err).Error("failed to initialize Terraform AWS Providers") | ||
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err)) | ||
|
||
return 1 | ||
} | ||
|
||
// TODO remove resource.NewAWS(sess) with awsls.NewClient() | ||
sess := session.Must(session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{Region: ®ion}, | ||
SharedConfigState: session.SharedConfigEnable, | ||
Profile: profile, | ||
})) | ||
|
||
client := resource.NewAWS(sess) | ||
clientKeys := make([]util.AWSClientKey, 0, len(clients)) | ||
for k := range clients { | ||
clientKeys = append(clientKeys, k) | ||
} | ||
|
||
awsClient, err := awsls.NewClient() | ||
// initialize a Terraform AWS provider for each AWS client with a matching config | ||
providers, err := util.NewProviderPool(clientKeys, "2.68.0", "~/.awsweeper", timeoutDuration) | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err)) | ||
|
||
return 1 | ||
} | ||
|
||
defer func() { | ||
for _, p := range providers { | ||
_ = p.Close() | ||
} | ||
}() | ||
|
||
internal.LogTitle("showing resources that would be deleted (dry run)") | ||
resources := resource.List(filter, client, awsClient, provider, outputType) | ||
resources := resource.List(filter, clients, providers, outputType) | ||
|
||
if len(resources) == 0 { | ||
internal.LogTitle("no resources found to delete") | ||
|
Oops, something went wrong.