pmcf 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -74,7 +74,7 @@ async function generateNamedDefs(location, targetDir) {
74
74
  };
75
75
  zones.push(zone);
76
76
 
77
- for await (const subnet of location.subnets()) {
77
+ for (const subnet of location.subnets()) {
78
78
  if (subnet.address) {
79
79
  const reverse = reverseAddress(subnet.address);
80
80
  const reverseArpa = reverseArpaAddress(subnet.address);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/model.mjs CHANGED
@@ -101,7 +101,7 @@ export class Base {
101
101
  }
102
102
 
103
103
  toString() {
104
- return this.typeName + ":" + this.owner.name + "/" + this.name;
104
+ return this.typeName + ":" + (this.owner?.name || "") + "/" + this.name;
105
105
  }
106
106
 
107
107
  get propertyNames() {
@@ -113,22 +113,69 @@ export class Base {
113
113
  }
114
114
  }
115
115
 
116
- export class World {
116
+ export class Owner extends Base {
117
+ #hosts = new Map();
118
+ #networks = new Map();
119
+ #subnets = new Map();
120
+
121
+ async *hosts() {
122
+ for (const host of this.#hosts.values()) {
123
+ yield host;
124
+ }
125
+ }
126
+
127
+ addHost(host) {
128
+ this.#hosts.set(host.name, host);
129
+ }
130
+
131
+ network(name) {
132
+ return this.#networks.get(name);
133
+ }
134
+
135
+ async *networks() {
136
+ for (const network of this.#networks.values()) {
137
+ yield network;
138
+ }
139
+ }
140
+
141
+ addNetwork(network) {
142
+ this.#networks.set(network.name, network);
143
+ }
144
+
145
+ addSubnet(subnet) {
146
+ this.#subnets.set(subnet.name, subnet);
147
+ }
148
+
149
+ subnet(name) {
150
+ return this.#subnets.get(name);
151
+ }
152
+
153
+ subnets() {
154
+ return this.#subnets.values();
155
+ }
156
+
157
+ toJSON() {
158
+ return {
159
+ ...super.toJSON(),
160
+ networks: [...this.#networks.keys()].sort(),
161
+ subnets: [...this.#subnets.keys()].sort(),
162
+ hosts: [...this.#hosts.keys()].sort()
163
+ };
164
+ }
165
+ }
166
+
167
+ export class World extends Owner {
117
168
  static get types() {
118
169
  return _types;
119
170
  }
120
171
 
121
- directory;
122
172
  #byName = new Map();
123
173
 
124
174
  constructor(directory) {
175
+ super(undefined, { name: "" });
125
176
  this.directory = directory;
126
177
  }
127
178
 
128
- get name() {
129
- return "";
130
- }
131
-
132
179
  get world() {
133
180
  return this;
134
181
  }
@@ -216,10 +263,6 @@ export class World {
216
263
  return this._loadType(name, Host);
217
264
  }
218
265
 
219
- addHost(host) {}
220
- addNetwork(network) {}
221
- network(name) {}
222
-
223
266
  async *subnets() {
224
267
  for await (const location of this.locations()) {
225
268
  yield* location.subnets();
@@ -235,6 +278,156 @@ export class World {
235
278
  }
236
279
  }
237
280
 
281
+ export class Location extends Owner {
282
+ domain;
283
+ dns;
284
+ #administratorEmail;
285
+
286
+ static get typeName() {
287
+ return "location";
288
+ }
289
+
290
+ constructor(owner, data) {
291
+ super(owner, data);
292
+
293
+ const networks = data.networks;
294
+ delete data.networks;
295
+ Object.assign(this, data);
296
+
297
+ if (networks) {
298
+ for (const [name, data] of Object.entries(networks)) {
299
+ data.name = name;
300
+ new Network(this, data);
301
+ }
302
+
303
+ /*
304
+ for (const network of this.#networks.values()) {
305
+ if (network.bridges) {
306
+ network.bridges = new Set(
307
+ network.bridges.map(b => {
308
+ const n = this.network(b);
309
+ if (!n) {
310
+ console.error(`No network named ${b}`);
311
+ }
312
+ return n;
313
+ })
314
+ );
315
+ }
316
+ }
317
+ */
318
+ }
319
+ }
320
+
321
+ async *hosts() {
322
+ for await (const host of this.owner.hosts()) {
323
+ if (host.location === this) {
324
+ yield host;
325
+ }
326
+ }
327
+ }
328
+
329
+ async service(filter) {
330
+ let best;
331
+ for await (const service of this.services(filter)) {
332
+ if (!best || service.priority < best.priority) {
333
+ best = service;
334
+ }
335
+ }
336
+
337
+ return best;
338
+ }
339
+
340
+ async *services(filter) {
341
+ for await (const host of this.hosts()) {
342
+ for (const service of Object.values(host.services)) {
343
+ if (
344
+ !filter ||
345
+ filter.type === "*" ||
346
+ filter.type === service.type ||
347
+ filter.name === service.name
348
+ ) {
349
+ yield service;
350
+ }
351
+ }
352
+ }
353
+ }
354
+
355
+ async *networkAddresses() {
356
+ for await (const host of this.hosts()) {
357
+ for (const networkAddresses of host.networkAddresses()) {
358
+ yield networkAddresses;
359
+ }
360
+ }
361
+ }
362
+
363
+ get dnsAllowedUpdates() {
364
+ return this.dns?.allowedUpdates || [];
365
+ }
366
+
367
+ get dnsRecordTTL() {
368
+ return this.dns?.recordTTL || "1W";
369
+ }
370
+
371
+ get administratorEmail() {
372
+ return this.#administratorEmail || "admin@" + this.domain;
373
+ }
374
+
375
+ get propertyNames() {
376
+ return [...super.propertyNames, "domain" /*, "hosts"*/];
377
+ }
378
+ }
379
+
380
+ export class Network extends Owner {
381
+ kind;
382
+ scope;
383
+ metric;
384
+ ipv4;
385
+ subnet;
386
+
387
+ static get typeName() {
388
+ return "network";
389
+ }
390
+
391
+ constructor(owner, data) {
392
+ super(owner, data);
393
+
394
+ Object.assign(this, data);
395
+
396
+ const subnetAddress = this.subnetAddress;
397
+
398
+ if (subnetAddress) {
399
+ let subnet = owner.subnet(subnetAddress);
400
+ if (!subnet) {
401
+ subnet = new Subnet(owner, { name: subnetAddress });
402
+ }
403
+
404
+ this.subnet = subnet;
405
+ subnet.networks.add(this);
406
+ }
407
+
408
+ owner.addNetwork(this);
409
+ }
410
+
411
+ get ipv4_netmask() {
412
+ const m = this.ipv4?.match(/\/(\d+)$/);
413
+ if (m) {
414
+ return m[1];
415
+ }
416
+ }
417
+
418
+ get subnetAddress() {
419
+ if (this.ipv4) {
420
+ const [addr, bits] = this.ipv4.split(/\//);
421
+ const parts = addr.split(/\./);
422
+ return parts.slice(0, bits / 8).join(".");
423
+ }
424
+ }
425
+
426
+ get propertyNames() {
427
+ return [...super.propertyNames, "kind", "ipv4", "scope", "metric"];
428
+ }
429
+ }
430
+
238
431
  export class Host extends Base {
239
432
  networkInterfaces = {};
240
433
  services = {};
@@ -318,7 +511,14 @@ export class Host extends Base {
318
511
  iface.host = this;
319
512
  iface.name = name;
320
513
  if (iface.network) {
321
- iface.network = this.network(iface.network);
514
+ const network = owner.network(iface.network);
515
+
516
+ if (!network) {
517
+ console.error(`${this.toString()}: Missing network`, iface.network);
518
+ } else {
519
+ iface.network = network;
520
+ network.addHost(this);
521
+ }
322
522
  }
323
523
  }
324
524
 
@@ -447,185 +647,11 @@ export class Host extends Base {
447
647
 
448
648
  export class Model extends Host {}
449
649
 
450
- export class Location extends Base {
451
- domain;
452
- dns;
453
- #administratorEmail;
454
- #hosts = new Map();
455
- #networks = new Map();
456
- #subnets = new Map();
457
-
458
- static get typeName() {
459
- return "location";
460
- }
461
-
462
- constructor(owner, data) {
463
- super(owner, data);
464
-
465
- const networks = data.networks;
466
- delete data.networks;
467
- Object.assign(this, data);
468
-
469
- if (networks) {
470
- for (const [name, network] of Object.entries(networks)) {
471
- network.name = name;
472
- this.addNetwork(network);
473
- }
474
-
475
- for (const network of this.#networks.values()) {
476
- if (network.bridges) {
477
- network.bridges = new Set(
478
- network.bridges.map(b => {
479
- const n = this.network(b);
480
- if (!n) {
481
- console.error(`No network named ${b}`);
482
- }
483
- return n;
484
- })
485
- );
486
- }
487
- }
488
- }
489
- }
490
-
491
- async *hosts() {
492
- for await (const host of this.owner.hosts()) {
493
- if (host.location === this) {
494
- yield host;
495
- }
496
- }
497
- }
498
-
499
- async service(filter) {
500
- let best;
501
- for await (const service of this.services(filter)) {
502
- if (!best || service.priority < best.priority) {
503
- best = service;
504
- }
505
- }
506
-
507
- return best;
508
- }
509
-
510
- async *services(filter) {
511
- for await (const host of this.hosts()) {
512
- for (const service of Object.values(host.services)) {
513
- if (
514
- !filter ||
515
- filter.type === "*" ||
516
- filter.type === service.type ||
517
- filter.name === service.name
518
- ) {
519
- yield service;
520
- }
521
- }
522
- }
523
- }
524
-
525
- async *networkAddresses() {
526
- for await (const host of this.hosts()) {
527
- for (const networkAddresses of host.networkAddresses()) {
528
- yield networkAddresses;
529
- }
530
- }
531
- }
532
-
533
- async network(name) {
534
- return this.#networks.get(name);
535
- }
536
-
537
- async *networks() {
538
- for (const network of this.#networks.values()) {
539
- yield network;
540
- }
541
- }
542
-
543
- async *subnets() {
544
- for (const subnet of this.#subnets.values()) {
545
- yield subnet;
546
- }
547
- }
548
-
549
- addNetwork(data) {
550
- if (!data?.name) {
551
- return undefined;
552
- }
553
-
554
- let network = this.#networks.get(data.name);
555
- if (network) {
556
- return network;
557
- }
558
-
559
- if(data instanceof Network) {
560
- this.#networks.set(data.name, data);
561
- return data;
562
- }
563
-
564
- network = new Network(this, data);
565
- this.#networks.set(data.name, network);
566
-
567
- const subnetAddress = network.subnetAddress;
568
-
569
- if (subnetAddress) {
570
- let subnet = this.#subnets.get(subnetAddress);
571
- if (!subnet) {
572
- subnet = new Subnet(this, subnetAddress);
573
- this.#subnets.set(subnetAddress, subnet);
574
- }
575
- network.subnet = subnet;
576
- subnet.networks.add(network);
577
- }
578
- return network;
579
- }
580
-
581
- addHost(host) {
582
- this.#hosts.set(host.name, host);
583
-
584
- for (const [name, iface] of Object.entries(host.networkInterfaces)) {
585
- const network = this.addNetwork({ name: iface.network });
586
- if (!network) {
587
- console.error("Missing network", host.name, name);
588
- } else {
589
- network.addHost(host);
590
- }
591
- }
592
- }
593
-
594
- get dnsAllowedUpdates() {
595
- return this.dns?.allowedUpdates || [];
596
- }
597
-
598
- get dnsRecordTTL() {
599
- return this.dns?.recordTTL || "1W";
600
- }
601
-
602
- get administratorEmail() {
603
- return this.#administratorEmail || "admin@" + this.domain;
604
- }
605
-
606
- get propertyNames() {
607
- return [...super.propertyNames, "domain" /*, "hosts"*/];
608
- }
609
-
610
- toJSON() {
611
- return {
612
- ...super.toJSON(),
613
- hosts: [...this.#hosts.keys()].sort()
614
- };
615
- }
616
- }
617
-
618
- export class Network extends Base {
619
- #hosts = new Map();
620
- kind;
621
- scope;
622
- metric;
623
- ipv4;
624
- ipv4_netmask;
625
- subnet;
650
+ export class Subnet extends Base {
651
+ networks = new Set();
626
652
 
627
653
  static get typeName() {
628
- return "network";
654
+ return "subnet";
629
655
  }
630
656
 
631
657
  constructor(owner, data) {
@@ -633,48 +659,7 @@ export class Network extends Base {
633
659
 
634
660
  Object.assign(this, data);
635
661
 
636
- if (this.ipv4) {
637
- const m = this.ipv4.match(/\/(\d+)$/);
638
- if (m) {
639
- this.ipv4_netmask = m[1];
640
- }
641
- }
642
-
643
- owner.addNetwork(this);
644
- }
645
-
646
- get subnetAddress() {
647
- if (this.ipv4) {
648
- const [addr, bits] = this.ipv4.split(/\//);
649
- const parts = addr.split(/\./);
650
- return parts.slice(0, bits / 8).join(".");
651
- }
652
- }
653
-
654
- async *hosts() {
655
- for (const host of this.#hosts.values()) {
656
- yield host;
657
- }
658
- }
659
-
660
- addHost(host) {
661
- this.#hosts.set(host.name, host);
662
- }
663
-
664
- get propertyNames() {
665
- return [...super.propertyNames, "kind", "ipv4", "scope", "metric"];
666
- }
667
- }
668
-
669
- export class Subnet extends Base {
670
- networks = new Set();
671
-
672
- static get typeName() {
673
- return "subnet";
674
- }
675
-
676
- constructor(owner, address) {
677
- super(owner, { name: address });
662
+ owner.addSubnet(this);
678
663
  }
679
664
 
680
665
  get address() {
package/types/model.d.mts CHANGED
@@ -23,13 +23,27 @@ export class Base {
23
23
  toJSON(): {};
24
24
  #private;
25
25
  }
26
- export class World {
26
+ export class Owner extends Base {
27
+ hosts(): AsyncGenerator<any, void, unknown>;
28
+ addHost(host: any): void;
29
+ network(name: any): any;
30
+ networks(): AsyncGenerator<any, void, unknown>;
31
+ addNetwork(network: any): void;
32
+ addSubnet(subnet: any): void;
33
+ subnet(name: any): any;
34
+ subnets(): MapIterator<any>;
35
+ toJSON(): {
36
+ networks: any[];
37
+ subnets: any[];
38
+ hosts: any[];
39
+ };
40
+ #private;
41
+ }
42
+ export class World extends Owner {
27
43
  static get types(): {
28
44
  [k: string]: typeof Location | typeof Host | typeof Network | typeof Subnet | typeof Service;
29
45
  };
30
46
  constructor(directory: any);
31
- directory: any;
32
- get name(): string;
33
47
  get world(): this;
34
48
  _loadType(name: any, type: any): Promise<any>;
35
49
  load(): Promise<void>;
@@ -39,9 +53,6 @@ export class World {
39
53
  domains(): AsyncGenerator<any, void, unknown>;
40
54
  location(name: any): Promise<any>;
41
55
  host(name: any): Promise<any>;
42
- addHost(host: any): void;
43
- addNetwork(network: any): void;
44
- network(name: any): void;
45
56
  subnets(): AsyncGenerator<any, void, unknown>;
46
57
  networkAddresses(): AsyncGenerator<{
47
58
  address: any;
@@ -49,6 +60,26 @@ export class World {
49
60
  }, void, unknown>;
50
61
  #private;
51
62
  }
63
+ export class Location extends Owner {
64
+ domain: any;
65
+ dns: any;
66
+ service(filter: any): Promise<any>;
67
+ services(filter: any): AsyncGenerator<any, void, unknown>;
68
+ networkAddresses(): AsyncGenerator<any, void, unknown>;
69
+ get dnsAllowedUpdates(): any;
70
+ get dnsRecordTTL(): any;
71
+ get administratorEmail(): any;
72
+ #private;
73
+ }
74
+ export class Network extends Owner {
75
+ kind: any;
76
+ scope: any;
77
+ metric: any;
78
+ ipv4: any;
79
+ subnet: any;
80
+ get ipv4_netmask(): any;
81
+ get subnetAddress(): any;
82
+ }
52
83
  export class Host extends Base {
53
84
  static prepareData(world: any, data: any): Promise<typeof Host>;
54
85
  networkInterfaces: {};
@@ -82,37 +113,6 @@ export class Host extends Base {
82
113
  }
83
114
  export class Model extends Host {
84
115
  }
85
- export class Location extends Base {
86
- domain: any;
87
- dns: any;
88
- hosts(): AsyncGenerator<any, void, unknown>;
89
- service(filter: any): Promise<any>;
90
- services(filter: any): AsyncGenerator<any, void, unknown>;
91
- networkAddresses(): AsyncGenerator<any, void, unknown>;
92
- networks(): AsyncGenerator<any, void, unknown>;
93
- subnets(): AsyncGenerator<any, void, unknown>;
94
- addNetwork(data: any): any;
95
- addHost(host: any): void;
96
- get dnsAllowedUpdates(): any;
97
- get dnsRecordTTL(): any;
98
- get administratorEmail(): any;
99
- toJSON(): {
100
- hosts: any[];
101
- };
102
- #private;
103
- }
104
- export class Network extends Base {
105
- kind: any;
106
- scope: any;
107
- metric: any;
108
- ipv4: any;
109
- ipv4_netmask: any;
110
- subnet: any;
111
- get subnetAddress(): any;
112
- hosts(): AsyncGenerator<any, void, unknown>;
113
- addHost(host: any): void;
114
- #private;
115
- }
116
116
  export class Subnet extends Base {
117
117
  networks: Set<any>;
118
118
  get address(): any;