summaryrefslogtreecommitdiff
path: root/src/obnam.rs
blob: e4ed09e0800470ec240c384b8bbb20e1f4ae096b (plain)
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
//! Manage and execute Obnam.

use lazy_static::lazy_static;
use serde::Serialize;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};

const SERVER_PORT: u16 = 8888;

lazy_static! {
    static ref TLS_KEY: Vec<u8> =
        std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/tls.key")).unwrap();
    static ref TLS_CERT: Vec<u8> =
        std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/tls.pem")).unwrap();
}

/// An Obnam system.
///
/// Manage an Obnam server and run the Obnam client.
pub struct Obnam {
    configs: TempDir,
    root: TempDir,
    chunks: TempDir,
}

/// Possible errors from managing an Obnam system.
#[derive(Debug, thiserror::Error)]
pub enum ObnamError {
    /// Error creating a temporary directory.
    #[error(transparent)]
    TempDir(#[from] std::io::Error),
}

impl Obnam {
    pub fn new() -> Result<Self, ObnamError> {
        let o = Self {
            configs: tempdir()?,
            root: tempdir()?,
            chunks: tempdir()?,
        };
        o.configure()?;
        Ok(o)
    }

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

    fn chunks(&self) -> &Path {
        self.chunks.path()
    }

    fn configs(&self) -> &Path {
        self.configs.path()
    }

    fn server_config(&self) -> PathBuf {
        self.configs().join("server.yaml")
    }

    fn tls_key(&self) -> PathBuf {
        self.configs().join("tls.key")
    }

    fn tls_cert(&self) -> PathBuf {
        self.configs().join("tls.pem")
    }

    fn client_config(&self) -> PathBuf {
        self.configs().join("client.yaml")
    }

    fn configure(&self) -> Result<(), ObnamError> {
        let key = self.tls_key();
        let cert = self.tls_cert();
        std::fs::write(&key, TLS_KEY.to_vec())?;
        std::fs::write(&cert, TLS_KEY.to_vec())?;
        ServerConfig::new(SERVER_PORT, self.chunks(), &key, &cert).write(&self.server_config())?;
        ClientConfig::new(SERVER_PORT, self.root()).write(&self.client_config())?;
        Ok(())
    }

    pub fn start_server(&mut self) -> Result<(), ObnamError> {
        Ok(())
    }

    pub fn stop_server(&mut self) -> Result<(), ObnamError> {
        Ok(())
    }

    pub fn backup(&mut self) -> Result<(), ObnamError> {
        Ok(())
    }

    pub fn restore(&mut self) -> Result<(), ObnamError> {
        Ok(())
    }
}

#[derive(Debug, Serialize)]
struct ServerConfig {
    address: String,
    chunks: PathBuf,
    tls_key: PathBuf,
    tls_cert: PathBuf,
}

impl ServerConfig {
    fn new(port: u16, chunks: &Path, tls_key: &Path, tls_cert: &Path) -> Self {
        Self {
            address: format!("localhost:{}", port),
            chunks: chunks.to_path_buf(),
            tls_key: tls_key.to_path_buf(),
            tls_cert: tls_cert.to_path_buf(),
        }
    }

    fn write(&self, filename: &Path) -> Result<(), ObnamError> {
        std::fs::write(filename, serde_yaml::to_string(self).unwrap())?;
        Ok(())
    }
}

#[derive(Debug, Serialize)]
struct ClientConfig {
    server_url: String,
    verify_tls_cert: bool,
    roots: Vec<PathBuf>,
    log: Option<PathBuf>,
}

impl ClientConfig {
    fn new(port: u16, root: &Path) -> Self {
        Self {
            server_url: format!("https://localhost:{}", port),
            verify_tls_cert: false,
            roots: vec![root.to_path_buf()],
            log: None,
        }
    }

    fn write(&self, filename: &Path) -> Result<(), ObnamError> {
        std::fs::write(filename, serde_yaml::to_string(self).unwrap())?;
        Ok(())
    }
}