Skip to content

Commit 9fae02c

Browse files
author
Oleksii Tsvietnov
committed
Add a new example
1 parent a1c60bd commit 9fae02c

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ To get started and find out some features, I suggest to go through these example
9797
* Another useful use case is using TrivialRC for [process managing](https://github.com/vorakl/TrivialRC/tree/master/examples/process-manager) of a group of processes which represent one compound application and can be invoked then on the system from the Sysdemd
9898
* This solution shows how to [configure services in a docker container by using templates](https://github.com/vorakl/TrivialRC/tree/master/examples/docker-config-templates) and environment variables
9999
* This trick shows how to [create configuration on the fly](https://github.com/vorakl/TrivialRC/tree/master/examples/self-configuring) from the `boot` stage
100+
* [Serial launching of group of parallel processes](https://github.com/vorakl/TrivialRC/tree/master/examples/sync-run-of-async-groups) and fails immediately if some group failed
100101

101102
## Verbose levels
102103

@@ -151,7 +152,7 @@ You can also use some of internal functions in async/sync tasks:
151152
The life cycle of TrivialRC consists of different stages, with different isolation.
152153
By default, all configuration files (or trc.d/ directory with them) are searched in the directory from which was executed `trc` itself. For instance, if you've installed trc in /usr/bin/ and run it by using only its name, like `trc`, then configuration will also be searched in /usr/bin/. Though, you can place configuration files anywhere you like and specify their location in the `-w|--workdir` option, like `trc -w /etc/`.
153154

154-
Let's check that:
155+
Let's check:
155156

156157
```bash
157158
$ which trc
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Serial launching of group of parallel processes and fails immediately if some group failed
2+
3+
This example uses the idea of ["create configuration on the fly"](https://github.com/vorakl/TrivialRC/tree/master/examples/self-configuring) example. The trick is to run one TrivialRC with one wait policy a group of other TrivialRCs with another wait policy. The goal of a first copy of TrivialRC is:
4+
5+
* to construct a proper list of sync tasks
6+
* itereate over the list checking exit status after each task (because this trc is run with 'wait_err' wait policy)
7+
8+
Each sync task runs another copy of TrivialRC with another wait policy ('wait_all') to iterate over a smaller group of IP address (or other parameters from a command line) by running commands in parallel. All final commands will be forms at the `boot` stage in the same way.
9+
10+
So, it would look like:
11+
12+
```bash
13+
RC_WAIT_POLICY=wait_err trc --workdir .
14+
\- RC_WAIT_POLICY=wait_all trc --workdir sub-trc IP1 IP2 IP3 ... IP10
15+
a \- cmd IP1
16+
s \- cmd IP2
17+
y \- cmd IP3
18+
n \- .......
19+
c \- cmd IP10
20+
s \- .....
21+
y \- .....
22+
n \- .....
23+
c \- RC_WAIT_POLICY=wait_all trc --workdir sub-trc IP1 IP2 IP3 ... IP10
24+
a \- cmd IP1
25+
s \- cmd IP2
26+
y \- cmd IP3
27+
n \- .......
28+
c \- cmd IP10
29+
```
30+
The main idea is to have only boot scripts for bootstrapping subsequent behavior of the first TrivialRC copy by
31+
running one by one another copy of TrivialRC which eventually runs commands in parallel (one command per one IP).
32+
33+
A number of parallel tasks and a command can be changed by environment variables ASYNC_TASKS and TASK_CMD accordingly.
34+
35+
## Test
36+
37+
```bash
38+
$ RC_WAIT_POLICY=wait_err trc -w .
39+
40+
$ TASK_CMD="uname -a" RC_WAIT_POLICY=wait_err trc -w .
41+
42+
$ ASYNC_TASKS=20 TASK_CMD="id -un" RC_WAIT_POLICY=wait_err trc -w .
43+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local _ip _params
2+
3+
for _ip in "$@"; do
4+
_params="${_params}-D 'ssh -i admin.key -o StrictHostKeyChecking=no admin@${_ip} ${TASK_CMD:-uptime}' "
5+
done
6+
7+
# replaces all command line parameters
8+
eval set -- ${_params}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The script gets IP address,
2+
# converts them to lines by ${ASYNC_TASKS} elements (or 10 by default),
3+
# iterates over the list and for each new line
4+
# forms a part of a future command line for itself and then
5+
# updates its own command line to have a number of sync process when
6+
# it has moved to the next stage.
7+
8+
local _ips _params
9+
10+
while read _ips; do
11+
_params="${_params}-F 'RC_WAIT_POLICY=wait_all trc -w sub-trc ${_ips}' "
12+
done < <( curl -sSLf 'http://service-discovery.domain.com/api/nodes' | \
13+
jq -r '.[] | select(.ipaddr != null) | .ipaddr' | \
14+
xargs -n ${ASYNC_TASKS:-10} echo )
15+
16+
eval set -- "$@" ${_params}

0 commit comments

Comments
 (0)