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
use std::{ffi::OsStr, path::PathBuf, process::Stdio};

use anyhow::{anyhow, Context, Error};
use async_trait::async_trait;
use ezexec::lookup::Shell;
use log::{debug, info};
use tokio::fs::OpenOptions;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter};
use tokio::process::Command;

/// Utility class: run a script in a shell.
///
/// Based on ezexec, customized to improve the ability to log.
pub struct Executor {
    /// The shell used to execute the script.
    shell: Shell,
}
impl Executor {
    pub fn try_new() -> Result<Self, Error> {
        let shell = ezexec::lookup::Shell::find()
            .map_err(|e| anyhow!("Could not find a shell to execute command: {}", e))?;
        Ok(Self { shell })
    }

    /// Prepare a `Command` from a script.
    ///
    /// The resulting `Command` will be ready to execute in the shell.
    /// You may customize it with e.g. `env()`.
    pub fn command<P>(&self, cmd: P) -> Result<Command, Error>
    where
        P: AsRef<str>,
    {
        // Lookup shell.
        let shell: &OsStr = self.shell.as_ref();
        let mut command = Command::new(shell);

        // Prefix `command` with the strings we need to call the shell.
        let cmd = cmd.as_ref();
        let execstring_args = self
            .shell
            .execstring_args()
            .map_err(|e| anyhow!("Could not find a shell string: {}", e))?;
        let args = execstring_args.iter().chain(std::iter::once(&cmd));

        command.args(args);
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());

        Ok(command)
    }
}

/// Utility function: spawn an async task to asynchronously write the contents
/// of a reader to both a file and a log.
fn spawn_logger<T>(name: &'static str, reader: BufReader<T>, dest: PathBuf, command: &str)
where
    BufReader<T>: AsyncBufReadExt + Unpin,
    T: 'static + Send,
{
    debug!("Storing {} logs in {:?}", name, dest);
    let command = format!("\ncommand: {}\n", command.to_string());
    tokio::task::spawn(async move {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(dest)
            .await
            .with_context(|| format!("Could not create log file {}", name))?;
        {
            // Create a buffered writer, we don't want to hit the disk with
            // every single byte.
            let mut writer = BufWriter::new(&mut file);
            writer
                .write_all(command.as_bytes())
                .await
                .with_context(|| format!("Could not write log file {}", name))?;
            writer
                .flush()
                .await
                .with_context(|| format!("Could not write log file {}", name))?;

            let mut lines = reader.lines();
            while let Ok(Some(line)) = lines.next_line().await {
                // Display logs.
                info!("{}: {}", name, line);
                // Write logs to `dest`.
                writer
                    .write_all(line.as_bytes())
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                writer
                    .write_all(b"\n")
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
                // Flush after each write, in case of crash.
                writer
                    .flush()
                    .await
                    .with_context(|| format!("Could not write log file {}", name))?;
            }
        }
        let _ = file.sync_data().await;
        Ok(()) as Result<(), anyhow::Error>
    });
}

/// Extension trait for `Command`.
#[async_trait]
pub trait CommandExt {
    /// Spawn a command, logging its stdout/stderr to files and to the env logger.
    async fn spawn_logged(
        &mut self,
        log_dir: &PathBuf,
        name: &'static str,
        line: &str,
    ) -> Result<(), Error>;
}

#[async_trait]
impl CommandExt for Command {
    async fn spawn_logged(
        &mut self,
        log_dir: &PathBuf,
        name: &'static str,
        line: &str,
    ) -> Result<(), Error> {
        let mut child = self
            .spawn()
            .with_context(|| format!("Could not spawn process for `{}`", name))?;
        // Spawn background tasks to write down stdout.
        if let Some(stdout) = child.stdout.take() {
            let reader = BufReader::new(stdout);
            let log_path = log_dir.join(format!("{name}.out", name = name));
            spawn_logger(name, reader, log_path, line);
        }
        // Spawn background tasks to write down stderr.
        if let Some(stderr) = child.stderr.take() {
            let reader = BufReader::new(stderr);
            let log_path = log_dir.join(format!("{name}.log", name = name));
            spawn_logger(name, reader, log_path, line);
        }
        let status = child.wait().await.context("Child process not launched")?;
        if status.success() {
            return Ok(());
        }
        Err(anyhow!("Child `{}` failed: `{}`", name, status))
    }
}