Skip to content

Commit

Permalink
(feat) display more information about aws creds being used.
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Oct 1, 2024
1 parent 963a1d9 commit 04c6e52
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
33 changes: 26 additions & 7 deletions cmd/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/google/uuid"
"github.com/overmindtech/aws-source/proc"
"github.com/overmindtech/cli/tfutils"
"github.com/overmindtech/discovery"
"github.com/overmindtech/pterm"
"github.com/overmindtech/sdp-go"
stdlibsource "github.com/overmindtech/stdlib-source/sources"
stdlibSource "github.com/overmindtech/stdlib-source/sources"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/sourcegraph/conc/pool"
Expand All @@ -37,11 +38,12 @@ var exploreCmd = &cobra.Command{
// any query or request during the runtime of the CLI. for proper cleanup,
// execute the returned function. The method returns once the sources are
// started. Progress is reported into the provided multi printer.
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, multi *pterm.MultiPrinter) (func(), error) {
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, failOverToAws bool, multi *pterm.MultiPrinter) (func(), error) {
var err error

stdlibSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting stdlib source engine")
awsSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting AWS source engine")
statusArea := pterm.DefaultSection.WithWriter(multi.NewWriter())

natsOptions := natsOptions(ctx, oi, token)
heartbeatOptions := heartbeatOptions(oi, token)
Expand All @@ -54,7 +56,7 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
p := pool.NewWithResults[*discovery.Engine]().WithErrors()

p.Go(func() (*discovery.Engine, error) {
stdlibEngine, err := stdlibsource.InitializeEngine(
stdlibEngine, err := stdlibSource.InitializeEngine(
natsOptions,
fmt.Sprintf("stdlib-source-%v", hostname),
fmt.Sprintf("cli-%v", cliVersion),
Expand All @@ -65,13 +67,14 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
)
if err != nil {
stdlibSpinner.Fail("Failed to initialize stdlib source engine")
statusArea.Println(fmt.Sprintf("Failed to initialize stdlib source engine: %v", err))
return nil, fmt.Errorf("failed to initialize stdlib source engine: %w", err)
}

// todo: pass in context with timeout to abort timely and allow Ctrl-C to work
err = stdlibEngine.Start()
if err != nil {
stdlibSpinner.Fail("Failed to start stdlib source engine")
statusArea.Println(fmt.Sprintf("Failed to start stdlib source engine: %v", err))
return nil, fmt.Errorf("failed to start stdlib source engine: %w", err)
}
stdlibSpinner.Success("Stdlib source engine started")
Expand All @@ -82,16 +85,17 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
tfEval, err := tfutils.LoadEvalContext(tfArgs, os.Environ())
if err != nil {
awsSpinner.Fail("Failed to load variables from the environment")
statusArea.Println(fmt.Sprintf("Failed to load variables from the environment: %v", err))
return nil, fmt.Errorf("failed to load variables from the environment: %w", err)
}

providers, err := tfutils.ParseAWSProviders(".", tfEval)
if err != nil {
awsSpinner.Fail("Failed to parse providers")
statusArea.Println(fmt.Sprintf("Failed to parse providers: %v", err))
return nil, fmt.Errorf("failed to parse providers: %w", err)
}
configs := []aws.Config{}

for _, p := range providers {
if p.Error != nil {
// skip providers that had errors. This allows us to use
Expand All @@ -102,11 +106,23 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
c, err := tfutils.ConfigFromProvider(ctx, *p.Provider)
if err != nil {
awsSpinner.Fail("Error when converting provider to config")
statusArea.Println(fmt.Sprintf("Error when converting provider to config: %v", err))
return nil, fmt.Errorf("error when converting provider to config: %w", err)
}
credentials, _ := c.Credentials.Retrieve(ctx)
statusArea.Println(fmt.Sprintf("Using AWS provider in %s with %s.", p.FilePath, credentials.Source))
configs = append(configs, c)
}

if len(configs) == 0 && failOverToAws {
userConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
awsSpinner.Fail("Failed to load default AWS config")
statusArea.Println(fmt.Sprintf("Failed to load default AWS config: %v", err))
return nil, fmt.Errorf("failed to load default AWS config: %w", err)
}
statusArea.Println("Using default AWS CLI config. No AWS terraform providers found.")
configs = append(configs, userConfig)
}
awsEngine, err := proc.InitializeAwsSourceEngine(
ctx,
fmt.Sprintf("aws-source-%v", hostname),
Expand All @@ -120,13 +136,15 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
)
if err != nil {
awsSpinner.Fail("Failed to initialize AWS source engine")
statusArea.Println(fmt.Sprintf("Failed to initialize AWS source engine: %v", err))
return nil, fmt.Errorf("failed to initialize AWS source engine: %w", err)
}

// todo: pass in context with timeout to abort timely and allow Ctrl-C to work
err = awsEngine.Start()
if err != nil {
awsSpinner.Fail("Failed to start AWS source engine")
statusArea.Println(fmt.Sprintf("Failed to start AWS source engine: %v", err))
return nil, fmt.Errorf("failed to start AWS source engine: %w", err)
}

Expand Down Expand Up @@ -163,8 +181,9 @@ func Explore(cmd *cobra.Command, args []string) error {
return err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, true, &multi)
if err != nil {
pterm.Error.Printf("Failed to start sources: %v", err)
return err
}
defer cleanup()
Expand Down
2 changes: 1 addition & 1 deletion cmd/pterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func StartSources(ctx context.Context, cmd *cobra.Command, args []string) (conte
return ctx, sdp.OvermindInstance{}, nil, nil, err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, false, &multi)
if err != nil {
return ctx, sdp.OvermindInstance{}, nil, nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
PTermSetup()

hasPlanSet := false
autoapprove := false
autoApprove := false
planFile := "overmind.plan"
if len(args) >= 1 {
f, err := os.Stat(args[len(args)-1])
Expand All @@ -51,7 +51,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}
if hasPlanSet {
planFile = args[len(args)-1]
autoapprove = true
autoApprove = true
}
}

Expand All @@ -76,18 +76,18 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
// therefore we only check for the flag when no plan file is supplied
for _, a := range args {
if a == "-auto-approve" || a == "-auto-approve=true" || a == "-auto-approve=TRUE" || a == "--auto-approve" || a == "--auto-approve=true" || a == "--auto-approve=TRUE" {
autoapprove = true
autoApprove = true
}
if a == "-auto-approve=false" || a == "-auto-approve=FALSE" || a == "--auto-approve=false" || a == "--auto-approve=FALSE" {
autoapprove = false
autoApprove = false
}
}
}

args = append([]string{"apply"}, args...)

needPlan := !hasPlanSet
needApproval := !autoapprove
needApproval := !autoApprove

ctx, oi, _, cleanup, err := StartSources(ctx, cmd, args)
if err != nil {
Expand All @@ -103,7 +103,6 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}

if needApproval {

pterm.Println("")
pterm.Println("Do you want to perform these actions?")
pterm.Println("")
Expand Down

0 comments on commit 04c6e52

Please sign in to comment.