cbradio 0.1.0

System orchestration based on Redis
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
// SPDX-FileCopyrightText: 2021 Jakub Pastuszek <jpastuszek@protonmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
use cotton::prelude::*;
use cbradio::{BaseStation, Agent, Tags, Request, Reply, ReplyMessage, timestamp, project_dir, with_create_dir};
use std::process::{Command, Stdio};
use maybe_string::MaybeString;
use std::thread::{self, JoinHandle};
use std::os::unix::process::ExitStatusExt;
use cotton::chrono::format::{DelayedFormat, strftime::StrftimeItems};
use yansi::{Color, Style};
use serde::{Serialize, Deserialize};
use std::collections::BTreeMap;
use fs2::FileExt;

#[derive(Debug, StructOpt)]
enum CliRequest {
    /// Ping listening agents
    Ping,
    /// Run a command
    Run {
        command: String
    },
}

#[derive(Debug, StructOpt)]
enum Action {
    /// Listen for base station commands
    Agent {
        /// Agent tags for station to match on
        #[structopt(long, short = "t")]
        tags: Vec<String>,

        /// Log output of commands to stderr
        #[structopt(long, short = "l")]
        log_command_output: bool,

        /// Directory path where command scripts are located
        #[structopt(long, short = "r", default_value = ".")]
        run_directory: PathBuf,
    },
    /// Issue commands to agents
    Station {
        /// Path to file where list of discovered agents is maintained
        #[structopt(long, short = "d")]
        discovery_file: Option<PathBuf>,

        /// Don't use discovery file
        #[structopt(long, short = "D")]
        no_discovery: bool,

        /// Exit with status +2 if there were missing agents detected
        #[structopt(long, short = "m")]
        fail_missing: bool,

        /// Only agents that have all matching tags will reply
        #[structopt(long, short = "t", number_of_values = 1)]
        tags: Vec<String>,

        /// Duration in seconds to wait for the first reply (0 = infinity)
        #[structopt(long, short = "H", default_value = "2")]
        hello_wait: usize,

        /// Duration in seconds to wait for the next reply when some agents are active (0 = infinity)
        #[structopt(long, short = "R", default_value = "120")]
        reply_wait: usize,

        /// Minimum duration in seconds to wait for replies after sending the request (0 = 1)
        #[structopt(long, short = "M", default_value = "4")]
        minimum_wait: usize,

        #[structopt(subcommand)]
        request: CliRequest,
    }
}

type Timestamp = i64;

fn format_timestamp(timestamp: Timestamp) -> DelayedFormat<StrftimeItems<'static>> {
    let ts = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp / 1000, (timestamp - (timestamp / 1000 * 1000)) as u32 * 1000000), Utc);
    ts.format("%Y-%m-%d %H:%M:%S%.3f")
}

/// Orchestration system based on Redis
#[derive(Debug, StructOpt)]
struct Cli {
    #[structopt(flatten)]
    logging: LoggingOpt,

    /// Redis connection string
    #[structopt(long,short = "r", env = "REDIS_CONNECTION_STRING")]
    connection_string: String,

    /// Identity of this base station or agent
    #[structopt(long,short = "i", env = "HOSTNAME")]
    identity: String,

    /// Communication channel identity
    #[structopt(long,short = "c", env = "CBRADIO_CHANNEL")]
    channel: String,

    /// Path to public key
    #[structopt(long, short = "b")]
    base_station_public_key: Option<PathBuf>,

    /// Path to secret key
    #[structopt(long, short = "B")]
    base_station_secret_key: Option<PathBuf>,

    /// Path to public key
    #[structopt(long, short = "n")]
    network_public_key: Option<PathBuf>,

    /// Path to secret key
    #[structopt(long, short = "N")]
    network_secret_key: Option<PathBuf>,

    #[structopt(subcommand)]
    action: Action,
}

/// Reads out line by line and calls on_line function with result in dedicated thread.
///
/// It will ensure that all data is read to let the process finish.
fn process_output<R, F>(mut out: R, mut on_line: F) -> JoinHandle<()>
where
    R: BufRead + Send + 'static,
    F: FnMut(Result<MaybeString, io::Error>) -> PResult<()> + Send + 'static
{
    thread::spawn(move || {
        let out = &mut out;
        in_context_of("process output collection thread", || {
            for line in out.split(b'\n') {
                on_line(line.map(MaybeString))?;
            }
            Ok(())
        }).ok_or_log_error();

        for line in out.split(b'\n') {
            drop(line) // unblock the process in case of problems processing line
        }
    })
}

#[derive(Debug, Default, Clone)]
struct ReplyStyles {
    timestamp: Style,
    from: Style,
    notice: Style,
    stdout: Style,
    stderr: Style,
    error: Style,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct DiscoveredAgent {
    last_seen: Timestamp,
    tags: Tags,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct DiscoveredChannel {
    agents: BTreeMap<String, DiscoveredAgent>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct DiscoverFile {
    channels: BTreeMap<String, DiscoveredChannel>,
    #[serde(skip)]
    locked_file: Option<File>,
}

#[derive(Debug, PartialEq, Eq)]
enum AgentResult {
    Ok,
    TimeOut,
    AgentError(String),
    CommandError(String),
    Missing(Timestamp),
}

fn main() -> FinalResult {
    let args = Cli::from_args();
    init_logger(&args.logging, vec![module_path!()]);

    let reply_styles = if args.logging.force_colors || stdout_is_tty() {
        ReplyStyles {
            timestamp: Color::Green.style(),
            from: Color::Blue.style(),
            notice: Color::Yellow.style(),
            stdout: Color::Cyan.style(),
            stderr: Color::Magenta.style(),
            error: Color::Red.style(),
        }
    } else {
        Default::default()
    };

    match args.action {
        Action::Agent { tags, run_directory, log_command_output } => {
            let run_directory = run_directory.canonicalize().problem_while("canonicalizing run directory")?;

            info!("Starting Agent with run directory: {:?}", run_directory);
            let mut agent = Agent::new(&args.connection_string, args.identity.clone(), tags.into(), args.channel, args.base_station_public_key.as_deref(), args.network_secret_key.as_deref())?;
            let identity = args.identity;
            let run_directory = &run_directory;
            loop {
                agent.rx(|request, mut response| {
                    match request {
                        Request::Ping => response.reply(Reply::Pong)?,
                        Request::Run(command) => {
                            match run_directory.join(&command).canonicalize() {
                                Ok(command_path) => {
                                    if !command_path.starts_with(run_directory) {
                                        response.reply(Reply::Error(format!("Bad command: {:?}", command)))?;
                                    } else {
                                        if !command_path.is_file() {
                                            response.reply(Reply::Error(format!("No such command: {:?} ", command)))?;
                                        } else {
                                            info!("Running command: {:?}", command_path);
                                            response.reply(Reply::Run { path: command_path.display().to_string() })?;
                                            match Command::new(command_path)
                                                .current_dir(&run_directory)
                                                .stdin(Stdio::null())
                                                .stdout(Stdio::piped())
                                                .stderr(Stdio::piped())
                                                .args(&[&identity])
                                                .spawn() {
                                                Ok(mut child) => {
                                                    let stdout = child.stdout.take().unwrap();
                                                    let stderr = child.stderr.take().unwrap();

                                                    let stdout = process_output(BufReader::new(stdout), {
                                                        let command = command.clone();
                                                        let mut response = response.clone();
                                                        let styles = reply_styles.clone();
                                                        move |result| {
                                                            match result.problem_while("reading stdout") {
                                                                Ok(line) => {
                                                                    if log_command_output {
                                                                        eprintln!("[{}] {} {}",
                                                                            styles.timestamp.paint(format_timestamp(timestamp())), styles.stdout.paint("STDOUT:"), line);
                                                                    }
                                                                    response.reply(Reply::Stdout(line))
                                                                },
                                                                Err(err) => response.reply(Reply::Error(format!("Failed to process stdout: {:?}: {}", command, err))),
                                                            }
                                                        }
                                                    });

                                                    let stderr = process_output(BufReader::new(stderr), {
                                                        let command = command.clone();
                                                        let mut response = response.clone();
                                                        let styles = reply_styles.clone();
                                                        move |result| {
                                                            match result.problem_while("reading stderr") {
                                                                Ok(line) => {
                                                                    if log_command_output {
                                                                        eprintln!("[{}] {} {}",
                                                                            styles.timestamp.paint(format_timestamp(timestamp())), styles.stderr.paint("STDERR:"), line);
                                                                    }
                                                                    response.reply(Reply::Stderr(line))
                                                                },
                                                                Err(err) => response.reply(Reply::Error(format!("Failed to process stderr: {:?}: {}", command, err))),
                                                            }
                                                        }
                                                    });

                                                    stdout.join().ok().ok_or_problem("Joining stdout thread")?;
                                                    stderr.join().ok().ok_or_problem("Joining stderr thread")?;

                                                    let status = child.wait().problem_while("getting command status code")?;

                                                    response.reply(Reply::Status {
                                                        signal: status.signal(),
                                                        code: status.code(),
                                                    })?;
                                                    if let Some(code) = status.code() {
                                                        info!("Command finished with status code: {}", code);
                                                    } else if let Some(signal) = status.signal() {
                                                        info!("Command finished with signal: {}", signal);
                                                    } else {
                                                        info!("Command finished with unknown status");
                                                    }
                                                }
                                                Err(err) => response.reply(Reply::Error(format!("Failed to start command: {:?}: {}", command, err)))?,
                                            }

                                        }
                                    }
                                }
                                Err(_) => response.reply(Reply::Error(format!("No such command: {:?} ", command)))?,
                            };
                        }
                    };
                    Ok(())
                }).ok_or_log_error();
            }
        }
        Action::Station {
            discovery_file,
            no_discovery,
            fail_missing, tags,
            hello_wait,
            reply_wait,
            minimum_wait,
            request
        } => {
            let discovery_file = if no_discovery {
                None
            } else {
                Some(discovery_file.map(Ok).unwrap_or_else(|| with_create_dir(project_dir().cache_dir().join("discovery.toml")))?)
            };

            let mut discovery = discovery_file.as_deref().map(|discovery_file| {
                let mut file = OpenOptions::new()
                    .read(true)
                    .append(true)
                    .create(true)
                    .open(discovery_file)?;

                if file.try_lock_exclusive().is_err() {
                    warn!("Discovery file is locked (is there another base station instance running?); waiting for lock...");
                    file.lock_exclusive()?;
                }

                in_context_of_with(|| format!("reading discovery file: {:?}", discovery_file), || {
                    debug!("Reading discovery file: {:?}", discovery_file);

                    let mut data = String::new();
                    file.read_to_string(&mut data)?;

                    let mut discovery: DiscoverFile = if data.is_empty() {
                        Default::default()
                    } else {
                        toml::from_str(&data)?
                    };

                    discovery.locked_file = Some(file);
                    Ok(discovery)
                })
            }).transpose()?;

            let mut base_station = BaseStation::new(&args.connection_string, args.identity, args.channel.clone(), args.network_public_key.as_deref(), args.base_station_secret_key.as_deref())?;

            let session_timestamp = timestamp();
            let tags: Tags = tags.into();

            let mut reply_iter = base_station.request(tags.clone(), hello_wait, match request {
                    CliRequest::Ping => Request::Ping,
                    CliRequest::Run { command } => Request::Run(command),
                }
            )?;

            if let Some(discovery) = discovery.as_ref() {
                let total = discovery.channels.get(&args.channel)
                    .into_iter().map(|channel| channel.agents.values()).flatten()
                    .filter(|agent| tags.agent_tags_match(&agent.tags)).count();
                info!("Expecting {} agents to reply", total);
            }

            let mut agent_results = BTreeMap::new();

            loop {
                let replies = reply_iter.next();
                if let Some(replies) = replies.map(|result| result.problem_while("reading reply (late reply or wrong channel?)").ok_or_log_error()).flatten() {
                    for reply in replies {
                        print_reply(&reply, &reply_styles);

                        if let Some(discovery) = discovery.as_mut() {
                            let first_reply = if let Some(agent) = discovery.channels.get(&args.channel).and_then(|channel| channel.agents.get(&reply.from)) {
                                agent.last_seen < session_timestamp
                            } else {
                                true
                            };

                            if first_reply {
                                let agent = DiscoveredAgent {
                                    last_seen: session_timestamp,
                                    tags: reply.tags,
                                };

                                let channel = discovery.channels.entry(args.channel.clone()).or_default();
                                if channel.agents.insert(reply.from.clone(), agent).is_none() {
                                    warn!("Discovered new agent: {:?}", reply.from)
                                }

                                let agents = channel.agents.values().filter(|agent| tags.agent_tags_match(&agent.tags));
                                let total = agents.clone().count();
                                let seen = agents.clone().filter(|agent| agent.last_seen >= session_timestamp).count();
                                info!("Got a reply from {} of {} agents so far", seen, total);
                            }
                        }

                        match reply.reply {
                            Reply::Run { .. } => { agent_results.insert(reply.from,
                                AgentResult::TimeOut); },
                            Reply::Pong => { agent_results.insert(reply.from,
                                AgentResult::Ok); },
                            Reply::Status { code, signal } => { match (code, signal) {
                                (Some(0), _) => agent_results.insert(reply.from,
                                    AgentResult::Ok),
                                (Some(status), _) => agent_results.insert(reply.from,
                                    AgentResult::CommandError(format!("Command finished with non-zero status code: {}", status))),
                                (None, Some(signal)) => agent_results.insert(reply.from,
                                    AgentResult::CommandError(format!("Command aborted with signal: {}", signal))),
                                (None, None) => agent_results.insert(reply.from,
                                    AgentResult::CommandError(format!("Command aborted for unknown reason"))),
                            }; },
                            Reply::Error(err) => { agent_results.insert(reply.from, AgentResult::AgentError(err)); },
                            _ => (),
                        };
                    }
                } else {
                    break;
                }

                if agent_results.values().any(|result| *result == AgentResult::TimeOut) {
                    // Wait longer if we have agents that has still not provided the final result
                    reply_iter.set_timeout(reply_wait)?;
                } else {
                    let sec_elapsed = ((timestamp() - session_timestamp) / 1000) as usize;
                    let timeout = std::cmp::max(minimum_wait.saturating_sub(sec_elapsed), 1);
                    reply_iter.set_timeout(timeout)?;
                }
            }

            if let Some(discovery) = discovery {
                for (identity, agent) in discovery.channels.get(&args.channel).into_iter().map(|channel| channel.agents.iter()).flatten() {
                    if agent.last_seen != session_timestamp && tags.agent_tags_match(&agent.tags) {
                        agent_results.insert(identity.to_owned(), AgentResult::Missing(agent.last_seen));
                    }
                }

                if let Some(mut file) = discovery.locked_file.as_ref() {
                    in_context_of_with(|| format!("writing discovery file: {:?}", discovery_file), || {
                        debug!("Writing discovery file: {:?}", discovery_file);
                        file.set_len(0)?;
                        file.write_all(&toml::to_vec(&discovery)?)?;
                        Ok(())
                    })?;
                }
            }

            print_result(&agent_results, &reply_styles);

            let mut missing: bool = false;
            let mut command_error: bool = false;
            let mut timeout_error: bool = false;
            let mut agent_error: bool = false;

            for (_, result) in agent_results {
                match result {
                    AgentResult::Ok => (),
                    AgentResult::Missing(_) => missing = true,
                    AgentResult::CommandError(_) => command_error = true,
                    AgentResult::TimeOut => timeout_error = true,
                    AgentResult::AgentError(_) => agent_error = true,
                }
            }

            let mut ret = 0;
            let mut messages = Vec::new();
            if fail_missing && missing {
                ret += 2;
                messages.push("missing agent");
            }
            if command_error {
                ret += 4;
                messages.push("command error");
            }
            if timeout_error {
                ret += 8;
                messages.push("timeout error");
            }
            if agent_error {
                ret += 16;
                messages.push("agent error");
            }

            if ret > 0 {
                problem!("Finished with problems: {}", messages.join(", ")).fatal_with_status(ret)?;
            }
        }
    }

    Ok(())
}

fn print_reply(reply: &ReplyMessage, styles: &ReplyStyles) {
    print!("[{}] {}: ", styles.timestamp.paint(format_timestamp(reply.timestamp)), styles.from.paint(&reply.from));
    match &reply.reply {
        Reply::Run { path } => println!("{} {}", styles.notice.paint(">>"), path),
        Reply::Stdout(message) => println!("{} {}", styles.stdout.paint("O>"), message),
        Reply::Stderr(message) => println!("{} {}", styles.stderr.paint("E>"), message),
        Reply::Error(message) => println!("{} {}", styles.error.paint("!>"), message),
        Reply::Status { code: Some(code), signal: None } => println!("{} Finished with status code: {}", styles.notice.paint("<<"), code),
        Reply::Status { code: None, signal: Some(signal) } => println!("{} Finished with signal: {}", styles.error.paint("!!"), signal),
        reply @ Reply::Pong => println!("{} {:?}", styles.notice.paint("<<"), reply),
        reply => println!("{} {:?}", styles.notice.paint("?>"), reply),
    }
}

fn print_result(agent_results: &BTreeMap<String, AgentResult>, styles: &ReplyStyles) {
    for (agent, result) in agent_results {
        match result {
            AgentResult::Ok => info!("{}: {}", styles.from.paint(agent), styles.stdout.paint("OK")),
            AgentResult::Missing(last_seen) => warn!("{}: {}: Previously seen agent did not reply; last seen: {}", styles.from.paint(agent), styles.notice.paint("Missing"), format_timestamp(*last_seen)),
            AgentResult::TimeOut => warn!("{}: {}: Command started but did not finish in time", styles.from.paint(agent), styles.error.paint("Expired")),
            AgentResult::CommandError(err) => warn!("{}: {}: Agent's command finished with error: {}", styles.from.paint(agent), styles.error.paint("KO"), err),
            AgentResult::AgentError(err) => error!("{}: {}: Agent finished with error: {}", styles.from.paint(agent), styles.error.paint("Error"), err),
        }
    }
}