/
paths.cpp
634 lines (525 loc) · 16.7 KB
/
paths.cpp
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <list>
#include <string>
#include <vector>
#include <mesos/mesos.hpp>
#include <mesos/resources.hpp>
#include <mesos/roles.hpp>
#include <mesos/type_utils.hpp>
#include <stout/check.hpp>
#include <stout/fs.hpp>
#include <stout/nothing.hpp>
#include <stout/os.hpp>
#include <stout/path.hpp>
#include <stout/strings.hpp>
#include <stout/try.hpp>
#include <stout/unreachable.hpp>
#include <glog/logging.h>
#include "common/validation.hpp"
#include "messages/messages.hpp"
#include "slave/paths.hpp"
#include "slave/validation.hpp"
using std::list;
using std::string;
using std::vector;
namespace mesos {
namespace internal {
namespace slave {
namespace paths {
// File names.
const char BOOT_ID_FILE[] = "boot_id";
const char SLAVE_INFO_FILE[] = "slave.info";
const char FRAMEWORK_PID_FILE[] = "framework.pid";
const char FRAMEWORK_INFO_FILE[] = "framework.info";
const char LIBPROCESS_PID_FILE[] = "libprocess.pid";
const char EXECUTOR_INFO_FILE[] = "executor.info";
const char EXECUTOR_SENTINEL_FILE[] = "executor.sentinel";
const char HTTP_MARKER_FILE[] = "http.marker";
const char FORKED_PID_FILE[] = "forked.pid";
const char TASK_INFO_FILE[] = "task.info";
const char TASK_UPDATES_FILE[] = "task.updates";
const char RESOURCES_INFO_FILE[] = "resources.info";
const char RESOURCES_TARGET_FILE[] = "resources.target";
const char SLAVES_DIR[] = "slaves";
const char FRAMEWORKS_DIR[] = "frameworks";
const char EXECUTORS_DIR[] = "executors";
const char CONTAINERS_DIR[] = "runs";
Try<ExecutorRunPath> parseExecutorRunPath(
const string& _rootDir,
const string& dir)
{
// TODO(josephw): Consider using `<regex>` here, which requires GCC 4.9+.
// Make sure there's a separator at the end of the `rootdir` so that
// we don't accidentally slice off part of a directory.
const string rootDir = path::join(_rootDir, "");
if (!strings::startsWith(dir, rootDir)) {
return Error(
"Directory '" + dir + "' does not fall under "
"the root directory: " + rootDir);
}
vector<string> tokens = strings::tokenize(dir.substr(rootDir.size()), "/");
// A complete executor run path consists of at least 8 tokens, which
// includes the four named directories and the four IDs.
if (tokens.size() < 8) {
return Error(
"Path after root directory is not long enough to be an "
"executor run path: " + path::join(tokens));
}
// All four named directories much match.
if (tokens[0] == SLAVES_DIR &&
tokens[2] == FRAMEWORKS_DIR &&
tokens[4] == EXECUTORS_DIR &&
tokens[6] == CONTAINERS_DIR) {
ExecutorRunPath path;
path.slaveId.set_value(tokens[1]);
path.frameworkId.set_value(tokens[3]);
path.executorId.set_value(tokens[5]);
path.containerId.set_value(tokens[7]);
return path;
}
return Error("Could not parse executor run path from directory: " + dir);
}
string getMetaRootDir(const string& rootDir)
{
return path::join(rootDir, "meta");
}
string getSandboxRootDir(const string& rootDir)
{
return path::join(rootDir, SLAVES_DIR);
}
string getProvisionerDir(const string& rootDir)
{
return path::join(rootDir, "provisioner");
}
string getBootIdPath(const string& rootDir)
{
return path::join(rootDir, BOOT_ID_FILE);
}
string getLatestSlavePath(const string& rootDir)
{
return path::join(rootDir, SLAVES_DIR, LATEST_SYMLINK);
}
string getSlavePath(
const string& rootDir,
const SlaveID& slaveId)
{
return path::join(rootDir, SLAVES_DIR, stringify(slaveId));
}
string getSlaveInfoPath(
const string& rootDir,
const SlaveID& slaveId)
{
return path::join(getSlavePath(rootDir, slaveId), SLAVE_INFO_FILE);
}
Try<list<string>> getFrameworkPaths(
const string& rootDir,
const SlaveID& slaveId)
{
return fs::list(
path::join(getSlavePath(rootDir, slaveId), FRAMEWORKS_DIR, "*"));
}
string getFrameworkPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId)
{
return path::join(
getSlavePath(rootDir, slaveId), FRAMEWORKS_DIR, stringify(frameworkId));
}
string getFrameworkPidPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId)
{
return path::join(
getFrameworkPath(rootDir, slaveId, frameworkId), FRAMEWORK_PID_FILE);
}
string getFrameworkInfoPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId)
{
return path::join(
getFrameworkPath(rootDir, slaveId, frameworkId), FRAMEWORK_INFO_FILE);
}
Try<list<string>> getExecutorPaths(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId)
{
return fs::list(path::join(
getFrameworkPath(rootDir, slaveId, frameworkId),
EXECUTORS_DIR,
"*"));
}
string getExecutorPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId)
{
return path::join(
getFrameworkPath(rootDir, slaveId, frameworkId),
EXECUTORS_DIR,
stringify(executorId));
}
string getExecutorInfoPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId)
{
return path::join(
getExecutorPath(rootDir, slaveId, frameworkId, executorId),
EXECUTOR_INFO_FILE);
}
Try<list<string>> getExecutorRunPaths(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId)
{
return fs::list(path::join(
getExecutorPath(rootDir, slaveId, frameworkId, executorId),
CONTAINERS_DIR,
"*"));
}
string getExecutorRunPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return path::join(
getExecutorPath(rootDir, slaveId, frameworkId, executorId),
CONTAINERS_DIR,
stringify(containerId));
}
string getExecutorHttpMarkerPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
HTTP_MARKER_FILE);
}
string getExecutorSentinelPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
EXECUTOR_SENTINEL_FILE);
}
string getExecutorLatestRunPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId)
{
return path::join(
getExecutorPath(rootDir, slaveId, frameworkId, executorId),
CONTAINERS_DIR,
LATEST_SYMLINK);
}
string getLibprocessPidPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
"pids",
LIBPROCESS_PID_FILE);
}
string getForkedPidPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
"pids",
FORKED_PID_FILE);
}
Try<list<string>> getTaskPaths(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId)
{
return fs::list(path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
"tasks",
"*"));
}
string getTaskPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId,
const TaskID& taskId)
{
return path::join(
getExecutorRunPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId),
"tasks",
stringify(taskId));
}
string getTaskInfoPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId,
const TaskID& taskId)
{
return path::join(
getTaskPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId,
taskId),
TASK_INFO_FILE);
}
string getTaskUpdatesPath(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId,
const TaskID& taskId)
{
return path::join(
getTaskPath(
rootDir,
slaveId,
frameworkId,
executorId,
containerId,
taskId),
TASK_UPDATES_FILE);
}
string getResourcesInfoPath(
const string& rootDir)
{
return path::join(rootDir, "resources", RESOURCES_INFO_FILE);
}
string getResourcesTargetPath(
const string& rootDir)
{
return path::join(rootDir, "resources", RESOURCES_TARGET_FILE);
}
string getPersistentVolumePath(
const string& rootDir,
const string& role,
const string& persistenceId)
{
// Role names might contain literal `/` if the role is part of a
// role hierarchy. Since `/` is not allowed in a directory name
// under Linux, we could either represent such sub-roles with
// sub-directories, or encode the `/` with some other identifier.
// To clearly distinguish artifacts in a volume from subroles we
// choose to encode `/` in role names as ` ` (literal space) as
// opposed to using subdirectories. Whitespace is not allowed as
// part of a role name. Also, practically all modern filesystems can
// use ` ` in filenames. There are some limitations in auxilary
// tooling which are not relevant here, e.g., many shell constructs
// require quotes around filesnames containing ` `; containers using
// persistent volumes would not see the ` ` as the role-related part
// of the path would not be part of a mapping into the container
// sandbox.
string serializableRole = strings::replace(role, "/", " ");
return path::join(
rootDir, "volumes", "roles", serializableRole, persistenceId);
}
string getPersistentVolumePath(
const string& rootDir,
const Resource& volume)
{
CHECK_GT(volume.reservations_size(), 0);
CHECK(volume.has_disk());
CHECK(volume.disk().has_persistence());
const string& role = Resources::reservationRole(volume);
// Additionally check that the role and the persistent ID are valid
// before using them to construct a directory path.
CHECK_NONE(roles::validate(role));
CHECK_NONE(common::validation::validateID(volume.disk().persistence().id()));
// If no `source` is provided in `DiskInfo` volumes are mapped into
// the `rootDir`.
if (!volume.disk().has_source()) {
return getPersistentVolumePath(
rootDir,
role,
volume.disk().persistence().id());
}
// If a `source` was provided for the volume, we map it according
// to the `type` of disk. Currently only the `PATH` and 'MOUNT'
// types are supported.
switch (volume.disk().source().type()) {
case Resource::DiskInfo::Source::PATH: {
// For `PATH` we mount a directory inside the `root`.
CHECK(volume.disk().source().has_path());
CHECK(volume.disk().source().path().has_root());
return getPersistentVolumePath(
volume.disk().source().path().root(),
role,
volume.disk().persistence().id());
}
case Resource::DiskInfo::Source::MOUNT: {
// For `MOUNT` we map straight onto the root of the mount.
CHECK(volume.disk().source().has_mount());
CHECK(volume.disk().source().mount().has_root());
return volume.disk().source().mount().root();
}
case Resource::DiskInfo::Source::UNKNOWN:
LOG(FATAL) << "Unsupported DiskInfo.Source.type";
break;
}
UNREACHABLE();
}
string createExecutorDirectory(
const string& rootDir,
const SlaveID& slaveId,
const FrameworkID& frameworkId,
const ExecutorID& executorId,
const ContainerID& containerId,
const Option<string>& user)
{
// These IDs should be valid as they are either assigned by the
// master/agent or validated by the master but we do a sanity check
// here before using them to create a directory.
CHECK_NONE(common::validation::validateSlaveID(slaveId));
CHECK_NONE(common::validation::validateFrameworkID(frameworkId));
CHECK_NONE(common::validation::validateExecutorID(executorId));
CHECK_NONE(slave::validation::container::validateContainerId(containerId));
const string directory =
getExecutorRunPath(rootDir, slaveId, frameworkId, executorId, containerId);
Try<Nothing> mkdir = os::mkdir(directory);
CHECK_SOME(mkdir)
<< "Failed to create executor directory '" << directory << "'";
// Remove the previous "latest" symlink.
const string latest =
getExecutorLatestRunPath(rootDir, slaveId, frameworkId, executorId);
if (os::exists(latest)) {
CHECK_SOME(os::rm(latest))
<< "Failed to remove latest symlink '" << latest << "'";
}
// Symlink the new executor directory to "latest".
Try<Nothing> symlink = ::fs::symlink(directory, latest);
CHECK_SOME(symlink)
<< "Failed to symlink directory '" << directory
<< "' to '" << latest << "'";
// `os::chown()` is not available on Windows.
#ifndef __WINDOWS__
if (user.isSome()) {
// Per MESOS-2592, we need to set the ownership of the executor
// directory during its creation. We should not rely on subsequent
// phases of the executor creation to ensure the ownership as
// those may be conditional and in some cases leave the executor
// directory owned by the slave user instead of the specified
// framework or per-executor user.
LOG(INFO) << "Trying to chown '" << directory << "' to user '"
<< user.get() << "'";
Try<Nothing> chown = os::chown(user.get(), directory);
if (chown.isError()) {
// TODO(nnielsen): We currently have tests which depend on using
// user names which may not be available on the test machines.
// Therefore, we cannot make the chown validation a hard
// CHECK().
LOG(WARNING) << "Failed to chown executor directory '" << directory
<< "'. This may be due to attempting to run the executor "
<< "as a nonexistent user on the agent; see the description"
<< " for the `--switch_user` flag for more information: "
<< chown.error();
}
}
#endif // __WINDOWS__
return directory;
}
string createSlaveDirectory(
const string& rootDir,
const SlaveID& slaveId)
{
// `slaveId` should be valid because it's assigned by the master but
// we do a sanity check here before using it to create a directory.
CHECK_NONE(common::validation::validateSlaveID(slaveId));
const string directory = getSlavePath(rootDir, slaveId);
Try<Nothing> mkdir = os::mkdir(directory);
CHECK_SOME(mkdir)
<< "Failed to create agent directory '" << directory << "'";
// Remove the previous "latest" symlink.
const string latest = getLatestSlavePath(rootDir);
if (os::exists(latest)) {
CHECK_SOME(os::rm(latest))
<< "Failed to remove latest symlink '" << latest << "'";
}
// Symlink the new slave directory to "latest".
Try<Nothing> symlink = ::fs::symlink(directory, latest);
CHECK_SOME(symlink)
<< "Failed to symlink directory '" << directory
<< "' to '" << latest << "'";
return directory;
}
} // namespace paths {
} // namespace slave {
} // namespace internal {
} // namespace mesos {