github.com/timstclair/heapster@v0.20.0-alpha1/integration/utils.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 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 implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package integration 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "os/exec" 22 "path" 23 ) 24 25 func buildDockerImage(imageName string) error { 26 out, err := exec.Command("./build.sh", imageName).CombinedOutput() 27 if err != nil { 28 return fmt.Errorf("failed to build docker binary (%q) - %q", err, out) 29 } 30 31 return nil 32 } 33 34 func copyDockerImage(imageName, hostname, zone string) error { 35 tempfile, err := ioutil.TempFile("", hostname) 36 if err != nil { 37 return err 38 } 39 defer os.Remove(tempfile.Name()) 40 out, err := exec.Command("docker", "save", "-o", tempfile.Name(), imageName).CombinedOutput() 41 if err != nil { 42 return fmt.Errorf("failed to save docker binary (%q) - %q", err, out) 43 } 44 remoteFile := path.Join("/tmp", path.Base(tempfile.Name())) 45 out, err = exec.Command("gcloud", "compute", "copy-files", "--zone", zone, tempfile.Name(), fmt.Sprintf("%s:%s", hostname, remoteFile)).CombinedOutput() 46 if err != nil { 47 return fmt.Errorf("failed to push docker binary to %q (%q) - %q", hostname, err, out) 48 } 49 out, err = exec.Command("gcloud", "compute", "ssh", "--zone", zone, hostname, "--command", fmt.Sprintf("sudo docker load -i %s", remoteFile)).CombinedOutput() 50 if err != nil { 51 err = fmt.Errorf("failed to load docker image %q using temp file %q on host %q (%q) - %q", imageName, remoteFile, hostname, err, out) 52 } 53 out, rmErr := exec.Command("gcloud", "compute", "ssh", "--zone", zone, hostname, "--command", fmt.Sprintf("sudo rm -f %s", remoteFile)).CombinedOutput() 54 if rmErr != nil { 55 if err != nil { 56 rmErr = fmt.Errorf("%v\nfailed to remove tempfile on host %q (%q) - %q", err, hostname, err, out) 57 } 58 return rmErr 59 } 60 return err 61 } 62 63 func removeDockerImage(imageName string) error { 64 out, err := exec.Command("docker", "rmi", "-f", imageName).CombinedOutput() 65 if err != nil { 66 return fmt.Errorf("failed to remove docker image %q (%q) - %q", imageName, err, out) 67 } 68 return nil 69 } 70 71 func cleanupRemoteHost(hostname, zone string) { 72 _ = exec.Command("gcloud", "compute", "ssh", "--zone", zone, hostname, "--command", "\"sudo docker rm `docker ps -a -q`\"") 73 _ = exec.Command("gcloud", "compute", "ssh", "--zone", zone, hostname, "--command", "\"sudo docker rmi `docker images -a -q`\"") 74 }