-
Notifications
You must be signed in to change notification settings - Fork 7
feat: replace bash cleanup script with native Go implementation #433
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
ArangoGutierrez
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
ArangoGutierrez:cleanup_cmd
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cleanup | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/NVIDIA/holodeck/internal/logger" | ||
"github.com/NVIDIA/holodeck/pkg/cleanup" | ||
|
||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
type command struct { | ||
log *logger.FunLogger | ||
region string | ||
forceDelete bool | ||
} | ||
|
||
// NewCommand constructs the cleanup command with the specified logger | ||
func NewCommand(log *logger.FunLogger) *cli.Command { | ||
c := &command{ | ||
log: log, | ||
} | ||
return c.build() | ||
} | ||
|
||
func (m *command) build() *cli.Command { | ||
// Create the 'cleanup' command | ||
cleanup := cli.Command{ | ||
Name: "cleanup", | ||
Usage: "Clean up AWS VPC resources", | ||
Description: `Clean up AWS VPC resources by VPC ID. | ||
|
||
This command will: | ||
- Check GitHub job status (if GITHUB_TOKEN is set and tags are present) | ||
- Delete all resources in the VPC including: | ||
* EC2 instances | ||
* Security groups | ||
* Subnets | ||
* Route tables | ||
* Internet gateways | ||
* The VPC itself | ||
|
||
Examples: | ||
# Clean up a single VPC | ||
holodeck cleanup vpc-12345678 | ||
|
||
# Clean up multiple VPCs | ||
holodeck cleanup vpc-12345678 vpc-87654321 | ||
|
||
# Force cleanup without job status check | ||
holodeck cleanup --force vpc-12345678 | ||
|
||
# Clean up in a specific region | ||
holodeck cleanup --region us-west-2 vpc-12345678`, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "region", | ||
Aliases: []string{"r"}, | ||
Usage: "AWS region (overrides AWS_REGION env var)", | ||
Destination: &m.region, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "force", | ||
Aliases: []string{"f"}, | ||
Usage: "Force cleanup without checking job status", | ||
Destination: &m.forceDelete, | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if c.NArg() == 0 { | ||
return fmt.Errorf("at least one VPC ID is required") | ||
} | ||
return m.run(c) | ||
}, | ||
} | ||
|
||
return &cleanup | ||
} | ||
|
||
func (m *command) run(c *cli.Context) error { | ||
// Determine the region | ||
region := m.region | ||
if region == "" { | ||
region = os.Getenv("AWS_REGION") | ||
if region == "" { | ||
region = os.Getenv("AWS_DEFAULT_REGION") | ||
if region == "" { | ||
return fmt.Errorf("AWS region must be specified via --region flag or AWS_REGION environment variable") | ||
} | ||
} | ||
} | ||
|
||
// Create the cleaner | ||
cleaner, err := cleanup.New(m.log, region) | ||
if err != nil { | ||
return fmt.Errorf("failed to create cleaner: %w", err) | ||
} | ||
|
||
// Process each VPC ID | ||
successCount := 0 | ||
failCount := 0 | ||
|
||
for _, vpcID := range c.Args().Slice() { | ||
m.log.Info("Processing VPC: %s", vpcID) | ||
|
||
var err error | ||
if m.forceDelete { | ||
// Skip job status check | ||
err = cleaner.DeleteVPCResources(vpcID) | ||
} else { | ||
// Check job status first | ||
err = cleaner.CleanupVPC(vpcID) | ||
} | ||
|
||
if err != nil { | ||
m.log.Error(fmt.Errorf("failed to cleanup VPC %s: %v", vpcID, err)) | ||
failCount++ | ||
} else { | ||
m.log.Info("Successfully cleaned up VPC %s", vpcID) | ||
successCount++ | ||
} | ||
} | ||
|
||
if failCount > 0 { | ||
return fmt.Errorf("cleanup completed with errors: %d succeeded, %d failed", successCount, failCount) | ||
} | ||
|
||
m.log.Info("Cleanup completed successfully: %d VPCs cleaned up", successCount) | ||
return nil | ||
} |
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,79 @@ | ||
# Cleanup Command | ||
|
||
The `cleanup` command deletes AWS VPC resources, with optional GitHub job status | ||
checking. | ||
|
||
## Usage | ||
|
||
```bash | ||
holodeck cleanup [options] VPC_ID [VPC_ID...] | ||
``` | ||
|
||
## Description | ||
|
||
The cleanup command performs comprehensive deletion of AWS VPC resources including: | ||
|
||
- EC2 instances | ||
- Security groups (with ENI detachment) | ||
- Subnets | ||
- Route tables | ||
- Internet gateways | ||
- The VPC itself | ||
|
||
Before deletion, it can optionally check GitHub Actions job status using VPC tags | ||
to ensure jobs are completed. | ||
|
||
## Options | ||
|
||
- `--region, -r`: AWS region (overrides AWS_REGION environment variable) | ||
- `--force, -f`: Force cleanup without checking GitHub job status | ||
|
||
## Environment Variables | ||
|
||
- `AWS_REGION`: Default AWS region if not specified via flag | ||
- `AWS_DEFAULT_REGION`: Fallback region if AWS_REGION is not set | ||
- `GITHUB_TOKEN`: GitHub token for checking job status (optional) | ||
|
||
## Examples | ||
|
||
### Clean up a single VPC | ||
|
||
```bash | ||
holodeck cleanup vpc-12345678 | ||
``` | ||
|
||
### Clean up multiple VPCs | ||
|
||
```bash | ||
holodeck cleanup vpc-12345678 vpc-87654321 | ||
``` | ||
|
||
### Force cleanup without job status check | ||
|
||
```bash | ||
holodeck cleanup --force vpc-12345678 | ||
``` | ||
|
||
### Clean up in a specific region | ||
|
||
```bash | ||
holodeck cleanup --region us-west-2 vpc-12345678 | ||
``` | ||
|
||
## GitHub Job Status Checking | ||
|
||
If the VPC has the following tags and `GITHUB_TOKEN` is set: | ||
|
||
- `GitHubRepository`: The repository in format `owner/repo` | ||
- `GitHubRunId`: The GitHub Actions run ID | ||
|
||
The command will check if all jobs in that run are completed before proceeding with | ||
deletion. Use `--force` to skip this check. | ||
|
||
## Notes | ||
|
||
- The command handles dependencies between resources automatically | ||
- Security groups attached to ENIs are detached before deletion | ||
- Non-main route tables are handled appropriately | ||
- VPC deletion includes retry logic (3 attempts with 30-second delays) | ||
- Partial failures are logged but don't stop the cleanup process |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we download a stable release instead of building it .