forked from mchang6137/quilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindings.js
573 lines (484 loc) · 15.3 KB
/
bindings.js
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
// The default deployment object. createDeployment overwrites this.
var deployment = new Deployment({});
// The label used by the QRI to denote connections with public internet.
var publicInternetLabel = "public";
// Global unique ID counter.
var uniqueIDCounter = 0;
// Overwrite the deployment object with a new one.
function createDeployment(deploymentOpts) {
deployment = new Deployment(deploymentOpts);
return deployment;
}
function Deployment(deploymentOpts) {
deploymentOpts = deploymentOpts || {};
this.maxPrice = deploymentOpts.maxPrice || 0;
this.namespace = deploymentOpts.namespace || "default-namespace";
this.adminACL = deploymentOpts.adminACL || [];
this.machines = [];
this.containers = {};
this.services = [];
this.connections = [];
this.placements = [];
this.invariants = [];
}
function omitSSHKey(key, value) {
if (key == "sshKeys") {
return undefined;
}
return value;
}
// Returns a globally unique integer ID.
function uniqueID() {
return uniqueIDCounter++;
}
// key creates a string key for objects that have a _refID, namely Containers
// and Machines.
function key(obj) {
var keyObj = obj.clone();
keyObj._refID = "";
return JSON.stringify(keyObj, omitSSHKey);
}
// setQuiltIDs deterministically sets the id field of objects based on
// their attributes. The _refID field is required to differentiate between multiple
// references to the same object, and multiple instantiations with the exact
// same attributes.
function setQuiltIDs(objs) {
// The refIDs for each identical instance.
var refIDs = {};
objs.forEach(function(obj) {
var k = key(obj);
if (!refIDs[k]) {
refIDs[k] = [];
}
refIDs[k].push(obj._refID);
});
// If there are multiple references to the same object, there will be duplicate
// refIDs.
Object.keys(refIDs).forEach(function(k) {
refIDs[k] = _.sortBy(_.uniq(refIDs[k]), _.identity);
});
objs.forEach(function(obj) {
var k = key(obj);
obj.id = hash(k + refIDs[k].indexOf(obj._refID));
});
}
// Convert the deployment to the QRI deployment format.
Deployment.prototype.toQuiltRepresentation = function() {
this.vet();
setQuiltIDs(this.machines);
var containers = [];
this.services.forEach(function(serv) {
serv.containers.forEach(function(c) {
containers.push(c);
});
});
setQuiltIDs(containers);
// Map from container ID to container.
var containerMap = {};
var services = [];
var connections = [];
var placements = [];
// For each service, convert the associated connections and placement rules.
// Also, aggregate all containers referenced by services.
this.services.forEach(function(service) {
connections = connections.concat(service.getQuiltConnections());
placements = placements.concat(service.getQuiltPlacements());
// Collect the containers IDs, and add them to the container map.
var ids = [];
service.containers.forEach(function(container) {
ids.push(container.id);
containerMap[container.id] = container;
});
services.push({
name: service.name,
ids: ids,
annotations: service.annotations
});
});
var containers = [];
Object.keys(containerMap).forEach(function(cid) {
containers.push(containerMap[cid]);
});
return {
machines: this.machines,
labels: services,
containers: containers,
connections: connections,
placements: placements,
invariants: this.invariants,
namespace: this.namespace,
adminACL: this.adminACL,
maxPrice: this.maxPrice
};
};
// Check if all referenced services in connections and placements are really deployed.
Deployment.prototype.vet = function() {
var labelMap = {};
this.services.forEach(function(service) {
labelMap[service.name] = true;
});
var dockerfiles = {};
var hostnames = {};
this.services.forEach(function(service) {
service.connections.forEach(function(conn) {
var to = conn.to.name;
if (!labelMap[to]) {
throw service.name + " has a connection to undeployed service: " + to;
}
});
var hasFloatingIp = false;
service.placements.forEach(function(plcm) {
if (plcm.floatingIp) {
hasFloatingIp = true;
}
var otherLabel = plcm.otherLabel;
if (otherLabel !== undefined && !labelMap[otherLabel]) {
throw service.name + " has a placement in terms of an " +
"undeployed service: " + otherLabel;
}
});
if (hasFloatingIp && service.incomingPublic.length
&& service.containers.length > 1) {
throw service.name + " has a floating IP and multiple containers. " +
"This is not yet supported."
}
service.containers.forEach(function(c) {
var name = c.image.name;
if (dockerfiles[name] != undefined && dockerfiles[name] != c.image.dockerfile) {
throw name + " has differing Dockerfiles";
}
dockerfiles[name] = c.image.dockerfile;
if (c.hostname !== undefined) {
if (hostnames[c.hostname]) {
throw "hostname \"" + c.hostname + "\" used for multiple containers";
}
hostnames[c.hostname] = true;
}
})
});
};
// deploy adds an object, or list of objects, to the deployment.
// Deployable objects must implement the deploy(deployment) interface.
Deployment.prototype.deploy = function(toDeployList) {
if (toDeployList.constructor !== Array) {
toDeployList = [toDeployList];
}
var that = this;
toDeployList.forEach(function(toDeploy) {
if (!toDeploy.deploy) {
throw "only objects that implement \"deploy(deployment)\" can be deployed";
}
toDeploy.deploy(that);
});
};
Deployment.prototype.assert = function(rule, desired) {
this.invariants.push(new Assertion(rule, desired));
};
function Service(name, containers) {
this.name = uniqueLabelName(name);
this.containers = containers;
this.annotations = [];
this.placements = [];
this.connections = [];
this.outgoingPublic = [];
this.incomingPublic = [];
}
// Get the Quilt hostname that represents the entire service.
Service.prototype.hostname = function() {
return this.name + ".q";
};
// Get a list of Quilt hostnames that address the containers within the service.
Service.prototype.children = function() {
var i;
var res = [];
for (i = 1; i < this.containers.length + 1; i++) {
res.push(i + "." + this.name + ".q");
}
return res;
};
Service.prototype.annotate = function(annotation) {
this.annotations.push(annotation);
};
Service.prototype.canReach = function(target) {
if (target === publicInternet) {
return reachable(this.name, publicInternetLabel);
}
return reachable(this.name, target.name);
};
Service.prototype.canReachACL = function(target) {
return reachableACL(this.name, target.name);
};
Service.prototype.between = function(src, dst) {
return between(src.name, this.name, dst.name);
};
Service.prototype.neighborOf = function(target) {
return neighbor(this.name, target.name);
};
Service.prototype.deploy = function(deployment) {
deployment.services.push(this);
};
Service.prototype.connect = function(range, to) {
range = boxRange(range);
if (to === publicInternet) {
return this.connectToPublic(range);
}
this.connections.push(new Connection(range, to));
};
// publicInternet is an object that looks like another service that can be
// connected to or from. However, it is actually just syntactic sugar to hide
// the connectToPublic and connectFromPublic functions.
var publicInternet = {
connect: function(range, to) {
to.connectFromPublic(range);
},
canReach: function(to) {
return reachable(publicInternetLabel, to.name);
}
};
// Allow outbound traffic from the service to public internet.
Service.prototype.connectToPublic = function(range) {
range = boxRange(range);
if (range.min != range.max) {
throw "public internet cannot connect on port ranges";
}
this.outgoingPublic.push(range);
};
// Allow inbound traffic from public internet to the service.
Service.prototype.connectFromPublic = function(range) {
range = boxRange(range);
if (range.min != range.max) {
throw "public internet cannot connect on port ranges";
}
this.incomingPublic.push(range);
};
Service.prototype.place = function(rule) {
this.placements.push(rule);
};
Service.prototype.getQuiltConnections = function() {
var connections = [];
var that = this;
this.connections.forEach(function(conn) {
connections.push({
from: that.name,
to: conn.to.name,
minPort: conn.minPort,
maxPort: conn.maxPort
});
});
this.outgoingPublic.forEach(function(rng) {
connections.push({
from: that.name,
to: publicInternetLabel,
minPort: rng.min,
maxPort: rng.max
});
});
this.incomingPublic.forEach(function(rng) {
connections.push({
from: publicInternetLabel,
to: that.name,
minPort: rng.min,
maxPort: rng.max
});
});
return connections;
};
Service.prototype.getQuiltPlacements = function() {
var placements = [];
var that = this;
this.placements.forEach(function(placement) {
placements.push({
targetLabel: that.name,
exclusive: placement.exclusive,
otherLabel: placement.otherLabel || "",
provider: placement.provider || "",
size: placement.size || "",
region: placement.region || "",
floatingIp: placement.floatingIp || ""
});
});
return placements;
};
var labelNameCount = {};
function uniqueLabelName(name) {
if (!(name in labelNameCount)) {
labelNameCount[name] = 0;
}
var count = ++labelNameCount[name];
if (count == 1) {
return name;
}
return name + labelNameCount[name];
}
// Box raw integers into range.
function boxRange(x) {
if (x === undefined) {
return new Range(0, 0);
}
if (typeof x === "number") {
x = new Range(x, x);
}
return x;
}
function Machine(optionalArgs) {
this._refID = uniqueID();
this.provider = optionalArgs.provider || "";
this.role = optionalArgs.role || "";
this.region = optionalArgs.region || "";
this.size = optionalArgs.size || "";
this.floatingIp = optionalArgs.floatingIp || "";
this.diskSize = optionalArgs.diskSize || 0;
this.sshKeys = optionalArgs.sshKeys || [];
this.cpu = boxRange(optionalArgs.cpu);
this.ram = boxRange(optionalArgs.ram);
this.preemptible = optionalArgs.preemptible !== undefined ? optionalArgs.preemptible : false;
}
Machine.prototype.deploy = function(deployment) {
deployment.machines.push(this);
};
// Create a new machine with the same attributes.
Machine.prototype.clone = function() {
// _.clone only creates a shallow copy, so we must clone sshKeys ourselves.
var keyClone = _.clone(this.sshKeys);
var cloned = _.clone(this);
cloned.sshKeys = keyClone;
return new Machine(cloned);
};
Machine.prototype.withRole = function(role) {
var copy = this.clone();
copy.role = role;
return copy;
};
Machine.prototype.asWorker = function() {
return this.withRole("Worker");
};
Machine.prototype.asMaster = function() {
return this.withRole("Master");
};
// Create n new machines with the same attributes.
Machine.prototype.replicate = function(n) {
var i;
var res = [];
for (i = 0 ; i < n ; i++) {
res.push(this.clone());
}
return res;
};
function Image(name, dockerfile) {
this.name = name;
this.dockerfile = dockerfile;
}
Image.prototype.clone = function() {
return new Image(this.name, this.dockerfile);
}
function Container(image, command) {
// refID is used to distinguish deployments with multiple references to the
// same container, and deployments with multiple containers with the exact
// same attributes.
this._refID = uniqueID();
this.image = image;
if (typeof image === 'string') {
this.image = new Image(image);
}
if (this.image.constructor !== Image) {
throw new Error('bad image type');
}
this.command = command || [];
this.env = {};
this.filepathToContent = {};
}
// Create a new Container with the same attributes.
Container.prototype.clone = function() {
var cloned = new Container(this.image.clone(), _.clone(this.command));
cloned.env = _.clone(this.env);
cloned.filepathToContent = _.clone(this.filepathToContent);
return cloned;
};
// Create n new Containers with the same attributes.
Container.prototype.replicate = function(n) {
var i;
var res = [];
for (i = 0 ; i < n ; i++) {
res.push(this.clone());
}
return res;
};
Container.prototype.setEnv = function(key, val) {
this.env[key] = val;
};
Container.prototype.withEnv = function(env) {
var cloned = this.clone();
cloned.env = env;
return cloned;
};
Container.prototype.withFiles = function(fileMap) {
var cloned = this.clone();
cloned.filepathToContent = fileMap;
return cloned;
};
Container.prototype.setHostname = function(h) {
this.hostname = h;
};
Container.prototype.getHostname = function() {
if (this.hostname === undefined) {
throw new Error("no hostname");
}
return this.hostname + ".q";
};
var enough = { form: "enough" };
var between = invariantType("between");
var neighbor = invariantType("reachDirect");
var reachableACL = invariantType("reachACL");
var reachable = invariantType("reach");
function Assertion(invariant, desired) {
this.form = invariant.form;
this.nodes = invariant.nodes;
this.target = desired;
}
function invariantType(form) {
return function() {
// Convert the arguments object into a real array. We can't simply use
// Array.from because it isn't defined in Otto.
var nodes = [];
var i;
for (i = 0 ; i < arguments.length ; i++) {
nodes.push(arguments[i]);
}
return {
form: form,
nodes: nodes
};
};
}
function LabelRule(exclusive, otherService) {
this.exclusive = exclusive;
this.otherLabel = otherService.name;
}
function MachineRule(exclusive, optionalArgs) {
this.exclusive = exclusive;
if (optionalArgs.provider) {
this.provider = optionalArgs.provider;
}
if (optionalArgs.size) {
this.size = optionalArgs.size;
}
if (optionalArgs.region) {
this.region = optionalArgs.region;
}
if (optionalArgs.floatingIp) {
this.floatingIp = optionalArgs.floatingIp;
}
}
function Connection(ports, to) {
this.minPort = ports.min;
this.maxPort = ports.max;
this.to = to;
}
function Range(min, max) {
this.min = min;
this.max = max;
}
function Port(p) {
return new PortRange(p, p);
}
var PortRange = Range;