WIP: Create script to deploy in CI
All checks were successful
CI / build (push) Successful in 5m3s

This commit is contained in:
Fabian Hauser 2025-04-12 20:39:55 +03:00
parent e3cacda356
commit cb25445e23
10 changed files with 136 additions and 49 deletions

View file

@ -4,14 +4,21 @@ Note that you have to be connected to the `vpn.qo.is`
(or execute the deployment from a host that is in the `backplane.net.qo.is` overlay network) (or execute the deployment from a host that is in the `backplane.net.qo.is` overlay network)
and that you need to have SSH root access to the target machines. and that you need to have SSH root access to the target machines.
## Deploy system categories
This is also used in CI.
```bash
auto-deploy vm
auto-deploy physical
```
## Deploy to selected target hosts ## Deploy to selected target hosts
```bash ```bash
nix run .#deploy-qois .#<hostname>.system .#<hostname2>.system nix develop
```
## Deploy with extended timeouts (sometimes required for slow APU devices) deploy --skip-checks .#cyprianspitz.system-physical
deploy --skip-checks .#lindberg-build.system-vm
```bash
nix run .#deploy-qois .#calanda.system -- --confirm-timeout 600 --activation-timeout 600
``` ```

View file

@ -0,0 +1,27 @@
{
deployPkgs,
pkgs,
self,
...
}:
let
inherit (pkgs.lib) pipe filterAttrs mapAttrs;
in
{
nodes = pipe self.nixosConfigurations [
(filterAttrs (_n: v: v.config.services.qemuGuest.enable == false))
(mapAttrs (
host: config: {
hostname = "${host}.backplane.net.qo.is";
profiles.system-physical = {
sshUser = "root";
user = "root";
activationTimeout = 600;
confirmTimeout = 120;
remoteBuild = true;
path = deployPkgs.deploy-rs.lib.activate.nixos config;
};
}
))
];
}

View file

@ -0,0 +1,27 @@
{
deployPkgs,
pkgs,
self,
...
}:
let
inherit (pkgs.lib) pipe filterAttrs mapAttrs;
in
{
nodes = pipe self.nixosConfigurations [
(filterAttrs (_n: v: v.config.services.qemuGuest.enable))
(mapAttrs (
host: config: {
hostname = "${host}.backplane.net.qo.is";
profiles.system-vm = {
sshUser = "root";
user = "root";
activationTimeout = 300;
confirmTimeout = 60;
remoteBuild = true;
path = deployPkgs.deploy-rs.lib.activate.nixos config;
};
}
))
];
}

View file

@ -1,20 +0,0 @@
{
deployPkgs,
pkgs,
self,
system,
...
}:
{
nodes = pkgs.lib.mapAttrs (host: config: {
hostname = "${host}.backplane.net.qo.is";
profiles.system = {
sshUser = "root";
user = "root";
activationTimeout = 420;
confirmTimeout = 120;
path = deployPkgs.deploy-rs.lib.activate.nixos config;
};
}) self.nixosConfigurations;
}

View file

@ -29,9 +29,9 @@ in
pre-commit-check.enabledPackages pre-commit-check.enabledPackages
++ [ vscodium-with-extensions ] ++ [ vscodium-with-extensions ]
++ (with self.packages.${system}; [ ++ (with self.packages.${system}; [
deploy-qois
sops sops
sops-rekey sops-rekey
auto-deploy
]) ])
++ (with pkgs; [ ++ (with pkgs; [
attic-client attic-client

View 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;
}

View 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

View file

@ -1,14 +0,0 @@
{
deploy-rs,
flakeSelf,
writeShellApplication,
...
}:
writeShellApplication {
name = "deploy-qois";
meta.description = "Deploy configuration to specificed targets.";
runtimeInputs = [ deploy-rs ];
text = ''
deploy --remote-build --skip-checks --interactive --targets "''${@:-${flakeSelf}}"
'';
}

View file

@ -19,11 +19,13 @@
"*.toml" "*.toml"
] ]
++ [ ++ [
".envrc"
"robots.txt"
".vscode/*" ".vscode/*"
"nixos-modules/system/etc/*" "nixos-modules/system/etc/*"
"private"
"private/*" "private/*"
".envrc"
"robots.txt"
]; ];
formatter.jsonfmt.excludes = [ ".vscode/*.json" ]; formatter.jsonfmt.excludes = [ ".vscode/*.json" ];
}; };

View file

@ -22,13 +22,10 @@ Deploy updates:
nix develop nix develop
# Deploy vms # Deploy vms
deploy-qois .#lindberg-nextcloud .#lindberg-build auto-deploy vm
# Deploy fast physical hosts # Deploy physical hosts
deploy-qois .#lindberg auto-deploy physical
# Deploy slow physical hosts (maybe do individually)
deploy-qois --confirm-timeout 600 --activation-timeout 600 --targets .#stompert .#stompert
``` ```