-
Notifications
You must be signed in to change notification settings - Fork 26
/
remove.go
81 lines (70 loc) · 1.95 KB
/
remove.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func removeOldVersions(runner *Environ, workspace string, vargs GAE) error {
// read in the app.yaml file to grab the module/service mame we just deployed
appLoc := filepath.Join(workspace, vargs.Dir, "app.yaml")
appFile, err := os.Open(appLoc)
if err != nil {
return fmt.Errorf("error: %s\n", err)
}
defer appFile.Close()
var appStruct struct {
Service string `yaml:"service"`
Module string `yaml:"module"`
}
err = yaml.NewDecoder(appFile).Decode(&appStruct)
if err != nil {
return fmt.Errorf("error: %s\n", err)
}
service := appStruct.Service
if service == "" {
service = appStruct.Module
}
// look up existing versions for given service ordered by create time desc
var versionJSON bytes.Buffer
sout := runner.stdout
runner.stdout = &versionJSON
err = runner.Run(vargs.GCloudCmd, "app", "versions", "list",
"--service", service, "--project", vargs.Project,
"--format", "json", "--sort-by", "~version.createTime", "--quiet")
if err != nil {
return fmt.Errorf("error: %s\n", err)
}
var results []struct {
ID string `json:"id"`
TrafficSplit float64 `json:"traffic_split"`
}
err = json.NewDecoder(&versionJSON).Decode(&results)
if err != nil {
return err
}
var toDelete []string
for i, res := range results {
// keep newer versions, the newly deployed version or anything that has traffic
if i < vargs.MaxVersions || res.ID == vargs.Version || res.TrafficSplit > 0 {
continue
}
toDelete = append(toDelete, res.ID)
}
if len(toDelete) == 0 {
return nil
}
log.Printf("deleting %d versions: %s", len(toDelete), toDelete)
runner.stdout = sout
args := []string{"app", "versions", "delete",
"--service", service, "--project", vargs.Project, "--quiet"}
args = append(args, toDelete...)
err = runner.Run(vargs.GCloudCmd, args...)
if err != nil {
return fmt.Errorf("error: %s\n", err)
}
return nil
}