## Description
R7 is a shell remote configuration management framework designed to:
- work within site specific repositories organized with groups and nodes
- group functions and execute them in parallel on a list of nodes (hosts)
- map the executions (`_run/`) to a tree structure (`_output/`)
- trace the executions of functions and report issues or changes
R7 is inspired by the amazing work made by the
[rset](https://scriptedconfiguration.org/) tool. Initially this tooling was
written as the automation part for `inda.re` infrastructure.
## Tutorial
### Introduction
There is an example of r7 repository:
./nodes/
./nodes/hostA
./nodes/hostB
./nodes/hostC
./groups/
./groups/common
./groups/test0
./groups/common.dir
./groups/test0.dir
./.ssh/
./.ssh/config
./.ssh/id_ed25519_r7
./.ssh/id_ed25519_r7.pub
./site-config
_Nodes sub-directories are hostnames understood by the ssh client and
configuration._
_Currently, r7 requires valid ssh configuration and keyfiles in the repository
`.ssh/` directory._
_Repositories can be initialized with `rset init` or `rset -w
path/to/new-repository init`, the user will then have to provide valid
configurations and keys for `.ssh/` in the newly created directory._
There is an example of r7 site configuration (`site-config` file):
```sh
prefix install state doc
group common '.*'
group test0 hostB hostC
output-dump
summary
```
There is an exemple of the deployment of that configuration within the r7
repository:
$ r7 run-config
- (ok) hostA:common::install_nothing
- (ok) hostA:common::state_uname
- (ok) hostA:common::doc_tutorial
- (ok) hostB:common::install_nothing
- (ok) hostB:common::state_uname
- (ok) hostB:common::doc_tutorial
- (ok) hostC:common::install_nothing
- (ok) hostC:common::state_uname
- (ok) hostC:common::doc_tutorial
- (ok) hostB:test0::state_test
- (ok) hostC:test0::state_test
In this example, everything goes fine, groups are executed on each related
nodes, note the `'.*'` matching all hosts present in `./nodes`. Groups are POSIX
shell scripts that are a collection of functions.
There is the `common` group:
```sh
install_nothing() {
echo Nothing installed
}
state_uname() {
uname -a
}
doc_tutorial() {
echo "## Welcome to the R7 tutorial!"
}
```
And there is the `test0` group:
```sh
state_test() {
test 1 -eq 1
}
```
Group files names can't contain spaces, tabs or newlines, as well for nodes
dirnames. _The ':' character is not recommanded as well as it may reduce
lisibility of the interactive output._
Called groups and node directories are copied to related nodes in `hostname:/tmp/r7/`:
/tmp/r7/hostA/ # Contents of test.org/nodes/hostA/
/tmp/r7/common.dir # Contents of test.org/groups/common.dir/
/tmp/r7/test.dir # Contents of test.org/groups/test.dir
Groups may have no `.dir` directory, in this case, nothing is copied.
Once the group copy is done, functions are executed in the order of the defined
prefix (part of `site-config`):
prefix install state doc
This means the functions matching `^install_.*()`, `^state_.*()` and `^doc_.*()`
(the default R7_PREFIX) in the respective declaration order they're found in the
related group file will be executed.
Site confiurations are shell scripts sourced by r7 after it's environment is
initialized, so `prefix`, `group`, `output-dump`, `summary` are the functions or
aliases provided by r7 itself. This allow for scripting from the `site-config`
file. Here `output-dump`, and `summary` are called to output potential errors,
diffs and permissions changes at the end of the interactive output obtained by
`r7 run-config`. If nothing catched happened, nothing is added to the output.
### Errors during remote execution of groups
In our previous example, we modify `state_test` so the `test` fails.
```sh
state_test() {
test 1 -eq -2
}
```
And we re-run the configuration to see what's happenning.
$ r7 run-config
- (ok) hostA:common::install_nothing
- (ok) hostA:common::state_uname
- (ok) hostA:common::doc_tutorial
- (ok) hostB:common::install_nothing
- (ok) hostB:common::state_uname
- (ok) hostB:common::doc_tutorial
- (ok) hostC:common::install_nothing
- (ok) hostC:common::state_uname
- (ok) hostC:common::doc_tutorial
- (error 1) hostB:test0::state_test
- (error 1) hostC:test0::state_test
- (error 1) - in hostB test0 state_test (1748622980)
- (error 1) - in hostC test0 state_test (1748622980)
The last two lines are part of the `summary` output.
Let's modify the `state_test` function again to add some verbosity to it's
output.
_Note that we also change the return value._
```sh
state_test() {
echo something
false || {
echo >&2 failed
return 22
}
}
```
This will produce the following summary:
- (error 22) - in hostB test0 state_test (1748622980)
something
failed
- (error 22) - in hostC test0 state_test (1748622980)
something
failed
Diffs and permissions changes are also displayed by the `summary` command. When
they're are detected, the captured related output is shown.
---
## Group functions
Those functions are accessible during the execution of the group file's functions.
1. [trace](#trace)
2. [error](#error)
3. [node](#node)
4. [nodename](#nodename)
5. [nodedir](#nodedir)
6. [groupdir](#groupdir)
7. [install](#install)
8. [groupinstall](#groupinstall)
9. [nodeinstall](#nodeinstall)
---
### trace
***Description:***
Append a trace containing the current node name and `trace_id`.
**Usage:**
```sh
trace [string...]
```
---
### error
***Description:***
Trace an error message that will inform the user of an event in the summary
output.
**Usage:**
```sh
error [string...]
```
---
### node
***Description:***
Returns true if the argument is an existing file or directory inside `/tmp/r7/$(nodename)`. Else, returns 31.
***Usage:***
```sh
node [filename|dirname]
```
---
### nodename
***Description:***
Returns the small hostname of the current host.
***Usage:***
```sh
nodename
```
---
### nodedir
***Description:***
Returns `/tmp/r7/$(nodename)`.
***Usage:***
```sh
nodename
```
---
### groupdir
***Description:***
Returns `/tmp/r7/$groupname.dir`.
***Usage:***
```sh
groupdir
```
---
### install
***Description:***
Change directory to `/tmp/r7` and install files if changed and sets mode and
owner. Returns true if the file changed, otherwise returns 30.
***Usage:***
```sh
install [-o owner] [-m mode] source destintaion
```
---
### groupinstall
***Description:***
Change directory to the current group `.dir` directory in `/tmp/r7` then
performs like `install`.
***Usage:***
```sh
groupinstall [-o owner] [-m mode] source destintaion
```
---
### nodeinstall
***Description:***
Change directory to the current node directory in `/tmp/r7` then
performs like `install`.
***Usage:***
```sh
nodeinstall [-o owner] [-m mode] source destintaion
```
---
## r7 command line
***Usage:***
```sh
r7 [options] [command [arguments...]]
```
### Options
Below is a description of the command line options available:
- **-h**
Show usage and exit
- **-d**
Enable debug mode
- **-i**
Specify the SSH identity file
- **-F**
Specify the SSH client configuration file
- **-w**
Set the work directory where `./nodes` and `./groups` can be found
- **-r**
Set the run directory (default: `./_run`)
- **-o**
Set the output directory (default: `./_output`)
- **-c**
Specify the site configuration file (default: `./site-config`)
- **-n**
Enable dry run mode; prevents any modification on hosts
- **-p**
Run each host in parallel per group call
- **-O**
Show output interactively
- **-P**
Specify a prefix
- **-a**
Select all available prefixes for a group command
- **-s**
Apply output-dump and summary after a group command
### Commands
Below is a description of the available commands:
- **run-config**
Run the site configuration
- **group**
Deploy specified group to hosts
- **output-dump**
Dump the run directory to the output directory
- **output**
Consult the output
- **summary**
Produce a summary of errors and changes
- **digraph-host**
Produce a dot digraph for hosts
- **info**
Default command showing active nodes and summary
- **init**
Initialize a new work directory
# r7 library
## Interface
1. [info](#info)
2. [usage](#usage)
---
### info
***Description:***
Default command showing active nodes and summary, ran when sourced
***Usage:***
```sh
info
```
---
### usage
***Description:***
Shows the command line help
***Usage:***
```sh
usage
```
## SSH functions
1. [unlock](#unlock)
2. [copy](#copy)
3. [ssh](#ssh)
4. [ssh-debug](#ssh-debug)
5. [ssh-known-hosts](#ssh-known-hosts)
6. [ssh-authorized-keys-reset](#ssh-authorized-keys-reset)
7. [ssh-accept-new](#ssh-accept-new)
8. [ssh-control-master-clean](#ssh-control-master-clean)
9. [ssh-users](#ssh-users)
10. [ssh-ids](#ssh-ids)
11. [ssh-authorized-keys](#ssh-authorized-keys)
12. [ssh-ids-hosts](#ssh-ids-hosts)
---
### unlock
***Description:***
Resets the SSH agent environment and issues ssh-add for SSH_IDENTITY_FILE
***Usage:***
```sh
unlock
```
---
### copy
***Description:***
r7 openrsync, rsync, scp wrapper; selected in order of availability
***Usage:***
```sh
copy SRC DST
```
---
### ssh
***Description:***
r7 SSH wrapper; behaves like the standard OpenSSH command
***Usage:***
```sh
ssh OPTION HOST
```
---
### ssh-debug
***Description:***
r7 SSH with debug connection mode for a specific HOST
***Usage:***
```sh
ssh-debug HOST
```
---
### ssh-known-hosts
***Description:***
Calls ssh-keygen to dump the SSH known_hosts file
***Usage:***
```sh
ssh-known-hosts
```
---
### ssh-authorized-keys-reset
***Description:***
Resets SSH authorized_keys on the specified HOST
***Usage:***
```sh
ssh-authorized-keys-reset HOST
```
---
### ssh-accept-new
***Description:***
Adds a new host entry in the SSH known_hosts file
***Usage:***
```sh
ssh-accept-new HOST
```
---
### ssh-control-master-clean
***Description:***
Cleans the SSH control master directory
***Usage:***
```sh
ssh-control-master-clean
```
---
### ssh-users
***Description:***
Searches nodes for authorized_keys_* and returns a list of users
***Usage:***
```sh
ssh-users
```
---
### ssh-ids
***Description:***
Returns sorted SSH IDs
***Usage:***
```sh
ssh-ids
```
---
### ssh-authorized-keys
***Description:***
Returns the list of authorized_keys from nodes
***Usage:***
```sh
ssh-authorized-keys
```
---
### ssh-ids-hosts
***Description:***
Returns SSH IDs along with matching keyfile and hostname
***Usage:***
```sh
ssh-ids-hosts
```
---
## Deployment functions
1. [nodes](#nodes)
2. [nodes-active](#nodes-active)
3. [run-config](#run-config)
4. [members](#members)
5. [group](#group)
6. [group-functions](#group-functions)
7. [prefix](#prefix)
---
### nodes
***Description:***
Returns a list of available nodes
***Usage:***
```sh
nodes
```
---
### nodes-active
***Description:***
Returns a list of active nodes by detecting SSH availability
***Usage:***
```sh
nodes-active
```
---
### run-config
***Description:***
Sources R7_SITE_CONFIG
***Usage:***
```sh
run-config
```
---
### members
***Description:***
Returns a list of active nodes matching a list of REGEXES
***Usage:***
```sh
members REGEXES
```
---
### group
***Description:***
Applies GROUPNAME functions (in prefix order) to the provided MEMBERS list
***Usage:***
```sh
group GROUPNAME MEMBERS
```
---
### group-functions
***Description:***
Returns a list of functions found in the specified GROUPNAME
***Usage:***
```sh
group-functions GROUPNAME
```
---
### prefix
***Description:***
Sets the current PREFIX for matching function names
***Usage:***
```sh
prefix PREFIX
```
## Text output functions
1. [output-dump](#output-dump)
2. [output-latest](#output-latest)
3. [output-status](#output-status)
4. [output-duration](#output-duration)
5. [output](#output)
6. [output-labels](#output-labels)
7. [output-diffs](#output-diffs)
8. [output-permissions](#output-permissions)
9. [output-errors](#output-errors)
10. [summary](#summary)
11. [trace-id-last](#trace-id-last)
---
### output-dump
***Description:***
Dumps R7_RUNDIR to R7_OUTPUTDIR
***Usage:***
```sh
output-dump
```
---
### output-latest
***Description:***
Returns a list of outputs from the latest run
***Usage:***
```sh
output-latest
```
---
### output-status
***Description:***
Returns a list of current dumped functions exit status codes
***Usage:***
```sh
output-status
```
---
### output-duration
***Description:***
Returns a list of durations of executed groups
***Usage:***
```sh
output-duration
```
---
### output
***Description:***
Shows the dumped output matching HOST, GROUPNAME, and FUNCTION (or any if unspecified)
***Usage:***
```sh
output HOST GROUPNAME FUNCTION
```
---
### output-labels
***Description:***
Lists execution labels that match the provided REGEXES
***Usage:***
```sh
output-labels REGEXES
```
---
### trace-id-last
***Description:***
Returns the latest execution trace_id
***Usage:***
```sh
trace-id-last
```
---
### output-diffs
***Description:***
Returns the diffs from the latest execution
***Usage:***
```sh
output-diffs
```
---
### output-permissions
***Description:***
Returns the latest permission changes
***Usage:***
```sh
output-permissions
```
---
### output-errors
***Description:***
Returns the latest errors detected
***Usage:***
```sh
output-errors
```
---
### summary
***Description:***
Produces a summary of errors, diffs, and permission outputs
***Usage:***
```sh
summary
```
---
## Digraph output
1. [digraph-host](#digraph-host)
2. [digraph-ssh-id-hosts](#digraph-ssh-id-hosts)
---
### digraph-host
***Description:***
Returns a directed graph (in DOT format) showing relations among groups, functions, and nodes
***Usage:***
```sh
digraph-host
```
---
### digraph-ssh-id-hosts
***Description:***
Returns a directed graph (in DOT format) of relations between SSH IDs and hosts
***Usage:***
```sh
digraph-ssh-id-hosts
```
---