Skip to content

Commit c76ad94

Browse files
karalabefjl
authored andcommitted
.travis, build: autodelete old unstable archives (#13867)
This commit adds a build step to travis to auto-delete unstable archives older than 14 days (our regular release schedule) from Azure via ci.go purge. The commit also pulls in the latest Azure storage code, also switching over from the old import path (github.com/Azure/azure-sdk-for-go) to the new split one (github.com/Azure/azure-storage-go).
1 parent 3d8de95 commit c76ad94

68 files changed

Lines changed: 7826 additions & 1118 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.travis.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ matrix:
178178

179179
- go run build/ci.go xcode -signer IOS_SIGNING_KEY -deploy trunk -upload gethstore/builds
180180

181+
# This builder does the Azure archive purges to avoid accumulating junk
182+
- os: linux
183+
dist: trusty
184+
sudo: required
185+
go: 1.8
186+
env:
187+
- azure-purge
188+
script:
189+
- go run build/ci.go purge -store gethstore/builds -days 14
190+
181191
install:
182192
- go get golang.org/x/tools/cmd/cover
183193
script:

‎build/ci.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Available commands are:
3232
aar [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an Android archive
3333
xcode [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an iOS XCode framework
3434
xgo [ -alltools ] [ options ] -- cross builds according to options
35+
purge [ -store blobstore ] [ -days threshold ] -- purges old archives from the blobstore
3536
3637
For all commands, -n prevents execution of external programs (dry run mode).
3738
@@ -146,6 +147,8 @@ func main() {
146147
doXCodeFramework(os.Args[2:])
147148
case "xgo":
148149
doXgo(os.Args[2:])
150+
case "purge":
151+
doPurge(os.Args[2:])
149152
default:
150153
log.Fatal("unknown command ", os.Args[1])
151154
}
@@ -427,6 +430,10 @@ func maybeSkipArchive(env build.Environment) {
427430
log.Printf("skipping because this is a PR build")
428431
os.Exit(0)
429432
}
433+
if env.IsCronJob {
434+
log.Printf("skipping because this is a cron job")
435+
os.Exit(0)
436+
}
430437
if env.Branch != "master" && !strings.HasPrefix(env.Tag, "v1.") {
431438
log.Printf("skipping because branch %q, tag %q is not on the whitelist", env.Branch, env.Tag)
432439
os.Exit(0)
@@ -952,3 +959,62 @@ func xgoTool(args []string) *exec.Cmd {
952959
}
953960
return cmd
954961
}
962+
963+
// Binary distribution cleanups
964+
965+
func doPurge(cmdline []string) {
966+
var (
967+
store = flag.String("store", "", `Destination from where to purge archives (usually "gethstore/builds")`)
968+
limit = flag.Int("days", 30, `Age threshold above which to delete unstalbe archives`)
969+
)
970+
flag.CommandLine.Parse(cmdline)
971+
972+
if env := build.Env(); !env.IsCronJob {
973+
log.Printf("skipping because not a cron job")
974+
os.Exit(0)
975+
}
976+
// Create the azure authentication and list the current archives
977+
auth := build.AzureBlobstoreConfig{
978+
Account: strings.Split(*store, "/")[0],
979+
Token: os.Getenv("AZURE_BLOBSTORE_TOKEN"),
980+
Container: strings.SplitN(*store, "/", 2)[1],
981+
}
982+
blobs, err := build.AzureBlobstoreList(auth)
983+
if err != nil {
984+
log.Fatal(err)
985+
}
986+
// Iterate over the blobs, collect and sort all unstable builds
987+
for i := 0; i < len(blobs); i++ {
988+
if !strings.Contains(blobs[i].Name, "unstable") {
989+
blobs = append(blobs[:i], blobs[i+1:]...)
990+
i--
991+
}
992+
}
993+
for i := 0; i < len(blobs); i++ {
994+
for j := i + 1; j < len(blobs); j++ {
995+
iTime, err := time.Parse(time.RFC1123, blobs[i].Properties.LastModified)
996+
if err != nil {
997+
log.Fatal(err)
998+
}
999+
jTime, err := time.Parse(time.RFC1123, blobs[j].Properties.LastModified)
1000+
if err != nil {
1001+
log.Fatal(err)
1002+
}
1003+
if iTime.After(jTime) {
1004+
blobs[i], blobs[j] = blobs[j], blobs[i]
1005+
}
1006+
}
1007+
}
1008+
// Filter out all archives more recent that the given threshold
1009+
for i, blob := range blobs {
1010+
timestamp, _ := time.Parse(time.RFC1123, blob.Properties.LastModified)
1011+
if time.Since(timestamp) < time.Duration(*limit)*24*time.Hour {
1012+
blobs = blobs[:i]
1013+
break
1014+
}
1015+
}
1016+
// Delete all marked as such and return
1017+
if err := build.AzureBlobstoreDelete(auth, blobs); err != nil {
1018+
log.Fatal(err)
1019+
}
1020+
}

‎internal/build/azure.go‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222

23-
"github.com/Azure/azure-sdk-for-go/storage"
23+
storage "github.com/Azure/azure-storage-go"
2424
)
2525

2626
// AzureBlobstoreConfig is an authentication and configuration struct containing
@@ -42,7 +42,6 @@ func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig)
4242
fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
4343
return nil
4444
}
45-
4645
// Create an authenticated client against the Azure cloud
4746
rawClient, err := storage.NewBasicClient(config.Account, config.Token)
4847
if err != nil {
@@ -63,3 +62,50 @@ func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig)
6362
}
6463
return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil)
6564
}
65+
66+
// AzureBlobstoreList lists all the files contained within an azure blobstore.
67+
func AzureBlobstoreList(config AzureBlobstoreConfig) ([]storage.Blob, error) {
68+
// Create an authenticated client against the Azure cloud
69+
rawClient, err := storage.NewBasicClient(config.Account, config.Token)
70+
if err != nil {
71+
return nil, err
72+
}
73+
client := rawClient.GetBlobService()
74+
75+
// List all the blobs from the container and return them
76+
container := client.GetContainerReference(config.Container)
77+
78+
blobs, err := container.ListBlobs(storage.ListBlobsParameters{
79+
MaxResults: 1024 * 1024 * 1024, // Yes, fetch all of them
80+
Timeout: 3600, // Yes, wait for all of them
81+
})
82+
if err != nil {
83+
return nil, err
84+
}
85+
return blobs.Blobs, nil
86+
}
87+
88+
// AzureBlobstoreDelete iterates over a list of files to delete and removes them
89+
// from the blobstore.
90+
func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []storage.Blob) error {
91+
if *DryRunFlag {
92+
for _, blob := range blobs {
93+
fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container)
94+
}
95+
return nil
96+
}
97+
// Create an authenticated client against the Azure cloud
98+
rawClient, err := storage.NewBasicClient(config.Account, config.Token)
99+
if err != nil {
100+
return err
101+
}
102+
client := rawClient.GetBlobService()
103+
104+
// Iterate over the blobs and delete them
105+
for _, blob := range blobs {
106+
if err := client.DeleteBlob(config.Container, blob.Name, nil); err != nil {
107+
return err
108+
}
109+
}
110+
return nil
111+
}

‎internal/build/env.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`)
3131
BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`)
3232
PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
33+
CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
3334
)
3435

3536
// Environment contains metadata provided by the build environment.
@@ -39,6 +40,7 @@ type Environment struct {
3940
Commit, Branch, Tag string // Git info
4041
Buildnum string
4142
IsPullRequest bool
43+
IsCronJob bool
4244
}
4345

4446
func (env Environment) String() string {
@@ -59,6 +61,7 @@ func Env() Environment {
5961
Tag: os.Getenv("TRAVIS_TAG"),
6062
Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"),
6163
IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
64+
IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
6265
}
6366
case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
6467
return Environment{
@@ -69,6 +72,7 @@ func Env() Environment {
6972
Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"),
7073
Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"),
7174
IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
75+
IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
7276
}
7377
default:
7478
return LocalEnv()
@@ -118,5 +122,8 @@ func applyEnvFlags(env Environment) Environment {
118122
if *PullRequestFlag {
119123
env.IsPullRequest = true
120124
}
125+
if *CronJobFlag {
126+
env.IsCronJob = true
127+
}
121128
return env
122129
}

‎vendor/github.com/Azure/azure-sdk-for-go/storage/README.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)