pmcf 1.5.0 → 1.5.2

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.
@@ -5,18 +5,16 @@ import { join } from "node:path";
5
5
  import { writeLines, sectionLines } from "../src/model.mjs";
6
6
  import { prepare } from "../src/cmd.mjs";
7
7
 
8
- const { world, args } = prepare();
8
+ const { world, args, options } = prepare();
9
9
 
10
10
  const hostName = args[0];
11
11
 
12
12
  const host = await world.host(hostName);
13
13
 
14
- const targetDir = args[1] || `pkg/host-${host.hostName}`;
15
-
16
- await generateNetworkDefs(host, targetDir);
17
- await generateMachineInfo(host, targetDir);
18
- await copySshKeys(host, targetDir);
19
- await generateKnownHosts(world.hosts(), join(targetDir, "root", ".ssh"));
14
+ await generateNetworkDefs(host, options.output);
15
+ await generateMachineInfo(host, options.output);
16
+ await copySshKeys(host, options.output);
17
+ await generateKnownHosts(world.hosts(), join(options.output, "root", ".ssh"));
20
18
 
21
19
  console.log("provides", "host", ...host.provides);
22
20
  console.log("depends", `location-${host.location.name}`, ...host.depends);
@@ -5,12 +5,11 @@ import { join } from "node:path";
5
5
  import { writeLines, sectionLines } from "../src/model.mjs";
6
6
  import { prepare } from "../src/cmd.mjs";
7
7
 
8
- const { world, args } = prepare();
8
+ const { world, args, options } = prepare();
9
9
 
10
10
  const location = await world.location(args[0] || "SW");
11
- const targetDir = args[1];
12
11
 
13
- await generateLocationDefs(location, targetDir);
12
+ await generateLocationDefs(location, options.output);
14
13
 
15
14
  console.log(
16
15
  "provides",
@@ -5,10 +5,9 @@ import { createHmac } from "node:crypto";
5
5
  import { writeLines } from "../src/model.mjs";
6
6
  import { prepare } from "../src/cmd.mjs";
7
7
 
8
- const { world, args } = prepare();
8
+ const { world, args, options } = prepare();
9
9
 
10
10
  const location = await world.location(args[0] || "SW");
11
- const targetDir = args[1] || `pkg/mf-named-${location.name}`;
12
11
  const ttl = location.dnsRecordTTL;
13
12
  const updates = [
14
13
  Math.ceil(Date.now() / 1000),
@@ -20,7 +19,7 @@ const updates = [
20
19
 
21
20
  const NAME_LEN = 35;
22
21
 
23
- await generateNamedDefs(location, targetDir);
22
+ await generateNamedDefs(location, options.output);
24
23
 
25
24
  console.log("depends", "mf-named");
26
25
  console.log("replaces", "mf-named-zones");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/model.mjs CHANGED
@@ -51,6 +51,17 @@ export class Base {
51
51
  return this.constructor.typeName;
52
52
  }
53
53
 
54
+ get world() {
55
+ return this.owner.world;
56
+ }
57
+
58
+ get location() {
59
+ if (this instanceof Location) {
60
+ return this;
61
+ }
62
+ return this.owner.location;
63
+ }
64
+
54
65
  get host() {
55
66
  if (this instanceof Host) {
56
67
  return this;
@@ -58,11 +69,8 @@ export class Base {
58
69
  return this.owner.host;
59
70
  }
60
71
 
61
- get network() {
62
- if (this instanceof Network) {
63
- return this;
64
- }
65
- return this.owner.network;
72
+ async network(name) {
73
+ return this.owner.network(name);
66
74
  }
67
75
 
68
76
  #directory;
@@ -121,6 +129,10 @@ export class World {
121
129
  return "";
122
130
  }
123
131
 
132
+ get world() {
133
+ return this;
134
+ }
135
+
124
136
  async _loadType(name, type) {
125
137
  const baseName = type.baseName(name);
126
138
 
@@ -134,11 +146,22 @@ export class World {
134
146
  )
135
147
  );
136
148
 
149
+ let owner;
150
+ let path = baseName.split("/");
151
+
152
+ if (path.length > 1 && path[0] !== "model" && path[0] != "services") {
153
+ // TODO
154
+ path.length -= 1;
155
+ owner = await this._loadType(path.join("/"), Location);
156
+ } else {
157
+ owner = this;
158
+ }
159
+
137
160
  data.name = baseName;
138
161
 
139
162
  type = await type.prepareData(this, data);
140
- object = new type(this, data);
141
- this.#byName.set(data.name, object);
163
+ object = new type(owner, data);
164
+ this.#byName.set(object.name, object);
142
165
  }
143
166
 
144
167
  return object;
@@ -193,6 +216,10 @@ export class World {
193
216
  return this._loadType(name, Host);
194
217
  }
195
218
 
219
+ addHost(host) {}
220
+ addNetwork(network) {}
221
+ network(name) {}
222
+
196
223
  async *subnets() {
197
224
  for await (const location of this.locations()) {
198
225
  yield* location.subnets();
@@ -212,7 +239,6 @@ export class Host extends Base {
212
239
  networkInterfaces = {};
213
240
  services = {};
214
241
  postinstall = [];
215
- location;
216
242
  #extends = [];
217
243
  #provides = new Set();
218
244
  #replaces = new Set();
@@ -221,6 +247,7 @@ export class Host extends Base {
221
247
  #os;
222
248
  #distribution;
223
249
  #deployment;
250
+ #location;
224
251
 
225
252
  static get typeName() {
226
253
  return "host";
@@ -245,11 +272,15 @@ export class Host extends Base {
245
272
  constructor(owner, data) {
246
273
  super(owner, data);
247
274
 
275
+ if (data.location !== undefined) {
276
+ this.#location = data.location;
277
+ delete data.location;
278
+ }
279
+
248
280
  if (data.deployment !== undefined) {
249
281
  this.#deployment = data.deployment;
250
282
  delete data.deployment;
251
283
  }
252
-
253
284
  if (data.extends !== undefined) {
254
285
  this.#extends = data.extends;
255
286
  delete data.extends;
@@ -281,13 +312,13 @@ export class Host extends Base {
281
312
 
282
313
  Object.assign(this, { services: {}, networkInterfaces: {} }, data);
283
314
 
284
- this.location?.addHost(this);
315
+ owner.addHost(this);
285
316
 
286
317
  for (const [name, iface] of Object.entries(this.networkInterfaces)) {
287
318
  iface.host = this;
288
319
  iface.name = name;
289
320
  if (iface.network) {
290
- iface.network = this.location?.network(iface.network);
321
+ iface.network = this.network(iface.network);
291
322
  }
292
323
  }
293
324
 
@@ -307,6 +338,10 @@ export class Host extends Base {
307
338
  return this.#extends.map(e => this.expand(e));
308
339
  }
309
340
 
341
+ get location() {
342
+ return this.#location || super.location;
343
+ }
344
+
310
345
  get provides() {
311
346
  let provides = new Set(this.#provides);
312
347
  this.extends.forEach(h => (provides = provides.union(h.provides)));
@@ -453,10 +488,6 @@ export class Location extends Base {
453
488
  }
454
489
  }
455
490
 
456
- async load() {
457
- for await (const host of this.owner.hosts());
458
- }
459
-
460
491
  async *hosts() {
461
492
  for await (const host of this.owner.hosts()) {
462
493
  if (host.location === this) {
@@ -491,12 +522,7 @@ export class Location extends Base {
491
522
  }
492
523
  }
493
524
 
494
- network(name) {
495
- return this.#networks.get(name);
496
- }
497
-
498
525
  async *networkAddresses() {
499
- await this.load();
500
526
  for await (const host of this.hosts()) {
501
527
  for (const networkAddresses of host.networkAddresses()) {
502
528
  yield networkAddresses;
@@ -504,15 +530,17 @@ export class Location extends Base {
504
530
  }
505
531
  }
506
532
 
533
+ async network(name) {
534
+ return this.#networks.get(name);
535
+ }
536
+
507
537
  async *networks() {
508
- await this.load();
509
538
  for (const network of this.#networks.values()) {
510
539
  yield network;
511
540
  }
512
541
  }
513
542
 
514
543
  async *subnets() {
515
- await this.load();
516
544
  for (const subnet of this.#subnets.values()) {
517
545
  yield subnet;
518
546
  }
@@ -528,6 +556,11 @@ export class Location extends Base {
528
556
  return network;
529
557
  }
530
558
 
559
+ if(data instanceof Network) {
560
+ this.#networks.set(data.name, data);
561
+ return data;
562
+ }
563
+
531
564
  network = new Network(this, data);
532
565
  this.#networks.set(data.name, network);
533
566
 
@@ -571,7 +604,7 @@ export class Location extends Base {
571
604
  }
572
605
 
573
606
  get propertyNames() {
574
- return [...super.propertyNames, "domain"];
607
+ return [...super.propertyNames, "domain" /*, "hosts"*/];
575
608
  }
576
609
 
577
610
  toJSON() {
@@ -606,6 +639,8 @@ export class Network extends Base {
606
639
  this.ipv4_netmask = m[1];
607
640
  }
608
641
  }
642
+
643
+ owner.addNetwork(this);
609
644
  }
610
645
 
611
646
  get subnetAddress() {
@@ -774,8 +809,13 @@ function extractFrom(object, propertyNames) {
774
809
  const json = {};
775
810
  for (const p of propertyNames) {
776
811
  const value = object[p];
812
+
777
813
  if (value !== undefined) {
778
- json[p] = value;
814
+ if (value instanceof Base) {
815
+ json[p] = { name: object.name };
816
+ } else {
817
+ json[p] = value;
818
+ }
779
819
  }
780
820
  }
781
821
  return json;
package/types/model.d.mts CHANGED
@@ -11,8 +11,10 @@ export class Base {
11
11
  name: any;
12
12
  description: any;
13
13
  get typeName(): any;
14
+ get world(): any;
15
+ get location(): any;
14
16
  get host(): any;
15
- get network(): any;
17
+ network(name: any): Promise<any>;
16
18
  set directory(directory: any);
17
19
  get directory(): any;
18
20
  expand(object: any): any;
@@ -23,11 +25,12 @@ export class Base {
23
25
  }
24
26
  export class World {
25
27
  static get types(): {
26
- [k: string]: typeof Host | typeof Network | typeof Location | typeof Subnet | typeof Service;
28
+ [k: string]: typeof Location | typeof Host | typeof Network | typeof Subnet | typeof Service;
27
29
  };
28
30
  constructor(directory: any);
29
31
  directory: any;
30
32
  get name(): string;
33
+ get world(): this;
31
34
  _loadType(name: any, type: any): Promise<any>;
32
35
  load(): Promise<void>;
33
36
  named(name: any): Promise<any>;
@@ -36,6 +39,9 @@ export class World {
36
39
  domains(): AsyncGenerator<any, void, unknown>;
37
40
  location(name: any): Promise<any>;
38
41
  host(name: any): Promise<any>;
42
+ addHost(host: any): void;
43
+ addNetwork(network: any): void;
44
+ network(name: any): void;
39
45
  subnets(): AsyncGenerator<any, void, unknown>;
40
46
  networkAddresses(): AsyncGenerator<{
41
47
  address: any;
@@ -48,7 +54,6 @@ export class Host extends Base {
48
54
  networkInterfaces: {};
49
55
  services: {};
50
56
  postinstall: any[];
51
- location: any;
52
57
  get deployment(): any;
53
58
  get extends(): any[];
54
59
  get provides(): Set<any>;
@@ -80,11 +85,9 @@ export class Model extends Host {
80
85
  export class Location extends Base {
81
86
  domain: any;
82
87
  dns: any;
83
- load(): Promise<void>;
84
88
  hosts(): AsyncGenerator<any, void, unknown>;
85
89
  service(filter: any): Promise<any>;
86
90
  services(filter: any): AsyncGenerator<any, void, unknown>;
87
- network(name: any): any;
88
91
  networkAddresses(): AsyncGenerator<any, void, unknown>;
89
92
  networks(): AsyncGenerator<any, void, unknown>;
90
93
  subnets(): AsyncGenerator<any, void, unknown>;