libside 0.3.0

a library for building configuration management tools
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
use crate::{builder::Packages, graph::VerificationState};
use apply::SystemState;
use builder::{fs::CreateDirectory, Builder};
use requirements::{Requirement, Supports};
use serde::{de::DeserializeOwned, Serialize};
use std::{
    io::{BufRead, Cursor},
    num::ParseIntError,
    path::{Path, PathBuf},
};
use structopt::StructOpt;
use system::System;

pub use libside_procmacro::config_file;

pub mod apply;
pub mod builder;
pub mod config;
pub mod graph;
pub mod requirements;
pub mod secrets;
pub mod system;
pub mod testing;
pub mod utils;

#[derive(Debug, thiserror::Error)]
pub enum RunError<S: System, B: Builder> {
    #[error("The applications executable could not be located: {}", .0)]
    CurrentExeNotFound(std::io::Error),

    #[error("Initialization failed: {}", .0)]
    InitFailed(InitError<S>),

    #[error("Build failed: {}", .0)]
    BuildFailed(BuildError<S, B>),

    #[error("Verification failed")]
    VerificationFailed,
}

#[derive(Debug, thiserror::Error)]
pub enum InitError<S: System> {
    #[error("The base directory {:?} is not empty", .0)]
    BaseDirectoryNotEmpty(PathBuf),

    #[error("Could not open {:?}: {}", .0, .1)]
    UnableToOpen(PathBuf, S::Error),

    #[error("Unable to create directory {:?}: {}", .0, .1)]
    UnableToCreateDir(PathBuf, S::Error),

    #[error("Unable to write current version to {:?}: {}", .0, .1)]
    UnableToWriteCurrentVersion(PathBuf, S::Error),

    #[error("Unable to write database: {}", .0)]
    UnableToWriteDb(DbWriteError<S>),
}

#[derive(Debug, thiserror::Error)]
pub enum DbWriteError<S: System> {
    #[error("Unable to write database to {}: {}", .0.display(), .1)]
    UnableToWriteCurrentVersion(PathBuf, S::Error),

    #[error("Unable to serialize database: {}", .0)]
    UnableToSerialize(serde_json::Error),

    #[error("Unable to create database {}: {}", .0.display(), .1)]
    UnableToCreateDb(PathBuf, S::Error),
}

#[derive(Debug, thiserror::Error)]
pub enum GetCurrentStateError<S: System> {
    #[error("Unable to read {:?}: {}", .0, .1)]
    UnableToReadCurrentState(PathBuf, S::Error),

    #[error("Failed to parse the current version number: {}", .0)]
    CurrentNotANumber(ParseIntError),
}

#[derive(Debug, thiserror::Error)]
pub enum BuildError<S: System, B: Builder> {
    #[error("Build failed: {}", .0)]
    BuildFailed(B::BuildError),

    #[error("Unable to generate files needed for the build: ")]
    UnableToGenerateFiles(()),

    #[error("Unable to change the current install: {}", .0)]
    UnableToChangeCurrentInstall(S::Error),

    #[error("Unable to determine differences with previous build: {}", .0)]
    DiffFailed(<B::Requirement as Requirement>::HasBeenCreatedError<S>),

    #[error("Unable to generate an application sequence: ")]
    ApplicationSequenceGenerationFailed(()),

    #[error("Unable to apply the build: {}", .0)]
    ApplyFailed(graph::RunError<B::Requirement, S>),

    #[error("Unable to save new state: ")]
    SaveError(()),
}

impl<S: System, B: Builder> From<BuildError<S, B>> for RunError<S, B> {
    fn from(err: BuildError<S, B>) -> Self {
        RunError::BuildFailed(err)
    }
}

pub struct Dirs {
    base: PathBuf,

    /// /srv/packages
    packages: PathBuf,

    /// /srv/installed
    installed: PathBuf,

    /// /srv/chroots/
    chroots: PathBuf,

    /// /srv/files/exposed
    files_exposed: PathBuf,

    /// /srv/files/config
    files_config: PathBuf,

    /// /srv/files/deleted
    deleted: PathBuf,

    /// /srv/data
    data: PathBuf,

    /// /srv/backups
    backups: PathBuf,

    /// /srv/secrets
    secrets: PathBuf,
}

fn create_dir_with_err<S: System>(system: &mut S, dir: &Path) -> Result<(), InitError<S>> {
    system
        .make_dir_all(dir)
        .map_err(|e| InitError::UnableToCreateDir(dir.to_owned(), e))
}

impl Dirs {
    pub fn new<P: AsRef<Path>>(base: P) -> Self {
        let base = base.as_ref();
        Dirs {
            base: base.to_owned(),
            packages: base.join("packages"),
            installed: base.join("installed"),
            files_exposed: base.join("files/exposed"),
            files_config: base.join("files/config"),
            deleted: base.join("files/deleted"),
            chroots: base.join("chroots"),
            data: base.join("data"),
            backups: base.join("backups"),
            secrets: base.join("secrets"),
        }
    }

    pub fn get_install(&self, version: u64) -> StateDirs {
        let v = version.to_string();
        let versioned_base = self.installed.join(&v);

        StateDirs {
            version,
            db: versioned_base.join("db"),
            generated: versioned_base.join("generated"),
            config: self.files_config.clone(),
            base: versioned_base,
            chroots: self.chroots.join(&v),
            files_exposed: self.files_exposed.clone(),
            data: self.data.clone(),
            backup: self.backups.clone(),
        }
    }

    fn current_path(&self) -> PathBuf {
        self.installed.join("current")
    }

    pub fn current_install<S: System>(
        &self,
        system: &mut S,
    ) -> Result<StateDirs, GetCurrentStateError<S>> {
        let current = self.current_path();
        let current = String::from_utf8(
            system
                .file_contents(&current)
                .map_err(|e| GetCurrentStateError::UnableToReadCurrentState(current, e))?,
        )
        .unwrap();
        let version = current
            .parse::<u64>()
            .map_err(GetCurrentStateError::CurrentNotANumber)?;

        Ok(self.get_install(version))
    }

    pub fn set_current_install<S: System>(
        &self,
        new: &StateDirs,
        system: &mut S,
    ) -> Result<(), S::Error> {
        let current = self.current_path();
        system.put_file_contents(&current, format!("{}", new.version).as_bytes())
    }

    pub fn fresh_install<S: System>(
        &self,
        system: &mut S,
    ) -> Result<StateDirs, GetCurrentStateError<S>> {
        let mut max = 0u64;
        for dir in system.read_dir(&self.installed).unwrap() {
            match dir.parse::<u64>() {
                Ok(n) => max = max.max(n),
                _ => (),
            }
        }

        let next_version = max + 1;
        Ok(self.get_install(next_version))
    }

    pub fn initialize<R: Requirement, S: System>(
        &self,
        system: &mut S,
    ) -> Result<(), InitError<S>> {
        create_dir_with_err(system, &self.base)?;

        // Make sure nobody besides us can read the base dir
        system.chmod(&self.base, 0o700).unwrap();

        if !system
            .dir_is_empty(&self.base)
            .map_err(|e| InitError::UnableToOpen(self.base.to_owned(), e))?
        {
            return Err(InitError::BaseDirectoryNotEmpty(self.base.to_owned()));
        }

        create_dir_with_err(system, &self.packages)?;
        create_dir_with_err(system, &self.installed)?;
        create_dir_with_err(system, &self.chroots)?;
        create_dir_with_err(system, &self.files_exposed)?;
        create_dir_with_err(system, &self.files_config)?;
        create_dir_with_err(system, &self.data)?;
        create_dir_with_err(system, &self.backups)?;
        create_dir_with_err(system, &self.secrets)?;

        let install = self.get_install(0);
        install.create_dirs(system)?;
        install
            .write_dbs(system, &SystemState::<R>::default())
            .map_err(InitError::UnableToWriteDb)?;

        self.set_current_install(&install, system)
            .map_err(|e| InitError::UnableToWriteCurrentVersion(self.current_path(), e))?;

        Ok(())
    }
}

pub struct VersionedPath {
    path: PathBuf,
    version: u64,
}

impl VersionedPath {
    pub fn join<P: AsRef<Path>>(&self, other: P) -> VersionedPath {
        VersionedPath {
            path: self.path.join(other),
            version: self.version,
        }
    }

    pub fn unversioned_path(&self) -> &Path {
        self.path.as_path()
    }

    pub fn full_path(&self) -> PathBuf {
        self.path.join(self.version.to_string())
    }
}

pub struct StateDirs {
    version: u64,
    base: PathBuf,
    db: PathBuf,
    generated: PathBuf,
    chroots: PathBuf,
    data: PathBuf,
    config: PathBuf,
    files_exposed: PathBuf,
    backup: PathBuf,
}

impl StateDirs {
    pub fn db(&self) -> &Path {
        &self.db
    }

    pub fn create_dirs<S: System>(&self, system: &mut S) -> Result<(), InitError<S>> {
        create_dir_with_err(system, &self.generated)?;
        create_dir_with_err(system, &self.chroots)?;

        Ok(())
    }

    pub fn exposed_path(&self, name: &str) -> VersionedPath {
        VersionedPath {
            path: self.files_exposed.join(name),
            version: self.version,
        }
    }

    pub fn generated_path(&self, name: &str) -> PathBuf {
        self.generated.join(name)
    }

    pub fn chroot_path(&self, name: &str) -> PathBuf {
        self.chroots.join(name)
    }

    pub fn config_path(&self, name: &str) -> PathBuf {
        self.config.join(name)
    }

    pub fn userdata_path(&self, name: &str) -> PathBuf {
        self.data.join(name).join("userdata")
    }

    pub fn backup_path(&self, name: &str) -> PathBuf {
        self.backup.join(name)
    }

    pub fn deleted_file_backup_path(&self, name: &str) -> PathBuf {
        self.generated.join(name).join("deleted-file-backup")
    }

    pub fn load_install<R: DeserializeOwned, S: System>(&self, system: &mut S) -> SystemState<R> {
        let contents = system.file_contents(&self.db).unwrap();

        SystemState {
            graph: serde_json::from_reader(&mut Cursor::new(contents)).unwrap(),
        }
    }

    pub fn write_dbs<R: Serialize, S: System>(
        &self,
        system: &mut S,
        dbs: &SystemState<R>,
    ) -> Result<(), DbWriteError<S>> {
        let contents =
            serde_json::to_string(&dbs.graph).map_err(DbWriteError::UnableToSerialize)?;
        system
            .put_file_contents(&self.db, contents.as_bytes())
            .map_err(|e| DbWriteError::UnableToCreateDb(self.db.clone(), e))?;

        Ok(())
    }
}

pub struct SiDe {}

#[derive(StructOpt)]
pub enum Command {
    Init,
    Status,
    Build {
        #[structopt(long = "ignore-verification")]
        ignore_verification: bool,

        #[structopt(long = "ask-overwrite")]
        ask_overwrite: bool,
    },
    Apply {
        target: u64,

        #[structopt(long = "ignore-verification")]
        ignore_verification: bool,

        #[structopt(long = "ask-overwrite")]
        ask_overwrite: bool,
    },
    Verify {
        #[structopt(long = "fix")]
        fix: bool,
    },
}

#[derive(StructOpt)]
pub struct Args {
    base_dir: PathBuf,

    #[structopt(subcommand)]
    command: Command,
}

#[derive(Serialize)]
pub struct Status {
    current_version: u64,
    base_path: PathBuf,
    backup_path: PathBuf,
}

impl SiDe {
    pub fn run<S: System, B: Builder>(system: &mut S, builder: B) -> Result<(), RunError<S, B>>
    where
        B::Requirement: Supports<CreateDirectory>,
    {
        let args = Args::from_args();
        let dirs = Dirs::new(&args.base_dir);

        Self::run_command(args.command, &dirs, system, builder)
    }

    pub fn run_command<S: System, B: Builder>(
        command: Command,
        dirs: &Dirs,
        system: &mut S,
        builder: B,
    ) -> Result<(), RunError<S, B>>
    where
        B::Requirement: Supports<CreateDirectory>,
    {
        match command {
            Command::Init => {
                dirs.initialize::<B::Requirement, S>(system)
                    .map_err(RunError::InitFailed)?;

                Ok(())
            }
            Command::Status => {
                let current = dirs.current_install(system).unwrap();

                println!(
                    "{}",
                    serde_json::to_string_pretty(&Status {
                        current_version: current.version,
                        base_path: dirs.base.clone(),
                        backup_path: dirs.backups.clone(),
                    })
                    .unwrap()
                );

                Ok(())
            }
            Command::Apply {
                target,
                ignore_verification,
                ask_overwrite,
            } => {
                let current = dirs.current_install(system).unwrap();
                let target = dirs.get_install(target);
                let current_state = current.load_install::<B::Requirement, S>(system);
                let target_state = target.load_install::<B::Requirement, S>(system);

                if ignore_verification {
                    println!("Skipping verification of current state...");
                } else {
                    println!("Verifying current state...");
                    match current_state.verify_system_state(system).unwrap() {
                        VerificationState::Ok => println!("Verification OK"),
                        err @ VerificationState::Invalid { .. } => {
                            panic!("Verification failed:\n{}", err)
                        }
                    }
                }

                println!("Current: {}", current.version);
                println!("Target : {}", target.version);

                let cmp = target_state
                    .graph
                    .compare_with(system, &current_state.graph)
                    .map_err(BuildError::DiffFailed)?;
                let instructions = cmp
                    .generate_application_sequence(system)
                    .map_err(BuildError::ApplicationSequenceGenerationFailed)?;

                // The result returned by run describes which requirements were pre-existing;
                // That's not relevant to us, because we want to keep the original values that we determined when we created this install.
                match instructions.run(system, |s| {
                    if ask_overwrite {
                        println!("Can {} be overwritten? Type 'yes' to continue or anything else to abort", s);
                        let line = std::io::stdin().lock().lines().next().unwrap().unwrap();
                        return line.trim() == "yes";
                    }

                    return false;
                }) {
                    Ok(_) => {}
                    Err(err) => {
                        println!();
                        println!("Error: {}", err);
                        println!("Reverting...");
                        instructions
                            .revert(system, &err.revert_info)
                            .unwrap();

                        println!("Revert OK");
                        return Err(BuildError::ApplyFailed(err).into());
                    }
                }

                dirs.set_current_install(&target, system)
                    .map_err(BuildError::UnableToChangeCurrentInstall)?;
                println!("Done!");

                Ok(())
            }
            Command::Build {
                ignore_verification,
                ask_overwrite,
            } => {
                let current = dirs.current_install(system).unwrap();
                let current_state = current.load_install::<B::Requirement, S>(system);

                if ignore_verification {
                    println!("Skipping verification of current state...");
                } else {
                    println!("Verifying current state...");
                    match current_state.verify_system_state(system).unwrap() {
                        VerificationState::Ok => println!("Verification OK"),
                        err @ VerificationState::Invalid { .. } => {
                            panic!("Verification failed:\n{}", err)
                        }
                    }
                }

                let new_install = dirs.fresh_install(system).unwrap();
                println!("Current install: {}", current.base.display());
                println!("New install: {}", new_install.base.display());

                let packages = Packages::load(&dirs, system).unwrap();
                let prepared = builder::run(&dirs, system, packages, &new_install, builder)
                    .map_err(BuildError::BuildFailed)?;

                let graph = prepared
                    .generate_files(system, &current_state)
                    .map_err(BuildError::UnableToGenerateFiles)?;
                let cmp = graph
                    .compare_with(system, &current_state.graph)
                    .map_err(BuildError::DiffFailed)?;
                let instructions = cmp
                    .generate_application_sequence(system)
                    .map_err(BuildError::ApplicationSequenceGenerationFailed)?;
                match instructions.run(system, |s| {
                    if ask_overwrite {
                        println!("Can {} be overwritten? Type 'yes' to continue or anything else to abort", s);
                        let line = std::io::stdin().lock().lines().next().unwrap().unwrap();
                        return line.trim() == "yes";
                    }

                    return false;
                }) {
                    Ok(result) => {
                        let _new_state = prepared.save(system, result).map_err(BuildError::SaveError)?;
                        dirs.set_current_install(&new_install, system)
                            .map_err(BuildError::UnableToChangeCurrentInstall)?;

                        Ok(())
                    }
                    Err(err) => {
                        println!();
                        println!("Error: {}", err);
                        println!("Reverting...");
                        instructions
                            .revert(system, &err.revert_info)
                            .unwrap();

                        println!("Revert OK");
                        Err(BuildError::ApplyFailed(err).into())
                    }
                }
            }
            Command::Verify { fix } => {
                let current = dirs.current_install(system).unwrap();
                println!("Current install: {}", current.base.display());

                let current_state = current.load_install::<B::Requirement, S>(system);
                match current_state.verify_system_state(system).unwrap() {
                    VerificationState::Ok => println!("Verification OK"),
                    err @ VerificationState::Invalid { .. } => {
                        println!("Verification failed:\n{}", err);

                        if fix {
                            let seq = current_state.graph.generate_fix_sequence(system).unwrap();

                            // The result returned by run describes which requirements were pre-existing;
                            // That's not relevant to us, because we want to keep the original values that we determined when we created this install.
                            let _ = seq.run(system, |_| false).unwrap();

                            println!("Fixing successful!");
                        } else {
                            return Err(RunError::VerificationFailed);
                        }
                    }
                }

                Ok(())
            }
        }
    }
}