forked from rancher/agent
/
storage_common.go
133 lines (123 loc) · 3.62 KB
/
storage_common.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
package storage
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rancher/agent/core/marshaller"
"github.com/rancher/agent/core/progress"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/config"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
)
func isManagedVolume(volume model.Volume) bool {
driver := volume.Data.Fields.Driver
if driver == "" {
return false
}
if volume.Name == "" {
return false
}
return true
}
func imageBuild(image model.Image, progress *progress.Progress, dockerClient *client.Client) error {
opts := image.Data.Fields.Build
if opts.Context != "" {
file, err := utils.DownloadFile(opts.Context, config.Builds(), nil, "")
if err == nil {
opts.FileObj = file
if buildErr := doBuild(opts, progress, dockerClient); buildErr != nil {
return errors.Wrap(buildErr, constants.ImageBuildError+"failed to build image")
}
}
if file != "" {
// ignore this error because we don't care if that file doesn't exist
os.Remove(file)
}
} else {
remote := opts.Remote
if strings.HasPrefix(utils.InterfaceToString(remote), "git@github.com:") {
remote = strings.Replace(utils.InterfaceToString(remote), "git@github.com:", "git://github.com/", -1)
}
opts.Remote = remote
if buildErr := doBuild(opts, progress, dockerClient); buildErr != nil {
return errors.Wrap(buildErr, constants.ImageBuildError+"failed to build image")
}
}
return nil
}
func doBuild(opts model.BuildOptions, progress *progress.Progress, client *client.Client) error {
remote := opts.Remote
if remote == "" {
remote = opts.Context
}
imageBuildOptions := types.ImageBuildOptions{
RemoteContext: remote,
Remove: true,
Tags: []string{opts.Tag},
}
response, err := client.ImageBuild(context.Background(), nil, imageBuildOptions)
if err != nil {
return errors.Wrap(err, constants.DoBuildError+"failed to build image")
}
defer response.Body.Close()
buffer := utils.ReadBuffer(response.Body)
statusList := strings.Split(buffer, "\r\n")
for _, rawStatus := range statusList {
if rawStatus != "" {
status := marshaller.FromString(rawStatus)
if value, ok := utils.GetFieldsIfExist(status, "stream"); ok {
progress.Update(utils.InterfaceToString(value), "yes", nil)
}
}
}
return nil
}
func isBuild(image model.Image) bool {
build := image.Data.Fields.Build
if build.Context != "" || build.Remote != "" {
return true
}
return false
}
func pathToVolume(volume model.Volume) string {
return strings.Replace(volume.URI, "file://", "", -1)
}
func pullImageWrap(client *client.Client, imageUUID string, opts types.ImagePullOptions, progress *progress.Progress) error {
lastMessage := ""
message := ""
reader, err := client.ImagePull(context.Background(), imageUUID, opts)
if err != nil {
return errors.Wrap(err, "Failed to pull image")
}
defer reader.Close()
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
status := marshaller.FromString(scanner.Text())
if utils.HasKey(status, "error") {
return fmt.Errorf("Image [%s] failed to pull: %s", imageUUID, message)
}
if utils.HasKey(status, "status") {
message = utils.InterfaceToString(status["status"])
}
if lastMessage != message && progress != nil {
progress.Update(message, "yes", nil)
lastMessage = message
}
}
return nil
}
func wrapAuth(auth types.AuthConfig) string {
buf, err := json.Marshal(auth)
if err != nil {
return ""
}
return base64.URLEncoding.EncodeToString(buf)
}