|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 12 | + "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/s3/types" |
| 15 | + |
| 16 | + "github.com/ActiveState/cli/internal/condition" |
| 17 | +) |
| 18 | + |
| 19 | +var awsRegionName, awsBucketName, basePrefix string |
| 20 | +var client *s3.Client |
| 21 | + |
| 22 | +func main() { |
| 23 | + if !condition.InUnitTest() { |
| 24 | + if len(os.Args) != 3 && len(os.Args) != 4 { |
| 25 | + log.Fatalf("Usage: %s <region-name> <bucket-name> [base-prefix]", os.Args[0]) |
| 26 | + } |
| 27 | + |
| 28 | + awsRegionName = os.Args[1] |
| 29 | + awsBucketName = os.Args[2] |
| 30 | + if len(os.Args) == 4 { |
| 31 | + basePrefix = os.Args[3] |
| 32 | + if basePrefix != "" && !strings.HasSuffix(basePrefix, "/") { |
| 33 | + basePrefix += "/" |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + run() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func run() { |
| 42 | + fmt.Printf("Promoting staging files to production in bucket: %s\n", awsBucketName) |
| 43 | + if basePrefix != "" { |
| 44 | + fmt.Printf("Using base prefix: %s\n", basePrefix) |
| 45 | + } |
| 46 | + |
| 47 | + createClient() |
| 48 | + |
| 49 | + // List all objects with <basePrefix>staging/ prefix |
| 50 | + allObjects, err := listObjectsWithPrefix(basePrefix + "staging/") |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("Failed to list staging objects: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Filter out the root staging directory itself (but keep subdirectories) |
| 56 | + var stagingObjects []types.Object |
| 57 | + stagingPrefix := basePrefix + "staging/" |
| 58 | + for _, obj := range allObjects { |
| 59 | + if *obj.Key == stagingPrefix { |
| 60 | + continue |
| 61 | + } |
| 62 | + stagingObjects = append(stagingObjects, obj) |
| 63 | + } |
| 64 | + |
| 65 | + if len(stagingObjects) == 0 { |
| 66 | + fmt.Println("No staging files found to promote.") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Printf("Found %d staging files to promote:\n", len(stagingObjects)) |
| 71 | + for _, obj := range stagingObjects { |
| 72 | + fmt.Printf(" - %s\n", *obj.Key) |
| 73 | + } |
| 74 | + |
| 75 | + // Copy each staging object to production location and delete the staging version |
| 76 | + for _, obj := range stagingObjects { |
| 77 | + stagingKey := *obj.Key |
| 78 | + relativeKey := strings.TrimPrefix(stagingKey, basePrefix+"staging/") |
| 79 | + destinationKey := basePrefix + "release/" + relativeKey |
| 80 | + |
| 81 | + fmt.Printf("Promoting %s -> %s\n", stagingKey, destinationKey) |
| 82 | + |
| 83 | + err := copyObject(stagingKey, destinationKey) |
| 84 | + if err != nil { |
| 85 | + log.Fatalf("Failed to copy %s to %s: %v", stagingKey, destinationKey, err) |
| 86 | + } |
| 87 | + |
| 88 | + err = deleteObject(stagingKey) |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("Failed to delete staging object %s: %v", stagingKey, err) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fmt.Printf("Successfully promoted %d files from staging to production.\n", len(stagingObjects)) |
| 95 | +} |
| 96 | + |
| 97 | +func createClient() { |
| 98 | + var err error |
| 99 | + |
| 100 | + cfg, err := config.LoadDefaultConfig(context.Background(), |
| 101 | + config.WithRegion(awsRegionName), |
| 102 | + ) |
| 103 | + if err != nil { |
| 104 | + log.Fatalf("failed to load config, %s", err.Error()) |
| 105 | + } |
| 106 | + |
| 107 | + // For Windows workstations, you might need to handle profile selection differently |
| 108 | + if runtime.GOOS == "windows" && !condition.OnCI() { |
| 109 | + cfg, err = config.LoadDefaultConfig(context.Background(), |
| 110 | + config.WithRegion(awsRegionName), |
| 111 | + config.WithSharedConfigProfile("mfa"), |
| 112 | + ) |
| 113 | + if err != nil { |
| 114 | + log.Fatalf("failed to load config with profile, %s", err.Error()) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + client = s3.NewFromConfig(cfg) |
| 119 | +} |
| 120 | + |
| 121 | +func listObjectsWithPrefix(prefix string) ([]types.Object, error) { |
| 122 | + var objects []types.Object |
| 123 | + |
| 124 | + paginator := s3.NewListObjectsV2Paginator(client, &s3.ListObjectsV2Input{ |
| 125 | + Bucket: aws.String(awsBucketName), |
| 126 | + Prefix: aws.String(prefix), |
| 127 | + }) |
| 128 | + |
| 129 | + for paginator.HasMorePages() { |
| 130 | + page, err := paginator.NextPage(context.Background()) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + objects = append(objects, page.Contents...) |
| 135 | + } |
| 136 | + |
| 137 | + return objects, nil |
| 138 | +} |
| 139 | + |
| 140 | +func copyObject(sourceKey, destinationKey string) error { |
| 141 | + copySource := fmt.Sprintf("%s/%s", awsBucketName, sourceKey) |
| 142 | + |
| 143 | + _, err := client.CopyObject(context.Background(), &s3.CopyObjectInput{ |
| 144 | + Bucket: aws.String(awsBucketName), |
| 145 | + CopySource: aws.String(copySource), |
| 146 | + Key: aws.String(destinationKey), |
| 147 | + ACL: types.ObjectCannedACLPublicRead, |
| 148 | + }) |
| 149 | + |
| 150 | + return err |
| 151 | +} |
| 152 | + |
| 153 | +func deleteObject(key string) error { |
| 154 | + _, err := client.DeleteObject(context.Background(), &s3.DeleteObjectInput{ |
| 155 | + Bucket: aws.String(awsBucketName), |
| 156 | + Key: aws.String(key), |
| 157 | + }) |
| 158 | + |
| 159 | + return err |
| 160 | +} |
0 commit comments