mirror of
https://github.com/house-of-vanity/rexec.git
synced 2025-07-08 09:14:08 +00:00
Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
52f4e54bc4 | |||
9dc659cea1 | |||
1235e17f6b | |||
aaa28eae67 | |||
c016ef8a0f | |||
13ab3f3dbd | |||
70136e9074 | |||
2334c1a8f3 | |||
da6ae2ce37 | |||
e2cc36adf1 | |||
080ce8984a | |||
b360e29666 | |||
1d26a9c6be | |||
48e7e2a4e4 | |||
aa0a4e8bd2 | |||
554b68b51e | |||
c206ddaef2 | |||
e620348834 | |||
dc1a67fe01 | |||
1d208149f5 | |||
093157b6ef | |||
04da0fb97c | |||
967d9c0e7d | |||
eb0fd40d9f | |||
d0bbb29078 | |||
5a388853c5 | |||
41bf46c408 | |||
a2b05a7fa6 |
21
.github/workflows/release.yml
vendored
21
.github/workflows/release.yml
vendored
@ -67,7 +67,7 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
name: Release binary
|
name: Making New Release ^_^
|
||||||
needs:
|
needs:
|
||||||
- build
|
- build
|
||||||
- build_arch
|
- build_arch
|
||||||
@ -96,3 +96,22 @@ jobs:
|
|||||||
artifacts: "${{ steps.download_binary.outputs.download-path }}/*,${{ steps.download_pkg.outputs.download-path }}/*"
|
artifacts: "${{ steps.download_binary.outputs.download-path }}/*,${{ steps.download_pkg.outputs.download-path }}/*"
|
||||||
allowUpdates: true
|
allowUpdates: true
|
||||||
body: Static musl build for x86_64-linux and Arch Linux package
|
body: Static musl build for x86_64-linux and Arch Linux package
|
||||||
|
|
||||||
|
dispatch:
|
||||||
|
name: Notify my Arch Repo
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
- build_arch
|
||||||
|
- release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 5
|
||||||
|
steps:
|
||||||
|
- name: Dispatch to workflows
|
||||||
|
run: |
|
||||||
|
curl -L \
|
||||||
|
-X POST \
|
||||||
|
-H "Accept: application/vnd.github+json" \
|
||||||
|
-H "Authorization: Bearer ${{ secrets.ACTIONS_KEY }}" \
|
||||||
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
||||||
|
https://api.github.com/repos/house-of-vanity/arch_repo/dispatches \
|
||||||
|
-d '{"event_type":"new_package","client_payload":{}}'
|
||||||
|
15
.github/workflows/update_repo.yml
vendored
Normal file
15
.github/workflows/update_repo.yml
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
name: Dispatcher
|
||||||
|
on: ["push"]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
dispatch:
|
||||||
|
name: Dispatch
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 5
|
||||||
|
steps:
|
||||||
|
- name: Dispatch to workflows
|
||||||
|
run: |
|
||||||
|
curl -H "Accept: application/vnd.github.everest-preview+json" \
|
||||||
|
-H "Authorization: token ${{ secrets.ACTIONS_KEY }}" \
|
||||||
|
--request POST \
|
||||||
|
--data '{"event_type": "some_event", "client_payload": { "foo": "bar" }}' https://api.github.com/repos/house-of-vanity/arch_repo/dispatches
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1066,7 +1066,7 @@ checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rexec"
|
name = "rexec"
|
||||||
version = "1.0.6"
|
version = "1.0.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"brace-expand",
|
"brace-expand",
|
||||||
"clap 4.3.4",
|
"clap 4.3.4",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rexec"
|
name = "rexec"
|
||||||
version = "1.0.7"
|
version = "1.1.0"
|
||||||
readme = "https://github.com/house-of-vanity/rexec#readme"
|
readme = "https://github.com/house-of-vanity/rexec#readme"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Parallel SSH executor"
|
description = "Parallel SSH executor"
|
||||||
@ -22,7 +22,6 @@ lazy-st = "0.2.2"
|
|||||||
regex = "1"
|
regex = "1"
|
||||||
clap = { version = "4.3.4", features = ["derive"] }
|
clap = { version = "4.3.4", features = ["derive"] }
|
||||||
colored = "2.0.0"
|
colored = "2.0.0"
|
||||||
#dialoguer = "0.10.4"
|
|
||||||
itertools = "0.10.5"
|
itertools = "0.10.5"
|
||||||
brace-expand = "0.1.0"
|
brace-expand = "0.1.0"
|
||||||
question = "0.2.2"
|
question = "0.2.2"
|
||||||
|
70
README.md
70
README.md
@ -1,14 +1,20 @@
|
|||||||
# rexec
|
# rexec
|
||||||
Parallel SSH executor in Rust. It can read servers from `~/.ssh/known_hosts`
|
**Parallel SSH executor** in Rust with static binary. It can read servers from `~/.ssh/known_hosts`
|
||||||
|
|
||||||
or even expand servers from bash expanshion line `cassandra.{prod,dev}.example.com`
|
|
||||||
|
|
||||||
|
or even expand servers from bash expanshion line `cassandra-[1:5].{prod,dev}.example.com`
|
||||||
|
|
||||||
[](https://github.com/house-of-vanity/rexec/actions/workflows/release.yml)
|
[](https://github.com/house-of-vanity/rexec/actions/workflows/release.yml)
|
||||||
|
---
|
||||||
|
|
||||||
|
## Usage
|
||||||
```shell
|
```shell
|
||||||
[ab@test]$ ./rexec -k admin-gce-sc.* --help
|
_|_|_|_| _| _| _|_|_|_| _|_|_|
|
||||||
|
_| _|_| _| _| _| _| _|
|
||||||
|
_|_| _|_|_| _| _|_|_| _|
|
||||||
|
_| _| _| _| _| _|
|
||||||
|
_| _|_|_|_| _| _| _|_|_|_| _|_|_|
|
||||||
|
|
||||||
|
|
||||||
Parallel SSH executor in Rust
|
Parallel SSH executor in Rust
|
||||||
|
|
||||||
Usage: rexec [OPTIONS] --expression <EXPRESSION> --command <COMMAND>
|
Usage: rexec [OPTIONS] --expression <EXPRESSION> --command <COMMAND>
|
||||||
@ -24,39 +30,33 @@ Options:
|
|||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
-V, --version Print version
|
-V, --version Print version
|
||||||
```
|
```
|
||||||
|
---
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
## Examples
|
||||||
```shell
|
```shell
|
||||||
[ab@test debug]$ ./rexec -u ab -k -c uptime -e admin.* -f
|
$ rexec -f \
|
||||||
|
-e 'cassandra-gce-or-[1:2]' \
|
||||||
|
-u ab \
|
||||||
|
-c 'uname -r; date'
|
||||||
|
[INFO ] Using string expansion to build server list.
|
||||||
[INFO ] Matched hosts:
|
[INFO ] Matched hosts:
|
||||||
[INFO ] admin-gce-sc-1.lca-prod.** [35.211.27.195]
|
[INFO ] cassandra-gce-or-1.prod.example.com [2.22.123.79]
|
||||||
[INFO ] admin-gce-sc-1.mmk-prod.** [35.211.79.202]
|
[INFO ] cassandra-gce-or-2.prod.example.com [2.22.123.158]
|
||||||
[ERROR] admin-gce-sc-1.led-prod.** couldn't be resolved.
|
Continue on following 2 servers? y
|
||||||
[INFO ] admin-gce-sc-1.msq-dev.** [35.211.0.24]
|
[INFO ] Run command on 2 servers.
|
||||||
[ERROR] admin-gce-sc-1.hui-dev.** couldn't be resolved.
|
|
||||||
|
|
||||||
[INFO ] Run command on 3 servers.
|
cassandra-gce-or-1.prod.example.com
|
||||||
|
Exit code [0] / stdout 45 bytes / stderr 0 bytes
|
||||||
|
STDOUT
|
||||||
|
║ 5.15.0-1040-gcp
|
||||||
|
║ Thu Sep 7 13:44:40 UTC 2023
|
||||||
|
|
||||||
|
cassandra-gce-or-2.prod.example.com
|
||||||
[INFO ] admin.gnb-prod.**
|
Exit code [0] / stdout 45 bytes / stderr 0 bytes
|
||||||
Code 0
|
STDOUT
|
||||||
STDOUT:
|
║ 5.15.0-1040-gcp
|
||||||
23:31:21 up 294 days, 14:14, 0 users, load average: 0.53, 0.64, 0.52
|
║ Thu Sep 7 13:44:40 UTC 2023
|
||||||
|
|
||||||
STDERR:
|
|
||||||
|
|
||||||
[INFO ] admin.abe-prod.**
|
|
||||||
Code 0
|
|
||||||
STDOUT:
|
|
||||||
23:31:22 up 154 days, 9:24, 0 users, load average: 0.31, 0.25, 0.18
|
|
||||||
|
|
||||||
STDERR:
|
|
||||||
|
|
||||||
[INFO ] admin-gce-be-1.toy-prod.**
|
|
||||||
Code 0
|
|
||||||
STDOUT:
|
|
||||||
23:31:22 up 98 days, 6:20, 0 users, load average: 0.88, 0.74, 0.80
|
|
||||||
|
|
||||||
STDERR:
|
|
||||||
no server running on /tmp/tmux-1019/default
|
|
||||||
open terminal failed: not a terminal
|
|
||||||
```
|
```
|
||||||
|
92
src/main.rs
92
src/main.rs
@ -9,7 +9,6 @@ use std::process;
|
|||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use colored::*;
|
use colored::*;
|
||||||
//use dialoguer::Confirm;
|
|
||||||
use dns_lookup::lookup_host;
|
use dns_lookup::lookup_host;
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -35,9 +34,10 @@ struct Args {
|
|||||||
#[arg(
|
#[arg(
|
||||||
short,
|
short,
|
||||||
long,
|
long,
|
||||||
|
num_args = 1..,
|
||||||
help = "Expression to build server list. List and range expansion are supported. Example: 'web-[1:12]-io-{prod,dev}'"
|
help = "Expression to build server list. List and range expansion are supported. Example: 'web-[1:12]-io-{prod,dev}'"
|
||||||
)]
|
)]
|
||||||
expression: String,
|
expression: Vec<String>,
|
||||||
|
|
||||||
#[arg(short, long, help = "Command to execute on servers")]
|
#[arg(short, long, help = "Command to execute on servers")]
|
||||||
command: String,
|
command: String,
|
||||||
@ -157,22 +157,30 @@ fn main() {
|
|||||||
let hosts = if args.known_hosts {
|
let hosts = if args.known_hosts {
|
||||||
info!("Using ~/.ssh/known_hosts to build server list.");
|
info!("Using ~/.ssh/known_hosts to build server list.");
|
||||||
let known_hosts = read_known_hosts();
|
let known_hosts = read_known_hosts();
|
||||||
// Build regex
|
let mut all_hosts = Vec::new();
|
||||||
let re = match Regex::new(&args.expression) {
|
for expression in args.expression.iter() {
|
||||||
Ok(result) => result,
|
let re = match Regex::new(expression) {
|
||||||
Err(e) => {
|
Ok(result) => result,
|
||||||
error!("Error parsing regex. {}", e);
|
Err(e) => {
|
||||||
process::exit(1);
|
error!("Error parsing regex. {}", e);
|
||||||
}
|
process::exit(1);
|
||||||
};
|
}
|
||||||
// match hostnames from known_hosts to regex
|
};
|
||||||
known_hosts
|
let matched: Vec<Host> = known_hosts
|
||||||
.into_iter()
|
.clone()
|
||||||
.filter(|r| re.is_match(&r.name.clone()))
|
.into_iter()
|
||||||
.collect()
|
.filter(|r| re.is_match(&r.name.clone()))
|
||||||
|
.collect();
|
||||||
|
all_hosts.extend(matched);
|
||||||
|
}
|
||||||
|
all_hosts
|
||||||
} else {
|
} else {
|
||||||
info!("Using string expansion to build server list.");
|
info!("Using string expansion to build server list.");
|
||||||
expand_string(&args.expression)
|
let mut all_hosts = Vec::new();
|
||||||
|
for expression in args.expression.iter() {
|
||||||
|
all_hosts.extend(expand_string(expression));
|
||||||
|
}
|
||||||
|
all_hosts
|
||||||
};
|
};
|
||||||
|
|
||||||
// Dedup hosts from known_hosts file
|
// Dedup hosts from known_hosts file
|
||||||
@ -234,9 +242,7 @@ fn main() {
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
info!("\n");
|
|
||||||
info!("Run command on {} servers.", &config.hosts.len());
|
info!("Run command on {} servers.", &config.hosts.len());
|
||||||
info!("\n");
|
|
||||||
|
|
||||||
// Run a command on all the configured hosts.
|
// Run a command on all the configured hosts.
|
||||||
// Receive the result of the command for each host and print its output.
|
// Receive the result of the command for each host and print its output.
|
||||||
@ -248,8 +254,8 @@ fn main() {
|
|||||||
.collect::<Vec<_>>()[0]
|
.collect::<Vec<_>>()[0]
|
||||||
.to_string();
|
.to_string();
|
||||||
let ip = ip.parse::<IpAddr>().unwrap();
|
let ip = ip.parse::<IpAddr>().unwrap();
|
||||||
info!(
|
println!(
|
||||||
"{}",
|
"\n{}",
|
||||||
hosts_and_ips
|
hosts_and_ips
|
||||||
.get(&ip)
|
.get(&ip)
|
||||||
.unwrap_or(&"Couldn't parse IP".to_string())
|
.unwrap_or(&"Couldn't parse IP".to_string())
|
||||||
@ -265,14 +271,50 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if output.exit_status == 0 {
|
let code_string = if output.exit_status == 0 {
|
||||||
println!("Code {}", output.exit_status);
|
format!("{}", output.exit_status.to_string().green())
|
||||||
} else {
|
} else {
|
||||||
error!("Code {}", output.exit_status);
|
format!("{}", output.exit_status.to_string().red())
|
||||||
};
|
};
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
format!(
|
||||||
|
"Exit code [{}] | std out/err [{}/{}] bytes",
|
||||||
|
code_string,
|
||||||
|
output.stdout.len(),
|
||||||
|
output.stderr.len()
|
||||||
|
)
|
||||||
|
.bold()
|
||||||
|
);
|
||||||
|
|
||||||
if !args.code {
|
if !args.code {
|
||||||
println!("STDOUT:\n{}", String::from_utf8(output.stdout).unwrap());
|
match String::from_utf8(output.stdout) {
|
||||||
println!("STDERR:\n{}", String::from_utf8(output.stderr).unwrap());
|
Ok(stdout) => match stdout.as_str() {
|
||||||
|
"" => {}
|
||||||
|
_ => {
|
||||||
|
let prefix = if output.exit_status != 0 {
|
||||||
|
format!("{}", "║".cyan())
|
||||||
|
} else {
|
||||||
|
format!("{}", "║".green())
|
||||||
|
};
|
||||||
|
for line in stdout.lines() {
|
||||||
|
println!("{} {}", prefix, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
match String::from_utf8(output.stderr) {
|
||||||
|
Ok(stderr) => match stderr.as_str() {
|
||||||
|
"" => {}
|
||||||
|
_ => {
|
||||||
|
for line in stderr.lines() {
|
||||||
|
println!("{} {}", "║".red(), line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user