This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
/
image.go
282 lines (242 loc) · 7.92 KB
/
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
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
package image
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"golang.org/x/net/context"
"regexp"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/cli/command/image/build"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/urlutil"
"github.com/docker/docker/registry"
"github.com/maliceio/malice/config"
"github.com/maliceio/malice/malice/docker/client"
er "github.com/maliceio/malice/malice/errors"
log "github.com/Sirupsen/logrus"
)
// Pull pulls docker image:tag
// TODO: add trusted pull for offcial malice plugins
func Pull(docker *client.Docker, id string, tag string) {
responseBody, err := docker.Client.ImagePull(context.Background(), id, types.ImagePullOptions{})
defer responseBody.Close()
er.CheckError(err)
jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, os.Stdout.Fd(), true, nil)
}
// Build builds docker image from git repository
func Build(docker *client.Docker, repository string, tags []string, buildArgs map[string]*string, labels map[string]string, quiet bool) {
var (
buildCtx io.ReadCloser
err error
)
var (
contextDir string
tempDir string
relDockerfile string
progBuff io.Writer
buildBuff io.Writer
)
progBuff = os.Stdout
buildBuff = os.Stdout
switch {
case repository == "-":
buildCtx, relDockerfile, err = build.GetContextFromReader(os.Stdin, "")
case urlutil.IsGitURL(repository):
tempDir, relDockerfile, err = build.GetContextFromGitURL(repository, "")
case urlutil.IsURL(repository):
buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, repository, "")
default:
_, relDockerfile, err = build.GetContextFromLocalDir(repository, "")
}
if tempDir != "" {
defer os.RemoveAll(tempDir)
contextDir = tempDir
}
if buildCtx == nil {
// And canonicalize dockerfile name to a platform-independent one
relDockerfile, err = archive.CanonicalTarNameForPath(relDockerfile)
if err != nil {
log.Fatalf("cannot canonicalize dockerfile path %s: %v", relDockerfile, err)
}
f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
if err != nil && !os.IsNotExist(err) {
er.CheckError(err)
}
defer f.Close()
var excludes []string
if err == nil {
excludes, err = dockerignore.ReadAll(f)
if err != nil {
er.CheckError(err)
}
}
if err := build.ValidateContextDirectory(contextDir, excludes); err != nil {
log.Fatalf("Error checking context: '%s'.", err)
}
// If .dockerignore mentions .dockerignore or the Dockerfile
// then make sure we send both files over to the daemon
// because Dockerfile is, obviously, needed no matter what, and
// .dockerignore is needed to know if either one needs to be
// removed. The daemon will remove them for us, if needed, after it
// parses the Dockerfile. Ignore errors here, as they will have been
// caught by validateContextDirectory above.
var includes = []string{"."}
keepThem1, _ := fileutils.Matches(".dockerignore", excludes)
keepThem2, _ := fileutils.Matches(relDockerfile, excludes)
if keepThem1 || keepThem2 {
includes = append(includes, ".dockerignore", relDockerfile)
}
buildCtx, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
Compression: archive.Uncompressed,
ExcludePatterns: excludes,
IncludeFiles: includes,
})
er.CheckError(err)
}
// Setup an upload progress bar
progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(progBuff, true)
var body io.Reader = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
buildOptions := types.ImageBuildOptions{
Tags: tags,
SuppressOutput: quiet,
// RemoteContext string
NoCache: true,
// Remove bool
// ForceRemove bool
// PullParent bool
// Isolation container.Isolation
// CPUSetCPUs string
// CPUSetMems string
// CPUShares int64
// CPUQuota int64
// CPUPeriod int64
// Memory int64
// MemorySwap int64
// CgroupParent string
// ShmSize int64
Dockerfile: relDockerfile,
// Ulimits []*units.Ulimit
BuildArgs: buildArgs,
// AuthConfigs map[string]AuthConfig
// Context io.Reader
Labels: labels,
}
response, err := docker.Client.ImageBuild(context.Background(), body, buildOptions)
defer response.Body.Close()
er.CheckError(err)
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, os.Stdout.Fd(), true, nil)
if err != nil {
if jerr, ok := err.(*jsonmessage.JSONError); ok {
// If no error code is set, default to 1
if jerr.Code == 0 {
jerr.Code = 1
}
if quiet {
fmt.Fprintf(os.Stderr, "%s%s", progBuff, buildBuff)
}
}
}
}
// Exists returns APIImages images list and true
// if the image name exists, otherwise false.
func Exists(docker *client.Docker, name string) (types.ImageSummary, bool, error) {
log.WithFields(log.Fields{"env": config.Conf.Environment.Run}).Debug("Searching for image: ", name)
images, err := List(docker, name, false)
if err != nil {
return types.ImageSummary{}, false, err
}
r := regexp.MustCompile(name)
if len(images) != 0 {
for _, image := range images {
for _, tag := range image.RepoTags {
if r.MatchString(tag) {
log.WithFields(log.Fields{"env": config.Conf.Environment.Run}).Debug("Image FOUND: ", name)
return image, true, nil
}
}
}
}
log.WithFields(log.Fields{"env": config.Conf.Environment.Run}).Debug("Image NOT Found: ", name)
return types.ImageSummary{}, false, nil
}
// List lists all images
func List(docker *client.Docker, name string, all bool) ([]types.ImageSummary, error) {
options := types.ImageListOptions{
All: all,
// Filters filters.Args
}
imageList, err := docker.Client.ImageList(context.Background(), options)
if err != nil {
log.Error(err)
return nil, err
}
return imageList, nil
}
type searchOptions struct {
term string
noTrunc bool
limit int
filter []string
// Deprecated
stars uint
automated bool
}
// Search searches for malice images
func Search(docker *client.Docker, term string) error {
opts := searchOptions{
term: term,
limit: registry.DefaultSearchLimit,
}
options := types.ImageSearchOptions{
// RegistryAuth: encodedAuth,
// PrivilegeFunc: requestPrivilege,
// Filters: searchFilters,
Limit: opts.limit,
}
unorderedResults, err := docker.Client.ImageSearch(context.Background(), opts.term, options)
if err != nil {
return err
}
results := searchResultsByStars(unorderedResults)
sort.Sort(results)
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
for _, res := range results {
// --automated and -s, --stars are deprecated since Docker 1.12
if (opts.automated && !res.IsAutomated) || (int(opts.stars) > res.StarCount) {
continue
}
desc := strings.Replace(res.Description, "\n", " ", -1)
desc = strings.Replace(desc, "\r", " ", -1)
if !opts.noTrunc && len(desc) > 45 {
desc = stringutils.Truncate(desc, 42) + "..."
}
fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
if res.IsOfficial {
fmt.Fprint(w, "[OK]")
}
fmt.Fprint(w, "\t")
if res.IsAutomated {
fmt.Fprint(w, "[OK]")
}
fmt.Fprint(w, "\n")
}
w.Flush()
return nil
}
// SearchResultsByStars sorts search results in descending order by number of stars.
type searchResultsByStars []registrytypes.SearchResult
func (r searchResultsByStars) Len() int { return len(r) }
func (r searchResultsByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r searchResultsByStars) Less(i, j int) bool { return r[j].StarCount < r[i].StarCount }