forked from buildpacks/pack
/
container_ops.go
249 lines (212 loc) · 7.25 KB
/
container_ops.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
package build
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"github.com/BurntSushi/toml"
"github.com/docker/docker/api/types"
dcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/YousefHaggyHeroku/pack/internal/paths"
"github.com/YousefHaggyHeroku/pack/internal/archive"
"github.com/YousefHaggyHeroku/pack/internal/builder"
"github.com/YousefHaggyHeroku/pack/internal/container"
)
type ContainerOperation func(ctrClient client.CommonAPIClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error
// CopyDir copies a local directory (src) to the destination on the container while filtering files and changing it's UID/GID.
func CopyDir(src, dst string, uid, gid int, os string, fileFilter func(string) bool) ContainerOperation {
return func(ctrClient client.CommonAPIClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
tarPath := dst
if os == "windows" {
tarPath = paths.WindowsToSlash(dst)
}
reader, err := createReader(src, tarPath, uid, gid, fileFilter)
if err != nil {
return errors.Wrapf(err, "create tar archive from '%s'", src)
}
defer reader.Close()
if os == "windows" {
return copyDirWindows(ctx, ctrClient, containerID, reader, dst, stdout, stderr)
}
return copyDir(ctx, ctrClient, containerID, reader)
}
}
func copyDir(ctx context.Context, ctrClient client.CommonAPIClient, containerID string, appReader io.Reader) error {
var clientErr, err error
doneChan := make(chan interface{})
pr, pw := io.Pipe()
go func() {
clientErr = ctrClient.CopyToContainer(ctx, containerID, "/", pr, types.CopyToContainerOptions{})
close(doneChan)
}()
func() {
defer pw.Close()
_, err = io.Copy(pw, appReader)
}()
<-doneChan
if err == nil {
err = clientErr
}
return err
}
// copyDirWindows provides an alternate, Windows container-specific implementation of copyDir.
// This implementation is needed because copying directly to a mounted volume is currently buggy
// for Windows containers and does not work. Instead, we perform the copy from inside a container
// using xcopy.
// See: https://github.com/moby/moby/issues/40771
func copyDirWindows(ctx context.Context, ctrClient client.CommonAPIClient, containerID string, reader io.Reader, dst string, stdout, stderr io.Writer) error {
info, err := ctrClient.ContainerInspect(ctx, containerID)
if err != nil {
return err
}
baseName := paths.WindowsBasename(dst)
mnt, err := findMount(info, dst)
if err != nil {
return err
}
ctr, err := ctrClient.ContainerCreate(ctx,
&dcontainer.Config{
Image: info.Image,
Cmd: []string{
"cmd",
"/c",
//xcopy args
// e - recursively create subdirectories
// h - copy hidden and system files
// b - copy symlinks, do not dereference
// x - copy attributes
// y - suppress prompting
fmt.Sprintf(`xcopy c:\windows\%s %s /e /h /b /x /y`, baseName, dst),
},
WorkingDir: "/",
User: windowsContainerAdmin,
},
&dcontainer.HostConfig{
Binds: []string{fmt.Sprintf("%s:%s", mnt.Name, mnt.Destination)},
Isolation: dcontainer.IsolationProcess,
},
nil, "",
)
if err != nil {
return errors.Wrapf(err, "creating prep container")
}
defer ctrClient.ContainerRemove(context.Background(), ctr.ID, types.ContainerRemoveOptions{Force: true})
err = ctrClient.CopyToContainer(ctx, ctr.ID, "/windows", reader, types.CopyToContainerOptions{})
if err != nil {
return errors.Wrap(err, "copy app to container")
}
return container.Run(
ctx,
ctrClient,
ctr.ID,
ioutil.Discard, // Suppress xcopy output
stderr,
)
}
func findMount(info types.ContainerJSON, dst string) (types.MountPoint, error) {
for _, m := range info.Mounts {
if m.Destination == dst {
return m, nil
}
}
return types.MountPoint{}, fmt.Errorf("no matching mount found for %s", dst)
}
// WriteStackToml writes a `stack.toml` based on the StackMetadata provided to the destination path.
func WriteStackToml(dstPath string, stack builder.StackMetadata, os string) ContainerOperation {
return func(ctrClient client.CommonAPIClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
buf := &bytes.Buffer{}
err := toml.NewEncoder(buf).Encode(stack)
if err != nil {
return errors.Wrap(err, "marshaling stack metadata")
}
tarBuilder := archive.TarBuilder{}
tarPath := dstPath
if os == "windows" {
tarPath = paths.WindowsToSlash(dstPath)
}
tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes())
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
defer reader.Close()
if os == "windows" {
dirName := paths.WindowsDir(dstPath)
return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr)
}
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
}
}
func createReader(src, dst string, uid, gid int, fileFilter func(string) bool) (io.ReadCloser, error) {
fi, err := os.Stat(src)
if err != nil {
return nil, err
}
if fi.IsDir() {
var mode int64 = -1
if runtime.GOOS == "windows" {
mode = 0777
}
return archive.ReadDirAsTar(src, dst, uid, gid, mode, false, fileFilter), nil
}
return archive.ReadZipAsTar(src, dst, uid, gid, -1, false, fileFilter), nil
}
//EnsureVolumeAccess grants full access permissions to volumes for UID/GID-based user
//When UID/GID are 0 it grants explicit full access to BUILTIN\Administrators and any other UID/GID grants full access to BUILTIN\Users
//Changing permissions on volumes through stopped containers does not work on Docker for Windows so we start the container and make change using icacls
//See: https://github.com/moby/moby/issues/40771
func EnsureVolumeAccess(uid, gid int, os string, volumeNames ...string) ContainerOperation {
return func(ctrClient client.CommonAPIClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
if os != "windows" {
return nil
}
containerInfo, err := ctrClient.ContainerInspect(ctx, containerID)
if err != nil {
return err
}
cmd := ""
binds := []string{}
for i, volumeName := range volumeNames {
containerPath := fmt.Sprintf("c:/volume-mnt-%d", i)
binds = append(binds, fmt.Sprintf("%s:%s", volumeName, containerPath))
if cmd != "" {
cmd += "&&"
}
//icacls args
// /grant - add new permissions instead of replacing
// (OI) - object inherit
// (CI) - container inherit
// F - full access
// /t - recursively apply
// /l - perform on a symbolic link itself versus its target
// /q - suppress success messages
cmd += fmt.Sprintf(`icacls %s /grant *%s:(OI)(CI)F /t /l /q`, containerPath, paths.WindowsPathSID(uid, gid))
}
ctr, err := ctrClient.ContainerCreate(ctx,
&dcontainer.Config{
Image: containerInfo.Image,
Cmd: []string{"cmd", "/c", cmd},
WorkingDir: "/",
User: windowsContainerAdmin,
},
&dcontainer.HostConfig{
Binds: binds,
Isolation: dcontainer.IsolationProcess,
},
nil, "",
)
if err != nil {
return err
}
defer ctrClient.ContainerRemove(context.Background(), ctr.ID, types.ContainerRemoveOptions{Force: true})
return container.Run(
ctx,
ctrClient,
ctr.ID,
ioutil.Discard, // Suppress icacls output
stderr,
)
}
}