-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_image.go
71 lines (57 loc) · 1.74 KB
/
load_image.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
package nodetasks
import (
"fmt"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/cloudinit"
"k8s.io/kops/upup/pkg/fi/nodeup/local"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kops/util/pkg/hashing"
"os/exec"
"path"
"strings"
)
// LoadImageTask is responsible for downloading a docker image
type LoadImageTask struct {
Source string
Hash string
}
var _ fi.Task = &LoadImageTask{}
func (t *LoadImageTask) String() string {
return fmt.Sprintf("LoadImageTask: %s", t.Source)
}
func (e *LoadImageTask) Find(c *fi.Context) (*LoadImageTask, error) {
glog.Warningf("LoadImageTask checking if image present not yet implemented")
return nil, nil
}
func (e *LoadImageTask) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *LoadImageTask) CheckChanges(a, e, changes *LoadImageTask) error {
return nil
}
func (_ *LoadImageTask) RenderLocal(t *local.LocalTarget, a, e, changes *LoadImageTask) error {
hash, err := hashing.FromString(e.Hash)
if err != nil {
return err
}
url := e.Source
localFile := path.Join(t.CacheDir, hash.String()+"_"+utils.SanitizeString(url))
_, err = fi.DownloadURL(url, localFile, hash)
if err != nil {
return err
}
// Load the image into docker
args := []string{"docker", "load", "-i", localFile}
human := strings.Join(args, " ")
glog.Infof("running command %s", human)
cmd := exec.Command(args[0], args[1:]...)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error loading docker image with '%s': %v: %s", human, err, string(output))
}
return nil
}
func (_ *LoadImageTask) RenderCloudInit(t *cloudinit.CloudInitTarget, a, e, changes *LoadImageTask) error {
return fmt.Errorf("LoadImageTask::RenderCloudInit not implemented")
}