-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJustfile
More file actions
1238 lines (1073 loc) · 39.1 KB
/
Copy pathJustfile
File metadata and controls
1238 lines (1073 loc) · 39.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import? 'scripts/recipes/aws.just'
import? 'scripts/recipes/demo.just'
import? 'scripts/recipes/custom.just'
import? 'scripts/recipes/governance.just'
set shell := ["bash", "-uc"]
set positional-arguments
alias terraform := tofu
alias tf := tofu
# Defaults
null := ""
stateDir := "STATEDIR=" + statePrefix / "$(basename $(git remote get-url origin))"
statePrefix := "~/.local/share"
# Machines provisioned directly by tofu, not managed by colmena/NixOS
# The list structure should be like: ["machine-a", "machine-b"] with optional comma delimiter
nonNixosMachines := '[]'
# Environment variables can be used to change the default template diff and path comparison sources.
# If TEMPLATE_PATH is set, it will have precedence, otherwise git url will be used for source templates.
templateBranch := env_var_or_default("TEMPLATE_BRANCH","main")
templatePath := env_var_or_default("TEMPLATE_PATH","no-path-given")
templateRepo := env_var_or_default("TEMPLATE_REPO","cardano-parts")
templateUrl := "https://raw.githubusercontent.com/input-output-hk/" + templateRepo + "/" + templateBranch + "/templates/cardano-parts-project"
# Common code
checkEnv := '''
TESTNET_MAGIC="${2:-""}"
''' + checkEnvWithoutOverride + '''
# Allow a magic override if the just recipe optional var is provided
if ! [ -z "${TESTNET_MAGIC:-}" ]; then
MAGIC="$TESTNET_MAGIC"
fi
'''
checkEnvWithoutOverride := '''
ENV="${1:-}"
if ! [[ "$ENV" =~ ^mainnet$|^preprod$|^preview$|^dijkstra$|^demo$|^leios$|^sanchonet$ ]]; then
>&2 echo "Error: only node environments for demo, dijkstra, leios, mainnet, preprod, preview and sanchonet are supported"
>&2 echo "Usage: just set-default-cardano-env <env>"
exit 1
fi
if [ "$ENV" = "mainnet" ]; then
MAGIC="764824073"
elif [ "$ENV" = "preprod" ]; then
MAGIC="1"
elif [ "$ENV" = "preview" ]; then
MAGIC="2"
elif [ "$ENV" = "sanchonet" ]; then
MAGIC="4"
elif [ "$ENV" = "dijkstra" ]; then
MAGIC="6"
elif [ "$ENV" = "leios" ]; then
MAGIC="164"
elif [ "$ENV" = "demo" ]; then
MAGIC="42"
fi
'''
checkSshConfig := '''
if not (".ssh_config" | path exists) {
print $"(ansi "bg_light_red")Please run:(ansi reset) `just save-ssh-config` first to create the .ssh_config file"
exit 1
}
def file-ts [path] {
if ($path | path exists) {
(stat -c %Y $path) | into int
} else {
0
}
}
def list-diff [left right lName rName] {
{
$lName: ($left | where $it not-in $right)
$rName: ($right | where $it not-in $left)
eq: ($left | where $it in $right)
}
| transpose where item
| flatten
| select item where
| where where != "eq"
| sort-by item
| enumerate
| each { |r| { index: ($r.index + 1) } | merge $r.item }
}
const checkFile = ".consistency-check-ts"
const hasIpModule = ("flake/nixosModules/ips-DONT-COMMIT.nix" | path exists)
let runCheck = if ($checkFile | path exists) {
let checkTs = (file-ts $checkFile)
let colmenaTs = (file-ts "flake/colmena.nix")
let sshHostsTs = (file-ts ".ssh_config")
let moduleIpsTs = (file-ts "flake/nixosModules/ips-DONT-COMMIT.nix")
($checkTs < $colmenaTs) or ($checkTs < $sshHostsTs) or ($checkTs < $moduleIpsTs)
} else {
true
}
if $runCheck {
print "Checking nixosCfg, sshCfg, and ipModuleCfg for consistency..."
let nonNixosMachines = ''' + nonNixosMachines + '''
let nixosCfg = (nix eval --json ".#nixosConfigurations" --apply "builtins.attrNames" | from json)
let sshCfg = (
open .ssh_config
| collect
| parse --regex `(?m)Host (.*)\n\s+HostName (.*)`
| rename machine ip
| sort-by machine
)
let ssh4Cfg = (
$sshCfg
| where ($it.machine | str ends-with ".ipv4")
| rename machine pubIpv4
| update machine { $in | str replace ".ipv4" "" }
| update pubIpv4 { $in | if $in == "unavailable.ipv4" { null } else { $in } }
| sort-by machine
)
let ssh6Cfg = (
$sshCfg
| where ($it.machine | str ends-with ".ipv6")
| rename machine pubIpv6
| update machine { $in | str replace ".ipv6" "" }
| update pubIpv6 { $in | if $in == "unavailable.ipv6" { null } else { $in } }
| sort-by machine
)
let moduleIps = if $hasIpModule {
open flake/nixosModules/ips-DONT-COMMIT.nix
| collect
| parse --regex `(?ms)(.*)^ };\nin {.*`
| get capture0
| parse --regex `(?m) (?<machine>.*) = {$\n(\s+privateIpv4 = \"(?<privIpv4>.*)";\n)?(\s+publicIpv4 = \"(?<pubIpv4>.*)";\n)?(\s+publicIpv6 = \"(?<pubIpv6>.*)";\n)?\s+};`
| select machine privIpv4 pubIpv4 pubIpv6
| update cells { if ($in | is-empty) { null } else { $in } }
| where {|r| ($nonNixosMachines | where {|m| $m == $r.machine} | is-empty) }
| sort-by machine
} else {
[]
}
let ssh4NixosCfg = ($ssh4Cfg | where {|r| ($nonNixosMachines | where {|m| $m == $r.machine} | is-empty) })
let ssh6NixosCfg = ($ssh6Cfg | where {|r| ($nonNixosMachines | where {|m| $m == $r.machine} | is-empty) })
let comparisons = [
{
label: "NixosConfigurations vs SSH hosts",
result: (list-diff $nixosCfg ($ssh4NixosCfg | get machine) onlyInNixosCfg onlyInSshCfg),
hint: "just save-ssh-config or just tf apply"
}
{
label: "SSH IPv4 vs SSH IPv6 machines",
result: (list-diff ($ssh4NixosCfg | get machine) ($ssh6NixosCfg | get machine) onlyInSsh4Cfg onlyInSsh6Cfg),
hint: "just save-ssh-config or just tf apply"
}
{
label: "NixosConfigurations vs IP module machines",
result: (if $hasIpModule {
list-diff $nixosCfg ($moduleIps | get machine) onlyInNixosCfg onlyInIpsModuleCfg
} else {[]}),
hint: "just update-ips"
}
{
label: "SSH public IPv4 vs IP module IPv4 values",
result: (if $hasIpModule {
list-diff ($ssh4NixosCfg | get pubIpv4) ($moduleIps | get pubIpv4) onlyInSshCfg onlyInIpsModuleCfg
} else {[]}),
hint: "just update-ips"
}
{
label: "SSH public IPv6 vs IP module IPv6 values",
result: (if $hasIpModule {
list-diff ($ssh6NixosCfg | get pubIpv6) ($moduleIps | get pubIpv6) onlyInSshCfg onlyInIpsModuleCfg
} else {[]}),
hint: "just update-ips"
}
]
let inconsistent = (
$comparisons
| where {|comp| $comp.result | is-not-empty}
)
$inconsistent | each {|comp|
print $"(ansi "bg_light_red")WARNING:(ansi reset) ($comp.label)"
print $" You may need to run `($comp.hint)`"
print " Differences found are:"
print $comp.result
print ""
}
if ($inconsistent | is-empty) {
touch $checkFile
}
}
'''
checkSshKey := '''
if not ('.ssh_key' | path exists) {
just save-bootstrap-ssh-key
}
'''
sopsConfigSetup := '''
# To support searching for sops config files from the target path rather than cwd up,
# implement a userland solution until natively sops supported.
#
# This enables $NO_DEPLOY_DIR to be separate from the default $STAKE_POOL_DIR/no-deploy default location.
# Ref: https://github.com/getsops/sops/issues/242#issuecomment-999809670
function sops_config() {
# Suppress xtrace on this fn as the return string is observed from the caller's output
{ SHOPTS="$-"; set +x; } 2> /dev/null
FILE="$1"
CONFIG_DIR=$(dirname "$(realpath "$FILE")")
while ! [ -f "$CONFIG_DIR/.sops.yaml" ]; do
if [ "$CONFIG_DIR" = "/" ]; then
>&2 echo "error: no .sops.yaml file was found while walking the directory structure upwards from the target file: \"$FILE\""
exit 1
fi
CONFIG_DIR=$(dirname "$CONFIG_DIR")
done
echo "$CONFIG_DIR/.sops.yaml"
# Reset the xtrace option to its state prior to suppression
[ -n "${SHOPTS//[^x]/}" ] && set -x
}
'''
# List all just recipes available
default:
@just --list
# Deploy select machines
apply *ARGS:
colmena apply --verbose --on {{ARGS}}
# Deploy all machines
apply-all *ARGS:
colmena apply --verbose {{ARGS}}
# Deploy select machines with the bootstrap key
apply-bootstrap *ARGS:
#!/usr/bin/env bash
set -euo pipefail
[ -f .ssh_key ] || just save-bootstrap-ssh-key
sed '/^Host /a\ IdentityFile .ssh_key\n IdentitiesOnly yes' .ssh_config > .ssh_config_bootstrap
SSH_CONFIG_FILE=".ssh_config_bootstrap" just apply {{ARGS}}
rm .ssh_config_bootstrap
# Build a nixos configuration
build-machine MACHINE *ARGS:
nix build -L .#nixosConfigurations.{{MACHINE}}.config.system.build.toplevel {{ARGS}}
# Build all nixosConfigurations
build-machines *ARGS:
#!/usr/bin/env nu
let nodes = (nix eval --json '.#nixosConfigurations' --apply builtins.attrNames | from json)
for node in $nodes {just build-machine $node {{ARGS}}}
# Run a local cardano-testnet
cardano-testnet isNg *ARGS:
#!/usr/bin/env bash
set -euo pipefail
if [ "{{isNg}}" = "true" ]; then
CARDANO_CLI=$(command -v cardano-cli-ng)
CARDANO_NODE=$(command -v cardano-node-ng)
CARDANO_TESTNET="cardano-testnet-ng"
elif [ "{{isNg}}" = "false" ]; then
CARDANO_CLI=$(command -v cardano-cli)
CARDANO_NODE=$(command -v cardano-node)
CARDANO_TESTNET="cardano-testnet"
else
echo "ERROR: isNg must be either true to use the pre-release (aka next generation or \"ng\") version of node and cli,"
echo " or false to use the release version of node and cli."
exit 1
fi
export CARDANO_CLI
export CARDANO_NODE
eval "$CARDANO_TESTNET" {{ARGS}}
# Deploy a cloudFormation stack
cf STACKNAME:
#!/usr/bin/env nu
mkdir cloudFormation
let secretName = (nix eval --raw '.#cardano-parts.cluster.infra.generic.costCenter')
let costCenter = (
just sops-decrypt-binary secrets/tf/cluster.tfvars
| lines
| where { |it| $it =~ $secretName }
| parse $"($secretName) = \"{secret}\""
| get 0.secret
| to text
)
nix eval --json '.#cloudFormation.{{STACKNAME}}' | from json | save --force 'cloudFormation/{{STACKNAME}}.json'
rain deploy --debug --params costCenter=($costCenter) --termination-protection ./cloudFormation/{{STACKNAME}}.json
# Prep dbsync for delegation analysis
dbsync-prep ENV HOST ACCTS="501":
#!/usr/bin/env bash
set -euo pipefail
{{checkEnvWithoutOverride}}
TMPFILE="/tmp/create-faucet-stake-keys-table-{{ENV}}.sql"
echo "Creating stake key sql injection command for environment {{ENV}} (this will take a minute)..."
NOMENU=true \
scripts/setup-delegation-accounts.py \
--print-only \
--testnet-magic "$MAGIC" \
--wallet-mnemonic <(sops -d "secrets/envs/{{ENV}}/utxo-keys/faucet.mnemonic") \
--signing-key-file <(sops -d "secrets/envs/{{ENV}}/utxo-keys/rich-utxo.skey") \
--num-accounts {{ACCTS}} \
> "$TMPFILE"
echo
echo "Pushing stake key sql injection command for environment {{ENV}}..."
just scp "$TMPFILE" {{HOST}}:"$TMPFILE"
echo
echo "Executing stake key sql injection command for environment {{ENV}}..."
just ssh {{HOST}} -t "psql -XU cexplorer cexplorer < \"$TMPFILE\""
# Start a remote dbsync psql session
dbsync-psql HOSTNAME:
#!/usr/bin/env bash
just ssh {{HOSTNAME}} -t 'psql -U cexplorer cexplorer'
# Analyze pool performance
dbsync-pool-analyze HOSTNAME TABLE="summary" PSQL_ARGS="-xX" LOVELACE="2E12":
#!/usr/bin/env bash
set -euo pipefail
echo "Pushing pool analysis sql command on {{HOSTNAME}}..."
just scp scripts/dbsync-pool-perf.sql {{HOSTNAME}}:/tmp/
echo
echo "Executing pool analysis sql command on host {{HOSTNAME}} with:"
echo " Table: {{TABLE}}"
echo " Psql args: \"{{PSQL_ARGS}}\""
echo " Lovelace threshold (lt): {{LOVELACE}}"
QUERY=$(just ssh {{HOSTNAME}} -t "'psql -P pager=off -v table={{TABLE}} -v lovelace={{LOVELACE}} -U cexplorer cexplorer {{PSQL_ARGS}} < /tmp/dbsync-pool-perf.sql'")
if [ "{{TABLE}}" = "summary" ] && [ "{{PSQL_ARGS}}" = "-xX" ]; then
echo
echo "Query output:"
echo "$QUERY" | tail -n +2
echo
JSON=$(grep -oP '^faucet_pool_summary_json[[:space:]]+\| \K{.*$' <<< "$QUERY" | jq .)
DEDELEGATE_POOLS=$(jq '.faucet_to_dedelegate' <<< "$JSON")
echo "$JSON"
echo
echo "Faucet pools to de-delegate are:"
echo "$DEDELEGATE_POOLS"
echo
if [ "$DEDELEGATE_POOLS" != "null" ]; then
echo "The string of indexes of faucet pools to de-delegate from the JSON above are:"
jq -r '.faucet_to_dedelegate | to_entries | map(.key) | join(" ")' <<< "$JSON"
echo
MAX_SHIFT=$(grep -oP '^faucet_pool_to_dedelegate_shift_pct[[:space:]]+\| \K.*$' <<< "$QUERY")
echo "The maximum percentage difference de-delegation of all these pools will make in chain density is: $MAX_SHIFT"
else
echo "There are no faucet delegated non-performing pools to de-delegate at this time."
fi
else
echo "$QUERY"
fi
# De-delegation pools for given faucet stake indexes
dedelegate-pools ENV *IDXS=null:
#!/usr/bin/env bash
set -euo pipefail
{{checkEnvWithoutOverride}}
if ! [[ "$ENV" =~ ^dijkstra$|^leios$|^preprod$|^preview$|^sanchonet$ ]]; then
echo "Error: only node environments for preprod, preview, dijkstra, leios and sanchonet are supported"
exit 1
fi
if [ "{{ENV}}" = "mainnet" ]; then
echo "Dedelegation cannot be performed on the mainnet environment"
exit 1
fi
source <(just set-default-cardano-env "{{ENV}}")
if [ "$(jq -re .syncProgress <<< "$(just query-tip "{{ENV}}")")" != "100.00" ]; then
echo "Please wait until the local tip of environment {{ENV}} is 100.00 before dedelegation"
exit 1
fi
if [ "${USE_SHELL_BINS:-}" = "true" ]; then
CARDANO_CLI="cardano-cli"
elif [ -n "${UNSTABLE:-}" ] && [ "${UNSTABLE:-}" != "true" ]; then
CARDANO_CLI="cardano-cli"
elif [ "${UNSTABLE:-}" = "true" ]; then
CARDANO_CLI="cardano-cli-ng"
elif [[ "$ENV" =~ ^preprod$|^preview$ ]]; then
CARDANO_CLI="cardano-cli"
fi
echo
read -p "Press any key to start de-delegating {{ENV}} faucet pool delegations for stake key indexes {{IDXS}}" -n 1 -r -s
echo
echo "Starting de-delegation of the following stake key indexes: {{IDXS}}"
for i in {{IDXS}}; do
echo "De-delegating index $i"
NOMENU=true scripts/restore-delegation-accounts.py \
--testnet-magic "$MAGIC" \
--signing-key-file <(just sops-decrypt-binary "secrets/envs/{{ENV}}/utxo-keys/rich-utxo.skey") \
--wallet-mnemonic <(just sops-decrypt-binary "secrets/envs/{{ENV}}/utxo-keys/faucet.mnemonic") \
--delegation-index "$i"
TXID=$(eval "$CARDANO_CLI" latest transaction txid --tx-file tx-deleg-account-$i-restore.txsigned | jq -r .txhash)
EXISTS="true"
while [ "$EXISTS" = "true" ]; do
EXISTS=$(eval "$CARDANO_CLI" latest query tx-mempool tx-exists $TXID | jq -r .exists || true)
if [ "$EXISTS" = "true" ]; then
echo "Pool de-delegation index $i tx still exists in the mempool, sleeping 5s: $TXID"
else
echo "Pool de-delegation index $i tx has been removed from the mempool."
fi
sleep 5
done
echo
echo
done
# Get a wallet address from mnemonic file
gen-payment-address FILE OFFSET="0":
cardano-address key from-recovery-phrase Shelley < {{FILE}} \
| cardano-address key child 1852H/1815H/0H/0/{{OFFSET}} \
| cardano-address key public --with-chain-code \
| cardano-address address payment --network-tag testnet
# Standard lint check
lint:
deadnix -f
statix check
# List machines
list-machines:
#!/usr/bin/env nu
def safe-run [block msg] {
let res = (do -i $block | complete)
if $res.exit_code != 0 {
print $msg
print "The output was:"
print
print $res
exit 1
}
$res.stdout
}
def default-row [machine] {
{
Name: $machine,
Nix: $"(ansi green)OK",
pubIpv4: $"(ansi red)--",
pubIpv6: $"(ansi red)--",
Id: $"(ansi red)--",
Type: $"(ansi red)--"
Region: $"(ansi red)--"
}
}
def main [] {
{{checkSshConfig}}
let nixosJson = (safe-run { ^nix eval --json ".#nixosConfigurations" --apply "builtins.attrNames" } "Nix eval failed.")
let sshJson = (safe-run { ^scj dump /dev/stdout -c .ssh_config } "scj failed.")
let baseTable = ($nixosJson | from json | each { |it| default-row $it })
let sshTable = ($sshJson | from json | where {|e| $e | get -o HostName | is-not-empty } | reject -o ProxyCommand)
let mergeTable = (
$sshTable | reduce --fold $baseTable { |it, acc|
let host = $it.Host
let hostData = $it.HostName
let machine = ($host | str replace -r '\.ipv(4|6)$' '')
let update = if ($host | str ends-with ".ipv4") {
{ pubIpv4: $hostData, Region: $it.Tag }
} else if ($host | str ends-with ".ipv6") {
{ pubIpv6: $hostData }
} else {
{ Id: $hostData, Type: $it.Tag }
}
if ($acc | any {|row| $row.Name == $machine }) {
$acc | each {|row|
if $row.Name == $machine {
$row | merge $update
} else {
$row
}
}
} else {
$acc ++ [ (default-row $machine | merge $update) ]
}
}
)
$mergeTable
| sort-by Name
| enumerate
| each { |r| { index: ($r.index + 1) } | merge $r.item }
}
# Copy a nix store path to a machine and pin it with a gc root
nix-copy-to-machine MACHINE STORE_PATH:
#!/usr/bin/env bash
set -euo pipefail
echo "Copying {{STORE_PATH}} to {{MACHINE}}..."
NIX_SSHOPTS="-F $(pwd)/.ssh_config" nix copy --to "ssh://{{MACHINE}}" "{{STORE_PATH}}"
echo "Creating GC root on {{MACHINE}}..."
just ssh {{MACHINE}} "nix-store --add-root /nix/var/nix/gcroots/$(basename {{STORE_PATH}}) --realise {{STORE_PATH}}"
echo "Done. Store path pinned on {{MACHINE}}."
# Pin a path to the local nix store
nix-store-pin PATH:
#!/usr/bin/env bash
set -euo pipefail
echo "Adding {{PATH}} to the nix store..."
STORE_PATH=$(nix store add "{{PATH}}")
echo "Creating GC root..."
sudo nix-store --add-root "/nix/var/nix/gcroots/$(basename "$STORE_PATH")" --realise "$STORE_PATH"
echo "Done. Stored and pinned at: $STORE_PATH"
# Query the tip of all running envs
query-tip-all:
#!/usr/bin/env bash
set -euo pipefail
QUERIED=0
for i in mainnet preprod preview dijkstra demo leios sanchonet; do
TIP=$(just query-tip $i 2>&1) && {
echo "Environment: $i"
echo "$TIP"
echo
QUERIED=$((QUERIED + 1))
}
done
[ "$QUERIED" = "0" ] && echo "No environments running." || true
# Query the current envs tip
query-tip ENV TESTNET_MAGIC=null:
#!/usr/bin/env bash
set -euo pipefail
{{checkEnv}}
{{stateDir}}
if [ "${USE_SHELL_BINS:-}" = "true" ]; then
CARDANO_CLI="cardano-cli"
elif [ -n "${UNSTABLE:-}" ] && [ "${UNSTABLE:-}" != "true" ]; then
CARDANO_CLI="cardano-cli"
elif [ "${UNSTABLE:-}" = "true" ]; then
CARDANO_CLI="cardano-cli-ng"
elif [[ "$ENV" =~ ^mainnet$|^preprod$|^preview$|^leios$ ]]; then
CARDANO_CLI="cardano-cli"
elif [[ "$ENV" =~ ^dijkstra$|^demo$|^sanchonet$ ]]; then
CARDANO_CLI="cardano-cli-ng"
fi
eval "$CARDANO_CLI" latest query tip \
--socket-path "$STATEDIR/node-{{ENV}}.socket" \
--testnet-magic "$MAGIC"
# Save the cluster bootstrap ssh key
save-bootstrap-ssh-key:
#!/usr/bin/env nu
print "Retrieving ssh key from tofu..."
nix build ".#opentofu.cluster" --out-link terraform.tf.json
tofu init -reconfigure
tofu workspace select -or-create cluster
let tf = (tofu show -json | from json)
let key = ($tf.values.root_module.resources | where type == tls_private_key and name == bootstrap)
$key.values.private_key_openssh | to text | save --force .ssh_key
chmod 0600 .ssh_key
# Save bulk credentials from select commit
save-bulk-creds ENV COMMIT="HEAD":
#!/usr/bin/env bash
mkdir -p workbench/custom/rundir
DATE=$(git show --no-patch --format=%cs {{COMMIT}})
for i in 1 2 3; do
sops --config /dev/null --input-type binary --output-type binary --decrypt \
<(git cat-file blob "{{COMMIT}}:secrets/groups/{{ENV}}$i/no-deploy/bulk.creds.pools.json") \
| jq -r '.[]'
done \
| jq -s \
> "workbench/custom/rundir/bulk.creds.secret.{{ENV}}.{{COMMIT}}.$DATE.pools.json"
echo
echo "Bulk credentials file for environment {{ENV}}, commit {{COMMIT}} has been saved at:"
echo " workbench/custom/rundir/bulk.creds.secret.{{ENV}}.{{COMMIT}}.$DATE.pools.json"
echo
echo "Do not commit this file and delete it when local workbench work is completed."
# Save ssh config
save-ssh-config:
#!/usr/bin/env nu
print "Retrieving ssh config from tofu..."
nix build ".#opentofu.cluster" --out-link terraform.tf.json
tofu init -reconfigure
tofu workspace select -or-create cluster
let tf = (tofu show -json | from json)
let key = ($tf.values.root_module.resources | where type == local_file and name == ssh_config)
$key.values.content | (parse --regex '(?ms)(.*)\n').capture0 | to text | save --force .ssh_config
chmod 0600 .ssh_config
# Set the shell's default node env - outputs export commands to stdout for sourcing
set-default-cardano-env ENV TESTNET_MAGIC=null:
#!/usr/bin/env bash
set -euo pipefail
{{checkEnv}}
{{stateDir}}
# The log and socket file may not exist immediately upon node startup, so only check for the pid file
if ! [ -s "$STATEDIR/node-{{ENV}}.pid" ]; then
>&2 echo "Error: Environment {{ENV}} does not appear to be running as $STATEDIR/node-{{ENV}}.pid does not exist"
exit 1
fi
>&2 echo "Linking: $(ln -sfv "$STATEDIR/node-{{ENV}}.socket" node.socket)"
>&2 echo "Linking: $(ln -sfv "$STATEDIR/node-{{ENV}}.log" node.log)"
>&2 echo ""
>&2 echo "To set environment variables in your shell, run:"
>&2 echo " source <(just set-default-cardano-env \"{{ENV}}\")"
echo "export CARDANO_NODE_SOCKET_PATH=\"$(pwd)/node.socket\""
echo "export CARDANO_NODE_NETWORK_ID=\"$MAGIC\""
echo "export TESTNET_MAGIC=\"$MAGIC\""
# Show nix flake details
show-flake *ARGS:
nix flake show --allow-import-from-derivation {{ARGS}}
# Show DNS nameservers
show-nameservers:
#!/usr/bin/env nu
let domain = (nix eval --raw '.#cardano-parts.cluster.infra.aws.domain')
let zones = (aws route53 list-hosted-zones-by-name | from json).HostedZones
let id = ($zones | where Name == $"($domain).").Id.0
let sets = (aws route53 list-resource-record-sets --hosted-zone-id $id | from json).ResourceRecordSets
let ns = ($sets | where Type == "NS").ResourceRecords.0.Value
print "Nameservers for the following hosted zone need to be added to the NS record of the delegating authority"
print $"Nameservers for domain: ($domain) \(hosted zone id: ($id)) are:"
print ($ns | to text)
# Decrypt a file to stdout using .sops.yaml rules
sops-decrypt-binary FILE:
#!/usr/bin/env bash
set -euo pipefail
{{sopsConfigSetup}}
[ -n "${DEBUG:-}" ] && set -x
# Default to stdout decrypted output.
# This supports the common use case of obtaining decrypted state for cmd arg input while leaving the encrypted file intact on disk.
sops --config "$(sops_config "{{FILE}}")" --input-type binary --output-type binary --decrypt "{{FILE}}"
# Decrypt a file in place using .sops.yaml rules
sops-decrypt-binary-in-place FILE:
#!/usr/bin/env bash
set -euo pipefail
{{sopsConfigSetup}}
[ -n "${DEBUG:-}" ] && set -x
sops --config "$(sops_config "{{FILE}}")" --input-type binary --output-type binary --decrypt "{{FILE}}" | sponge "{{FILE}}"
# Encrypt a file in place using .sops.yaml rules
sops-encrypt-binary FILE:
#!/usr/bin/env bash
set -euo pipefail
{{sopsConfigSetup}}
[ -n "${DEBUG:-}" ] && set -x
# Default to in-place encrypted output.
# This supports the common use case of first time encrypting plaintext state for public storage, ex: git repo commit.
sops --config "$(sops_config "{{FILE}}")" --input-type binary --output-type binary --encrypt "{{FILE}}" | sponge "{{FILE}}"
# Rotate sops encryption using .sops.yaml rules
sops-rotate-binary FILE:
#!/usr/bin/env bash
set -euo pipefail
{{sopsConfigSetup}}
[ -n "${DEBUG:-}" ] && set -x
# Default to in-place encryption rotation.
# This supports the common use case of rekeying, for example if recipient keys have changed.
just sops-decrypt-binary "{{FILE}}" | sponge "{{FILE}}"
just sops-encrypt-binary "{{FILE}}"
# Scp using repo ssh config
scp *ARGS:
#!/usr/bin/env nu
{{checkSshConfig}}
scp -o LogLevel=ERROR -F .ssh_config {{ARGS}}
# Ssh using repo ssh config
ssh HOSTNAME *ARGS:
#!/usr/bin/env nu
{{checkSshConfig}}
ssh -o LogLevel=ERROR -F .ssh_config "{{HOSTNAME}}" {{ARGS}}
# Generate example .ssh_config code
ssh-config-example:
#!/usr/bin/env bash
set -euo pipefail
echo "The following config example shows the expected struct of the \".ssh_config\" file."
echo "This may be used as a template if a custom .ssh_config file needs to be"
echo "created and managed manually, for example, in a non-aws environment."
echo
echo "Note that per host customization should come after the HostName line to preserve pattern parsing!"
echo
echo "---"
echo
cat <<"EOF"
Host *
User root
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
ServerAliveCountMax 2
ServerAliveInterval 60
Host machine-example-1
HostName i-EXAMPLE_AWS_EC2ID
# Per host customization should come after the HostName line to preserve pattern parsing
ProxyCommand sh -c "aws --region eu-central-1 ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
Tag t3a.medium
Host machine-example-1.ipv4
HostName 1.2.3.5
Tag eu-central-1
Host machine-example-1.ipv6
HostName ff00::01
EOF
# Ssh using cluster bootstrap key
ssh-bootstrap HOSTNAME *ARGS:
#!/usr/bin/env nu
{{checkSshConfig}}
{{checkSshKey}}
ssh -o LogLevel=ERROR -o IdentitiesOnly=yes -F .ssh_config -i .ssh_key "{{HOSTNAME}}" {{ARGS}}
# Ssh to all
ssh-for-all *ARGS:
#!/usr/bin/env nu
let nodes = (nix eval --json '.#nixosConfigurations' --apply builtins.attrNames | from json)
$nodes | par-each {|node|
let result = (do -i { ^just ssh -q $node {{ARGS}} } | complete)
{
index: $node,
result: $result
}
}
# Ssh for select
ssh-for-each HOSTNAMES *ARGS:
colmena exec --verbose --parallel 0 --on "{{HOSTNAMES}}" {{ARGS}}
# List machine id, ipv4, ipv6, name or region based on regex pattern
ssh-list TYPE PATTERN:
#!/usr/bin/env nu
const type = "{{TYPE}}"
let sshCfg = (
scj dump /dev/stdout -c .ssh_config
| from json
| default "" Host
| default "" HostName
)
if ($type == "id") {
$sshCfg
| where not ($it.Host =~ ".ipv(4|6)$")
| where Host =~ "{{PATTERN}}"
| get HostName
| str join " "
} else if ($type == "ipv4") {
$sshCfg
| where ($it.Host =~ ".ipv4$")
| where Host =~ "{{PATTERN}}"
| get HostName
| str join " "
} else if ($type == "ipv6") {
$sshCfg
| where ($it.Host =~ ".ipv6$")
| where Host =~ "{{PATTERN}}"
| get HostName
| str join " "
} else if ($type == "name") {
$sshCfg
| where not ($it.Host =~ ".ipv(4|6)$")
| where Host =~ "{{PATTERN}}"
| get Host
| str join " "
} else if ($type == "region") {
$sshCfg
| where ($it.Host =~ ".ipv4$")
| where Host =~ "{{PATTERN}}"
| get Tag
| str join " "
} else {
print "The TYPE must be one of: id, ipv4, ipv6, name or region"
}
# Start a local node for a specific env
start-node ENV:
#!/usr/bin/env bash
set -euo pipefail
{{stateDir}}
if ! [[ "{{ENV}}" =~ ^mainnet$|^preprod$|^preview$|^dijkstra$|^leios$|^sanchonet$ ]]; then
echo "Error: only node environments for mainnet, preprod, preview, dijkstra, leios and sanchonet are supported for start-node recipe"
exit 1
fi
# Stop any existing running local node env for a clean restart
just stop-node "{{ENV}}"
echo "Starting cardano-node for envrionment {{ENV}}"
mkdir -p "$STATEDIR"
if [[ "{{ENV}}" =~ ^mainnet$|^preprod$|^preview$ ]]; then
UNSTABLE=false
UNSTABLE_LIB=false
UNSTABLE_MITHRIL=false
USE_NODE_CONFIG_BP=false
elif [[ "{{ENV}}" == leios ]]; then
export CARDANO_NODE_SHELL_BIN="$(nix build -Lv github:IntersectMBO/cardano-node/leios-prototype#cardano-node --no-link --print-out-paths)/bin/cardano-node"
export USE_SHELL_BINS=true
export LEIOS_DB_PATH="$STATEDIR/db-leios/node/leios.db"
UNSTABLE=false
UNSTABLE_LIB=true
UNSTABLE_MITHRIL=false
USE_NODE_CONFIG_BP=false
else
UNSTABLE=true
UNSTABLE_LIB=true
UNSTABLE_MITHRIL=true
USE_NODE_CONFIG_BP=false
fi
# Set required entrypoint vars and run node in a new nohup background session
ENVIRONMENT="{{ENV}}" \
UNSTABLE="$UNSTABLE" \
UNSTABLE_LIB="$UNSTABLE_LIB" \
UNSTABLE_MITHRIL="$UNSTABLE_MITHRIL" \
USE_NODE_CONFIG_BP="$USE_NODE_CONFIG_BP" \
DATA_DIR="$STATEDIR" \
SOCKET_PATH="$STATEDIR/node-{{ENV}}.socket" \
nohup setsid nix run .#run-cardano-node &> "$STATEDIR/node-{{ENV}}.log" & echo $! > "$STATEDIR/node-{{ENV}}.pid" &
echo "Node started for {{ENV}}"
echo ""
echo "Set up your shell environment with:"
echo " source <(just set-default-cardano-env \"{{ENV}}\")"
# Stop all local nodes
stop-all:
#!/usr/bin/env bash
set -euo pipefail
for i in mainnet preprod preview dijkstra demo leios sanchonet; do
just stop-node $i
done
# Stop a local node for a specific env
stop-node ENV:
#!/usr/bin/env bash
set -euo pipefail
{{stateDir}}
if [ -f "$STATEDIR/node-{{ENV}}.pid" ]; then
echo "Stopping cardano-node for envrionment {{ENV}}"
kill $(< "$STATEDIR/node-{{ENV}}.pid") 2> /dev/null || true
rm -f "$STATEDIR/node-{{ENV}}.pid" "$STATEDIR/node-{{ENV}}.socket"
fi
# Clone a cardano-parts template
template-clone FILE:
#!/usr/bin/env bash
set -euo pipefail
# If a local copy already exists and there is a diff, force awareness
if [ -f "{{FILE}}" ]; then
if ! git diff-index --quiet HEAD -- "{{FILE}}"; then
echo "A local copy exists with uncommitted git modifications: {{FILE}}"
echo "Please commit or revert modifications and try again."
exit 1
fi
else
# Ensure the path to it exists
mkdir -p "$(dirname "{{FILE}}")"
fi
TMPFILE=$(mktemp -t template-clone-XXXXXX)
if [ "{{templatePath}}" = "no-path-given" ]; then
echo "Retreiving a template file copy from:"
echo " {{templateUrl}}/{{FILE}}"
if ! curl -H 'Cache-Control: no-cache' -sL "{{templateUrl}}/{{FILE}}" > "$TMPFILE"; then
echo "Unable to curl the requested resource successfully with:"
echo "curl -H 'Cache-Control: no-cache' -sL \"{{templateUrl}}/{{FILE}}\" > \"$TMPFILE\""
rm -f "$TMPFILE"
exit 1
fi
else
echo "Retreiving a template file copy from:"
echo " {{templatePath}}/{{FILE}}"
if [ -f "{{templatePath}}/{{FILE}}" ]; then
cp "{{templatePath}}/{{FILE}}" "$TMPFILE"
else
echo "Unable to find the requested template file at {{templatePath}}/{{FILE}}"
exit 1
fi
fi
echo
echo "Moving the template file copy into place and setting file permissions to 0644"
mv -f "$TMPFILE" "{{FILE}}"
chmod 0644 "{{FILE}}"
echo
echo "Git adding file:"
echo " {{FILE}}"
git add {{FILE}}
# Diff against cardano-parts template
template-diff FILE *ARGS:
#!/usr/bin/env bash
set -euo pipefail
if ! [ -f "{{FILE}}" ]; then
FILE="<(echo '')"
else
FILE="{{FILE}}"
fi
if [ "{{templatePath}}" = "no-path-given" ]; then
SRC_FILE="<(curl -H 'Cache-Control: no-cache' -sL \"{{templateUrl}}/{{FILE}}\")"
SRC_NAME="{{templateUrl}}/{{FILE}}"
else
SRC_FILE="{{templatePath}}/{{FILE}}"
SRC_NAME="$SRC_FILE"
fi
eval "icdiff -L {{FILE}} -L \"$SRC_NAME\" {{ARGS}} $FILE $SRC_FILE"
# Patch against cardano-parts template
template-patch FILE:
#!/usr/bin/env bash
set -euo pipefail
if git status --porcelain "{{FILE}}" | grep -q "{{FILE}}"; then
echo "Git file {{FILE}} is dirty. Please revert or commit changes to clean state and try again."
exit 1
fi
if [ "{{templatePath}}" = "no-path-given" ]; then
SRC_FILE="<(curl -H 'Cache-Control: no-cache' -sL \"{{templateUrl}}/{{FILE}}\")"
else
SRC_FILE="{{templatePath}}/{{FILE}}"
fi
PATCH_FILE=$(eval "diff -Naru \"{{FILE}}\" $SRC_FILE || true")
patch "{{FILE}}" < <(echo "$PATCH_FILE")
git add -p "{{FILE}}"
# Run tofu for bootstrap, cluster or grafana workspace
tofu *ARGS:
#!/usr/bin/env bash
set -euo pipefail
IGREEN='\033[1;92m'
IRED='\033[1;91m'
NC='\033[0m'
SOPS=("sops" "--input-type" "binary" "--output-type" "binary" "--decrypt")
read -r -a ARGS <<< "{{ARGS}}"
if [[ ${ARGS[0]} =~ bootstrap|cluster|grafana ]]; then
WORKSPACE="${ARGS[0]}"
ARGS=("${ARGS[@]:1}")
else
WORKSPACE="cluster"