-
Notifications
You must be signed in to change notification settings - Fork 1
/
c
executable file
·117 lines (113 loc) · 4.23 KB
/
c
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
#!/usr/bin/env bash
set -eo pipefail
export CKIE_CONFIG_REV="$(git rev-parse HEAD)"
show_trace=""
c_trace=""
if [ "$1" == "trace" ]; then
show_trace="--show-trace"
c_trace="trace"
shift
fi
deployMeta="$(nix-instantiate $show_trace --json --strict --eval deploy/binc-info.nix)"
function morp {
$($0 $c_trace eval build 'import ./deploy/secrets.nix { inherit uncheckedNodes; }')
echo morph $@ $show_trace
set +e
morph $@ $show_trace
morphexit="$?"
[ $morphexit -eq 0 ] || (echo ; exit $morphexit)
set -e
}
action=${2:-deploy}
if [ "$action" == "deploy" ]; then
deployExtra="switch --passwd"
fi
if [ "$1" == "" ]; then
morp $action deploy/morph.nix $deployExtra --on "$(hostname)"
elif [ "$1" == "all" ]; then
morp $action deploy/morph.nix $deployExtra
elif [ "$1" == "installer" ]; then
shift
echo evaluating..
drv="$($0 $c_trace eval fast nodes.installer.config.system.build.isoImage | jq -r .drvPath)"
echo realising "$drv"..
store_path="$(nix-store --realise "$drv")"
echo built "$store_path"
if [ "$1" == "flash" ]; then
shift
target="$1"
shift
file="$(ls "$store_path"/iso/*)"
cmd="sudo dd if=$file of=$target bs=4M status=progress $@"
echo "$cmd"
echo -n "I'll be running this command, please type YES if you agree to have $target OVERWRITTEN: "
read confirm
if [ "$confirm" == "YES" ]; then
echo "Okay!"
$cmd
fi
fi
elif [ "$1" == "virt" ]; then
# TODO: take $2 for host to run in virt
vmpath="$($0 $c_trace eval build nodes.virt.config.system.build.vm)"
QEMU_NET_OPTS='hostfwd=tcp::5555-:22' "$vmpath/bin/"run-*-vm &
while true; do
ssh localhost -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 5555 && kill %1 && break
sleep 0.2
done
elif echo "$deployMeta" | jq -r .hosts | grep -E "$1" >/dev/null; then
morp $action deploy/morph.nix $deployExtra --on "$1"
elif svchost=$(echo "$deployMeta" | jq ".serviceHosts["'"'$1'"'"]"); echo "$svchost" | grep -v null >/dev/null; then
morp $action deploy/morph.nix $deployExtra --on "{$(echo "$svchost" | jq -r 'join(",")')}"
elif [ "$1" == "bump" ] || [ "$1" == "demp" ] || [ "$1" == "cemp" ] || [ "$1" == "cdemp" ]; then
niv update $2
if [ "$1" == "demp" ] || [ "$1" == "cdemp" ]; then
# try to build & deploy this demp (="buMP DEploy")
$0 $c_trace $2
fi
if [ "$1" == "cemp" ] || [ "$1" == "cdemp" ]; then
# naively commit
git add nix/sources.json
git commit -m "niv: bump $2"
fi
elif [ "$1" == "eval" ]; then
shift
file="eval-$RANDOM.nix"
deferred() {
rm $file
}
trap deferred EXIT
mkfile() {
echo 'let sources = import ./nix/sources.nix; eval = import "${sources.morph}/data/eval-machines.nix" { networkExpr = ./deploy/morph.nix; }; pkgs = import sources.nixpkgs {}; inherit (eval) uncheckedNodes nodes; inherit (pkgs) lib; in' > "$file"
echo >> "$file"
echo "$@" >> "$file"
}
if [ "$1" == "fast" ]; then
shift
mkfile $@
# Each worker uses a lot of RAM..
workers=$(cat /proc/meminfo | rg 'MemAvailable:\s+(\d+) kB' --replace '$1' | jq '. / (6 * 1024 * 1024) | round')
nix-eval-jobs $show_trace --workers "$workers" $file
elif [ "$1" == "build" ]; then
shift
mkfile $@
nix-build $show_trace $file
else
mkfile $@
nix-instantiate $show_trace --json --strict --eval $file | jq .
fi
echo
elif [ "$1" == "farm" ]; then
tmpfile="$(mktemp)"
$0 $c_trace eval fast 'with lib; mapAttrs (_: n: n.config.system.build.toplevel) nodes' | jq -r .drvPath | grep -v null > "$tmpfile"
echo sending $(wc -l $tmpfile | cut -d' ' -f1) derivations...
cat "$tmpfile" | xargs nix copy --derivation --substitute-on-destination --to ssh://pansear
rsync "$tmpfile" pansear:~/aldhy/new-jobs
elif [ "$1" == "hmport" ]; then
shift
path="$(nix-instantiate deploy/hm-only.nix)"
nix copy --to "ssh://$@" --derivation "$path"
ssh "$@" '$(nix-store -r '$path')/bin/ckie-hm-port'
else
echo "usage: c [trace] <all|eval [fast]|bump <source>|installer [flash <device path>]|hmport <target>>"
fi