|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package binremote |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/golang/glog" |
| 27 | +) |
| 28 | + |
| 29 | +func CreateDriverArchive(archiveName, pkgPath, binPath string) (string, error) { |
| 30 | + glog.V(2).Infof("Building archive...") |
| 31 | + tarDir, err := ioutil.TempDir("", "driver-temp-archive") |
| 32 | + if err != nil { |
| 33 | + return "", fmt.Errorf("failed to create temporary directory %v", err) |
| 34 | + } |
| 35 | + defer os.RemoveAll(tarDir) |
| 36 | + |
| 37 | + // Call the suite function to setup the test package. |
| 38 | + err = setupBinaries(tarDir, pkgPath, binPath) |
| 39 | + if err != nil { |
| 40 | + return "", fmt.Errorf("failed to setup test package %q: %v", tarDir, err) |
| 41 | + } |
| 42 | + |
| 43 | + // Build the tar |
| 44 | + out, err := exec.Command("tar", "-zcvf", archiveName, "-C", tarDir, ".").CombinedOutput() |
| 45 | + if err != nil { |
| 46 | + return "", fmt.Errorf("failed to build tar %v. Output:\n%s", err, out) |
| 47 | + } |
| 48 | + |
| 49 | + dir, err := os.Getwd() |
| 50 | + if err != nil { |
| 51 | + return "", fmt.Errorf("failed to get working directory %v", err) |
| 52 | + } |
| 53 | + return filepath.Join(dir, archiveName), nil |
| 54 | +} |
| 55 | + |
| 56 | +func setupBinaries(tarDir, pkgPath, binPath string) error { |
| 57 | + glog.V(4).Infof("Making binaries and copying to temp dir...") |
| 58 | + out, err := exec.Command("make", "-C", pkgPath).CombinedOutput() |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("Failed to make at %s: %v: %v", pkgPath, string(out), err) |
| 61 | + } |
| 62 | + |
| 63 | + // Copy binaries |
| 64 | + if _, err := os.Stat(binPath); err != nil { |
| 65 | + return fmt.Errorf("failed to locate test binary %s: %v", binPath, err) |
| 66 | + } |
| 67 | + out, err = exec.Command("cp", binPath, tarDir).CombinedOutput() |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to copy %q: %v Output: %q", binPath, err, out) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
0 commit comments