This commit is contained in:
parent
e3cacda356
commit
cb25445e23
10 changed files with 136 additions and 49 deletions
16
packages/auto-deploy/default.nix
Normal file
16
packages/auto-deploy/default.nix
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
deploy-rs,
|
||||
gitMinimal,
|
||||
writeShellApplication,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "auto-deploy";
|
||||
meta.description = "Deploy machines automatically.";
|
||||
runtimeInputs = [
|
||||
deploy-rs
|
||||
gitMinimal
|
||||
];
|
||||
text = lib.readFile ./script.bash;
|
||||
}
|
45
packages/auto-deploy/script.bash
Normal file
45
packages/auto-deploy/script.bash
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
#### Environment
|
||||
FLAKE_ROOT="$(git rev-parse --show-toplevel)"
|
||||
|
||||
export PROFILE=""
|
||||
case "${1:-''}" in
|
||||
vm | physical)
|
||||
PROFILE="system-$1"
|
||||
;;
|
||||
*)
|
||||
echo "🛑 Error: Please use 'vm' or 'physical' as first parameter."
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
HOSTS=$(nix eval --raw "$FLAKE_ROOT"#deploy.nodes --apply "
|
||||
nodes: let
|
||||
inherit (builtins) attrNames filter concatStringsSep;
|
||||
names = attrNames nodes;
|
||||
profile = \"$PROFILE\";
|
||||
filteredNames = filter (name: nodes.\${name}.profiles ? \${profile}) names;
|
||||
in concatStringsSep \"\\n\" filteredNames
|
||||
")
|
||||
|
||||
#### Helpers
|
||||
retry() {
|
||||
local -r -i max_attempts="$1"
|
||||
shift
|
||||
local -i attempt_num=1
|
||||
until "$@"; do
|
||||
if ((attempt_num == max_attempts)); then
|
||||
echo "⚠️ Warning: Attempt $attempt_num failed and there are no more attempts left!"
|
||||
return 1
|
||||
else
|
||||
echo "⚠️ Attempt $attempt_num failed! Trying again in $attempt_num seconds..."
|
||||
sleep $((attempt_num++))
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
#### Execution
|
||||
for HOST in $HOSTS; do
|
||||
retry 3 deploy --skip-checks --targets "${FLAKE_ROOT}#${HOST}.${PROFILE}"
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue