This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmake-targets.sh
executable file
·132 lines (111 loc) · 2.66 KB
/
make-targets.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env nix-shell
#!nix-shell -i bash ./shell.nix
# shellcheck shell=bash
set -eux
set -o pipefail
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
rm -rf "$scratch"
}
trap finish EXIT
machines() (
"$(dirname "$0")/terraform/enumerate-servers.sh"
)
mkdir "$scratch/machines"
networkentry() (
name=$1
ip=$2
cat <<EOF
"$name" = {
deployment = {
targetHost = "$ip";
targetUser = "root";
substituteOnDestination = true;
};
imports = [
../nixops/modules
./machines/$name.expr.nix
./machines/$name.system.nix
];
};
EOF
)
sshwrap() (
ssh \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile="$scratch/known_hosts" \
-o BatchMode=yes \
-o IdentitiesOnly=yes \
-i "$SSH_IDENTITY_FILE" \
"$@"
)
cfg_for_provisioner() (
local provisioner=$1
local name=$2
local ip=$3
case "$provisioner" in
"metal")
cfg_for_metal "$name" "$ip"
;;
"nixos-install")
cfg_for_nixos_install "$name" "$ip"
;;
*)
echo "Failed: no such provisioner: $provisioner"
exit 1
;;
esac
)
cfg_for_metal() (
local name=$1
local ip=$2
sshwrap "root@$ip" -- cat /etc/nixos/packet/system.nix > "$scratch/machines/${name}.system.nix"
)
cfg_for_nixos_install() (
local name=$1
local ip=$2
mkdir -p "$scratch/machines/${name}"
sshwrap "root@$ip" -- cat /etc/nixos/configuration.nix > "$scratch/machines/${name}/configuration.nix"
sshwrap "root@$ip" -- cat /etc/nixos/hardware-configuration.nix > "$scratch/machines/${name}/hardware-configuration.nix"
printf '{ imports = [ %s ]; }' "./${name}/configuration.nix" > "$scratch/machines/${name}.system.nix"
)
import_machine() (
local machine=$1
local name
name="$(jq -r .key <<<"$machine")"
local ip
ip="$(jq -r .value.ip <<<"$machine")"
local provisioner
provisioner=$(jq -r .value.provisioner <<<"$machine")
jq -r .value.expression <<<"$machine"
jq -r .value.expression <<<"$machine" > "$scratch/machines/${name}.expr.nix"
if cfg_for_provisioner "$provisioner" "$name" "$ip"; then
networkentry "$name" "$ip" >> "$scratch/default.nix"
fi
)
cat <<EOF > "$scratch/default.nix"
{
network = {
pkgs =
let
sources = import ../nix/sources.nix;
in
import sources.nixpkgs {
config = {
allowUnfree = true;
};
};
nixConfig = {
builders = "";
experimental-features = "nix-command";
};
};
EOF
machines | while read -r machine; do
import_machine "$machine" < /dev/null
done
echo "}" >> "$scratch/default.nix"
git rm -rf ./morph-network
rm -rf ./morph-network
mv "$scratch" ./morph-network
git add morph-network