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
// Copyright © 2018 libmussh developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Multiplex commands over hosts.
use crate::config::Host;
use crate::error::{MusshErrKind, MusshResult};
use crate::utils::{convert_duration, CmdType, MultiplexMapType};
use chrono::Utc;
use getset::{Getters, Setters};
use indexmap::{IndexMap, IndexSet};
use slog::{error, info, trace, Logger};
use slog_try::{try_error, try_info, try_trace};
use ssh2::Session;
use std::collections::HashMap;
use std::env;
use std::io::{BufRead, BufReader};
use std::net::TcpStream;
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::mpsc::{self, Receiver};
use std::thread;
use std::time::{Duration, Instant};
use wait_group::WaitGroup;

type MultiplexResult = Vec<MusshResult<Metrics>>;

/// Execution metrics
#[derive(Clone, Debug, Eq, Getters, PartialEq)]
pub struct Metrics {
    /// The hostname where the command was run
    #[get = "pub"]
    hostname: String,
    /// The name of the command that was run
    #[get = "pub"]
    cmd_name: String,
    /// The duration of the execution
    #[get = "pub"]
    duration: Duration,
    /// The timestamp when this metric was created
    #[get = "pub"]
    timestamp: i64,
}

impl Default for Metrics {
    fn default() -> Self {
        Self {
            hostname: String::new(),
            cmd_name: String::new(),
            duration: Duration::new(0, 0),
            timestamp: 0,
        }
    }
}

/// Multiplex ssh commands
#[derive(Clone, Debug, Default, Getters, Setters)]
pub struct Multiplex {
    /// Is this going to be a dry run?
    #[get = "pub"]
    #[set = "pub"]
    dry_run: bool,
    /// Run the commands synchronously?
    #[get = "pub"]
    #[set = "pub"]
    synchronous: bool,
    /// stdout logging
    #[get = "pub"]
    #[set = "pub"]
    stdout: Option<Logger>,
    /// stderr logging
    #[get = "pub"]
    #[set = "pub"]
    stderr: Option<Logger>,
    /// command output logging
    #[get = "pub"]
    #[set = "pub"]
    host_loggers: HashMap<String, Option<Logger>>,
}

impl Multiplex {
    /// Multiplex the requested commands over the requested hosts
    #[must_use]
    pub fn multiplex(
        self,
        sync_hosts: &IndexSet<String>,
        hosts_map: MultiplexMapType,
    ) -> MultiplexResult {
        let wg = WaitGroup::new();
        let (tx, rx) = mpsc::channel();
        let count = hosts_map.len();
        let mut results = Vec::new();

        for (hostname, (host, cmd_map)) in hosts_map {
            // Setup the commands to run pre-sync
            let mut pre_cmds = IndexMap::new();
            if let Some(commands) = cmd_map.get(&CmdType::Cmd) {
                pre_cmds = commands.clone();
            }

            // Setup the commands to run post-sync
            let mut sync_cmds = IndexMap::new();
            if let Some(commands) = cmd_map.get(&CmdType::SyncCmd) {
                sync_cmds = commands.clone();
            }

            // If this is a sync host, add it to the wait group, and mark it
            let mut sync_host = false;
            if sync_hosts.contains(&hostname) {
                sync_host = true;
                wg.add(1);
            }

            if !self.dry_run {
                // Setup the clones to move into the thread
                let wg_cl = wg.clone();
                let tx_cl = tx.clone();
                let h_cl = host.clone();
                let stdout_cl = self.stdout.clone();
                let stderr_cl = self.stderr.clone();
                let cmd_cl = self.host_loggers.get(&hostname).unwrap_or(&None).clone();

                // The worker thread that will run the commands on the host
                let _ = thread::spawn(move || {
                    let mut results = execute(&stdout_cl, &stderr_cl, &cmd_cl, &h_cl, &pre_cmds);

                    if sync_host {
                        results.extend(execute(&stdout_cl, &stderr_cl, &cmd_cl, &h_cl, &sync_cmds));
                        wg_cl.done();
                    } else {
                        wg_cl.wait();
                        results.extend(execute(&stdout_cl, &stderr_cl, &cmd_cl, &h_cl, &sync_cmds));
                    }
                    tx_cl.send(results).expect("unable to send response");
                });

                if self.synchronous {
                    self.receive(&rx, &mut results);
                }
            }
        }

        if !self.dry_run && !self.synchronous {
            // Wait for all the threads to finish
            for _ in 0..count {
                self.receive(&rx, &mut results);
            }
        }

        results
    }

    fn receive(&self, rx: &Receiver<MultiplexResult>, output: &mut Vec<MusshResult<Metrics>>) {
        match rx.recv() {
            Ok(results) => output.extend(results),
            Err(e) => try_error!(self.stderr, "{}", e),
        }
    }
}

fn execute(
    stdout: &Option<Logger>,
    stderr: &Option<Logger>,
    cmd_logger: &Option<Logger>,
    host: &Host,
    cmds: &IndexMap<String, String>,
) -> MultiplexResult {
    cmds.iter()
        .map(|(cmd_name, cmd)| execute_on_host(stdout, stderr, cmd_logger, host, cmd_name, cmd))
        .collect()
}

fn execute_on_host(
    stdout: &Option<Logger>,
    stderr: &Option<Logger>,
    cmd_logger: &Option<Logger>,
    host: &Host,
    cmd_name: &str,
    cmd: &str,
) -> MusshResult<Metrics> {
    if host.hostname() == "localhost" {
        execute_on_localhost(stdout, stderr, cmd_logger, host, cmd_name, cmd)
    } else {
        execute_on_remote(stdout, stderr, cmd_logger, host, cmd_name, cmd)
    }
}

fn execute_on_localhost(
    stdout: &Option<Logger>,
    stderr: &Option<Logger>,
    cmd_logger: &Option<Logger>,
    host: &Host,
    cmd_name: &str,
    cmd: &str,
) -> MusshResult<Metrics> {
    if let Some(shell_path) = env::var_os("SHELL") {
        let timer = Instant::now();
        let fish = shell_path.to_string_lossy().to_string();
        let mut command = Command::new(&fish);
        let _ = command.arg("-c");
        let _ = command.arg(cmd);
        let _ = command.stdout(Stdio::piped());
        let _ = command.stderr(Stdio::piped());

        if let Ok(mut child) = command.spawn() {
            let child_stdout = child.stdout.take().ok_or_else(|| "Unable to get stdout")?;
            let stdout_reader = BufReader::new(child_stdout);
            for line in stdout_reader.lines() {
                if let Ok(line) = line {
                    try_trace!(cmd_logger, "{}", line);
                }
            }

            let status = child.wait()?;
            let duration = timer.elapsed();
            let hostname = host.hostname().clone();
            let elapsed_str = convert_duration(&duration);

            if status.success() {
                let mut metrics = Metrics::default();
                metrics.hostname = hostname;
                metrics.cmd_name = cmd_name.to_string();
                metrics.duration = duration;
                metrics.timestamp = Utc::now().timestamp_millis();
                try_info!(
                    stdout,
                    "execute";
                    "host" => host.hostname(),
                    "cmd" => cmd_name,
                    "duration" => elapsed_str
                );
                Ok(metrics)
            } else {
                try_error!(
                    stderr,
                    "execute";
                    "host" => host.hostname(),
                    "cmd" => cmd_name,
                    "duration" => elapsed_str
                );
                let err_msg = format!("Failed to run '{}' on '{}'", hostname, cmd_name);
                Err(MusshErrKind::NonZero(err_msg).into())
            }
        } else {
            Err(MusshErrKind::Spawn.into())
        }
    } else {
        Err(MusshErrKind::ShellNotFound.into())
    }
}

fn execute_on_remote(
    stdout: &Option<Logger>,
    stderr: &Option<Logger>,
    cmd_logger: &Option<Logger>,
    host: &Host,
    cmd_name: &str,
    cmd: &str,
) -> MusshResult<Metrics> {
    if let Ok(mut sess) = Session::new() {
        let timer = Instant::now();
        let host_tuple = (&host.hostname()[..], host.port().unwrap_or_else(|| 22));
        let tcp = TcpStream::connect(host_tuple)?;
        sess.set_tcp_stream(tcp);
        sess.handshake()?;
        if let Some(pem) = host.pem() {
            sess.userauth_pubkey_file(host.username(), None, Path::new(&pem), None)?;
        } else {
            sess.userauth_agent(host.username())?;
        }

        if sess.authenticated() {
            try_trace!(stdout, "execute"; "message" => "Authenticated");
            let mut channel = sess.channel_session()?;
            channel.exec(cmd)?;

            {
                let stdout_stream = channel.stream(0);
                let stdout_reader = BufReader::new(stdout_stream);

                for line in stdout_reader.lines() {
                    if let Ok(line) = line {
                        try_trace!(cmd_logger, "{}", line);
                    }
                }
            }

            let duration = timer.elapsed();
            let elapsed_str = convert_duration(&duration);

            match channel.exit_status() {
                Ok(code) => {
                    if code == 0 {
                        let mut metrics = Metrics::default();
                        metrics.hostname = host.hostname().to_string();
                        metrics.cmd_name = cmd_name.to_string();
                        metrics.duration = duration;
                        metrics.timestamp = Utc::now().timestamp_millis();

                        try_info!(
                            stdout,
                            "execute";
                            "host" => host.hostname(),
                            "cmd" => cmd_name,
                            "duration" => elapsed_str
                        );
                        Ok(metrics)
                    } else {
                        try_error!(
                            stderr,
                            "execute";
                            "host" => host.hostname(),
                            "cmd" => cmd_name,
                            "duration" => elapsed_str
                        );
                        let err_msg =
                            format!("Failed to run '{}' on '{}'", host.hostname(), cmd_name);
                        Err(MusshErrKind::NonZero(err_msg).into())
                    }
                }
                Err(e) => {
                    try_error!(
                        stderr,
                        "execute"; "hostname" => host.hostname(), "cmd" => cmd_name, "error" => format!("{}", e)
                    );
                    let err_msg = format!("Failed to run '{}' on '{}'", host.hostname(), cmd_name);
                    Err(MusshErrKind::SshExec(err_msg).into())
                }
            }
        } else {
            Err(MusshErrKind::SshAuthentication.into())
        }
    } else {
        Err(MusshErrKind::SshSession.into())
    }
}

#[cfg(test)]
mod tests {
    use super::Multiplex;
    use crate::config::test::test_cli;
    use crate::config::{HostsCmds, Mussh};
    use crate::error::MusshResult;

    crate const MUSSH_FULL_TOML: &str = r#"[hostlist.most]
hostnames = ["m1", "m2", "m3", "m4"]
[hostlist.m1]
hostnames = ["m1"]
[hostlist.m2]
hostnames = ["m2"]
[hostlist.m3]
hostnames = ["m3"]
[hostlist.m4]
hostnames = ["m4"]
[hosts.m1]
hostname = "localhost"
username = "jozias"

[[hosts.m1.alias]]
command = "ls.mac"
aliasfor = "ls"

[hosts.m2]
hostname = "localhost"
username = "jozias"

[hosts.m3]
hostname = "localhost"
username = "jozias"

[hosts.m4]
hostname = "localhost"
username = "jozias"

[cmd.bar]
command = "sleep 1"
[cmd.ls]
command = "ls -al"
[cmd.uname]
command = "uname -a"
"#;

    #[test]
    fn ssh_multiplex() -> MusshResult<()> {
        let config: Mussh = toml::from_str(&MUSSH_FULL_TOML)?;
        let cli = vec![
            "test", "-h", "most", "-c", "ls,uname", "-s", "m3,m4", "-y", "bar",
        ];
        let matches = test_cli().get_matches_from_safe(cli)?;
        let hosts_cmds = HostsCmds::from(&matches);
        let hosts_map = config.to_host_map(&hosts_cmds);
        let multiplex = Multiplex::default();
        let _ = multiplex.multiplex(hosts_cmds.sync_hosts(), hosts_map);
        Ok(())
    }
}