/
task.go
379 lines (340 loc) · 11.5 KB
/
task.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package tasks
import (
"archive/tar"
"encoding/json"
"fmt"
"path/filepath"
"strings"
docker "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/determined-ai/determined/master/pkg/archive"
"github.com/determined-ai/determined/master/pkg/cproto"
"github.com/determined-ai/determined/master/pkg/device"
"github.com/determined-ai/determined/master/pkg/etc"
"github.com/determined-ai/determined/master/pkg/model"
"github.com/determined-ai/determined/master/pkg/schemas/expconf"
)
// File location constants.
const (
// DefaultWorkDir is the default workdir.
DefaultWorkDir = "/run/determined/workdir"
userPythonBaseDir = "/run/determined/pythonuserbase"
RunDir = "/run/determined"
infoDir = "/run/determined/info"
trainDir = "/run/determined/train"
modelCopy = "/run/determined/train/model"
rootDir = "/"
PasswdPath = "/run/determined/etc/passwd"
ShadowPath = "/run/determined/etc/shadow"
GroupPath = "/run/determined/etc/group"
certPath = "/run/determined/etc/ssl/master.crt"
// DtrainSSHPortBase is starting range for Dtrain ports.
DtrainSSHPortBase = 12350
// InterTrainProcessCommPort1Base is starting range for intertraincomm1 ports.
InterTrainProcessCommPort1Base = 12360
// InterTrainProcessCommPort2Base is starting range for intertraincomm2 ports.
InterTrainProcessCommPort2Base = 12365
// C10DPortBase is starting range for c10D ports.
C10DPortBase = 29400
// DTrainSSHPort is the name of a port.
DTrainSSHPort = "DTRAIN_SSH_PORT"
// InterTrainProcessCommPort1 is the name of a port.
InterTrainProcessCommPort1 = "INTER_TRAIN_PROCESS_COMM_PORT_1"
// InterTrainProcessCommPort2 is the name of a port.
InterTrainProcessCommPort2 = "INTER_TRAIN_PROCESS_COMM_PORT_2"
// C10DPort is the name of a port.
C10DPort = "C10D_PORT"
)
// TaskSpecifier creates a TaskSpec. ToTaskSpec must only be called once per specifier.
type TaskSpecifier interface {
ToTaskSpec() TaskSpec
}
// TaskSpec defines the spec of a task.
type TaskSpec struct {
// Fields that are only for task logics.
Description string
// LoggingFields are fields to include in each record of structured logging.
LoggingFields map[string]string
// Fields that are set on the cluster level.
ClusterID string
HarnessPath string
MasterCert []byte
SSHRsaSize int
SegmentEnabled bool
SegmentAPIKey string
// Fields that are set on the per-request basis.
// TaskContainerDefaults should be removed from TaskSpec once we move to using the same
// schema for the cluster-level defaults and the request-level configuration.
TaskContainerDefaults model.TaskContainerDefaultsConfig
Environment expconf.EnvironmentConfig
ResourcesConfig expconf.ResourcesConfig
WorkDir string
Owner *model.User
AgentUserGroup *model.AgentUserGroup
ExtraArchives []cproto.RunArchive
ExtraEnvVars map[string]string
Entrypoint []string
Mounts []mount.Mount
// UseHostMode is whether host mode networking would be desirable for this task.
// This is used by Docker only.
UseHostMode bool
ShmSize int64
// The parent task of an allocation.
TaskID string
// Fields that are set on per-resources basis.
AllocationID string
AllocationSessionToken string
ResourcesID string
ContainerID string
Devices []device.Device
UserSessionToken string
TaskType model.TaskType
SlurmConfig expconf.SlurmConfig
PbsConfig expconf.PbsConfig
ExtraProxyPorts expconf.ProxyPortsConfig
Workspace string
Project string
Labels []string
// Ports required by trial or commands and their respective base port values.
UniqueExposedPortRequests map[string]int
}
// ResolveWorkDir resolves the work dir.
func (t *TaskSpec) ResolveWorkDir() {
agentUser := ""
detUser := ""
if t.AgentUserGroup != nil {
agentUser = t.AgentUserGroup.User
}
if t.Owner != nil {
detUser = t.Owner.Username
}
workDir := strings.ReplaceAll(t.WorkDir, "$AGENT_USER", agentUser)
t.WorkDir = strings.ReplaceAll(workDir, "$DET_USER", detUser)
}
// Archives returns all the archives.
func (t *TaskSpec) Archives() ([]cproto.RunArchive, []cproto.RunArchive) {
res := []cproto.RunArchive{
workDirArchive(t.AgentUserGroup, t.WorkDir, t.WorkDir == DefaultWorkDir),
runDirHelpersArchive(t.AgentUserGroup),
injectUserArchive(t.AgentUserGroup, t.WorkDir),
harnessArchive(t.HarnessPath, t.AgentUserGroup),
masterCertArchive(t.MasterCert),
}
res = append(res, t.ExtraArchives...)
// Split into root and non root required files. In the case the user
// is root we will still differentiate files that need to be root
// versus files that should be owned by the user.
var user, root []cproto.RunArchive
for _, a := range res {
var uItems, rItems archive.Archive
for _, item := range a.Archive {
if item.IsRootItem {
rItems = append(rItems, item)
} else {
uItems = append(uItems, item)
}
}
if len(rItems) > 0 {
root = append(root, cproto.RunArchive{
Path: a.Path,
CopyOptions: a.CopyOptions,
Archive: rItems,
})
}
if len(uItems) > 0 {
user = append(user, cproto.RunArchive{
Path: a.Path,
CopyOptions: a.CopyOptions,
Archive: uItems,
})
}
}
return user, root
}
// EnvVars returns all the environment variables.
func (t TaskSpec) EnvVars() map[string]string {
e := map[string]string{
// PYTHONUSERBASE allows us to `pip install --user` into a location guaranteed to be owned by
// the user inside the container.
"PYTHONUSERBASE": userPythonBaseDir,
"DET_TASK_ID": t.TaskID,
"DET_ALLOCATION_ID": t.AllocationID,
"DET_RESOURCES_ID": t.ResourcesID,
"DET_CONTAINER_ID": t.ContainerID,
"DET_SESSION_TOKEN": t.AllocationSessionToken,
"DET_USER_TOKEN": t.UserSessionToken,
"DET_WORKDIR": t.WorkDir,
}
if t.Owner != nil {
e["DET_USER"] = t.Owner.Username
}
if t.TaskContainerDefaults.GLOOPortRange != "" {
e["GLOO_PORT_RANGE"] = t.TaskContainerDefaults.GLOOPortRange
}
networkInterface := t.TaskContainerDefaults.DtrainNetworkInterface
if networkInterface != "" {
e["DET_INTER_NODE_NETWORK_INTERFACE"] = networkInterface
}
if len(t.MasterCert) != 0 {
e["DET_USE_TLS"] = "true"
e["DET_MASTER_CERT_FILE"] = certPath
} else {
e["DET_USE_TLS"] = "false"
}
e["DET_SEGMENT_ENABLED"] = fmt.Sprintf("%v", t.SegmentEnabled)
if t.SegmentEnabled {
e["DET_SEGMENT_API_KEY"] = t.SegmentAPIKey
}
if t.LoggingFields != nil {
j, err := json.Marshal(t.LoggingFields)
if err != nil {
// TODO(DET-7565): propagate errors.
panic(fmt.Errorf("serializing logging fields: %w", err))
}
e["DET_TASK_LOGGING_METADATA"] = string(j)
}
for k, v := range t.ExtraEnvVars {
e[k] = v
}
return e
}
// LogShipperWrappedEntrypoint returns the configured Entrypoint wrapped with ship_logs.py.
func (t *TaskSpec) LogShipperWrappedEntrypoint() []string {
// Prepend the entrypoint like: `ship-logs.sh ship_logs.py "$@"`.
shipLogsShell := filepath.Join(RunDir, taskShipLogsShell)
shipLogsPython := filepath.Join(RunDir, taskShipLogsPython)
return append([]string{shipLogsShell, shipLogsPython}, t.Entrypoint...)
}
// ToDockerSpec converts a task spec to a docker container spec.
func (t *TaskSpec) ToDockerSpec() cproto.Spec {
var envVars []string
for k, v := range t.EnvVars() {
envVars = append(envVars, fmt.Sprintf("%s=%s", k, v))
}
env := t.Environment
deviceType := device.CPU
if len(t.Devices) > 0 {
deviceType = t.Devices[0].Type
}
envVars = append(envVars, env.EnvironmentVariables().For(deviceType)...)
network := t.TaskContainerDefaults.NetworkMode
if t.UseHostMode {
network = hostMode
}
shmSize := t.ShmSize
if shmSize == 0 {
shmSize = t.TaskContainerDefaults.ShmSizeBytes
}
resources := t.ResourcesConfig
var devices []docker.DeviceMapping
for _, device := range resources.Devices() {
devices = append(devices, docker.DeviceMapping{
PathOnHost: device.HostPath(),
PathInContainer: device.ContainerPath(),
CgroupPermissions: device.Mode(),
})
}
runArchives, rootArchives := t.Archives()
spec := cproto.Spec{
TaskType: string(t.TaskType),
PullSpec: cproto.PullSpec{
Registry: env.RegistryAuth(),
ForcePull: env.ForcePullImage(),
},
RunSpec: cproto.RunSpec{
ContainerConfig: docker.Config{
User: getUser(t.AgentUserGroup),
ExposedPorts: toPortSet(env.Ports()),
Env: envVars,
Cmd: t.LogShipperWrappedEntrypoint(),
Image: env.Image().For(deviceType),
WorkingDir: t.WorkDir,
},
HostConfig: docker.HostConfig{
NetworkMode: network,
Mounts: t.Mounts,
PublishAllPorts: true,
ShmSize: shmSize,
CapAdd: env.AddCapabilities(),
CapDrop: env.DropCapabilities(),
Resources: docker.Resources{
Devices: devices,
},
},
Archives: append(runArchives, rootArchives...),
DeviceType: deviceType,
Registry: env.RegistryAuth(),
},
}
return spec
}
// workDirArchive ensures that the workdir is created and owned by the user.
func workDirArchive(
aug *model.AgentUserGroup, workDir string, createWorkDir bool,
) cproto.RunArchive {
a := archive.Archive{
aug.OwnedArchiveItem(RunDir, nil, 0o700, tar.TypeDir),
aug.OwnedArchiveItem(infoDir, nil, 0o755, tar.TypeDir),
aug.OwnedArchiveItem(userPythonBaseDir, nil, 0o700, tar.TypeDir),
}
if createWorkDir {
a = append(a, aug.OwnedArchiveItem(workDir, nil, 0o700, tar.TypeDir))
}
return wrapArchive(a, rootDir)
}
// runDirHelpersArchive ensures helper scripts exist in the run dir.
func runDirHelpersArchive(aug *model.AgentUserGroup) cproto.RunArchive {
return wrapArchive(archive.Archive{
aug.OwnedArchiveItem(
taskSetupScript,
etc.MustStaticFile(etc.TaskSetupScriptResource),
taskSetupMode,
tar.TypeReg,
),
aug.OwnedArchiveItem(
taskShipLogsShell,
etc.MustStaticFile(etc.TaskShipLogsShellResource),
taskShipLogsShellMode,
tar.TypeReg,
),
aug.OwnedArchiveItem(
taskShipLogsPython,
etc.MustStaticFile(etc.TaskShipLogsPythonResource),
taskShipLogsPythonMode,
tar.TypeReg,
),
aug.OwnedArchiveItem(
SingularityEntrypointWrapperScript,
etc.MustStaticFile(etc.SingularityEntrypointWrapperScriptResource),
singularityEntrypointWrapperMode,
tar.TypeReg,
),
}, RunDir)
}
// injectUserArchive creates the user/UID/group/GID for a user by adding passwd/shadow/group files
// to /run/determined/etc, which will be read by libnss_determined inside the container. If
// libnss_determined is not present in the container, these files will be simply ignored and some
// non-root container features will not work properly.
func injectUserArchive(aug *model.AgentUserGroup, workDir string) cproto.RunArchive {
passwdBytes := []byte(
fmt.Sprintf("%v:x:%v:%v::%v:/bin/bash\n", aug.User, aug.UID, aug.GID, workDir),
)
// Disable login via password by * in shadow file. Cannot use ! as that locks the account
// when using SLURM/Singularity.
shadowBytes := []byte(fmt.Sprintf("%v:*:::::::\n", aug.User))
groupBytes := []byte(fmt.Sprintf("%v:x:%v:\n", aug.Group, aug.GID))
return wrapArchive(
archive.Archive{
archive.RootItem(PasswdPath, passwdBytes, 0o644, tar.TypeReg),
archive.RootItem(ShadowPath, shadowBytes, 0o600, tar.TypeReg),
archive.RootItem(GroupPath, groupBytes, 0o644, tar.TypeReg),
},
rootDir,
)
}
func getUser(agentUserGroup *model.AgentUserGroup) string {
if agentUserGroup == nil {
return ""
}
return fmt.Sprintf("%d:%d", agentUserGroup.UID, agentUserGroup.GID)
}