-
Notifications
You must be signed in to change notification settings - Fork 393
/
runmount.go
215 lines (208 loc) · 6.4 KB
/
runmount.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
package earthfile2llb
import (
"crypto/sha256"
"encoding/hex"
"os"
"path"
"strconv"
"strings"
"github.com/earthly/earthly/domain"
"github.com/earthly/earthly/util/llbutil/pllb"
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
)
func (c *Converter) parseMounts(mounts []string) ([]llb.RunOption, error) {
var runOpts []llb.RunOption
for _, mount := range mounts {
mountRunOpts, err := c.parseMount(mount)
if err != nil {
return nil, errors.Wrap(err, "parse mount")
}
runOpts = append(runOpts, mountRunOpts...)
}
return runOpts, nil
}
func (c *Converter) parseMount(mount string) ([]llb.RunOption, error) {
var state pllb.State
var mountSource string
var mountTarget string
var mountID string
var mountType string
var mountMode int
var mountOpts []llb.MountOption
sharingMode := llb.CacheMountLocked
kvPairs := strings.Split(mount, ",")
for _, kvPair := range kvPairs {
kvSplit := strings.SplitN(kvPair, "=", 2)
if len(kvSplit) == 0 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
switch kvSplit[0] {
case "id":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
mountID = kvSplit[1]
case "type":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
mountType = kvSplit[1]
case "source":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
mountSource = kvSplit[1]
case "target":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
mountTarget = kvSplit[1]
case "ro", "readonly":
if len(kvSplit) != 1 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
mountOpts = append(mountOpts, llb.Readonly)
case "uid":
return nil, errors.Errorf("not yet supported %s", kvPair)
// if len(kvSplit) != 2 {
// return nil, errors.Errorf("invalid mount arg %s", kvPair)
// }
// var err error
// uid, err = strconv.ParseInt(kvSplit[1], 10, 64)
// if err != nil {
// return nil, errors.Errorf("invalid mount arg %s", kvPair)
// }
case "gid":
return nil, errors.Errorf("not yet supported %s", kvPair)
// if len(kvSplit) != 2 {
// return nil, errors.Errorf("invalid mount arg %s", kvPair)
// }
// var err error
// gid, err = strconv.ParseInt(kvSplit[1], 10, 64)
// if err != nil {
// return nil, errors.Errorf("invalid mount arg %s", kvPair)
// }
case "mode", "chmod":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
var err error
mountMode, err = ParseMode(kvSplit[1])
if err != nil {
return nil, errors.Errorf("failed to parse mount %s %s", kvSplit[0], kvSplit[1])
}
case "sharing":
if len(kvSplit) != 2 {
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
switch kvSplit[1] {
case "shared":
sharingMode = llb.CacheMountShared
case "private":
sharingMode = llb.CacheMountPrivate
case "locked":
sharingMode = llb.CacheMountLocked
default:
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
case "from":
return nil, errors.Errorf("not yet supported %s", kvPair)
default:
return nil, errors.Errorf("invalid mount arg %s", kvPair)
}
}
if mountType == "" {
return nil, errors.Errorf("mount type not specified")
}
switch mountType {
case "bind-experimental":
if mountSource == "" {
return nil, errors.Errorf("mount source not specified")
}
if mountTarget == "" {
return nil, errors.Errorf("mount target not specified")
}
if mountMode != 0 {
return nil, errors.Errorf("mode is not supported for type=bind-experimental")
}
mountOpts = append(mountOpts, llb.HostBind(), llb.SourcePath(mountSource))
return []llb.RunOption{llb.AddMount(mountTarget, llb.Scratch(), mountOpts...)}, nil
case "cache":
if mountTarget == "" {
return nil, errors.Errorf("mount target not specified")
}
if mountMode == 0 {
mountMode = 0644
}
key := cacheKey(c.target)
cacheID := path.Join("/run/cache", key, path.Clean(mountTarget))
if c.ftrs.GlobalCache && mountID != "" {
cacheID = mountID
}
mountOpts = append(mountOpts, llb.AsPersistentCacheDir(cacheID, sharingMode))
state = c.cacheContext
state = state.File(pllb.Mkdir("/cache", os.FileMode(mountMode)))
mountOpts = append(mountOpts, llb.SourcePath("/cache"))
return []llb.RunOption{pllb.AddMount(mountTarget, state, mountOpts...)}, nil
case "tmpfs":
if mountTarget == "" {
return nil, errors.Errorf("mount target not specified")
}
if mountMode != 0 {
return nil, errors.Errorf("mode is not supported for type=tmpfs")
}
state = c.platr.Scratch()
mountOpts = append(mountOpts, llb.Tmpfs())
return []llb.RunOption{pllb.AddMount(mountTarget, state, mountOpts...)}, nil
case "ssh-experimental":
sshID := mountID
if sshID == "" {
sshID = path.Clean(mountTarget)
}
sshOpts := []llb.SSHOption{llb.SSHID(sshID)}
if mountTarget != "" {
sshOpts = append(sshOpts, llb.SSHSocketTarget(mountTarget))
}
if mountMode != 0 {
return nil, errors.Errorf("mode is not supported for type=ssh-experimental")
}
return []llb.RunOption{llb.AddSSHSocket(sshOpts...)}, nil
case "secret":
if mountTarget == "" {
return nil, errors.Errorf("mount target not specified")
}
if mountMode == 0 {
// TODO: Perhaps this should just default to the current user automatically from
// buildkit side. Then we wouldn't need to open this up to everyone.
mountMode = 0444
}
secretID := mountID
if secretID == "" {
secretID = path.Clean(mountTarget)
}
secretName := strings.TrimPrefix(secretID, "+secrets/")
secretOpts := []llb.SecretOption{
llb.SecretID(c.secretID(secretName)),
llb.SecretFileOpt(0, 0, mountMode),
}
return []llb.RunOption{llb.AddSecret(mountTarget, secretOpts...)}, nil
default:
return nil, errors.Errorf("invalid mount type %s", mountType)
}
}
var errInvalidOctal = errors.New("invalid octal")
func ParseMode(s string) (int, error) {
if len(s) == 0 || s[0] != '0' {
return 0, errInvalidOctal
}
mode, err := strconv.ParseInt(s, 8, 64)
return int(mode), err
}
// cacheKey returns a key that can be used to uniquely identify the target.
// Cache mounts use this key to ensure that the cache is unique to the target.
func cacheKey(target domain.Target) string {
target.Tag = "" // Strip away tag info (e.g. git sha)
digest := sha256.Sum256([]byte(target.StringCanonical()))
return hex.EncodeToString(digest[:])
}