-
Notifications
You must be signed in to change notification settings - Fork 24
feat(aws): add cleanup tool to AI #2157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lionello
wants to merge
5
commits into
main
Choose a base branch
from
lio/cleanup-ai
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
800c2ee
feat(aws): add cleanup tool to AI
lionello 7624c7e
fix: clean up records on internal zones
lionello 5df6607
Update Nix vendorHash to sha256-eNm2ChvQ10xAYPJ6jN+Cm4CCnZfqk2e2MFGQ1…
github-actions[bot] e2ac8d6
feat: switch AI debugger to default true
lionello b5c9e07
refactor(debug): update feedback prompt to capture detailed responses
lionello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/DefangLabs/defang/src/pkg/agent/common" | ||
| "github.com/DefangLabs/defang/src/pkg/auth" | ||
| "github.com/DefangLabs/defang/src/pkg/cli/client" | ||
| "github.com/DefangLabs/defang/src/pkg/elicitations" | ||
| "github.com/DefangLabs/defang/src/pkg/stacks" | ||
| "github.com/DefangLabs/defang/src/pkg/term" | ||
| ) | ||
|
|
||
| type CleanupParams struct { | ||
| common.LoaderParams | ||
| } | ||
|
|
||
| func HandleCleanupTool(ctx context.Context, loader client.Loader, params CleanupParams, cli CLIInterface, ec elicitations.Controller, sc StackConfig) (string, error) { | ||
| term.Debug("Function invoked: cli.Connect") | ||
| fabric, err := GetClientWithRetry(ctx, cli, sc.FabricAddr) | ||
| if err != nil { | ||
| var noBrowserErr auth.ErrNoBrowser | ||
| if errors.As(err, &noBrowserErr) { | ||
| return noBrowserErr.Error(), nil | ||
| } | ||
| return "", err | ||
| } | ||
|
|
||
| workingDir, _ := loader.ProjectWorkingDir(ctx) | ||
| sm, err := stacks.NewManager(fabric, workingDir, params.ProjectName, ec) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to create stack manager: %w", err) | ||
|
lionello marked this conversation as resolved.
|
||
| } | ||
| pp := NewProviderPreparer(cli, ec, fabric, sm) | ||
| _, provider, err := pp.SetupProvider(ctx, sc.Stack) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to setup provider: %w", err) | ||
| } | ||
|
|
||
| projectName, err := cli.LoadProjectNameWithFallback(ctx, loader, provider) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to load project name: %w", err) | ||
| } | ||
|
|
||
| if err := cli.CanIUseProvider(ctx, fabric, provider, projectName, 0); err != nil { | ||
| return "", fmt.Errorf("failed to use provider: %w", err) | ||
| } | ||
|
|
||
| cleaner, ok := provider.(client.OrphanCleaner) | ||
| if !ok { | ||
| return "Resource cleanup is currently only supported for AWS. The selected provider does not retain resources that need manual cleanup.", nil | ||
| } | ||
|
|
||
| orphans, err := cleaner.DiscoverOrphans(ctx, projectName) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to discover leftover resources: %w", err) | ||
| } | ||
| if len(orphans) == 0 { | ||
| return fmt.Sprintf("No leftover resources found for project %q that are blocking cleanup.", projectName), nil | ||
| } | ||
|
|
||
| var report strings.Builder | ||
| fmt.Fprintf(&report, "Found %d leftover resource(s) for project %q blocking cleanup:\n", len(orphans), projectName) | ||
|
|
||
| // Without interactive elicitation we cannot get confirmation for these destructive actions, | ||
| // so only report what was found and let the caller decide. | ||
| if !ec.IsSupported() { | ||
| for _, o := range orphans { | ||
| fmt.Fprintf(&report, "- [%s] %s — would %s\n", o.Category, o.Name, o.Action) | ||
| } | ||
| report.WriteString("\nRe-run this tool in an interactive session to apply these changes, then run `defang down` so Pulumi can finish removing the resources.") | ||
| return report.String(), nil | ||
| } | ||
|
|
||
| var cleaned, skipped, failed int | ||
| for _, o := range orphans { | ||
| confirm, err := ec.RequestEnum(ctx, | ||
| fmt.Sprintf("Cleanup will %s for %s %q. Proceed?", o.Action, o.Category, o.Name), | ||
| "confirm", []string{"no", "yes"}) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to confirm cleanup: %w", err) | ||
| } | ||
| if confirm != "yes" { | ||
| skipped++ | ||
| fmt.Fprintf(&report, "- [%s] %s — skipped\n", o.Category, o.Name) | ||
| continue | ||
| } | ||
| if err := cleaner.CleanupOrphan(ctx, o); err != nil { | ||
| failed++ | ||
| fmt.Fprintf(&report, "- [%s] %s — failed: %v\n", o.Category, o.Name, err) | ||
| continue | ||
| } | ||
| cleaned++ | ||
| fmt.Fprintf(&report, "- [%s] %s — done (%s)\n", o.Category, o.Name, o.Action) | ||
| } | ||
|
|
||
| fmt.Fprintf(&report, "\n%d cleaned, %d skipped, %d failed.", cleaned, skipped, failed) | ||
| if cleaned > 0 { | ||
| report.WriteString(" Run `defang down` (or the destroy tool) so Pulumi can finish removing the now-unblocked resources.") | ||
| } | ||
| return report.String(), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.