|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cleanup |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/NVIDIA/holodeck/internal/logger" |
| 24 | + "github.com/NVIDIA/holodeck/pkg/cleanup" |
| 25 | + |
| 26 | + cli "github.com/urfave/cli/v2" |
| 27 | +) |
| 28 | + |
| 29 | +type command struct { |
| 30 | + log *logger.FunLogger |
| 31 | + region string |
| 32 | + forceDelete bool |
| 33 | +} |
| 34 | + |
| 35 | +// NewCommand constructs the cleanup command with the specified logger |
| 36 | +func NewCommand(log *logger.FunLogger) *cli.Command { |
| 37 | + c := &command{ |
| 38 | + log: log, |
| 39 | + } |
| 40 | + return c.build() |
| 41 | +} |
| 42 | + |
| 43 | +func (m *command) build() *cli.Command { |
| 44 | + // Create the 'cleanup' command |
| 45 | + cleanup := cli.Command{ |
| 46 | + Name: "cleanup", |
| 47 | + Usage: "Clean up AWS VPC resources", |
| 48 | + Description: `Clean up AWS VPC resources by VPC ID. |
| 49 | +
|
| 50 | +This command will: |
| 51 | +- Check GitHub job status (if GITHUB_TOKEN is set and tags are present) |
| 52 | +- Delete all resources in the VPC including: |
| 53 | + * EC2 instances |
| 54 | + * Security groups |
| 55 | + * Subnets |
| 56 | + * Route tables |
| 57 | + * Internet gateways |
| 58 | + * The VPC itself |
| 59 | +
|
| 60 | +Examples: |
| 61 | + # Clean up a single VPC |
| 62 | + holodeck cleanup vpc-12345678 |
| 63 | +
|
| 64 | + # Clean up multiple VPCs |
| 65 | + holodeck cleanup vpc-12345678 vpc-87654321 |
| 66 | +
|
| 67 | + # Force cleanup without job status check |
| 68 | + holodeck cleanup --force vpc-12345678 |
| 69 | +
|
| 70 | + # Clean up in a specific region |
| 71 | + holodeck cleanup --region us-west-2 vpc-12345678`, |
| 72 | + Flags: []cli.Flag{ |
| 73 | + &cli.StringFlag{ |
| 74 | + Name: "region", |
| 75 | + Aliases: []string{"r"}, |
| 76 | + Usage: "AWS region (overrides AWS_REGION env var)", |
| 77 | + Destination: &m.region, |
| 78 | + }, |
| 79 | + &cli.BoolFlag{ |
| 80 | + Name: "force", |
| 81 | + Aliases: []string{"f"}, |
| 82 | + Usage: "Force cleanup without checking job status", |
| 83 | + Destination: &m.forceDelete, |
| 84 | + }, |
| 85 | + }, |
| 86 | + Action: func(c *cli.Context) error { |
| 87 | + if c.NArg() == 0 { |
| 88 | + return fmt.Errorf("at least one VPC ID is required") |
| 89 | + } |
| 90 | + return m.run(c) |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + return &cleanup |
| 95 | +} |
| 96 | + |
| 97 | +func (m *command) run(c *cli.Context) error { |
| 98 | + // Determine the region |
| 99 | + region := m.region |
| 100 | + if region == "" { |
| 101 | + region = os.Getenv("AWS_REGION") |
| 102 | + if region == "" { |
| 103 | + region = os.Getenv("AWS_DEFAULT_REGION") |
| 104 | + if region == "" { |
| 105 | + return fmt.Errorf("AWS region must be specified via --region flag or AWS_REGION environment variable") |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Create the cleaner |
| 111 | + cleaner, err := cleanup.New(m.log, region) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to create cleaner: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Process each VPC ID |
| 117 | + successCount := 0 |
| 118 | + failCount := 0 |
| 119 | + |
| 120 | + for _, vpcID := range c.Args().Slice() { |
| 121 | + m.log.Info("Processing VPC: %s", vpcID) |
| 122 | + |
| 123 | + var err error |
| 124 | + if m.forceDelete { |
| 125 | + // Skip job status check |
| 126 | + err = cleaner.DeleteVPCResources(vpcID) |
| 127 | + } else { |
| 128 | + // Check job status first |
| 129 | + err = cleaner.CleanupVPC(vpcID) |
| 130 | + } |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + m.log.Error(fmt.Errorf("failed to cleanup VPC %s: %v", vpcID, err)) |
| 134 | + failCount++ |
| 135 | + } else { |
| 136 | + m.log.Info("Successfully cleaned up VPC %s", vpcID) |
| 137 | + successCount++ |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if failCount > 0 { |
| 142 | + return fmt.Errorf("cleanup completed with errors: %d succeeded, %d failed", successCount, failCount) |
| 143 | + } |
| 144 | + |
| 145 | + m.log.Info("Cleanup completed successfully: %d VPCs cleaned up", successCount) |
| 146 | + return nil |
| 147 | +} |
0 commit comments