summaryrefslogtreecommitdiffstats
path: root/crates/core/plugin_sm/src/operation_logs.rs
blob: 7bf08eabbd053c5045b58ac4b8853c3b19b597c7 (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
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
use std::path::PathBuf;
use std::{cmp::Reverse, collections::HashMap};
use std::{collections::BinaryHeap, path::Path};
use time::{format_description, OffsetDateTime};
use tracing::log;

use crate::log_file::LogFile;

#[derive(Debug, thiserror::Error)]
pub enum OperationLogsError {
    #[error(transparent)]
    FromIo(#[from] std::io::Error),

    #[error(transparent)]
    FromTimeFormat(#[from] time::error::Format),

    #[error("Incorrect file format. Expected: `operation_name`-`timestamp`.log")]
    FileFormatError,
}

#[derive(Debug)]
pub struct OperationLogs {
    pub log_dir: PathBuf,
}

pub enum LogKind {
    SoftwareUpdate,
    SoftwareList,
    Operation(String),
}

const UPDATE_PREFIX: &str = "software-update";
const LIST_PREFIX: &str = "software-list";

impl OperationLogs {
    pub fn try_new(log_dir: PathBuf) -> Result<OperationLogs, OperationLogsError> {
        std::fs::create_dir_all(log_dir.clone())?;
        let operation_logs = OperationLogs { log_dir };

        if let Err(err) = operation_logs.remove_outdated_logs() {
            // In no case a log-cleaning error should prevent the agent to run.
            // Hence the error is logged but not returned.
            log::warn!("Fail to remove the out-dated log files: {}", err);
        }

        Ok(operation_logs)
    }

    pub async fn new_log_file(&self, kind: LogKind) -> Result<LogFile, OperationLogsError> {
        if let Err(err) = self.remove_outdated_logs() {
            // In no case a log-cleaning error should prevent the agent to run.
            // Hence the error is logged but not returned.
            log::warn!("Fail to remove the out-dated log files: {}", err);
        }

        let now = OffsetDateTime::now_utc();

        let file_prefix = match kind {
            LogKind::SoftwareUpdate => UPDATE_PREFIX,
            LogKind::SoftwareList => LIST_PREFIX,
            LogKind::Operation(ref operation_name) => operation_name.as_str(),
        };

        let file_name = format!(
            "{}-{}.log",
            file_prefix,
            now.format(&format_description::well_known::Rfc3339)?
        );

        let mut log_file_path = self.log_dir.clone();
        log_file_path.push(file_name);

        LogFile::try_new(log_file_path)
            .await
            .map_err(OperationLogsError::FromIo)
    }

    pub fn remove_outdated_logs(&self) -> Result<(), OperationLogsError> {
        let mut log_tracker: HashMap<String, BinaryHeap<Reverse<String>>> = HashMap::new();

        // FIXME: this is a hotfix to map "software-list" and "software-update" to "software-management"
        // this should be fixed in https://github.com/thin-edge/thin-edge.io/issues/1077
        for file in (self.log_dir.read_dir()?).flatten() {
            if let Some(path) = file.path().file_name().and_then(|name| name.to_str()) {
                if path.starts_with("software-list") {
                    log_tracker
                        .entry("software-list".to_string())
                        .or_insert_with(BinaryHeap::new)
                        .push(Reverse(path.to_string()));
                } else if path.starts_with("software-update") {
                    log_tracker
                        .entry("software-update".to_string())
                        .or_insert_with(BinaryHeap::new)
                        .push(Reverse(path.to_string()));
                } else {
                    let file_name = path
                        .split('-')
                        .next()
                        .ok_or(OperationLogsError::FileFormatError)?;
                    log_tracker
                        .entry(file_name.to_string())
                        .or_insert_with(BinaryHeap::new)
                        .push(Reverse(path.to_string()));
                }
            }
        }

        for (key, value) in log_tracker.iter_mut() {
            if key.starts_with("software-list") {
                // only allow one update list file in logs
                let () = remove_old_logs(value, &self.log_dir, 1)?;
            } else {
                // allow most recent five
                let () = remove_old_logs(value, &self.log_dir, 5)?;
            }
        }

        Ok(())
    }
}

fn remove_old_logs(
    log_tracker: &mut BinaryHeap<Reverse<String>>,
    dir_path: &Path,
    n: usize,
) -> Result<(), OperationLogsError> {
    while log_tracker.len() > n {
        if let Some(rname) = log_tracker.pop() {
            let name = rname.0;
            let path = dir_path.join(name.clone());
            if let Err(err) = std::fs::remove_file(&path) {
                log::warn!("Fail to remove out-dated log file {} : {}", name, err);
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::path::Path;
    use tempfile::TempDir;

    #[tokio::test]
    async fn on_start_keeps_only_the_latest_logs() -> Result<(), anyhow::Error> {
        // Create a log dir with a bunch of fake log files
        let log_dir = TempDir::new()?;

        let swlist_log_1 = create_file(log_dir.path(), "software-list-1996-02-22T16:39:57z");
        let update_log_1 = create_file(log_dir.path(), "software-update-1996-12-19T16:39:57z");
        let update_log_2 = create_file(log_dir.path(), "software-update-1996-12-20T16:39:57z");
        let update_log_3 = create_file(log_dir.path(), "software-update-1996-12-21T16:39:57z");
        let update_log_4 = create_file(log_dir.path(), "software-update-1996-12-22T16:39:57z");
        let swlist_log_2 = create_file(log_dir.path(), "software-list-1996-12-22T16:39:57z");
        let update_log_5 = create_file(log_dir.path(), "software-update-1996-12-23T16:39:57z");
        let update_log_6 = create_file(log_dir.path(), "software-update-1996-12-24T16:39:57z");
        let update_log_7 = create_file(log_dir.path(), "software-update-1996-12-25T16:39:57z");
        let unrelated_1 = create_file(log_dir.path(), "foo");
        let unrelated_2 = create_file(log_dir.path(), "bar");

        // Open the log dir
        let _operation_logs = OperationLogs::try_new(log_dir.path().to_path_buf())?;

        // Outdated logs are removed
        assert!(!update_log_1.exists());
        assert!(!update_log_2.exists());
        assert!(!swlist_log_1.exists());

        // The 5 latest update logs are kept
        assert!(update_log_3.exists());
        assert!(update_log_4.exists());
        assert!(update_log_5.exists());
        assert!(update_log_6.exists());
        assert!(update_log_7.exists());

        // The latest software list is kept
        assert!(swlist_log_2.exists());

        // Unrelated files are untouched
        assert!(unrelated_1.exists());
        assert!(unrelated_2.exists());

        Ok(())
    }

    #[tokio::test]
    async fn on_new_log_keep_the_latest_logs_plus_the_new_one() -> Result<(), anyhow::Error> {
        // Create a log dir
        let log_dir = TempDir::new()?;
        let operation_logs = OperationLogs::try_new(log_dir.path().to_path_buf())?;

        // Add a bunch of fake log files
        let swlist_log_1 = create_file(log_dir.path(), "software-list-1996-02-22T16:39:57z");
        let update_log_1 = create_file(log_dir.path(), "software-update-1996-12-19T16:39:57z");
        let update_log_2 = create_file(log_dir.path(), "software-update-1996-12-20T16:39:57z");
        let update_log_3 = create_file(log_dir.path(), "software-update-1996-12-21T16:39:57z");
        let update_log_4 = create_file(log_dir.path(), "software-update-1996-12-22T16:39:57z");
        let swlist_log_2 = create_file(log_dir.path(), "software-list-1996-12-22T16:39:57z");
        let update_log_5 = create_file(log_dir.path(), "software-update-1996-12-23T16:39:57z");
        let update_log_6 = create_file(log_dir.path(), "software-update-1996-12-24T16:39:57z");
        let update_log_7 = create_file(log_dir.path(), "software-update-1996-12-25T16:39:57z");

        // Create a new log file
        let new_log = operation_logs.new_log_file(LogKind::SoftwareUpdate).await?;

        // The new log has been created
        let new_path = Path::new(new_log.path());
        assert!(new_path.exists());

        // Outdated logs are removed
        assert!(!update_log_1.exists());
        assert!(!update_log_2.</