-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
577 lines (523 loc) · 20.2 KB
/
mod.rs
File metadata and controls
577 lines (523 loc) · 20.2 KB
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
use anyhow::{Result, anyhow};
use serde::Deserialize;
use std::path::PathBuf;
// TODO(sg): Switch to serde_yaml's `apply_merge()` once it supports recursive merges, cf.
// https://github.com/dtolnay/serde-yaml/issues/362
use yaml_merge_keys::merge_keys_serde;
use crate::Reclass;
use crate::config::RenderOpts;
use crate::fsutil::to_lexical_absolute;
use crate::list::{List, RemovableList, UniqueList};
use crate::refs::{ResolveState, Token};
use crate::types::{Mapping, Value};
mod nodeinfo;
pub(crate) use nodeinfo::*;
/// Represents a Reclass node or class
#[derive(Debug, Default, Deserialize)]
pub struct Node {
/// List of Reclass applications for this node
#[serde(default)]
pub applications: RemovableList,
/// List of Reclass classes included by this node
#[serde(default)]
pub classes: UniqueList,
/// Reclass parameters for this node as parsed from YAML
#[serde(default, rename = "parameters")]
params: serde_yaml::Mapping,
/// Reclass parameters for this node converted into our own mapping type
#[serde(skip)]
parameters: Mapping,
/// Location of this node relative to `classes_path`. `None` for nodes.
#[serde(skip)]
own_loc: Option<PathBuf>,
/// Information about the node, empty (default value) for Node objects parsed from classes.
#[serde(skip)]
meta: NodeInfoMeta,
}
impl Node {
/// Parse node from file with basename `name` in `r.nodes_path`.
///
/// The heavy lifting is done in `Reclass.discover_nodes()` and `Node::from_str`.
pub fn parse(r: &Reclass, name: &str) -> Result<Self> {
let nodeinfo = r.nodes.get(name).ok_or(anyhow!("Unknown node {name}"))?;
let invpath = r.config.node_path(&nodeinfo.path);
let ncontents = std::fs::read_to_string(invpath.canonicalize()?)?;
let uri = format!("yaml_fs://{}", to_lexical_absolute(&invpath)?.display());
let npath = nodeinfo.path.with_extension("");
// NOTE(sg): parts is only the name when compose-node-name isn't enabled
let meta_parts = if r.config.compose_node_name {
npath.clone()
} else {
PathBuf::from(name)
};
let meta = NodeInfoMeta::new(name, name, &uri, meta_parts, npath, "base");
Node::from_str(meta, None, &ncontents)
}
/// Initializes a `Node` struct from a string.
///
/// The given string is parsed as YAML. Parameter `npath` is interpreted as the node's location
/// in the class hierarchy. If the parameter is `None`, relative includes are treated as
/// relative to `classes_path`.
pub fn from_str(meta: NodeInfoMeta, npath: Option<PathBuf>, ncontents: &str) -> Result<Self> {
let mut n: Node = serde_yaml::from_str(ncontents)?;
n.own_loc = npath;
n.meta = meta;
// Transform any relative class names to absolute class names, based on the new node's
// `own_loc`.
let mut classes = UniqueList::with_capacity(n.classes.len());
for cls in n.classes.items_iter() {
classes.append_if_new(n.abs_class_name(cls)?);
}
classes.shrink_to_fit();
n.classes = classes;
// Resolve YAML merge keys in `params`
let p = merge_keys_serde(serde_yaml::Value::from(n.params))?
.as_mapping()
.unwrap()
.clone();
n.params = p;
// Convert serde_yaml::Mapping into our own Mapping type
n.parameters = n.params.clone().into();
Ok(n)
}
/// Turns a relative class name (prefixed with one or more `.`) into an absolute class name
/// based on the current `Node`'s location (field `own_loc`).
///
/// Note that an arbitrary number of leading dots will be consumed, but the top-most directory
/// which can anchor the class is the directory given as `classes_path`.
fn abs_class_name(&self, class: &str) -> Result<String> {
if !class.starts_with('.') {
// bail early for absolute classes
return Ok(class.to_string());
}
let mut cls = class;
// Parent starts out as the directory of our own class, or '.' for nodes
let mut parent = if let Some(loc) = &self.own_loc {
PathBuf::from(loc)
} else {
PathBuf::from(".")
};
if cls.starts_with('.') {
// push placeholder, so the popping in the loop correctly places .foo in the same
// directory as ourselves.
parent.push("<placeholder>");
}
// Process any number of prefixed `.`, moving up the hierarchy, stopping at the root.
while let Some(next) = cls.strip_prefix('.') {
parent.pop();
cls = next;
}
// Render the absolute path of the class (relative to `self.classes_dir`)
let mut absclass = String::new();
// If we have a relative reference past the lookup root, `components()` will be empty, and
// the resulting absolute path will be based in the lookup root.
for d in parent.components() {
match d {
std::path::Component::Normal(p) => {
absclass.push_str(p.to_str().unwrap());
absclass.push('.');
}
// if we've reached CurDir, we've reached the lookup root exactly
std::path::Component::CurDir => {}
_ => {
return Err(anyhow!(
"Unexpected non-normal path segment in class lookup: {d:?}",
));
}
}
}
absclass.push_str(cls);
Ok(absclass)
}
/// Looks up and parses `Node` from provided `class` string relative to own location.
///
/// If the current Node's location is empty, relative class references inherently turn into
/// absolute references, since relative references can't escape the `r.classes_path` base
/// directory.
///
/// If the class is not prefixed with one or more dots, it's looked up relative to
/// `r.classes_path`.
///
/// The method extracts the the relative file path for the class in `r.classes_dir` from
/// `r.classes`.
fn read_class(&self, r: &Reclass, class: &str) -> Result<Option<Self>> {
let cls = self.abs_class_name(class)?;
// Lookup path for provided class in r.classes, handling ignore_class_notfound
let Some(classinfo) = r.classes.get(&cls) else {
// ignore_class_notfound_regexp is only applied if ignore_class_notfound == true.
// By default the regexset has a single pattern for .* so that all missing classes are
// ignored.
if r.config.is_class_ignored(&cls) {
return Ok(None);
}
if r.config.ignore_class_notfound {
// return an error informing the user that we didn't ignore the missing class
// based on the configured regex patterns.
eprintln!(
"Missing class '{cls}' not ignored due to configured regex patterns: [{}]",
r.config
.get_ignore_class_notfound_regexp()
.iter()
.map(|s| format!("'{s}'"))
.collect::<Vec<_>>()
.join(", ")
);
}
return Err(anyhow!("Class {cls} not found"));
};
// Render inventory path of class based from `r.classes_path`.
let invpath = r.config.class_path(&classinfo.path);
// Load file contents and create Node
let mut meta = NodeInfoMeta::default();
let ccontents = std::fs::read_to_string(invpath.canonicalize()?)?;
meta.uri = format!("yaml_fs://{}", invpath.canonicalize()?.display());
Ok(Some(
Node::from_str(meta, Some(classinfo.loc.clone()), &ccontents)
.map_err(|e| anyhow!("Deserializing {cls}: {e}"))?,
))
}
/// Merges self into other, then updates self with merged values from other
fn merge_into(&mut self, other: &mut Self, opts: &RenderOpts) -> Result<()> {
// We use std::mem::take() here so we can merge self.applications into other.applications
// without having to call `clone()` twice. This doesn't destroy `self.applications` because
// we update `self.applications` with the result of the merge immediately afterwards.
let self_apps = std::mem::take(&mut self.applications);
other.applications.merge(self_apps);
self.applications = other.applications.clone();
// We use std::mem::take() here so we can merge self.classes into other.classes without
// having to call `clone()` twice. This doesn't destroy `self.classes` because we update
// `self.classes` with the result of the merge immediately afterwards.
let self_classes = std::mem::take(&mut self.classes);
other.classes.merge(self_classes);
self.classes = other.classes.clone();
other
.parameters
.merge(&self.parameters, &ResolveState::default(), opts)?;
self.parameters = other.parameters.clone();
Ok(())
}
/// Recursively loads classes and merges loaded data into self
fn render_impl(&mut self, r: &Reclass, seen: &mut Vec<String>, root: &mut Node) -> Result<()> {
for cls in self.classes.items_iter() {
let cls = if cls.contains("${") {
// Resolve any potential references if the class name contains an opening reference
// symbol.
let clstoken = Token::parse(&cls.clone())?;
if let Some(clstoken) = clstoken {
// If we got a token, render it, and convert it into a string with
// `raw_string()` to ensure no spurious quotes are injected.
let mut state = ResolveState::default();
clstoken
.render(&root.parameters, &mut state, &r.config.get_render_opts())?
.raw_string(&r.config.get_render_opts())?
} else {
// If Token::parse() returns None, the class name can't contain any references,
// just convert cls into an owned String.
cls.clone()
}
} else {
// If the class name doesn't contain any opening reference symbols, it can't
// contain any references, just convert cls into an owned String.
cls.clone()
};
// Check if we've seen the class already after resolving any references in the class
// name.
if seen.contains(&cls) {
continue;
}
// Load class, respecting the `ignore_class_notfound` option
let maybec = self.read_class(r, &cls);
let Ok(Some(mut c)) = maybec else {
if let Ok(None) = maybec {
#[cfg(not(feature = "bench"))]
eprintln!("ignore missing class {cls}");
continue;
}
return Err(maybec.unwrap_err());
};
// render class so we pick up further classes included in it
c.render_impl(r, seen, root)?;
// NOTE(sg): we don't need to merge here, since we've already merged into root as part
// of the recursive call to `render_impl()`
seen.push(cls.clone());
}
// merge self into root, then update self with merged values
self.merge_into(root, &r.config.get_render_opts())
}
/// Renders the Node's parameters by interpolating Reclass references and flattening
/// ValueLists.
fn render_parameters(&mut self, opts: &RenderOpts) -> Result<()> {
let p = std::mem::take(&mut self.parameters);
let mut f = Value::Mapping(p);
f.render_with_self(opts)?;
match f {
Value::Mapping(m) => {
self.parameters = m;
Ok(())
}
_ => Err(anyhow!(
"Rendered parameters are not a Mapping but a {}",
f.variant()
)),
}
}
/// Load included classes (recursively), and merge parameters.
///
/// Note that this method doesn't flatten overwritten parameters.
pub fn render(&mut self, r: &Reclass) -> Result<()> {
let mut base = Node {
// NOTE(sg): Similar to Python reclass, we initialize the base node with any classes
// that are included via the `class_mappings` configuration parameter.
classes: r.config.get_class_mappings(&self.meta)?,
..Default::default()
};
// NOTE(sg): We merge the `_reclass_` meta parameter into the base node before starting
// class loading. This roughly corresponds to Python reclass's
// `_get_automatic_parameters()`.
base.parameters
.insert("_reclass_".into(), self.meta.as_reclass(&r.config)?.into())?;
let mut seen = vec![];
let mut root = Node::default();
// First render base (i.e. mapped classes) into an empty Node, and update base with the
// result
base.render_impl(r, &mut seen, &mut root)?;
// Then render ourselves into the rendered base and update ourselves with the result
self.render_impl(r, &mut seen, &mut base)?;
self.render_parameters(&r.config.get_render_opts())
}
}
#[cfg(test)]
fn make_reclass() -> Reclass {
Reclass::new("./tests/inventory", "nodes", "classes", false).unwrap()
}
#[cfg(test)]
mod node_tests {
use super::*;
use std::str::FromStr;
#[test]
fn test_parse() {
let r = make_reclass();
let n = Node::parse(&r, "n1").unwrap();
assert_eq!(
n.classes,
UniqueList::from(vec!["cls1".to_owned(), "cls2".to_owned()])
);
assert_eq!(
n.applications,
RemovableList::from(vec!["app1".to_owned(), "app2".to_owned()])
);
let expected = r#"
foo:
foo: foo
bar:
foo: foo
"#;
let expected: serde_yaml::Mapping = serde_yaml::from_str(expected).unwrap();
assert_eq!(n.parameters, expected.into());
}
#[test]
#[should_panic(expected = "Unknown node n0")]
fn test_parse_error() {
let r = make_reclass();
Node::parse(&r, "n0").unwrap();
}
#[test]
fn test_from_str() {
let node = r#"
classes:
- foo
- bar
applications:
- foo
- bar
parameters:
foo:
bar: bar
"#;
let n = Node::from_str(NodeInfoMeta::default(), None, node).unwrap();
assert_eq!(
n.classes,
UniqueList::from(vec!["foo".to_owned(), "bar".to_owned()])
);
assert_eq!(
n.applications,
RemovableList::from(vec!["foo".to_owned(), "bar".to_owned()])
);
let mut foo = serde_yaml::Mapping::new();
foo.insert(
serde_yaml::Value::from("bar"),
serde_yaml::Value::from("bar"),
);
let mut params = serde_yaml::Mapping::new();
params.insert(serde_yaml::Value::from("foo"), serde_yaml::Value::from(foo));
assert_eq!(n.params, params);
}
#[test]
fn test_from_str_merge_keys() {
let node = r#"
parameters:
foo: &foo
bar: bar
fooer:
<<: *foo
"#;
let n = Node::from_str(NodeInfoMeta::default(), None, node).unwrap();
let expected = r#"
foo:
bar: bar
fooer:
bar: bar
"#;
let expected: serde_yaml::Mapping = serde_yaml::from_str(expected).unwrap();
assert_eq!(n.params, expected);
}
#[test]
fn test_from_str_merge_keys_nested() {
let node = r#"
parameters:
foo: &foo
bar: bar
fooer:
bar:
<<: *foo
"#;
let n = Node::from_str(NodeInfoMeta::default(), None, node).unwrap();
let expected = r#"
foo:
bar: bar
fooer:
bar:
bar: bar
"#;
let expected: serde_yaml::Mapping = serde_yaml::from_str(expected).unwrap();
assert_eq!(n.params, expected);
}
#[test]
fn test_from_str_merge_keys_recursive() {
// NOTE(sg): This test fails when using serde_yaml's `apply_merge` instead of the
// yaml-merge-keys crate. Example input taken from the serde_yaml issue linked at the top.
let node = r#"
parameters:
a: &a
a: a
b: &b
<<: *a
c:
- <<: *a
- <<: *b
"#;
let n = Node::from_str(NodeInfoMeta::default(), None, node).unwrap();
let expected = r#"
a:
a: a
b:
a: a
c:
- a: a
- a: a
"#;
let expected: serde_yaml::Mapping = serde_yaml::from_str(expected).unwrap();
assert_eq!(n.params, expected);
}
#[test]
fn abs_class_name_already_abs() {
let n = Node::default();
let p = n.abs_class_name("foo").unwrap();
assert_eq!(p, "foo");
}
#[test]
fn abs_class_name_already_abs_in_subclass() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name("foo.bar").unwrap();
assert_eq!(p, "foo.bar");
}
#[test]
fn abs_class_name_same_dir() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name(".foo").unwrap();
assert_eq!(p, "foo.bar.baz.foo");
}
#[test]
fn abs_class_name_same_dir_subclass() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name(".foo.bar").unwrap();
assert_eq!(p, "foo.bar.baz.foo.bar");
}
#[test]
fn abs_class_name_parent_dir() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name("..foo").unwrap();
assert_eq!(p, "foo.bar.foo");
}
#[test]
fn abs_class_name_multi_parent_dir() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name("...foo").unwrap();
assert_eq!(p, "foo.foo");
}
#[test]
fn abs_class_name_exact_root_dir() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name("....foo").unwrap();
assert_eq!(p, "foo");
}
#[test]
fn abs_class_name_past_root_dir() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name(".....foo").unwrap();
assert_eq!(p, "foo");
}
#[test]
fn abs_class_name_past_root_dir_subclass() {
let mut c = Node::default();
let cpath = PathBuf::from("foo/bar/baz");
c.own_loc = Some(cpath);
let p = c.abs_class_name(".....foo.bar").unwrap();
assert_eq!(p, "foo.bar");
}
#[test]
fn test_read_class() {
let r = make_reclass();
let n = Node::parse(&r, "n1").unwrap();
let c = n.read_class(&r, "cls1").unwrap().unwrap();
let expected = r#"
foo:
foo: cls1
bar: cls1
baz: cls1
"#;
let expected = Mapping::from_str(expected).unwrap();
assert_eq!(c.parameters, expected);
}
#[test]
fn test_read_class_relative() {
let r = make_reclass();
let n = Node::parse(&r, "n1").unwrap();
let c1 = n.read_class(&r, "nested.cls1").unwrap().unwrap();
let c2 = c1.read_class(&r, ".cls2").unwrap().unwrap();
let expected = r#"
foo:
foo: nested.cls2
"#;
let expected = Mapping::from_str(expected).unwrap();
assert_eq!(c2.parameters, expected);
}
}
#[cfg(test)]
mod node_render_tests;
#[cfg(test)]
mod node_render_tests_ignore_class_notfound_regexp;