blob: 76054c4272ad798475ca1907d4c90017d684460e [file] [log] [blame]
Serge Bazanskib3c67702021-09-10 22:27:24 +00001# Top-level wrapper script for calling per-machine provisioners.
2#
3# Given ops.machines."edge01.waw.bgp.wtf".config.passthru.hscloud.provision,
4# this script allows to run it by doing:
5# $ $(nix-build -A ops.provision) edge01.waw.bgp.wtf
6# Or, to first list all available machines by doing:
7# $ $(nix-build -A ops.provision)
8#
9# The main logic of the provisioner script is in machines.nix.
10
11{ hscloud, pkgs, lib, ... }:
12
13with lib; with builtins;
14
15let
16
17 # All machines from ops.machines, keyed by FQDN.
18 machines = filterAttrs (n: _: n != "__readTree") hscloud.ops.machines;
19 # Machines' provisioner scripts, keyed by machine FQDN.
20 machineProvisioners = mapAttrs (_: v: v.config.passthru.hscloud.provision) machines;
21 # List of machine FQDNs.
22 machineNames = attrNames machines;
23
24 # User-friendly list of machines by FQDN.
25 machineList = concatStringsSep "\n"
26 (map
27 (name: " - ${name}")
28 machineNames);
29
30 # Derivation containing bin/provision-FQDN symlinks to machines' provisioners.
31 forest = pkgs.linkFarm "provision-forest"
32 (mapAttrsToList
33 (fqdn: p: { name = "bin/provision-${fqdn}"; path = p; })
34 machineProvisioners);
35in
36
37pkgs.writeScript "provision" ''
38 #!/bin/sh
39 name="$1"
40
41 usage() {
42 echo >&2 "Usage: $0 machine|machine.hswaw.net"
43 echo >&2 "Available machines:"
44 echo >&2 "${machineList}"
45 }
46
47 if [ -z "$name" ]; then
48 usage
49 exit 1
50 fi
51
52 provisioner="${forest}/bin/provision-$name"
53 if [ ! -e "$provisioner" ]; then
54 name="$name.hswaw.net"
55 provisioner="${forest}/bin/provision-$name"
56 fi
57 if [ ! -e "$provisioner" ]; then
58 usage
59 exit 1
60 fi
61 # :^)
62 echo -ne "\e[34mh \e[31ms \e[33mc l \e[34mo \e[32mu \e[31md \e[0m"
63 echo ""
64 echo "Starting provisioner for $name..."
65 echo ""
66 echo "Too slow to evaluate? Equivalent faster command line that rebuilds just one node:"
67 echo " \$(nix-build -A 'ops.machines.\"$name\".config.passthru.hscloud.provision')"
68 echo ""
69 echo "Or, if you want to deploy the same configuration on different machines, just run"
70 echo "this script again without re-evaluating nix:"
71 echo " $0 $name"
72 echo ""
73 exec "$provisioner"
74''