Allow running commands across a group

This commit is contained in:
R Tyler Croy 2020-12-28 22:34:48 -08:00
parent b66d6824bc
commit e82ca1eff0
2 changed files with 27 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use ssh2::Session;
#[derive(Clone, Debug, Deserialize)]
struct Inventory {
groups: Vec<Group>,
targets: Vec<Target>,
}
@ -16,6 +17,12 @@ struct Target {
uri: String,
}
#[derive(Clone, Debug, Deserialize)]
struct Group {
name: String,
targets: Vec<String>,
}
fn main() {
let opts = MyOptions::parse_args_default_or_exit();
@ -30,18 +37,34 @@ fn main() {
match opts.command.unwrap() {
Command::Cmd(runopts) => {
if let Some(group) = inventory.groups.iter().find(|g| g.name == runopts.targets) {
std::process::exit(run_group(&runopts.command, &group, &inventory));
}
if let Some(target) = inventory.targets.iter().find(|t| t.name == runopts.targets) {
println!("run a command: {:?}", runopts);
std::process::exit(run(&runopts.command, &target));
}
else {
println!("Couldn't find a target named `{}`", runopts.targets);
}
println!("Couldn't find a target named `{}`", runopts.targets);
},
_ => {},
}
}
fn run_group(command: &str, group: &Group, inventory: &Inventory) -> i32 {
let mut status = 1;
for target_name in group.targets.iter() {
// XXX: This is inefficient
for target in inventory.targets.iter() {
if &target.name == target_name {
println!("Running on `{}`", target.name);
status = run(command, &target);
}
}
}
status
}
fn run(command: &str, target: &Target) -> i32 {
// Connect to the local SSH server

View File

@ -1,5 +1,5 @@
groups:
- name: containers
- name: bsd
targets:
- freebsd-tor
- gopher