cloudflare/Stout
Publicmirrored fromhttps://github.com/cloudflare/Stout
src/rollback.go
65lines · modecode
Reorganize CLI prompts, move CLI related checks to cli.go and heavily comment the source codef7df571
9 years ago
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log" |
| 6 | "path/filepath" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/zackbloom/goamz/s3" |
| 10 | ) |
| 11 | |
| 12 | /* |
| 13 | * Rollback |
| 14 | * Go go the version prefix folder on s3 and copy the html files over to the root as the currently active files |
| 15 | */ |
| 16 | func Rollback(options Options, version string) { |
| 17 | if s3Session == nil { |
| 18 | s3Session = openS3(options.AWSKey, options.AWSSecret, options.AWSRegion) |
| 19 | } |
| 20 | |
| 21 | bucket := s3Session.Bucket(options.Bucket) |
| 22 | |
| 23 | prefix := filepath.Join(options.Dest, version) + "/" |
| 24 | |
| 25 | //find files that start with the prefix |
| 26 | list, err := bucket.List(prefix, "", "", 1000) |
| 27 | panicIf(err) |
| 28 | |
| 29 | if list.IsTruncated { |
| 30 | panic(fmt.Sprintf("More than %d HTML files in version, rollback is not supported. Consider filing a GitHub issue if you need support for this.", list.MaxKeys)) |
| 31 | } |
| 32 | if len(list.Contents) == 0 { |
| 33 | log.Printf("A deploy with the provided id (%s) was not found in the specified bucket", version) |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | wg := sync.WaitGroup{} |
| 38 | |
| 39 | count := 0 |
| 40 | for _, file := range list.Contents { |
| 41 | wg.Add(1) |
| 42 | go func(file s3.Key) { |
| 43 | defer wg.Done() |
| 44 | |
| 45 | path := file.Key |
| 46 | if filepath.Ext(path) != ".html" { |
| 47 | log.Printf("Skipping non-html file %s", path) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | newPath := filepath.Join(options.Dest, path[len(prefix):]) |
| 52 | |
| 53 | log.Printf("Aliasing %s to %s", path, newPath) |
| 54 | |
| 55 | //replace old files with new prefixed files in root |
| 56 | copyFile(bucket, path, newPath, "text/html", LIMITED) |
| 57 | |
| 58 | count++ |
| 59 | }(file) |
| 60 | } |
| 61 | |
| 62 | wg.Wait() |
| 63 | |
| 64 | log.Printf("Reverted %d HTML files to version %s", count, version) |
| 65 | } |