commit - 6d7eff93a2323eacb999357c8f89df3cddd71d04
commit + 730e151b5c83431d0de36a1659aaee11ff9fa654
blob - 161d3a51bf855b79769a4e1f6836737958c27c35
blob + 2b05f5c2d23d4b38a13e5fd4fc6dc513720ccb11
--- Makefile
+++ Makefile
.POSIX:
PREFIX:=/usr/local
-SHELLS=dash bash ksh ksh93 zsh yash sh
-SHELLS=ksh yash sh
+SHELLS=ksh93 ksh yash sh
UTILITY_VERSION=0.0.1
UTILITY=$(PWD)/r7
.DEFAULT: all
all: tests
-website: index.md
+website: index.md documentation.html
pandoc --template=./r7doc.html \
-s --toc=true \
--metadata title='R7' \
scp index.html root@campfire:/var/www/inda.re/r7/
ssh root@campfire chmod 644 /var/www/inda.re/r7/index.html
-documentation.html: README.md ./r7 ./r7doc.html
+documentation.html: README.md ./r7
pandoc --template=./r7doc.html \
-s --toc=true \
--metadata title='R7 Documentation' \
scp documentation.html root@campfire:/var/www/inda.re/r7/
ssh root@campfire chmod 644 /var/www/inda.re/r7/documentation\*
+r7.1: README.md
+ pandoc --from markdown+smart \
+ -t man -o r7.1 README.md
+
+r7.1.pdf: README.md
+ pandoc --from markdown+smart \
+ -t pdf -o r7.1.pdf README.md
+
install:
cp ./r7 $(PREFIX)/bin/
chmod 755 $(PREFIX)/bin/r7
workdir0-arguments \
workdir0-digraph \
workdir1-init \
- workdir1-populate
+ workdir1-populate \
+ workdir2-init \
+ workdir2-ssh \
+ workdir2-grouplib \
+ workdir2-goodby \
+ workdir2-r7doc \
+ workdir2-source \
+ workdir2-importer
tests:
@echo
test -z "$$failed" -a -z "$$notfound" || (echo; false;)
usage:
+ @echo _________________________________________________________
+ $R -h
$R -h 2>&1 | grep -q -e usage: -e '\['
+ @echo _________________________________________________________
version:
+ @echo _________________________________________________________
$R -h 2>&1 | grep -q -e "$(UTILITY_VERSION)"
+ @echo _________________________________________________________
debug:
+ @echo _________________________________________________________
$R -dn 2>&1 run-config | grep -q "R7 DEBUG $(UTILITY_VERSION)"
+ @echo _________________________________________________________
workdir0-arguments:
+ @echo _________________________________________________________
rm -fr testdir/workdir0/_run
- $R -w testdir/workdir0 -dn run-config 2>&1 | tee testdir/wd01
- grep -q -e '^LOADING $(PWD)/testdir/workdir0/site-config' testdir/wd01
- #grep -q -e '^CREATED $(PWD)/testdir/workdir0/_run' testdir/wd01
- grep -q -e '^PROCESSING GROUP common' testdir/wd01
- grep -q -e '^PROCESSING GROUP test0' testdir/wd01
+ $R -w testdir/workdir0 -dn run-config 2>&1 | tee testdir/wd01.produced
+ grep -q -e '^LOADING $(PWD)/testdir/workdir0/site-config' testdir/wd01.produced
+ grep -q -e '^PROCESSING GROUP common' testdir/wd01.produced
+ grep -q -e '^PROCESSING GROUP test0' testdir/wd01.produced
+ @echo _________________________________________________________
workdir0-digraph:
+ @echo _________________________________________________________
$R -w testdir/workdir0 -dn digraph-host 2>/dev/null | \
tee testdir/wd01.dot.produced
cmp testdir/wd01.dot testdir/wd01.dot.produced
+ @echo _________________________________________________________
workdir1-init:
+ @echo _________________________________________________________
rm -fr testdir/workdir1
$R -dw testdir/workdir1 init
test -d testdir/workdir1
find testdir/workdir1 | tee testdir/wd1-init-find.produced
cmp testdir/wd1-init-find testdir/wd1-init-find.produced
+ @echo _________________________________________________________
workdir1-populate: workdir1-init
- cd testdir/workdir1 && \
+ @echo _________________________________________________________
+ { cd testdir/workdir1 && \
+ $R && \
$R run-config && \
- $R digraph-host && \
$R output-dump && \
$R output && \
$R group && \
- $R -s group
+ $R -s group ;} | wc -c | grep -q 0
+ @echo _________________________________________________________
+
+SSH_ID=$(HOME)/.ssh/managed/inda.re/id_ed25519_cobalt
+SSH_TEST_NODES=tools0
+workdir2-init:
+ @echo _________________________________________________________
+ rm -fr testdir/workdir2
+ $R -dw testdir/workdir2 init
+ test -d testdir/workdir2
+ cd testdir/workdir2 && $R | wc -c | grep -q 0
+ cd testdir/workdir2 && \
+ cp "$(SSH_ID)" .ssh/id_ed25519_r7 && \
+ cp "$(SSH_ID).pub" .ssh/id_ed25519_r7.pub && \
+ . $(UTILITY) && for node in $(SSH_TEST_NODES); do \
+ mkdir "nodes/$$node" && \
+ ssh_accept_new "$$node"; \
+ done;
+ @echo _________________________________________________________
+
+workdir2-ssh: workdir2-init
+ @echo _________________________________________________________
+ cd testdir/workdir2 && . $(UTILITY) && \
+ cat site-config && \
+ echo 'state_hello() { echo Hi! ; }' > groups/common && \
+ echo 'state_hello() { echo Hey! ; }' > groups/heyo && \
+ echo 'dummy_hello() { echo Heyo! ; }' >> groups/heyo && \
+ echo 'group heyo $(SSH_TEST_NODES)' >> site-config && export R7_DEBUG=yes && \
+ digraph_host && \
+ run_config && \
+ output_dump && \
+ output | grep Hey! && \
+ output && \
+ info && \
+ digraph_ssh_id_hosts && \
+ ssh_known_hosts && \
+ nodes && \
+ prefix dummy && group heyo '.*' && output_dump && \
+ output | grep Heyo! && \
+ trace_id_last && \
+ output_duration | grep 0 && \
+ output_status && \
+ summary && \
+ output_errors && \
+ output_permissions && \
+ output_diffs
+ @echo _________________________________________________________
+
+workdir2-grouplib: workdir2-init
+ @echo _________________________________________________________
+ cp testdir/grouplib testdir/workdir2/groups/
+ mkdir -p testdir/workdir2/groups/grouplib.dir/subdir
+ touch testdir/workdir2/groups/grouplib.dir/item
+ echo 'aaa' > testdir/workdir2/groups/grouplib.dir/subdir/subitem
+ echo 'bbb' > testdir/workdir2/groups/grouplib.dir/subdir/subitem2
+ cp testdir/testfile2 testdir/workdir2/groups/grouplib.dir/
+ cd testdir/workdir2 && . $(UTILITY) && \
+ echo 'prefix grouplib' > site-config && \
+ echo "group grouplib '.*'" >> site-config && \
+ run_config && output_dump && output_status && \
+ R7_DEBUG=yes summary && \
+ for node in $(SSH_TEST_NODES); do \
+ ssh "$$node" ls -la /tmp/item\* | grep item; \
+ ssh "$$node" ls -la /tmp/r7/my-empty-file | grep empty | grep -e '-rw-r-----' ; \
+ ssh "$$node" ls -la /tmp/newdir/newdir2/null | grep -e '-rw-r-----' ; \
+ done && output tools0 grouplib
+ @echo _________________________________________________________
+
+workdir2-goodby: workdir2-grouplib
+ @echo _________________________________________________________
+ cd testdir/workdir2 && \
+ rm -fr _output && \
+ $R -P grouplib -se group grouplib $(SSH_TEST_NODES)
+ @echo _________________________________________________________
+
+workdir2-r7doc: workdir2-init
+ @echo _________________________________________________________
+ rm -fr testdir/workdir2/_output
+ cp testdir/example1 testdir/workdir2/groups/
+ cd testdir/workdir2/ && $R -a group example1 $(SSH_TEST_NODES)
+ cd testdir/workdir2/ && $R -ea group example1 $(SSH_TEST_NODES)
+ rm -fr testdir/workdir2/_output
+ cp testdir/example2 testdir/workdir2/groups/
+ cd testdir/workdir2/ && $R -sea group example2 $(SSH_TEST_NODES)
+ @echo _________________________________________________________
+
+workdir2-source: workdir2-init
+ @echo _________________________________________________________
+ rm -fr testdir/workdir2/_output
+ rm -fr testdir/workdir2/_run
+ cp testdir/source testdir/workdir2/groups/
+ mkdir testdir/workdir2/groups/source.dir
+ echo : > testdir/workdir2/groups/source.dir/my-source
+ for node in $(SSH_TEST_NODES); do \
+ echo myvar=3333 > "testdir/workdir2/nodes/$$node/my-source" ; \
+ done
+ cd testdir/workdir2/ && $R -sea group source $(SSH_TEST_NODES)
+ sleep 1
+ cd testdir/workdir2/ && $R -sda group source $(SSH_TEST_NODES)
+ cd testdir/workdir2/ && $R output \* source source_nodesource2 | grep 3333
+ @echo _________________________________________________________
+
+workdir2-importer: workdir2-init
+ @echo _________________________________________________________
+ rm -fr testdir/workdir2/_output
+ rm -fr testdir/workdir2/_run
+ cd testdir/workdir2/ && \
+ for node in $(SSH_TEST_NODES); do \
+ . $(UTILITY) && import "$$node"; \
+ done
+ find testdir/workdir2/nodes | grep sshd_config
+ find testdir/workdir2/nodes
+ @echo _________________________________________________________
blob - 7feed5bc72fe19893a564ff6c178e03035f09828
blob + 25ae5442e8dc86140e1ba2156d58544c58769c96
--- README.md
+++ README.md
+---
+title: "R7 Documentation"
+man_manual: "r7(1)"
+section: 1
+---
+
## Description
R7 is a shell remote configuration management framework designed to:
- 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.
+R7 is inspired by the amazing work made by the [rset](https://scriptedconfiguration.org/) tool.
+_Supported shells are ksh93, oksh and yash. Supported platforms are OpenBSD, FreeBSD, NetBSD and GNU/Linux. Busybox support is planned._
+
+
## Tutorial
### Introduction
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
+```
+$ 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
}
```
+Any function managed by `r7` needs to be declared in the format `^PREFIX_NAME()`.
+
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._
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 (part of the `site-config` file shown before):
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
+(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
```sh
state_test() {
- test 1 -eq -2
+ 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)
+```
+$ 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
This will produce the following summary:
- - (error 22) - in hostB test0 state_test (1748622980)
+```
+- (error 22) - in hostB test0 state_test (1748622980)
- something
- failed
+ something
+ failed
- - (error 22) - in hostC test0 state_test (1748622980)
+- (error 22) - in hostC test0 state_test (1748622980)
- something
- failed
+ something
+ failed
+```
Diffs and permissions changes are also displayed by the `summary` command. When
they're are detected, the captured related output is shown.
----
+### Repository initialisation
-## Group functions
+Using the `init` command, we can specify the current or a specifed
+work directory to be created and act as an r7 repository.
-Those functions are accessible during the execution of the group file's functions.
+_Automated versionning of outputs are currently let to the user, a recommended
+place would be the end of the site configuration file._
-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)
+This command initializes a new directory called `test.org`:
----
+```sh
+r7 -w test.org init
+cd test.org
+```
-### trace
+Once this step is done, it is required to:
-***Description:***
-Append a trace containing the current node name and `trace_id`.
+- Copy or initialize the ssh key to the `.ssh` folder in the repository created
+ in `.ssh/id_ed25519-r7` and `./ssh/id_ed25519-r7.pub`
-**Usage:**
+- Adjust the SSH client configuration file in the repository `.ssh/config`
+
+_Note that it is possible to import additionnal SSH configurations, like the user
+one from the SSH configuration, but it is advised to keep them separate._
+
+### SSH agent handling
+
+The function `unlock` can be used to set the SSH agent environement, this will
+be required for any automated operation later done by r7:
+
```sh
-trace [string...]
+cd test.org
+. $(command -v r7) # Sourcing r7
+unlock
+tmux || screen
```
----
+### Adding new hosts
-### error
+For adding a new host, the simplest way is to have pre-configured
+`authorized_keys` for the target user, it is then required to add it to the
+`known_hosts` file. For that, it is possible to use r7 SSH functions:
-***Description:***
-Trace an error message that will inform the user of an event in the summary
-output.
+```sh
+. $(command -v r7) # Sourcing r7
+ssh-accept-new hostname
-**Usage:**
+```
+
+If resetting the target's user `authorized_keys` file is needed, the following
+function can be used:
+
```sh
-error [string...]
+. $(command -v r7) # Sourcing r7
+ssh-authorized-keys-reset hostname
```
----
+_**Warning:** This will resets the SSH authorized_keys file for the target user
+(root, by default) and put the `.ssh/id_ed25519-r7.pub` in place._
-### node
-***Description:***
-Returns true if the argument is an existing file or directory inside `/tmp/r7/$(nodename)`. Else, returns 31.
+Once the host is accepted in the `known_hosts` file, it's possible to import it
+automatically.
-***Usage:***
+### Importing hosts configurations
+
+_This feature currently only support OpenBSD._
+
+Avoiding to import the configuration manually is possible with the `import`
+command:
+
```sh
-node [filename|dirname]
+r7 import hostname
```
----
+Most common configuration files will be copied in a newly created
+`nodes/hostname` sub-directory.
-### nodename
+### Writing group files
-***Description:***
-Returns the small hostname of the current host.
+Group files collects fonctions related to the configuration of the operating
+system, networking, services, global and user configurations packages
+installations and application deployments.
-***Usage:***
+By specifying the execution prefix, the user determines the order of execution
+of the functions declared in the group file.
+
```sh
-nodename
+service_A() {
+ service A enable
+ service A restart
+}
+
+service_B() {
+ service B enable
+ service B restart
+}
+
+state_A() {
+ service A status
+}
+
+doc_A() {
+ echo "## Service A"
+ echo
+ echo "- [url](http://test.org/)"
+ echo
+}
```
----
+_**file**: `groups/example1`_
-### nodedir
+With the default prefix, `install`, `state`, `doc`, the fonction that will be
+executed are `state_A` and `doc_A`. To test that group file with all the
+functions inside we can call r7 the following way:
-***Description:***
-Returns `/tmp/r7/$(nodename)`.
+```sh
+r7 -P 'service state doc' group example1 hostname
+```
-***Usage:***
+Or using the `-a` flag which will create a prefix automatically with all
+functions present in the group file:
+
```sh
-nodename
+r7 -a group example1 hostname
```
----
+In a real situation, this example will have failing functions because the
+`service` command may not be found or the services named `A` or `B` does not
+exist.
-### groupdir
+```
+! (error 127) tools0:example1::service_A
+! (error 127) tools0:example1::service_B
+! (error 127) tools0:example1::state_A
+- (ok) tools0:example1::doc_A
+```
-***Description:***
-Returns `/tmp/r7/$groupname.dir`.
+We could force the group execution to stop using a combination of the `-e` flag
+and the `error` group function.
-***Usage:***
```sh
-groupdir
+service_A() {
+ service A enable &&
+ service A restart ||
+ error unable to setup service A
+}
+
+service_B() {
+ service B enable &&
+ service B restart ||
+ error unable to setup service B
+}
+
+state_A() {
+ service A status
+}
+
+doc_A() {
+ echo "## Service A"
+ echo
+ echo "- [url](http://test.org/)"
+ echo
+}
```
----
+_**file:** `groups/example2`_
-### install
+Let's run the updated example with the `-s` flag to trigger the summary output.
-***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.
+```sh
+rm -fr _output
+r7 -sea group example2 hostname
+```
-***Usage:***
+This will produce something like the following:
+
+```
+! (error) tools0:example2::service_A
+! (exit) tools0:example2::service_A
+- (error 127) - in tools0 example2 service_A (1748791986)
+
+ /bin/sh: <stdin>[155]: service: not found
+ :: 1748791986 tools0 ERROR unable to setup service A
+
+```
+
+### Using the r7 install functions
+
+The group library comes with the following functions that will allow
+installation of files and triggering actions if necessary: `install`,
+`groupinstall`, `nodeinstall`. They are all shortcuts to an internal
+`r7_install` function, it's just the wrappers changes directory to respectively
+`/tmp/r7`, `/tmp/r7/groupname.dir/` and `/tmp/r7/hostname/`.
+
+If we want to install files from the group directory we will use something like:
+
+
```sh
-install [-o owner] [-m mode] source destintaion
+install_program() {
+ groupinstall -m 755 -o root:bin my_program /usr/local/bin
+}
```
----
+_We are supposing to have the file `groups/example3.dir/my_program`_
-### groupinstall
+And if it's something from node, we should use:
-***Description:***
-Change directory to the current group `.dir` directory in `/tmp/r7` then
-performs like `install`.
+```sh
+install_program_config() {
+ nodeinstall -m 644 -o root:wheel my_config /etc
+}
+```
-***Usage:***
+We can now chain the `install` functions to configuration checking and
+initialization of programs, services or network features like in the following
+example, in which we install `ntp.conf` from the deployed node directory to
+`/etc`:
+
```sh
-groupinstall [-o owner] [-m mode] source destintaion
+service_ntpd() {
+ nodeinstall -m 644 -o root:wheel ntpd.conf /etc &&
+ ntpd -n &&
+ rcctl enable ntpd &&
+ rcctl restart ntpd
+}
```
----
+### Using the r7 node function
-### nodeinstall
+We may want to avoid executing a function or parts of it if the node has no
+related files before executing the installation and actions handling parts. This
+can be done with the help of the `node` function.
-***Description:***
-Change directory to the current node directory in `/tmp/r7` then
-performs like `install`.
+```sh
+service_ntpd() {
+ node ntpd.conf &&
+ nodeinstall -m 644 -o root:wheel ntpd.conf /etc &&
+ ntpd -n &&
+ rcctl enable ntpd &&
+ rcctl restart ntpd
+}
+```
-***Usage:***
+Here, `node` will return `31` if there is no file or directory called
+`ntpd.conf` inside the deployed node directory (`/tmp/r7/hostname`).
+
+`0`, `30` and `31` exit codes are ignored when r7 will search for execution
+errors. This means `30` and `31` exit code are reserved to r7.
+
+In other terms, this will update the NTP daemon configuration only if
+`ntpd.conf` is present in the node directory, otherwise it is kept by default,
+which is the system provided default `ntpd.conf`.
+
+### Sourcing specific files
+
+Here's an example how to source additionnal shell from the deployed node
+directory, or elsewhere:
+
```sh
-nodeinstall [-o owner] [-m mode] source destintaion
+service_sysrc() {
+ sysrc dumpdev=NO
+ sysrc kld_list="vmm if_tuntap if_bridge nmdm"
+ sysrc microcode_update_enable=YES
+ sysrc mountd_enable=YES
+ sysrc nfs_server_enable=YES
+ sysrc ntpdate_enable=YES
+ sysrc powerd_enable=YES
+ sysrc rcshutdown_timeout=900
+ sysrc rpcbind_enable=YES
+ sysrc sshd_enable=YES
+ sysrc zfs_enable=YES
+ nodesource sysrc.local
+}
+
```
----
+In this FreeBSD example, we configure the system RC globally with
+`service_sysrc` which apply, if `sysrc.local` is found in the root of the node
+directory, it's sourcing in the current scope via the help of `nodesource
+sysrc.local`.
+
+The file `nodes/hostname/sysrcl.local` contains the following:
+
+```sh
+sysrc ifconfig_igb0=DHCP
+sysrc ifconfig_igb1=up
+```
+
+### Other examples
+
+#### System shell profile
+
+Here we install the `profile` file provided in the `groupname.dir` directory or
+the profile provided by the node directory:
+
+```sh
+setup_profile() {
+ groupinstall -m 644 -o root:wheel profile /etc
+ node profile &&
+ nodeinstall -m 644 -o root:wheel profile /etc
+}
+```
+
+#### Users
+
+Here we add new users:
+
+```sh
+setup_users() {
+ useradd user1
+ useradd user2
+}
+```
+
+#### Crontab
+
+In the following example, we install a script and call it from a dedicated user crontab.
+
+```sh
+mail_admins=admins@test.org
+
+install_myscript() {
+ groupinstall -o root:bin -m 755 myscript /usr/local/bin &&
+ useradd -d /var/empty -s /sbin/nologin _myscript
+ crontab -u _myscript - <<-/
+ MAILTO=$mail_admins
+ */5 * * * * myscript
+ /
+}
+```
+
+### Consulting the output
+
+After the execution is done, the result are saved in the `_run` directory,
+`output-dump` needs to be called to produce the current `_output` structure.
+
+The r7 `output` command can then be used to show specific output groups or functions:
+
+```sh
+r7 output
+```
+
+Will show the whole output of the latest groups deployments.
+
+This format can be used to match specific host, group or function:
+
+```sh
+r7 output [hostname [groupname [funcname]]]
+```
+
+For example:
+
+```sh
+$ r7 output tools0 OpenBSD state_packages\*
+- tools0 OpenBSD state_packages_upgrades
+
+ quirks-7.103 signed on 2025-05-30T01:12:03Z
+
+- tools0 OpenBSD state_packages_repository
+
+ https://cdn.openbsd.org/pub/OpenBSD
+```
+
+_Note that the `output` function arguments are using shell's case globbing patterns._
+
## r7 command line
***Usage:***
- **-n**
Enable dry run mode; prevents any modification on hosts
+- **-e**
+ Exit the group execution on the error function
+
- **-p**
Run each host in parallel per group call
- **digraph-host**
Produce a dot digraph for hosts
+- **digraph-ssh**
+ Produce a dot digraph of SSH ID's, users and nodes relations
+
+- **import**
+ Import TARGET host in the current nodes directory
+
- **info**
Default command showing active nodes and summary
- **init**
Initialize a new work directory
+## 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)
+10. [source](#source)
+11. [groupsource](#groupsource)
+12. [nodesource](#groupsource)
+
+---
+
+### 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, exit the group execution if `-e` is set
+
+**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
+```
+---
+
+### source
+
+***Description:***
+Source a POSIX shell file in the current execution
+
+***Usage:***
+
+```sh
+source filename
+```
+
+### groupsource
+
+***Description:***
+Change directory to the current group directory then call source with the file
+name as argument, produces an error if filename is not found from the group
+directory
+
+```sh
+groupsource filename
+```
+
+### nodesource
+
+***Description:***
+Change directory to the current group directory then call node and source with the file
+name as argument without producing an error if the filename is not found
+
+```sh
+nodesource filename
+```
+
+
+
# r7 library
## Interface
prefix PREFIX
```
-
## Text output functions
1. [output-dump](#output-dump)
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)
+6. [output-diffs](#output-diffs)
+7. [output-permissions](#output-permissions)
+8. [output-errors](#output-errors)
+9. [summary](#summary)
+10. [trace-id-last](#trace-id-last)
---
---
-### output-labels
-
-***Description:***
-Lists execution labels that match the provided REGEXES
-
-***Usage:***
-```sh
-output-labels REGEXES
-```
-
----
-
### trace-id-last
***Description:***
```
---
+
+## HTML output
+
+1. [html](#html)
+
+---
+
+### html
+
+***Description:***
+Retunrs a report page with the content of `README.md` in HTML format.
+
+***Usage:***
+```sh
+html
+```
blob - 330388a11fac52d328d158215817f5bdecdf6c01
blob + 6365870c8c5a28a01dc8d1c9d96c2c816e7d78f4
--- r7
+++ r7
# shellcheck disable=SC2046,SC2031
# Global configuration
# ====================
-
VERSION=0.0.1
: "${HOSTNAME:=$(uname -n)}"
: "${R7_DRYRUN:=no}"
: "${R7_GROUP_ALLPREFIX:=no}"
: "${R7_GROUP_SUMMARY:=no}"
+ : "${R7_GROUP_EXITONERROR:=no}"
: "${R7_PARALLEL:=no}"
: "${R7_PREFIX:=install state doc}"
: "${R7_SHOWOUTPUT:=no}"
done
R7_WORKDIR_tmp=$(_resolv_dir "$R7_WORKDIR")
- test -d "$R7_WORKDIR_tmp" || _fail "Working directory not found: '$R7_WORKDIR'"
+ test -d "$R7_WORKDIR_tmp" || _error "Working directory not found: '$R7_WORKDIR'"
R7_WORKDIR=$R7_WORKDIR_tmp
R7_RUNDIR=$R7_WORKDIR/_run
R7_OUTPUTDIR=$R7_WORKDIR/_output
R7_SITE_CONFIG_tmp=$(_resolv_file "$R7_SITE_CONFIG")
- test -f "$R7_SITE_CONFIG_tmp" || _fail "Site configuration not found: '$R7_SITE_CONFIG'"
+ test -f "$R7_SITE_CONFIG_tmp" || _error "Site configuration not found: '$R7_SITE_CONFIG'"
R7_SITE_CONFIG=$R7_SITE_CONFIG_tmp
SSH_CONFIG_FILE_tmp=$(_resolv_file "$SSH_CONFIG_FILE")
- test -f "$SSH_CONFIG_FILE_tmp" || _fail "Unable to read SSH configuration: '$SSH_CONFIG_FILE'"
+ test -f "$SSH_CONFIG_FILE_tmp" || _error "Unable to read SSH configuration: '$SSH_CONFIG_FILE'"
SSH_CONFIG_FILE=$SSH_CONFIG_FILE_tmp
SSH_IDENTITY_FILE_tmp=$(_resolv_file "$SSH_IDENTITY_FILE")
- test -f "$SSH_IDENTITY_FILE_tmp" || _fail "Unable to read SSH identity file: '$SSH_IDENTITY_FILE'"
+ test -f "$SSH_IDENTITY_FILE_tmp" || _error "Unable to read SSH identity file: '$SSH_IDENTITY_FILE'"
SSH_IDENTITY_FILE=$SSH_IDENTITY_FILE_tmp
R7_ENV_READY=yes
# =========
_fail() {
- printf "FAILURE: %s\n" "$*"
+ printf "FAILURE: %s\n" "$*" >&2
exit 99
}
+_error() {
+ printf "ERROR: %s\n" "$*" >&2
+}
+
_debug() {
test "$R7_DEBUG" = yes && printf %s\\n "${*:-$(cat -)}" >&2
}
output_dump
summary
fi
+ return 0
}
alias help=usage
usage() {
cat <<eof
version: 0.0.1
-usage: $0 [options] command
+usage: $0 [options] [command [arguments...]]
usage options:
-h Show usage and exit
-d Debug
-o Output directory, default to ./_output
-c Site configuration file, default to ./site-config
-n Dry run, prevent any host modification
+ -e Exit the group execution on the error function
-p Run each host in parallel per group call
-O Show output interactively
-P Prefix
output Consult the output
summary Summary of errors and changes
digraph-host Produce digraph host
+ digraph-ssh Produce SSH IDs digraph
+ import Import TARGET host in current nodes directory
info Default command showing active nodes and summary
init Initialize a new work directory
# shellcheck disable=SC2086
_main() {
- while getopts 'P:F:r:o:i:w:c:npdsaOh' arg; do
+ test -n "$ZSH_VERSION" && {
+ echo >&2 "ZSH is unsupported. Exiting."
+ exit 99
+ }
+ while getopts 'P:F:r:o:i:w:c:npdseaOh' arg; do
case $arg in
h)
usage
o) R7_OUTPUTDIR=$OPTARG ;;
s) R7_GROUP_SUMMARY=yes ;;
a) R7_GROUP_ALLPREFIX=yes ;;
+ e) R7_GROUP_EXITONERROR=yes ;;
c) R7_SITE_CONFIG=$OPTARG ;;
n)
R7_DRYRUN=yes
case $1 in
html) html "$@" ;;
init | workdir-init)
- _workdir_init
+ shift
+ _workdir_init "$@"
exit $?
;;
esac
_init_env
case $1 in
- rc | run-config)
- run_config
- ;;
+ rc | run-config) run_config ;;
group)
shift
trace_id=$(_trace_id_new)
fi
;;
info) info ;;
+ import) import "$@" ;;
out | output)
shift
output "$@"
prefix $R7_PREFIX
digraph_host
;;
- '')
- info
+ digraph-ssh)
+ digraph_ssh_id_hosts
;;
+ '') info ;;
*)
case $0 in
- -)
- exit 33
- ;;
+ -) exit 33 ;;
*r7)
echo 1>&2 "Unknown command '$1'"
usage
exit 2
;;
- *)
- prefix $R7_PREFIX
- ;;
+ *) prefix $R7_PREFIX ;;
esac
;;
esac
# shellcheck disable=SC2030
_workdir_init() (
- mkdir -p "$R7_WORKDIR" || _fail "Unable to create directory: '$R7_WORKDIR'"
- cd "$R7_WORKDIR" || _fail "Unable to cd to directory: '$R7_WORKDIR'"
- _debug _WORKDIR_INIT "$PWD"
+ _debug _WORKDIR_INIT "$PWD" "$R7_WORKDIR"
+ if test -n "$R7_WORKDIR"; then
+ mkdir -p "$R7_WORKDIR" || _fail "Unable to create directory: '$R7_WORKDIR'"
+ cd "$R7_WORKDIR" || _fail "Unable to cd to directory: '$R7_WORKDIR'"
+ R7_WORKDIR=$PWD
+ fi
+ R7_WORKDIR=$PWD
mkdir -p groups
mkdir -p nodes
- mkdir -p .ssh
- : "${SSH_CONFIG_FILE:=.ssh/config}"
- : "${SSH_IDENTITY_FILE:=.ssh/id_ed25519_r7}"
+ mkdir -p .ssh/control
+ : "${SSH_CONFIG_FILE:=$R7_WORKDIR/.ssh/config}"
+ : "${SSH_IDENTITY_FILE:=$R7_WORKDIR/.ssh/id_ed25519_r7}"
R7_SITE_CONFIG=./site-config
if ! test -f "$R7_SITE_CONFIG"; then
cat >"$R7_SITE_CONFIG" <<-eof
# Example r7 site configuration
- group common '*'
+ group common '.*'
eof
fi
if ! test -f "$SSH_CONFIG_FILE"; then
ConnectTimeout 3
ConnectionAttempts 1
ControlMaster auto
- ControlPath $R7_WORKDIR/.ssh/control/%C
+ #ControlPath $R7_WORKDIR/.ssh/control/%C
+ ControlPath $R7_WORKDIR/.ssh/control/%r@%h:%p
ControlPersist 2m
IdentityFile $SSH_IDENTITY_FILE
StrictHostKeyChecking yes
alias ssh-users=ssh_users
ssh_users() {
- find "$R7_WORKDIR/nodes" -type f -name "authorized_keys_*" |
- sed "s;$R7_WORKDIR/nodes;;" |
+ cd "$R7_WORKDIR/nodes" || _fail "Unable to cd to '$R7_WORKDIR/nodes'."
+ find ./* -type f -name "authorized_keys_*" |
+ sed 's;./;;' |
sort | while read -r keyfile; do
echo "- $keyfile:"
grep -v "^#" "$R7_WORKDIR/nodes/$keyfile"
unlock() {
pkill ssh-agent
- set -e
eval "$(ssh-agent)"
ssh-add "$SSH_IDENTITY_FILE"
- set +e
}
copy() (
prefix() {
prefix="$*"
- export prefix
+ R7_PREFIX=$prefix
+ export prefix R7_PREFIX
}
_call() (
_payload() (
echo "trace_id=$trace_id"
echo "groupname=$new_name"
+ echo "exitonerror=$R7_GROUP_EXITONERROR"
cat <<-'eof'
trace() {
echo ":: $trace_id $(nodename) $*" 1>&2
}
error() {
+ _ret=$?
trace ERROR "$*" 1>&2
+ test "$exitonerror" = yes && {
+ case $_ret in (0) _ret=1 ;; esac
+ trace EXIT $_ret
+ exit $_ret
+ }
}
nodename() (
hostname=${hostname:-$(uname -n)}
groupdir() (
echo "/tmp/r7/$groupname.dir"
)
+ _presource() {
+ test $# -gt 0 || {
+ error missing source argument
+ return 2
+ }
+ source "$(test -f "$1" && printf %s "$PWD/${1##"$PWD"}")"
+ }
+ nodesource() {
+ node "$1" && cd "$(nodedir)" && _presource "$1"
+ }
+ groupsource() {
+ cd "$(groupdir)" && _presource "$1"
+ }
+ source() {
+ if sh -n "$1"; then
+ trace SOURCE "$1"
+ . "$1"
+ else
+ trace SOURCE FAILED "$1"
+ return 2
+ fi
+ }
install() ( cd /tmp/r7 && r7_install "$@" ; )
groupinstall() ( cd "$(groupdir)" && r7_install "$@" ; )
nodeinstall() ( cd "$(nodedir)" && r7_install "$@" ; )
+ _getperm() {
+ test -f "$1" && {
+ ls -l "$1" | grep -v total | awk '{ print $1 " " $3 " " $4 }'
+ }
+ }
+ _setperm() {
+ before=$(_getperm "$1")
+ if test -n "$owner"; then
+ chown "$owner" "$1" || {
+ error chown "$1"
+ return 2
+ }
+ fi
+ if test -n "$mode"; then
+ chmod "$mode" "$1" || {
+ error chmod "$1"
+ return 2
+ }
+ fi
+ after=$(_getperm "$1")
+ if test "$before" != "$after"; then
+ trace PERMISSIONS CHANGE "$before" TO "$after" ON "$1"
+ fi
+ }
+ _install() {
+ test -e "$2" && {
+ trace DIFF
+ diff -U2 "$2" "$1"
+ }
+ cp "$1" "$2" ||
+ { error copying "$source" to "$2"; return 2 ; }
+ _setperm "$2"
+ return 0
+ }
r7_install() (
while getopts "o:m:" arg; do
case $arg in
- o) owner="$OPTARG" ;;
- m) mode="$OPTARG" ;;
+ o) owner=$OPTARG ;;
+ m) mode=$OPTARG ;;
*) ;;
esac
done
shift $((OPTIND - 1))
source=$1
target=$2
- source_basename=${1##*/}
source_dirname=${1%/*}
- before=x
- after=x
- umask 027
- if ! test -f "$source"; then
+ if ! test -e "$source"; then
error "$source" not found
return 2
fi
+ case $target in
+ */) test -d "$target" || {
+ error "$target" directory not found
+ return 2
+ } ;;
+ esac
+ if test -e "$target"; then
+ cmp -s "$source" "$target"
+ ret=$?
+ destination_is=file
+ fi
if test -d "$target"; then
new_filename="$target/${source##$source_dirname/}"
- test -f "$new_filename" && before=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
cmp -s "$source" "$new_filename"
ret=$?
- else
- new_filename=$target
- test -f "$new_filename" && before=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
- cmp -s "$source" "$target"
- ret=$?
+ destination_is=directory
fi
- if test -f "$new_filename"; then
- if test -n "$owner"; then
- chown "$owner" "$new_filename" || {
- error unable to chown "$new_filename"
- return 2
- }
+ if test -e "$source"; then
+ if test -d "$target"; then
+ destination_is=directory
+ else
+ destination_is=file
fi
- if test -n "$mode"; then
- chmod "$mode" "$new_filename" || {
- error unable to chmod "$new_filename"
- return 2
- }
- fi
- after=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
- if test "$before" != "$after"; then
- trace PERMISSIONS CHANGE "$before" "->" "$after" "on" "$new_filename"
- fi
fi
case "$ret" in
0) return 30 ;;
- 1 | *)
- if test -f "$new_filename"; then
- trace DIFF "$new_filename" "$source"
- diff -U 2 "$new_filename" "$source"
- else
- trace DIFF "/dev/null" "$source"
- diff -U 2 /dev/null "$source"
- fi
- cp "$source" "$target" || { error copying "$source" "$target" return 2; }
- ;;
+ *)
+ case $destination_is in
+ directory)
+ new_filename="$target/${source##$source_dirname/}"
+ _install "$source" "$new_filename"
+ ;;
+ file)
+ _install "$source" "$target"
+ ;;
+ esac
+ ;;
esac
- return 0
)
- cd /tmp/r7 2>/dev/null
+ r7_installdir() (
+ while getopts "o:m:O:M:"; do
+ case $arg in
+ o) fileowner=$OPTARG ;;
+ m) filenode=$OPTARG ;;
+ O) dirowner=$OPTARG ;;
+ M) dirmode=$OPTARG ;;
+ esac
+ source=$1
+ target=$2
+ source_basename=${source##*/}
+ source_dirname=${source%%/*}
+ target_basename=${target##*/}
+ target_dirname=${target%%/*}
+ test -d "$source" || {
+ error "$source" directory not found
+ return 2
+ }
+ test -d "$target" || {
+ error "$target" directory not found
+ return 2
+ }
+ cd "$source" || return 2
+ for dir in $(find ./* -type d); do
+ newdir=${dir#./}
+ trace "$newdir"
+ (
+ cd "$target" || return 2
+ before=$(_getperm "$newdir")
+ mkdir -p "$newdir" || error unable to create "$newdir"
+ test -n "$dirowner" && chown "$dirowner" "$newdir"
+ test -n "$dirmode" && chmod "$dirmode" "$newdir"
+ after=$(_getperm "$newdir")
+ test "$before" != "$after" &&
+ trace PERMISSIONS CHANGE "$before" TO "$after" ON "$newdir"
+ )
+ done
+ for file in $(find ./* -type f); do
+ newfile=${file#./}
+ trace "$newfile"
+ (
+ r7_install -o "$fileowner" -m "$filemode" "$file" "$target"
+ )
+ done
+ done
+ )
+ set -e
+ cd /tmp/r7 && umask 027
+ set +e
+ nodename=$(nodename)
+ nodedir=$(nodedir)
+ groupdir=$(groupdir)
trace CONNECTED $(date +%s)
eof
cat "$1"
- _debug "PREFIX $prefix"
- for run in $prefix; do
+ _debug "PREFIX $R7_PREFIX"
+ for run in $R7_PREFIX; do
_call "$1" "$run"
done
cat <<-'eof'
END)
in_function=0
case $endcode in
- 0 | 30 | 31 | 32) echo "- (ok) $host:$group::$function" ;;
+ 0 | 30 | 31) echo "- (ok) $host:$group::$function" ;;
*) echo "! (error $endcode) $host:$group::$function" ;;
esac
;;
case $2 in
"$trace_id")
case $4 in
+ SOURCE) echo "+ (source) $host:$save_group::$save_function" ;;
DIFF) echo "+ (diff) $host:$save_group::$save_function" ;;
PERMISSIONS) echo "+ (permissions) $host:$save_group::$save_function" ;;
ERROR) echo "! (error) $host:$save_group::$save_function" ;;
+ EXIT) echo "! (exit) $host:$save_group::$save_function" ;;
esac
;;
esac
system("mkdir -p " output_node)
next
}
+ $2 == trace_id && $4 == "EXIT" {
+ in_function_block=0
+ system("echo " $5 " >" output_status)
+ }
$2 == trace_id && $6 == "END" {
in_function_block=0
system("echo " $7 " >" output_status)
)
test -z "$latest" && return 0
case $(uname) in
- OpenBSD | FreeBSD | Darwin)
+ OpenBSD | FreeBSD | NetBSD | Darwin)
latest_date=$(date -r "$latest")
;;
Linux)
alias output-duration=output_duration
alias outdur=output_duration
output_duration() (
+ cd "$R7_OUTPUTDIR" || return 2
_duration_files | {
echo HOST GROUP DURATION
while read -r duration; do
alias out=output
# shellcheck disable=SC2254
output() (
+ cd "$R7_OUTPUTDIR" || return 2
_output_files | while read -r output; do
echo "$output" | while IFS=/ read -r _ host groupname funcname _; do
case $host in ${1:-*})
done
)
-alias output-labels=output_labels
-alias outlabels=output_labels
-# shellcheck disable=SC2048,SC2086
-output_labels() (
- set -- $(printf -- '-e %s ' "$@")
- output | grep -e '^-' | grep $* |
- while read -r _ host groupname funcname; do
- echo "- $host" "$groupname" "$funcname"
- echo
- sed 's/^/ /' "$R7_OUTPUTDIR/$host/$groupname/$funcname/output"
- echo
- done
-)
-
alias trace-id-last=trace_id_last
alias lastid=trace_id_last
trace_id_last() {
output_diffs() (
trace_id=$(trace_id_last)
cd "$R7_OUTPUTDIR" || return 0
- grep -r "^:: ${trace_id:-} .* DIFF" | while read -r line; do
+ grep -r "^:: ${trace_id:-} .* DIFF" | uniq | while read -r line; do
set -- $line
output_file=${1%%:::}
OIFS=$IFS
echo
sed 's/^/ /' "$R7_OUTPUTDIR/$host/$groupname/$funcname/output" | grep -v "^ :: $trace_id $host"
echo
- done
+ done || return 0
+ return 0
)
# :: 1748415578 tools0 PERMISSIONS CHANGE -rw-r-xr-x root wheel -> -rw-r--r-- root wheel on /tmp/difftest
# shellcheck disable=SC2048,SC2086
output_permissions() (
trace_id=$(trace_id_last)
- grep -r "^:: ${trace_id:-} .* PERMISSIONS" "$R7_OUTPUTDIR" | while read -r line; do
+ cd "$R7_OUTPUTDIR" || return 0
+ grep -r "^:: ${trace_id:-} .* PERMISSIONS" | while read -r line; do
set -- $line
output_file=${1%%:::}
OIFS=$IFS
IFS=/
set -- $output_file
- host=$2 groupname=$3 funcname=$4
+ host=$1 groupname=$2 funcname=$3
IFS=$OIFS
echo "- (permissions) -" "in" "$host" "$groupname" "$funcname" "($trace_id)"
echo
- while read -r trace; do
- set -- $trace
- echo " $6 $7 $8 -> ${10} ${11} ${12} on ${14}"
- break
- done <"$R7_OUTPUTDIR/$host/$groupname/$funcname/output"
+ grep -e "^:: ${trace_id:-} .* PERMISSIONS CHANGE" "$R7_OUTPUTDIR/$host/$groupname/$funcname/output" |
+ while read -r _ _ _ _ _ mode owner group _ mode2 owner2 group2 _ filename; do
+ echo " - $filename from $mode $owner:$group to $mode2 $owner2:$group2"
+ done
echo
done
+ return 0
)
alias output-errors=output_errors
trace_id=$(trace_id_last)
output_status | tail -n +2 | while read -r host groupname funcname status; do
case $status in
- 0 | 30 | 31 | 32) continue ;;
+ 0 | 30 | 31) continue ;;
esac
echo "- (error $status) -" "in" "$host" "$groupname" "$funcname" "($trace_id)"
if test -f "$R7_OUTPUTDIR/$host/$groupname/$funcname/output"; then
echo
fi
done
+ return 0
)
summary() (
)
test -n "$summary_out" &&
printf "%s\n" "$summary_out"
+ return 0
)
# Digraph functions
echo "\"$node\" -> \"$groupname\""
done
for funct in $(group_functions "$groupname"); do
- echo "\"$funct\" [color=red]"
- echo "\"$groupname\" -> \"$funct\""
+ echo "\"$groupname::$funct\" [color=red]"
+ echo "\"$groupname\" -> \"$groupname::$funct\""
done
done
)
alias digraph-ssh-id-hosts=digraph_ssh_id_hosts
digraph_ssh_id_hosts() {
- echo "digraph ssh_id_hosts {"
- ssh_ids_hosts
- echo "}"
+ cat <<-eof
+ digraph ssh_id_hosts {
+ layout="dot"
+ splines="true"
+ graph [overlap="false"]
+ $(ssh_ids_hosts)
+ }
+ eof
}
# Html
# ====
+# TODO: WIP
md() {
- pandoc -t html -f markdown+smart
+ pandoc -t html -f markdown+smart "$@"
}
html() {
exit 0
}
+# Importers
+# =========
+# TODO: WIP
+
+import() {
+ cd "$R7_WORKDIR" || _fail "Shit!"
+ target=$1
+ etc_files_to_import='
+ acme-client.conf
+ dhcpd.conf
+ dhcpcd.conf
+ doas.conf
+ fstab
+ hosts
+ httpd.conf
+ installurl
+ login.conf
+ mygate
+ myname
+ ntpd.conf
+ pf.conf
+ sysctl.conf
+ unwind.conf
+ resolv.conf
+ '
+ etc_ssh_files_to_import='
+ ssh/sshd_config
+ ssh/ssh_config
+ '
+ var_unbound_files='
+ db/named.root
+ etc/unbound.conf
+ '
+ var_nsd_files='
+ etc/nsd.conf
+ '
+ var_postgresql_files='
+ data/pg_hba.conf
+ data/postgresql.conf
+ '
+ if test -n "$target"; then
+ echo >&2 "-> Importing target host: $target"
+ case $(ssh "$target" uname) in OpenBSD) : ;; *)
+ _fail "Importer only supports OpenBSD currently. Exiting."
+ ;;
+ esac
+ import_ssh_authorized_keys
+ import_etc_if_exists
+ import_etc_ssh_if_exists
+ import_unbound_if_enabled
+ import_nsd_if_enabled
+ import_postgresql_if_installed
+ echo >&2 "-> done: $target"
+ else
+ echo >&2 "usage: $0 target-host"
+ fi
+}
+
+import_ssh_authorized_keys() {
+ other_users=$(
+ ssh "$target" getent passwd | awk -F: '$3 >= 1000 { print $1 }' |
+ grep -v nobody
+ )
+
+ for user in root $other_users; do
+ # shellcheck disable=SC2029
+ home=$(ssh "$target" getent passwd "$user" | awk -F: '{ print $6 }')
+ authorized_keys="$home/.ssh/authorized_keys"
+ ssh "$target" test -f "$authorized_keys" || continue
+ mkdir -p "nodes/$target/ssh/"
+ echo >&2 "--> Importing $authorized_keys"
+ import_copy "root@$target:$authorized_keys" "nodes/$target/ssh/authorized_keys_$user"
+ done
+}
+
+import_etc_if_exists() {
+ mkdir -p nodes/"$target"
+ interfaces=$(ssh "$target" 'cd /etc && ls -1 hostname.*')
+ echo >&2 "--> Importing /etc"
+ for file in $etc_files_to_import $interfaces; do
+ ssh "$target" test -f "'/etc/$file'" &&
+ import_copy "$target:/etc/$file" "nodes/$target/"
+ done
+}
+
+import_etc_ssh_if_exists() {
+ mkdir -p nodes/"$target"
+ echo >&2 "--> Importing /etc/ssh"
+ for file in $etc_ssh_files_to_import; do
+ ssh "$target" test -f "'/etc/$file'" &&
+ import_copy "$target:/etc/$file" "nodes/$target/ssh"
+ done
+}
+
+import_unbound_if_enabled() {
+ ssh "$target" rcctl check unbound >/dev/null && {
+ echo >&2 "--> Importing /var/unbound"
+ for file in $var_unbound_files; do
+ prefix=${file%%/*}
+ mkdir -p "nodes/$target/unbound/$prefix"
+ import_copy "$target:/var/unbound/$file" "nodes/$target/unbound/$prefix/"
+ done
+ return 0
+ }
+}
+
+import_nsd_if_enabled() {
+ ssh "$target" rcctl check nsd >/dev/null && {
+ echo >&2 "--> Import /var/nsd"
+ for file in $var_nsd_files; do
+ prefix=${file%%/*}
+ mkdir -p "nodes/$target/nsd/$prefix"
+ import_copy "$target:/var/nsd/$file" "nodes/$target/nsd/$prefix/"
+ done
+ return 0
+ }
+}
+
+import_postgresql_if_installed() {
+ ssh "$target" postgres -V 2>/dev/null | grep -q PostgreSQL && {
+ echo >&2 "--> Importing /var/postgresql"
+ for file in $var_postgresql_files; do
+ prefix=${file%%/*}
+ mkdir -p "nodes/$target/postgresql/$prefix"
+ import_copy "$target:/var/postgresql/$file" "nodes/$target/postgresql/$prefix/"
+ done
+ echo >&2 "-- Importing /etc/login.conf.d/postgresql"
+ mkdir -p "nodes/$target/login.conf.d"
+ import_copy "$target:/etc/login.conf.d/postgresql" "nodes/$target/login.conf.d/"
+ return 0
+ }
+}
+
+import_copy() {
+ _debug "IMPORT $1 $2 USE OPENRSYNC"
+ openrsync -e "/usr/bin/ssh -oConnectTimeout=$SSH_CONNECT_TIMEOUT -F $SSH_CONFIG_FILE" \
+ --rsync-path="/usr/bin/openrsync" \
+ -a "$1" "$2" && return 0
+}
+
# Main
# ====
blob - 26967829f75f73db0c75d2e834d4761aa4558bc1
blob + bda05a792e62c73b1d8b9b026ffd1df55848b871
--- testdir/wd01.dot
+++ testdir/wd01.dot
"alpha" -> "common"
"beta" -> "common"
"gamma" -> "common"
-"state_test" [color=red]
-"common" -> "state_test"
-"state_test_2" [color=red]
-"common" -> "state_test_2"
+"common::state_test" [color=red]
+"common" -> "common::state_test"
+"common::state_test_2" [color=red]
+"common" -> "common::state_test_2"
"test0" [color=orange]
"beta" -> "test0"
"gamma" -> "test0"
blob - /dev/null
blob + 852c14c127a16cbb329c2e9853e36e0631565f30 (mode 644)
--- /dev/null
+++ testdir/grouplib
+#!/bin/sh
+grouplib_hey() {
+ trace hello from grouplib tests
+}
+
+_return14() { return 14; }
+grouplib_error() {
+ echo coucou
+ _return14 ||
+ error heyy
+ return 2
+}
+
+grouplib_install_1() {
+ echo should fail
+ groupinstall not-existing /tmp
+}
+
+grouplib_install_2() {
+ echo should fail
+ groupinstall item /install2/
+}
+
+grouplib_install_3() {
+ echo should pass
+ rm -fr /install2
+ groupinstall item /install2
+}
+
+grouplib_install_4() {
+ echo should pass
+ rm -f /tmp/item
+ echo blah >item
+ groupinstall item /tmp/
+}
+
+grouplib_install_5() {
+ echo should pass
+ pwd
+ install /dev/null my-empty-file
+}
+
+grouplib_install_6() {
+ echo should pass
+ pwd
+ mkdir -p /tmp/newdir/newdir2
+ install /dev/null /tmp/newdir/newdir2
+}
+
+grouplib_install_7() {
+ echo should pass
+ groupinstall testfile2 /tmp
+}
+
+grouplib_isntall_8() {
+ groupinstall subdir/subitem /tmp
+ groupinstall subdir/subitem2 /tmp/subitem
+}
+
+grouplib_permchange_1() {
+ rm /tmp/item2 2>/dev/null
+ install -m 644 /dev/null /tmp/item2
+ touch testfile3
+}
+
+grouplib_goodby() {
+ error goodby
+}
+
+grouplib_unreachable() {
+ error goodby2
+}
blob - ecab8f19639a026bda26c56ce859ddb7406ad87d
blob + 8d1d8574c85dd7ce8ca3fba05aa8391e28ddfff2
--- testdir/wd1-init-find
+++ testdir/wd1-init-find
testdir/workdir1/groups
testdir/workdir1/nodes
testdir/workdir1/.ssh
+testdir/workdir1/.ssh/control
testdir/workdir1/.ssh/config
testdir/workdir1/.ssh/id_ed25519_r7
testdir/workdir1/.ssh/id_ed25519_r7.pub
blob - /dev/null
blob + 61d2040021065a58ac6832aa0b8e232f2fcef8fc (mode 644)
--- /dev/null
+++ testdir/source
+source_nodesource1() {
+ nodesource my-source-failing
+}
+
+source_nodesource2() {
+ nodesource my-source
+ echo "$myvar"
+}