rash_core 2.21.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
/// ANCHOR: module
/// # btrfs
///
/// Manage Btrfs subvolumes, snapshots, and properties.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: full
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Example
///
/// ```yaml
/// - name: Create Btrfs subvolume
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/app
///     state: present
///
/// - name: Create subvolume with compression
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/compressed
///     state: present
///     compression: zstd
///
/// - name: Create read-only snapshot
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/app
///     snapshot: /data/app-snap
///     readonly: true
///
/// - name: Create read-write snapshot
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/app
///     snapshot: /data/app-rw-snap
///     readonly: false
///
/// - name: Set subvolume properties
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/app
///     state: present
///     properties:
///       compression: zstd
///
/// - name: Delete subvolume
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/old
///     state: absent
///
/// - name: Delete snapshot
///   btrfs:
///     device: /dev/sda1
///     subvolume: /data/app-snap
///     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 std::collections::HashMap;
use std::process::{Command, Output};

use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_norway::Value as YamlValue;

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

#[derive(Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// Path to the Btrfs device or mount point.
    device: String,
    /// Subvolume path relative to the mount point.
    subvolume: String,
    /// Whether the subvolume should exist or not.
    /// **[default: `"present"`]**
    #[serde(default)]
    state: State,
    /// Destination path for a snapshot of the subvolume.
    snapshot: Option<String>,
    /// Whether the snapshot should be read-only.
    /// **[default: `true`]**
    #[serde(default = "default_true")]
    readonly: bool,
    /// Dict of subvolume properties to set.
    properties: Option<HashMap<String, String>>,
    /// Compression algorithm (e.g., zstd, lzo, zlib).
    compression: Option<String>,
}

fn default_true() -> bool {
    true
}

#[derive(Debug)]
pub struct Btrfs;

impl Module for Btrfs {
    fn get_name(&self) -> &str {
        "btrfs"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        Ok((
            btrfs_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 BtrfsClient {
    check_mode: bool,
}

impl BtrfsClient {
    pub fn new(check_mode: bool) -> Self {
        BtrfsClient { 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 btrfs command: {}",
                    String::from_utf8_lossy(&output.stderr)
                ),
            ));
        }
        Ok(output)
    }

    pub fn get_mount_point(&self, device: &str) -> Result<String> {
        let output = self.exec_cmd(
            Command::new("findmnt").args(["-n", "-o", "TARGET", "-S", device]),
            true,
        )?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        Ok(stdout.trim().to_string())
    }

    pub fn subvolume_exists(&self, mount_point: &str, subvolume: &str) -> Result<bool> {
        let full_path = format!("{mount_point}{subvolume}");
        let output = self.exec_cmd(
            Command::new("btrfs").args(["subvolume", "show", &full_path]),
            false,
        );
        Ok(output.map(|o| o.status.success()).unwrap_or(false))
    }

    pub fn snapshot_exists(&self, mount_point: &str, snapshot: &str) -> Result<bool> {
        self.subvolume_exists(mount_point, snapshot)
    }

    fn get_property(
        &self,
        mount_point: &str,
        subvolume: &str,
        property: &str,
    ) -> Result<Option<String>> {
        let full_path = format!("{mount_point}{subvolume}");
        let output = self.exec_cmd(
            Command::new("btrfs").args(["property", "get", "-ts", &full_path, property]),
            false,
        )?;

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

        let stdout = String::from_utf8_lossy(&output.stdout);
        let trimmed = stdout.trim();
        if trimmed.is_empty() {
            return Ok(None);
        }

        Ok(trimmed.split_once('=').map(|(_, v)| v.trim().to_string()))
    }

    pub fn create_subvolume(&self, mount_point: &str, params: &Params) -> Result<BtrfsResult> {
        let full_path = format!("{}{}", mount_point, params.subvolume);

        diff(
            format!("subvolume: absent ({})", params.subvolume),
            format!("subvolume: present ({})", params.subvolume),
        );

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

        let output = self.exec_cmd(
            Command::new("btrfs").args(["subvolume", "create", &full_path]),
            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(BtrfsResult::new(true, output_str))
    }

    pub fn delete_subvolume(&self, mount_point: &str, params: &Params) -> Result<BtrfsResult> {
        let full_path = format!("{}{}", mount_point, params.subvolume);

        diff(
            format!("subvolume: present ({})", params.subvolume),
            format!("subvolume: absent ({})", params.subvolume),
        );

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

        let output = self.exec_cmd(
            Command::new("btrfs").args(["subvolume", "delete", &full_path]),
            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(BtrfsResult::new(true, output_str))
    }

    pub fn create_snapshot(&self, mount_point: &str, params: &Params) -> Result<BtrfsResult> {
        let snapshot_dest = params.snapshot.as_ref().ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidData,
                "snapshot is required when creating a snapshot",
            )
        })?;

        let source_path = format!("{}{}", mount_point, params.subvolume);
        let dest_path = format!("{}{}", mount_point, snapshot_dest);

        if self.snapshot_exists(mount_point, snapshot_dest)? {
            return Ok(BtrfsResult::no_change());
        }

        diff(
            format!("snapshot: absent ({snapshot_dest})"),
            format!(
                "snapshot: present ({snapshot_dest}) (readonly={})",
                params.readonly
            ),
        );

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

        let mut cmd = Command::new("btrfs");
        cmd.args(["subvolume", "snapshot"]);

        if params.readonly {
            cmd.arg("-r");
        }

        cmd.args([&source_path, &dest_path]);

        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(BtrfsResult::new(true, output_str))
    }

    pub fn set_properties(&self, mount_point: &str, params: &Params) -> Result<BtrfsResult> {
        let full_path = format!("{}{}", mount_point, params.subvolume);

        let mut changed = false;
        let mut changes = Vec::new();

        if let Some(compression) = &params.compression {
            let current_compression = self
                .get_property(mount_point, &params.subvolume, "compression")?
                .unwrap_or_else(|| "none".to_string());
            if current_compression != *compression {
                changes.push(format!(
                    "compression: {current_compression} -> {compression}"
                ));
                changed = true;
            }
        }

        if let Some(props) = &params.properties {
            for (key, value) in props {
                changes.push(format!("property: {key}={value}"));
                changed = true;
            }
        }

        if !changed {
            return Ok(BtrfsResult::no_change());
        }

        for change in &changes {
            diff("properties", change);
        }

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

        if let Some(compression) = &params.compression {
            self.exec_cmd(
                Command::new("btrfs").args([
                    "property",
                    "set",
                    &full_path,
                    "compression",
                    compression,
                ]),
                true,
            )?;
        }

        if let Some(props) = &params.properties {
            for (key, value) in props {
                self.exec_cmd(
                    Command::new("btrfs").args(["property", "set", &full_path, key, value]),
                    true,
                )?;
            }
        }

        Ok(BtrfsResult::new(true, None))
    }
}

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

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

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

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

    if params.subvolume.is_empty() {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "subvolume cannot be empty",
        ));
    }

    if !params.subvolume.starts_with('/') {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "subvolume must be an absolute path starting with /",
        ));
    }

    Ok(())
}

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

    let client = BtrfsClient::new(check_mode);
    let mount_point = client.get_mount_point(&params.device)?;

    if mount_point.is_empty() {
        return Err(Error::new(
            ErrorKind::NotFound,
            format!("Device {} is not mounted", params.device),
        ));
    }

    let subvol_exists = client.subvolume_exists(&mount_point, &params.subvolume)?;

    let result = match params.state {
        State::Present => {
            if let Some(ref _snapshot) = params.snapshot {
                if !subvol_exists {
                    return Err(Error::new(
                        ErrorKind::InvalidData,
                        format!("Source subvolume {} does not exist", params.subvolume),
                    ));
                }
                client.create_snapshot(&mount_point, &params)?
            } else if subvol_exists {
                let mut overall_result = BtrfsResult::no_change();

                if params.compression.is_some() || params.properties.is_some() {
                    let prop_result = client.set_properties(&mount_point, &params)?;
                    if prop_result.changed {
                        overall_result = prop_result;
                    }
                }

                overall_result
            } else {
                let mut create_result = client.create_subvolume(&mount_point, &params)?;

                if create_result.changed
                    && (params.compression.is_some() || params.properties.is_some())
                {
                    let prop_result = client.set_properties(&mount_point, &params)?;
                    if prop_result.changed {
                        create_result = prop_result;
                    }
                }

                create_result
            }
        }
        State::Absent => {
            if subvol_exists {
                client.delete_subvolume(&mount_point, &params)?
            } else {
                BtrfsResult::no_change()
            }
        }
    };

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

    if let Some(ref snapshot) = params.snapshot {
        extra.insert(
            "snapshot".to_string(),
            serde_json::Value::String(snapshot.clone()),
        );
        extra.insert(
            "snapshot_exists".to_string(),
            serde_json::Value::Bool(client.snapshot_exists(&mount_point, snapshot)?),
        );
    }

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

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

    #[test]
    fn test_parse_params_present() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            state: present
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.device, "/dev/sda1");
        assert_eq!(params.subvolume, "/data/app");
        assert_eq!(params.state, State::Present);
        assert!(params.snapshot.is_none());
        assert!(params.properties.is_none());
        assert!(params.compression.is_none());
    }

    #[test]
    fn test_parse_params_absent() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/old
            state: absent
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, State::Absent);
    }

    #[test]
    fn test_parse_params_snapshot_readonly() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            snapshot: /data/app-snap
            readonly: true
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.snapshot, Some("/data/app-snap".to_string()));
        assert!(params.readonly);
    }

    #[test]
    fn test_parse_params_snapshot_readwrite() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            snapshot: /data/app-rw-snap
            readonly: false
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.snapshot, Some("/data/app-rw-snap".to_string()));
        assert!(!params.readonly);
    }

    #[test]
    fn test_parse_params_with_compression() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/compressed
            state: present
            compression: zstd
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.compression, Some("zstd".to_string()));
    }

    #[test]
    fn test_parse_params_with_properties() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            state: present
            properties:
              compression: zstd
              label: mydata
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        let props = params.properties.unwrap();
        assert_eq!(props.get("compression"), Some(&"zstd".to_string()));
        assert_eq!(props.get("label"), Some(&"mydata".to_string()));
    }

    #[test]
    fn test_parse_params_default_state() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, State::Present);
    }

    #[test]
    fn test_parse_params_default_readonly() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            device: /dev/sda1
            subvolume: /data/app
            snapshot: /data/app-snap
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert!(params.readonly);
    }

    #[test]
    fn test_validate_params_empty_device() {
        let params = Params {
            device: "".to_string(),
            subvolume: "/data/app".to_string(),
            state: State::Present,
            snapshot: None,
            readonly: true,
            properties: None,
            compression: None,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_empty_subvolume() {
        let params = Params {
            device: "/dev/sda1".to_string(),
            subvolume: "".to_string(),
            state: State::Present,
            snapshot: None,
            readonly: true,
            properties: None,
            compression: None,
        };
        assert!(validate_params(&params).is_err());
    }

    #[test]
    fn test_validate_params_relative_subvolume() {
        let params = Params {
            device: "/dev/sda1".to_string(),
            subvolume: "data/app".to_string(),
            state: State::Present,
            snapshot: None,
            readonly: true,
            properties: None,
            compression: None,
        };
        assert!(validate_params(&params).is_err());
    }

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

    #[test]
    fn test_parse_params_missing_device() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            subvolume: /data/app
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

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