rash_core 2.20.0

Declarative shell scripting using Rust native bindings
Documentation
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/// ANCHOR: module
/// # vdo
///
/// Manage VDO (Virtual Data Optimizer) volumes.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: full
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Example
///
/// ```yaml
/// - name: Create VDO volume
///   vdo:
///     name: vdo_vol
///     device: /dev/sdb
///     logicalsize: 100G
///
/// - name: Create VDO volume with compression disabled
///   vdo:
///     name: vdo_vol
///     device: /dev/sdb
///     logicalsize: 200G
///     compression: false
///
/// - name: Create VDO volume with deduplication disabled
///   vdo:
///     name: vdo_vol
///     device: /dev/sdb
///     logicalsize: 50G
///     deduplication: false
///
/// - name: Remove VDO volume
///   vdo:
///     name: vdo_vol
///     state: absent
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::logger::diff;
use crate::modules::{Module, ModuleResult, parse_params};

#[cfg(feature = "docs")]
use rash_derive::DocJsonSchema;

use log::trace;
use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_json;
use serde_norway::{Value as YamlValue, value};
use std::process::{Command, Output};
#[cfg(feature = "docs")]
use strum_macros::{Display, EnumString};

#[derive(Clone, Copy, Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(EnumString, Display, JsonSchema))]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
enum State {
    #[default]
    Present,
    Absent,
}

fn default_compression() -> bool {
    true
}

fn default_deduplication() -> bool {
    true
}

#[derive(Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// VDO volume name.
    name: String,
    /// Underlying block device.
    device: Option<String>,
    /// Logical size of the VDO volume (e.g., 100G, 1T).
    logicalsize: Option<String>,
    /// Enable compression.
    /// **[default: `true`]**
    #[serde(default = "default_compression")]
    compression: bool,
    /// Enable deduplication.
    /// **[default: `true`]**
    #[serde(default = "default_deduplication")]
    deduplication: bool,
    /// Whether the VDO volume should exist or not.
    /// **[default: `"present"`]**
    #[serde(default)]
    state: State,
}

#[derive(Debug)]
pub struct Vdo;

impl Module for Vdo {
    fn get_name(&self) -> &str {
        "vdo"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        Ok((
            vdo_module(parse_params(optional_params)?, check_mode)?,
            None,
        ))
    }

    fn force_string_on_params(&self) -> bool {
        false
    }

    #[cfg(feature = "docs")]
    fn get_json_schema(&self) -> Option<Schema> {
        Some(Params::get_json_schema())
    }
}

struct VdoClient {
    check_mode: bool,
}

#[derive(Debug, Clone)]
struct VdoInfo {
    #[allow(dead_code)]
    name: String,
    device: String,
    logical_size: Option<String>,
    physical_size: Option<String>,
    compression: Option<String>,
    deduplication: Option<String>,
    operating_mode: Option<String>,
    write_policy: Option<String>,
}

impl VdoClient {
    pub fn new(check_mode: bool) -> Self {
        VdoClient { check_mode }
    }

    fn exec_cmd(&self, cmd: &mut Command, check_success: bool) -> Result<Output> {
        let output = cmd
            .output()
            .map_err(|e| Error::new(ErrorKind::SubprocessFail, e))?;
        trace!("command: `{cmd:?}`");
        trace!("{output:?}");

        if check_success && !output.status.success() {
            return Err(Error::new(
                ErrorKind::SubprocessFail,
                format!(
                    "Error executing VDO command: {}",
                    String::from_utf8_lossy(&output.stderr)
                ),
            ));
        }
        Ok(output)
    }

    pub fn vdo_exists(&self, name: &str) -> Result<bool> {
        let output = self.exec_cmd(Command::new("vdo").args(["list"]).env("LC_ALL", "C"), false)?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        for line in stdout.lines() {
            if line.trim() == name {
                return Ok(true);
            }
        }
        Ok(false)
    }

    pub fn get_vdo_info(&self, name: &str) -> Result<Option<VdoInfo>> {
        let output = self.exec_cmd(
            Command::new("vdo")
                .args(["status", "--name", name])
                .env("LC_ALL", "C"),
            false,
        )?;

        if !output.status.success() {
            return Ok(None);
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        Ok(Some(parse_vdo_status(&stdout, name)?))
    }

    pub fn create_vdo(&self, params: &Params) -> Result<VdoResult> {
        let device = params.device.as_ref().ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidData,
                "device is required when state is present",
            )
        })?;

        let logicalsize = params.logicalsize.as_ref().ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidData,
                "logicalsize is required when state is present",
            )
        })?;

        let _vdo_path = format!("/dev/mapper/{}", params.name);

        diff(
            format!("VDO volume {}: absent", params.name),
            format!(
                "VDO volume {}: present (device={}, logicalsize={})",
                params.name, device, logicalsize
            ),
        );

        if self.check_mode {
            return Ok(VdoResult::new(true, None));
        }

        let mut cmd = Command::new("vdo");
        cmd.args(["create", "--name", &params.name])
            .args(["--device", device])
            .args(["--vdoLogicalSize", logicalsize]);

        if !params.compression {
            cmd.arg("--compression");
            cmd.arg("disabled");
        }

        if !params.deduplication {
            cmd.arg("--deduplication");
            cmd.arg("disabled");
        }

        let output = self.exec_cmd(&mut cmd, true)?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let output_str = if stdout.trim().is_empty() {
            None
        } else {
            Some(stdout.trim().to_string())
        };

        Ok(VdoResult::new(true, output_str))
    }

    pub fn remove_vdo(&self, params: &Params) -> Result<VdoResult> {
        let vdo_path = format!("/dev/mapper/{}", params.name);

        diff(
            format!("VDO volume {}: present ({})", params.name, vdo_path),
            format!("VDO volume {}: absent", params.name),
        );

        if self.check_mode {
            return Ok(VdoResult::new(true, None));
        }

        let mut cmd = Command::new("vdo");
        cmd.args(["remove", "--name", &params.name]);

        let output = self.exec_cmd(&mut cmd, true)?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let output_str = if stdout.trim().is_empty() {
            None
        } else {
            Some(stdout.trim().to_string())
        };

        Ok(VdoResult::new(true, output_str))
    }

    pub fn update_vdo_settings(&self, params: &Params) -> Result<VdoResult> {
        let current_info = self.get_vdo_info(&params.name)?;
        let current_info = current_info.ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidData,
                "Cannot update settings: VDO volume does not exist",
            )
        })?;

        let compression_changed = current_info.compression.as_ref().is_some_and(|c| {
            let enabled = c == "enabled" || c == "online";
            enabled != params.compression
        });

        let deduplication_changed = current_info.deduplication.as_ref().is_some_and(|d| {
            let enabled = d == "enabled" || d == "online";
            enabled != params.deduplication
        });

        if !compression_changed && !deduplication_changed {
            return Ok(VdoResult::no_change());
        }

        if compression_changed {
            diff(
                format!(
                    "compression: {}",
                    current_info
                        .compression
                        .as_ref()
                        .map_or("unknown", |s| s.as_str())
                ),
                format!(
                    "compression: {}",
                    if params.compression {
                        "enabled"
                    } else {
                        "disabled"
                    }
                ),
            );
        }

        if deduplication_changed {
            diff(
                format!(
                    "deduplication: {}",
                    current_info
                        .deduplication
                        .as_ref()
                        .map_or("unknown", |s| s.as_str())
                ),
                format!(
                    "deduplication: {}",
                    if params.deduplication {
                        "enabled"
                    } else {
                        "disabled"
                    }
                ),
            );
        }

        if self.check_mode {
            return Ok(VdoResult::new(true, None));
        }

        let mut cmd = Command::new("vdo");
        cmd.args(["modify", "--name", &params.name]);

        if compression_changed {
            cmd.arg("--compression");
            cmd.arg(if params.compression {
                "enabled"
            } else {
                "disabled"
            });
        }

        if deduplication_changed {
            cmd.arg("--deduplication");
            cmd.arg(if params.deduplication {
                "enabled"
            } else {
                "disabled"
            });
        }

        let output = self.exec_cmd(&mut cmd, true)?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let output_str = if stdout.trim().is_empty() {
            None
        } else {
            Some(stdout.trim().to_string())
        };

        Ok(VdoResult::new(true, output_str))
    }
}

fn parse_vdo_status(output: &str, name: &str) -> Result<VdoInfo> {
    let mut device = String::new();
    let mut logical_size = None;
    let mut physical_size = None;
    let mut compression = None;
    let mut deduplication = None;
    let mut operating_mode = None;
    let mut write_policy = None;

    let mut in_vdo_section = false;

    for line in output.lines() {
        let line = line.trim();

        if line.starts_with("VDO volume") && line.contains(name) {
            in_vdo_section = true;
            continue;
        }

        if in_vdo_section {
            if line.is_empty() || line.starts_with("VDO volume") {
                break;
            }

            if let Some(value) = parse_field(line, "Device mapper name") {
                device = value;
            } else if let Some(value) = parse_field(line, "Physical size") {
                physical_size = Some(value);
            } else if let Some(value) = parse_field(line, "Logical size") {
                logical_size = Some(value);
            } else if let Some(value) = parse_field(line, "Compression") {
                compression = Some(value);
            } else if let Some(value) = parse_field(line, "Deduplication") {
                deduplication = Some(value);
            } else if let Some(value) = parse_field(line, "Operating mode") {
                operating_mode = Some(value);
            } else if let Some(value) = parse_field(line, "Write policy") {
                write_policy = Some(value);
            }
        }
    }

    Ok(VdoInfo {
        name: name.to_string(),
        device,
        logical_size,
        physical_size,
        compression,
        deduplication,
        operating_mode,
        write_policy,
    })
}

fn parse_field(line: &str, field_name: &str) -> Option<String> {
    if line.starts_with(field_name) {
        let colon_pos = line.find(':')?;
        Some(line[colon_pos + 1..].trim().to_string())
    } else {
        None
    }
}

#[derive(Debug)]
struct VdoResult {
    changed: bool,
    output: Option<String>,
}

impl VdoResult {
    fn new(changed: bool, output: Option<String>) -> Self {
        VdoResult { changed, output }
    }

    fn no_change() -> Self {
        VdoResult {
            changed: false,
            output: None,
        }
    }
}

fn validate_params(params: &Params) -> Result<()> {
    if params.name.is_empty() {
        return Err(Error::new(ErrorKind::InvalidData, "name cannot be empty"));
    }

    if params.state == State::Present {
        if params.device.is_none() {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "device is required when state is present",
            ));
        }
        if params.logicalsize.is_none() {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "logicalsize is required when state is present",
            ));
        }
    }

    if let Some(device) = &params.device {
        if device.is_empty() {
            return Err(Error::new(ErrorKind::InvalidData, "device cannot be empty"));
        }
        if !device.starts_with('/') {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "device must be an absolute path",
            ));
        }
    }

    if let Some(size) = &params.logicalsize
        && size.is_empty()
    {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "logicalsize cannot be empty",
        ));
    }

    Ok(())
}

fn vdo_module(params: Params, check_mode: bool) -> Result<ModuleResult> {
    validate_params(&params)?;

    let client = VdoClient::new(check_mode);
    let vdo_exists = client.vdo_exists(&params.name)?;

    let result = match params.state {
        State::Present => {
            if vdo_exists {
                client.update_vdo_settings(&params)?
            } else {
                client.create_vdo(&params)?
            }
        }
        State::Absent => {
            if vdo_exists {
                client.remove_vdo(&params)?
            } else {
                VdoResult::no_change()
            }
        }
    };

    let mut extra = serde_json::Map::new();
    extra.insert(
        "name".to_string(),
        serde_json::Value::String(params.name.clone()),
    );
    extra.insert(
        "exists".to_string(),
        serde_json::Value::Bool(client.vdo_exists(&params.name)?),
    );

    if let Some(info) = client.get_vdo_info(&params.name)? {
        extra.insert("device".to_string(), serde_json::Value::String(info.device));
        if let Some(size) = info.logical_size {
            extra.insert("logical_size".to_string(), serde_json::Value::String(size));
        }
        if let Some(size) = info.physical_size {
            extra.insert("physical_size".to_string(), serde_json::Value::String(size));
        }
        if let Some(c) = info.compression {
            extra.insert("compression".to_string(), serde_json::Value::String(c));
        }
        if let Some(d) = info.deduplication {
            extra.insert("deduplication".to_string(), serde_json::Value::String(d));
        }
        if let Some(m) = info.operating_mode {
            extra.insert("operating_mode".to_string(), serde_json::Value::String(m));
        }
        if let Some(w) = info.write_policy {
            extra.insert("write_policy".to_string(), serde_json::Value::String(w));
        }
    }

    Ok(ModuleResult {
        changed: result.changed,
        output: result.output,
        extra: Some(value::to_value(extra)?),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_params() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            device: /dev/sdb
            logicalsize: 100G
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(
            params,
            Params {
                name: "vdo_vol".to_owned(),
                device: Some("/dev/sdb".to_owned()),
                logicalsize: Some("100G".to_owned()),
                compression: true,
                deduplication: true,
                state: State::Present,
            }
        );
    }

    #[test]
    fn test_parse_params_with_all_options() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            device: /dev/sdb
            logicalsize: 100G
            compression: false
            deduplication: false
            state: absent
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, State::Absent);
        assert!(!params.compression);
        assert!(!params.deduplication);
    }

    #[test]
    fn test_parse_params_absent() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            state: absent
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, State::Absent);
        assert_eq!(params.device, None);
        assert_eq!(params.logicalsize, None);
    }

    #[test]
    fn test_parse_params_compression_disabled() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            device: /dev/sdb
            logicalsize: 200G
            compression: false
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert!(!params.compression);
        assert!(params.deduplication);
    }

    #[test]
    fn test_parse_params_deduplication_disabled() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            device: /dev/sdb
            logicalsize: 50G
            deduplication: false
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert!(params.compression);
        assert!(!params.deduplication);
    }

    #[test]
    fn test_validate_params_empty_name() {
        let params = Params {
            name: "".to_string(),
            device: Some("/dev/sdb".to_string()),
            logicalsize: Some("100G".to_string()),
            compression: true,
            deduplication: true,
            state: State::Present,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_missing_device() {
        let params = Params {
            name: "vdo_vol".to_string(),
            device: None,
            logicalsize: Some("100G".to_string()),
            compression: true,
            deduplication: true,
            state: State::Present,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_missing_logicalsize() {
        let params = Params {
            name: "vdo_vol".to_string(),
            device: Some("/dev/sdb".to_string()),
            logicalsize: None,
            compression: true,
            deduplication: true,
            state: State::Present,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_empty_device() {
        let params = Params {
            name: "vdo_vol".to_string(),
            device: Some("".to_string()),
            logicalsize: Some("100G".to_string()),
            compression: true,
            deduplication: true,
            state: State::Present,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_relative_device() {
        let params = Params {
            name: "vdo_vol".to_string(),
            device: Some("dev/sdb".to_string()),
            logicalsize: Some("100G".to_string()),
            compression: true,
            deduplication: true,
            state: State::Present,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_absent_no_device() {
        let params = Params {
            name: "vdo_vol".to_string(),
            device: None,
            logicalsize: None,
            compression: true,
            deduplication: true,
            state: State::Absent,
        };
        assert!(validate_params(&params).is_ok());
    }

    #[test]
    fn test_parse_params_invalid_field() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            name: vdo_vol
            device: /dev/sdb
            invalid_field: value
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_parse_field() {
        let line = "Compression: enabled";
        assert_eq!(
            parse_field(line, "Compression"),
            Some("enabled".to_string())
        );

        let line = "Logical size: 100G";
        assert_eq!(parse_field(line, "Logical size"), Some("100G".to_string()));

        let line = "Some other field: value";
        assert_eq!(parse_field(line, "Compression"), None);
    }

    #[test]
    fn test_parse_vdo_status() {
        let output = r#"VDO volume statistics:
VDO volume: vdo_vol
  Device mapper name: vdo_vol
  Physical size: 10G
  Logical size: 100G
  Compression: enabled
  Deduplication: enabled
  Operating mode: normal
  Write policy: sync
"#;
        let info = parse_vdo_status(output, "vdo_vol").unwrap();
        assert_eq!(info.name, "vdo_vol");
        assert_eq!(info.device, "vdo_vol");
        assert_eq!(info.physical_size, Some("10G".to_string()));
        assert_eq!(info.logical_size, Some("100G".to_string()));
        assert_eq!(info.compression, Some("enabled".to_string()));
        assert_eq!(info.deduplication, Some("enabled".to_string()));
        assert_eq!(info.operating_mode, Some("normal".to_string()));
        assert_eq!(info.write_policy, Some("sync".to_string()));
    }
}