Skip to content

Commit

Permalink
Update readme and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnavi committed Dec 26, 2024
1 parent 089078e commit 917bad6
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Komandan is a server automation tool that simplifies remote server management by
- [`komando` function](#komando-function)
- [Modules](#modules)
- [Built-in functions](#built-in-functions)
- [Default Values](#default-values)
- [Parallel Execution](#parallel-execution)
- [Error Handling](#error-handling)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -167,6 +169,78 @@ local env = komandan.defaults:get_env("ENV_VAR")
local env_all = komandan.defaults:get_all_env()
```

## Parallel Execution

Komandan supports parallel execution of tasks on multiple hosts using the `komando_parallel_hosts` function, and `komando_parallel_tasks` function for parallel execution of tasks on the same host.

```lua
-- parallel execution of a task on multiple hosts
local hosts = {
{
name = "server1",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
},
{
name = "server2",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
},
{
name = "server3",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
}
}

local task = {
name = "Ping Google",
komandan.modules.cmd({
cmd = "ping google.com -c 5",
}),
}

komandan.komando_parallel_hosts(hosts, task)
```

```lua
-- parallel execution of a task on the same host
local host = {
name = "My Server",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
}

local tasks = {
{
name = "Task 1",
komandan.modules.cmd({
cmd = "uname -a",
}),
},
{
name = "Task 2",
komandan.modules.cmd({
cmd = "ping google.com -c 7",
}),
},
{
name = "Task 3",
komandan.modules.apt({
package = "neovim",
update_cache = true
}),
elevate = true,
}
}

komandan.komando_parallel_tasks(host, tasks)
```

## Error Handling

Komandan provides error information through the return values of the `komando` function. If a task fails, the `exit_code` will be non-zero, and `stderr` may contain error messages. You can use the `ignore_exit_code` option in a task to continue execution even if a task fails.
Expand Down
47 changes: 47 additions & 0 deletions tests/komando_parallel_hosts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use komandan::create_lua;
use mlua::{chunk, Integer, Table, Value};

#[test]
fn test_komando_parallel_hosts() {
let lua = create_lua().unwrap();

let results = lua
.load(chunk! {
local hosts = {
{
name = "server1",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
},
{
name = "server2",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
},
{
name = "server3",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
}
}

local task = {
name = "Echo 1",
komandan.modules.cmd({
cmd = "echo 1",
}),
}

return komandan.komando_parallel_hosts(hosts, task)
})
.eval::<Table>()
.unwrap();

for pair in results.pairs::<Value, Table>() {
let (_, table) = pair.unwrap();
assert!(table.get::<Integer>("exit_code").unwrap() == 0);
}
}
47 changes: 47 additions & 0 deletions tests/komando_parallel_tasks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use komandan::create_lua;
use mlua::{chunk, Integer, Table, Value};

#[test]
fn test_komando_parallel_tasks() {
let lua = create_lua().unwrap();

let results = lua
.load(chunk! {
local host = {
name = "My Server",
address = "localhost",
user = "usertest",
private_key_file = os.getenv("HOME") .. "/.ssh/id_ed25519",
}

local tasks = {
{
name = "Task 1",
komandan.modules.cmd({
cmd = "uname -a",
}),
},
{
name = "Echo 1",
komandan.modules.cmd({
cmd = "echo 1",
}),
},
{
name = "Echo 2",
komandan.modules.cmd({
cmd = "echo 2"
}),
}
}

return komandan.komando_parallel_tasks(host, tasks)
})
.eval::<Table>()
.unwrap();

for pair in results.pairs::<Value, Table>() {
let (_, table) = pair.unwrap();
assert!(table.get::<Integer>("exit_code").unwrap() == 0);
}
}

0 comments on commit 917bad6

Please sign in to comment.