-
Notifications
You must be signed in to change notification settings - Fork 286
/
cleanup.go
64 lines (58 loc) · 1.92 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package e2e
import (
"context"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/eks-anywhere/internal/pkg/ec2"
"github.com/aws/eks-anywhere/internal/pkg/s3"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/validations"
)
func CleanUpAwsTestResources(storageBucket string, maxAge string, tag string) error {
session, err := session.NewSession()
if err != nil {
return fmt.Errorf("error creating session: %v", err)
}
logger.V(1).Info("Fetching list of EC2 instances")
key := "Integration-Test"
value := tag
maxAgeFloat, err := strconv.ParseFloat(maxAge, 64)
if err != nil {
return fmt.Errorf("error parsing max age: %v", err)
}
results, err := ec2.ListInstances(session, key, value, maxAgeFloat)
if err != nil {
return fmt.Errorf("error listing EC2 instances: %v", err)
}
logger.V(1).Info("Successfully listed EC2 instances for termination")
if len(results) != 0 {
logger.V(1).Info("Terminating EC2 instances")
err = ec2.TerminateEc2Instances(session, results)
if err != nil {
return fmt.Errorf("error terminating EC2 instacnes: %v", err)
}
logger.V(1).Info("Successfully terminated EC2 instances")
} else {
logger.V(1).Info("No EC2 instances available for termination")
}
logger.V(1).Info("Clean up s3 bucket objects")
err = s3.CleanUpS3Bucket(session, storageBucket, maxAgeFloat)
if err != nil {
return fmt.Errorf("error clean up s3 bucket objects: %v", err)
}
logger.V(1).Info("Successfully cleaned up s3 bucket")
return nil
}
func CleanUpVsphereTestResources(ctx context.Context, clusterName string) error {
clusterName, err := validations.ValidateClusterNameArg([]string{clusterName})
if err != nil {
return fmt.Errorf("error validating cluster name: %v", err)
}
err = vsphereRmVms(ctx, clusterName)
if err != nil {
return fmt.Errorf("error removing vcenter vms: %v", err)
}
logger.V(1).Info("Vsphere vcenter vms cleanup complete")
return nil
}