Debian Codegraph @

testserver/binaries.go

raw symbols SCIP
1 // Copyright 2017 The Cockroach Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 // implied. See the License for the specific language governing
13 // permissions and limitations under the License.
14
15 package testserver
16
17 import (
18 "fmt"
19 "io"
20 "log"
21 "mime"
22 "net/http"
23 "net/url"
24 "os"
25 "os/exec"
26 "path"
27 "path/filepath"
28 "regexp"
29 "runtime"
30 "time"
31 )
32
33 const (
34 latestSuffix = "LATEST"
35 finishedFileMode = 0555
36 )
37
38 func downloadFile(response *http.Response, filePath string) error {
39 output, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0200)
40 if err != nil {
41 return fmt.Errorf("error creating %s: %s", filePath, err)
42 }
43 defer func() { _ = output.Close() }()
44
45 log.Printf("saving %s to %s, this may take some time", response.Request.URL, filePath)
46
47 if _, err := io.Copy(output, response.Body); err != nil {
48 return fmt.Errorf("problem saving %s to %s: %s", response.Request.URL, filePath, err)
49 }
50
51 // Download was successful, add the rw bits.
52 if err := output.Chmod(finishedFileMode); err != nil {
53 return err
54 }
55
56 // We explicitly close here to ensure the error is checked; the deferred
57 // close above will likely error in this case, but that's harmless.
58 return output.Close()
59 }
60
61 var muslRE = regexp.MustCompile(`(?i)\bmusl\b`)
62
63 func downloadLatestBinary() (string, error) {
64 goos := runtime.GOOS
65 if goos == "linux" {
66 goos += func() string {
67 // Detect which C library is present on the system. See
68 // https://unix.stackexchange.com/a/120381.
69 cmd := exec.Command("ldd", "--version")
70 out, err := cmd.Output()
71 if err != nil {
72 log.Printf("%s: out=%q err=%s", cmd.Args, out, err)
73 } else if muslRE.Match(out) {
74 return "-musl"
75 }
76 return "-gnu"
77 }()
78 }
79 binaryName := fmt.Sprintf("cockroach.%s-%s", goos, runtime.GOARCH)
80 if runtime.GOOS == "windows" {
81 binaryName += ".exe"
82 }
83 url := &url.URL{
84 Scheme: "https",
85 Host: "edge-binaries.cockroachdb.com",
86 Path: path.Join("cockroach", fmt.Sprintf("%s.%s", binaryName, latestSuffix)),
87 }
88 log.Printf("GET %s", url)
89 response, err := http.Get(url.String())
90 if err != nil {
91 return "", err
92 }
93 defer func() { _ = response.Body.Close() }()
94
95 if response.StatusCode != 200 {
96 return "", fmt.Errorf("error downloading %s: %d (%s)", url, response.StatusCode, response.Status)
97 }
98
99 const contentDisposition = "Content-Disposition"
100 _, disposition, err := mime.ParseMediaType(response.Header.Get(contentDisposition))
101 if err != nil {
102 return "", fmt.Errorf("error parsing %s headers %s: %s", contentDisposition, response.Header, err)
103 }
104
105 filename, ok := disposition["filename"]
106 if !ok {
107 return "", fmt.Errorf("content disposition header %s did not contain filename", disposition)
108 }
109 localFile := filepath.Join(os.TempDir(), filename)
110 for {
111 info, err := os.Stat(localFile)
112 if os.IsNotExist(err) {
113 // File does not exist: download it.
114 break
115 }
116 if err != nil {
117 return "", err
118 }
119 // File already present: check mode.
120 if info.Mode().Perm() == finishedFileMode {
121 return localFile, nil
122 }
123 log.Printf("waiting for download of %s", localFile)
124 time.Sleep(time.Millisecond * 10)
125 }
126
127 if err := downloadFile(response, localFile); err != nil {
128 if err := os.Remove(localFile); err != nil {
129 log.Printf("failed to remove %s: %s", localFile, err)
130 }
131 return "", err
132 }
133
134 return localFile, nil
135 }