-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathteamcity.go
114 lines (101 loc) · 3.34 KB
/
teamcity.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package upgrade
import (
"context"
"fmt"
"io"
"net/http"
"os"
"runtime"
"time"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
)
var (
buildIDs = map[string]string{
"linux-amd64": "Cockroach_ScratchProjectPutTcExperimentsInHere_BazelBuild",
"linux-arm64": "Cockroach_UnitTests_BazelBuildLinuxArmCross",
"darwin-amd64": "Cockroach_UnitTests_BazelBuildMacOSCross",
"darwin-arm64": "Cockroach_Ci_Builds_BuildMacOSArm64",
"windows-amd64": "Cockroach_UnitTests_BazelBuildWindowsCross",
}
buildType = buildIDs[runtime.GOOS+"-"+runtime.GOARCH]
apiBase = "https://teamcity.cockroachdb.com/guestAuth/app/rest"
)
// DownloadLatestRoadprod attempts to download the latest binary to the
// current binary's directory. It returns the path to the downloaded binary.
// toFile is the path to the file to download to.
func DownloadLatestRoadprod(toFile string) error {
if buildType == "" {
fmt.Println("Supported platforms:")
for k := range buildIDs {
fmt.Printf("\t%s\n", k)
}
return fmt.Errorf("unable to find build type for this platform")
}
// Build are sorted by build date desc, so limiting to 1 will get the latest.
builds, err := getBuilds("count:1,status:SUCCESS,branch:master,buildType:" + buildType)
if err != nil {
return err
}
if len(builds.Build) == 0 {
return fmt.Errorf("no builds found")
}
err = downloadRoachprod(builds.Build[0].Id, toFile)
if err != nil {
return err
}
return nil
}
// getBuilds returns a list of builds matching the locator
// See https://www.jetbrains.com/help/teamcity/rest/buildlocator.html
func getBuilds(locator string) (TCBuildResponse, error) {
urlWithLocator := fmt.Sprintf("%s/builds?locator=%s", apiBase, locator)
buildResp := &TCBuildResponse{}
err := httputil.GetJSONWithOptions(*httputil.DefaultClient.Client, urlWithLocator, buildResp,
httputil.IgnoreUnknownFields())
return *buildResp, err
}
// downloadRoachprod downloads the roachprod binary from the build
// to the specified destination file.
func downloadRoachprod(buildID int32, destFile string) error {
if buildID <= 0 {
return fmt.Errorf("invalid build id: %v", buildID)
}
out, err := os.Create(destFile)
if err != nil {
return err
}
defer out.Close()
url := roachprodDownloadURL(buildID)
fmt.Printf("Downloading latest roachprod \n\tfrom:\t%s \n\tto :\t%s\n", url, destFile)
// Set a long timeout here because the download can take a while.
httpClient := httputil.NewClientWithTimeouts(httputil.StandardHTTPTimeout, 10*time.Minute)
resp, err := httpClient.Get(context.Background(), url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status downloading roachprod: %s", resp.Status)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func roachprodDownloadURL(buildID int32) string {
url := fmt.Sprintf("%s%s",
apiBase,
fmt.Sprintf("/builds/id:%v/artifacts/content/bazel-bin/pkg/cmd/roachprod/roachprod_/roachprod", buildID),
)
return url
}