forked from box-builder/box
/
copy.go
176 lines (142 loc) · 3.85 KB
/
copy.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
package builder
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/erikh/box/tar"
"github.com/erikh/box/util"
mruby "github.com/mitchellh/go-mruby"
)
func parseCopyArgs(args []*mruby.MrbValue) (string, string, []string, error) {
var source, target string
ignoreList := []string{}
for _, arg := range args {
switch arg.Type() {
case mruby.TypeString:
if source != "" {
if target != "" {
return "", "", nil, errors.New("too many arguments in copy")
}
target = arg.String()
continue
}
source = arg.String()
case mruby.TypeHash:
hash, err := coerceHash(arg.Hash())
if err != nil {
return "", "", nil, err
}
if _, ok := hash["ignore_list"]; ok {
list, err := util.InterfaceListToString(hash["ignore_list"])
if err != nil {
return "", "", nil, err
}
ignoreList = append(ignoreList, list...)
}
file, ok := hash["ignore_file"].(string)
if ok {
lines, err := util.ReadLines(file)
if err != nil {
return "", "", nil, err
}
ignoreList = append(ignoreList, lines...)
}
}
}
return source, target, ignoreList, nil
}
func checkCopyArgs(b *Builder, args []*mruby.MrbValue) (string, string, []string, error) {
if err := checkImage(b); err != nil {
return "", "", nil, err
}
source, target, ignoreList, err := parseCopyArgs(args)
if err != nil {
return "", "", nil, err
}
var rel string
relfiles, err := filepath.Glob(source)
if err != nil || len(relfiles) == 1 {
source, err = filepath.Abs(source)
if err != nil {
return "", "", nil, err
}
wd, err := os.Getwd()
if err != nil {
return "", "", nil, err
}
rel, err = filepath.Rel(wd, source)
if err != nil {
return "", "", nil, err
}
if strings.HasPrefix(rel, "..") {
return "", "", nil, fmt.Errorf("cannot use relative path %s because it may fall below the root build directory", source)
}
} else {
rel = source
}
workdir := b.exec.Config().WorkDir
var targetWd string
if workdir.Temporary == "" {
targetWd = workdir.Image
} else {
targetWd = workdir.Temporary
}
// special case `.`
if target == "." && len(relfiles) == 1 {
target = filepath.Join(targetWd, rel)
} else {
if !strings.HasPrefix(target, "/") {
target = filepath.Join(targetWd, target)
}
}
return filepath.Clean(rel), target, ignoreList, nil
}
func doCopy(b *Builder, cacheKey string, args []*mruby.MrbValue, m *mruby.Mrb, self *mruby.MrbValue) (mruby.Value, mruby.Value) {
rel, target, ignoreList, err := checkCopyArgs(b, args)
if err != nil {
return nil, createException(m, err.Error())
}
list, err := util.ReadLines(".dockerignore")
if os.IsNotExist(err) {
list = []string{}
} else if err != nil {
return nil, createException(m, err.Error())
}
ignoreList = append(ignoreList, list...)
// XXX for if we ever add volume support back
for _, volume := range b.exec.Config().Volumes {
if strings.HasPrefix(target, volume) {
return nil, createException(m, fmt.Sprintf("Volume %q cannot be copied into (you tried %q). This is caused by a bug in docker. We are working with docker on a fix.", volume, target))
}
}
fn, cacheKey, err := tar.Archive(b.config.Context, rel, target, ignoreList, b.Logger)
if err != nil {
return nil, createException(m, err.Error())
}
defer os.Remove(fn)
cacheKey = fmt.Sprintf("box:copy %s", cacheKey)
if b.exec.Image().GetCache() {
cached, err := b.exec.Image().CheckCache(cacheKey)
if err != nil {
return nil, createException(m, err.Error())
}
if cached {
return nil, nil
}
}
f, err := os.Open(fn)
if err != nil {
return nil, createException(m, err.Error())
}
defer f.Close()
hook := func(ctx context.Context, id string) (string, error) {
return "", b.exec.CopyToContainer(id, f)
}
if err := b.exec.Commit(cacheKey, hook); err != nil {
return nil, createException(m, err.Error())
}
return nil, nil
}