forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_utils.go
348 lines (317 loc) · 9.31 KB
/
common_utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/test/env"
)
const (
releaseURL = "https://github.com/istio/istio/releases/download/%s/istio-%s-%s.tar.gz"
)
// GetHeadCommitSHA finds the SHA of the commit to which the HEAD of branch points
func GetHeadCommitSHA(org, repo, branch string) (string, error) {
client := github.NewClient(nil)
githubRefObj, _, err := client.Git.GetRef(
context.Background(), org, repo, "refs/heads/"+branch)
if err != nil {
log.Warnf("Failed to get reference SHA of branch [%s]on repo [%s]\n", branch, repo)
return "", err
}
return *githubRefObj.Object.SHA, nil
}
// WriteTextFile overwrites the file on the given path with content
func WriteTextFile(filePath, content string) error {
if len(content) > 0 && content[len(content)-1] != '\n' {
content += "\n"
}
return ioutil.WriteFile(filePath, []byte(content), 0600)
}
// GitRootDir returns the absolute path to the root directory of the git repo
// where this function is called
func GitRootDir() (string, error) {
dir, err := Shell("git rev-parse --show-toplevel")
if err != nil {
return "", err
}
return strings.Trim(dir, "\n"), nil
}
// Poll executes do() after time interval for a max of numTrials times.
// The bool returned by do() indicates if polling succeeds in that trial
func Poll(interval time.Duration, numTrials int, do func() (bool, error)) error {
if numTrials < 0 {
return fmt.Errorf("numTrials cannot be negative")
}
for i := 0; i < numTrials; i++ {
if success, err := do(); err != nil {
return fmt.Errorf("error during trial %d: %v", i, err)
} else if success {
return nil
} else {
time.Sleep(interval)
}
}
return fmt.Errorf("max polling iteration reached")
}
// CreateTempfile creates a tempfile string.
func CreateTempfile(tmpDir, prefix, suffix string) (string, error) {
f, err := ioutil.TempFile(tmpDir, prefix)
if err != nil {
return "", err
}
var tmpName string
if tmpName, err = filepath.Abs(f.Name()); err != nil {
return "", err
}
if err = f.Close(); err != nil {
return "", err
}
if err = os.Remove(tmpName); err != nil {
log.Errorf("CreateTempfile unable to remove %s", tmpName)
return "", err
}
return tmpName + suffix, nil
}
// WriteTempfile creates a tempfile with the specified contents.
func WriteTempfile(tmpDir, prefix, suffix, contents string) (string, error) {
fname, err := CreateTempfile(tmpDir, prefix, suffix)
if err != nil {
return "", err
}
if err := ioutil.WriteFile(fname, []byte(contents), 0644); err != nil {
return "", err
}
return fname, nil
}
// Shell run command on shell and get back output and error if get one
func Shell(format string, args ...interface{}) (string, error) {
return sh(context.Background(), format, true, true, true, args...)
}
// ShellContext run command on shell and get back output and error if get one
func ShellContext(ctx context.Context, format string, args ...interface{}) (string, error) {
return sh(ctx, format, true, true, true, args...)
}
// ShellMuteOutput run command on shell and get back output and error if get one
// without logging the output
func ShellMuteOutput(format string, args ...interface{}) (string, error) {
return sh(context.Background(), format, true, false, true, args...)
}
// ShellMuteOutput run command on shell and get back output and error if get one
// without logging the output or errors
func ShellMuteOutputError(format string, args ...interface{}) (string, error) {
return sh(context.Background(), format, true, false, false, args...)
}
// ShellSilent runs command on shell and get back output and error if get one
// without logging the command or output.
func ShellSilent(format string, args ...interface{}) (string, error) {
return sh(context.Background(), format, false, false, false, args...)
}
func sh(ctx context.Context, format string, logCommand, logOutput, logError bool, args ...interface{}) (string, error) {
command := fmt.Sprintf(format, args...)
if logCommand {
log.Infof("Running command %s", command)
}
c := exec.CommandContext(ctx, "sh", "-c", command) // #nosec
bytes, err := c.CombinedOutput()
if logOutput {
if output := strings.TrimSuffix(string(bytes), "\n"); len(output) > 0 {
log.Infof("Command output: \n%s", output)
}
}
if err != nil {
if logError {
log.Infof("Command error: %v", err)
}
return string(bytes), fmt.Errorf("command failed: %q %v", string(bytes), err)
}
return string(bytes), nil
}
// RunBackground starts a background process and return the Process if succeed
func RunBackground(format string, args ...interface{}) (*os.Process, error) {
command := fmt.Sprintf(format, args...)
log.Infoa("RunBackground: ", command)
parts := strings.Split(command, " ")
c := exec.Command(parts[0], parts[1:]...) // #nosec
err := c.Start()
if err != nil {
log.Errorf("%s, command failed!", command)
return nil, err
}
return c.Process, nil
}
// Record run command and record output into a file
func Record(command, record string) error {
resp, err := Shell(command)
if err != nil {
return err
}
err = ioutil.WriteFile(record, []byte(resp), 0600)
return err
}
// HTTPDownload download from src(url) and store into dst(local file)
func HTTPDownload(dst string, src string) error {
log.Infof("Start downloading from %s to %s ...\n", src, dst)
var err error
var out *os.File
var resp *http.Response
out, err = os.Create(dst)
if err != nil {
return err
}
defer func() {
if err = out.Close(); err != nil {
log.Errorf("Error: close file %s, %s", dst, err)
}
}()
resp, err = http.Get(src)
if err != nil {
return err
}
defer func() {
if err = resp.Body.Close(); err != nil {
log.Errorf("Error: close downloaded file from %s, %s", src, err)
}
}()
if resp.StatusCode != 200 {
return fmt.Errorf("http get request, received unexpected response status: %s", resp.Status)
}
if _, err = io.Copy(out, resp.Body); err != nil {
return err
}
log.Info("Download successfully!")
return err
}
// GetOsExt returns the current OS tag.
func GetOsExt() (string, error) {
var osExt string
switch runtime.GOOS {
case "linux":
osExt = "linux"
case "darwin":
osExt = "osx"
default:
return "", fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
return osExt, nil
}
// CopyFile create a new file to src based on dst
func CopyFile(src, dst string) error {
var in, out *os.File
var err error
in, err = os.Open(src)
if err != nil {
return err
}
defer func() {
if err = in.Close(); err != nil {
log.Errorf("Error: close file from %s, %s", src, err)
}
}()
out, err = os.Create(dst)
if err != nil {
return err
}
defer func() {
if err = out.Close(); err != nil {
log.Errorf("Error: close file from %s, %s", dst, err)
}
}()
if _, err = io.Copy(out, in); err != nil {
return err
}
err = out.Sync()
return err
}
// GetResourcePath give "path from WORKSPACE", return absolute path at runtime
func GetResourcePath(p string) string {
return filepath.Join(env.IstioSrc, p)
}
// DownloadRelease gets the specified release from istio repo to tmpDir.
func DownloadRelease(version, tmpDir string) (string, error) {
err := os.Chdir(tmpDir)
if err != nil {
return "", err
}
osExt, err := GetOsExt()
if err != nil {
return "", err
}
url := fmt.Sprintf(releaseURL, version, version, osExt)
fname := fmt.Sprintf("istio-%s.tar.gz", version)
tgz := filepath.Join(tmpDir, fname)
err = HTTPDownload(tgz, url)
if err != nil {
return "", err
}
f, err := os.Open(tgz)
if err != nil {
return "", err
}
err = ExtractTarGz(f)
if err != nil {
return "", err
}
subdir := "istio-" + version
return filepath.Join(tmpDir, subdir), nil
}
// ExtractTarGz extracts a .tar.gz file into current dir.
func ExtractTarGz(gzipStream io.Reader) error {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return errors.Wrap(err, "Fail to uncompress")
}
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrap(err, "ExtractTarGz: Next() failed")
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(header.Name, 0755); err != nil {
return errors.Wrap(err, "ExtractTarGz: Mkdir() failed")
}
case tar.TypeReg:
outFile, err := os.Create(header.Name)
if err != nil {
return errors.Wrap(err, "ExtractTarGz: Create() failed")
}
defer outFile.Close() // nolint: errcheck
if _, err := io.Copy(outFile, tarReader); err != nil {
return errors.Wrap(err, "ExtractTarGz: Copy() failed")
}
default:
return fmt.Errorf("unknown type: %s in %s",
string(header.Typeflag), header.Name)
}
}
return nil
}