bento: add a new feature to work in a push model

This commit is contained in:
Solene Rapenne 2022-10-04 23:02:41 +02:00
parent 9223913444
commit 80c0de326b
3 changed files with 39 additions and 2 deletions

24
bento
View File

@ -419,6 +419,12 @@ build_config()
TMPLOG="$(mktemp /tmp/bento-build-log.XXXXXXXXXXXX)"
rsync -rltgoDL "$SOURCES/" "$TMP/"
PARAMS=""
if [ -n "$TARGET_IP" ]
then
PARAMS="--use-remote-sudo --target-host $TARGET_IP"
fi
if [ -z "VERBOSE" ]
then
output="/dev/null"
@ -437,9 +443,9 @@ build_config()
test -d .git || git init >/dev/null 2>/dev/null
git add . >/dev/null
$SUDO nixos-rebuild "${COMMAND}" --flake ".#${NAME}" | tee "${output}" 2>"${TMPLOG}" >"${TMPLOG}"
$SUDO env NIX_SSHOPTS=$NIX_SSHOPTS nixos-rebuild ${PARAMS} "${COMMAND}" --flake ".#${NAME}" | tee "${output}" 2>"${TMPLOG}" >"${TMPLOG}"
else
$SUDO nixos-rebuild "${COMMAND}" --no-flake -I nixos-config="$TMP/configuration.nix" | tee "${output}" 2>"${TMPLOG}" >"${TMPLOG}"
$SUDO env NIX_SSHOPTS=$NIX_SSHOPTS nixos-rebuild ${PARAMS} "${COMMAND}" --no-flake -I nixos-config="$TMP/configuration.nix" | tee "${output}" 2>"${TMPLOG}" >"${TMPLOG}"
fi
if [ $? -eq 0 ]; then printf "success " ; else printf "failure " ; BAD_HOSTS="${NAME} ${BAD_HOSTS}" ; SUCCESS=$(( SUCCESS + 1 )) ; cat "${TMPLOG}" ; fi
ELAPSED=$(elapsed_time $SECONDS)
@ -673,10 +679,24 @@ then
test -n "${SINGLE_FLAKE}" && ! [ "$host" = "${SINGLE_FLAKE}" ] && continue
printf "%${PRETTY_OUT_COLUMN}s " "${host}"
build_config "$i" "$COMMAND" "$SUDO" "$host"
# if build succeeded and we used TARGET_IP
# populate the sftp directory with the new version
if [ "$?" -eq 0 ] && [ -n "$TARGET_IP" ]
then
deploy_files "$i" "${host}" "${host}"
fi
done
else
printf "%${PRETTY_OUT_COLUMN}s " "${i}"
build_config "$i" "$COMMAND" "$SUDO" "$i"
# if build succeeded and we used TARGET_IP
# populate the sftp directory with the new version
if [ "$?" -eq 0 ] && [ -n "$TARGET_IP" ]
then
deploy_files "$i" "$i"
fi
fi
done
exit 0

View File

@ -115,3 +115,17 @@ source: +701.9 KiB
systemsettings: +62.6 KiB
-------------
```
# Push a configuration to a remote system
It's possible to use `bento` in a *push* model using `TARGET_IP`:
```
env TARGET_IP=10.43.43.1 NAME=myserver bento build switch
```
If the remote system is using a non-standard port, you need to define the according ssh option with `NIX_SSHOPTS`:
```
env NIX_SSHOPTS="-p2222" TARGET_IP=10.43.43.1 NAME=laptop bento build switch
```

View File

@ -11,6 +11,7 @@
- `bento build [dry-run|build|test|switch]`
- dry-build or build configurations. Using `test` or `switch`, can be used to apply a configuration locally. Default is `build`.
- when using `TARGET_IP`, the command is run on a remote server
- `bento status [delay]`
- display information for remote hosts, if `delay` is set, loop infinitely to display the status every `delay` seconds. Default delay is `0` and doesn't loop.
@ -36,6 +37,8 @@ A local file `config.sh` is required for Bento, it contains variable used to gen
- `BENTO_DIR`: contains the path of a bento directory, so you can run `bento` commands from anywhere
- `NAME`: contains machine names (flake config or directory in `hosts/`) to restrict commands `deploy`, `diff` and `build` to this machine only
- `VERBOSE`: if defined to anything, display `nixos-rebuild` output for local builds done with `bento build` or `bento deploy`
- `TARGET_IP`: can be used to push a configuration closure to a remote system, it's wrapping `nixos-rebuild`. Only work with `bento build [switch|test]`.
- `NIX_SSHOPTS`: parameters for ssh to be used when `TARGET_IP` is used. Example: `NIX_SSHOPTS=-p2222` to use port 2222 instead of the standard port 22.
# Self update mode