-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathbench-schelk.nu
More file actions
145 lines (129 loc) · 4.32 KB
/
Copy pathbench-schelk.nu
File metadata and controls
145 lines (129 loc) · 4.32 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
#!/usr/bin/env nu
def clean-marker [state_path: string] {
let marker_dir = ($env.BENCH_SCHELK_MARKER_DIR? | default $env.HOME)
let stem = ($state_path | path parse | get stem)
$"($marker_dir)/.tempo-bench-schelk-clean-($stem)"
}
def schelk-state [state_path: string] {
sudo cat $state_path | from json
}
def state-is-mounted [state_path: string] {
let state = (schelk-state $state_path)
($state | get --optional is_mounted) == true
}
def actual-is-mounted [mount_point: string] {
(mountpoint -q $mount_point | complete).exit_code == 0
}
def full-recover [state_path: string] {
let marker = (clean-marker $state_path)
rm -f $marker
sudo schelk --state-path $state_path full-recover -y
touch $marker
}
def cmd-detect [] {
if (($env.SCHELK_STATE_PATH? | default "") != "") and (($env.SCHELK_MOUNT? | default "") != "") {
print $"SCHELK_STATE_PATH=($env.SCHELK_STATE_PATH)"
print $"SCHELK_MOUNT=($env.SCHELK_MOUNT)"
return
}
if ("/var/lib/schelk/a.json" | path exists) {
print "SCHELK_STATE_PATH=/var/lib/schelk/a.json"
print "SCHELK_MOUNT=/reth-bench-a"
} else {
print "::error::No dual-schelk state file found"
exit 1
}
}
def cmd-restore [state_path: string, mount_point: string] {
let marker = (clean-marker $state_path)
print $"Restoring schelk snapshot \(($mount_point)\)..."
if (state-is-mounted $state_path) or (actual-is-mounted $mount_point) {
let result = (sudo schelk --state-path $state_path recover -y --kill | complete)
if $result.stdout != "" { print $result.stdout }
if $result.stderr != "" { print $result.stderr }
if $result.exit_code == 0 {
touch $marker
} else {
print $"Schelk recover failed for ($mount_point), falling back to full-recover..."
full-recover $state_path
}
} else if ($marker | path exists) {
print "Schelk volume is already clean and unmounted; skipping recovery."
} else {
print "Schelk volume is unmounted without a clean marker; running full-recover."
full-recover $state_path
}
let mount_result = (sudo schelk --state-path $state_path mount | complete)
if $mount_result.stdout != "" { print $mount_result.stdout }
if $mount_result.stderr != "" { print $mount_result.stderr }
if $mount_result.exit_code != 0 {
print $"Schelk mount failed for ($mount_point), falling back to full-recover..."
full-recover $state_path
sudo schelk --state-path $state_path mount
}
sudo chown -R (whoami | str trim) $mount_point
}
def cmd-mark-dirty [state_path: string] {
rm -f (clean-marker $state_path)
}
def cmd-cleanup [state_path: string] {
let marker = (clean-marker $state_path)
let result = (sudo schelk --state-path $state_path recover -y --kill | complete)
if $result.stdout != "" { print $result.stdout }
if $result.stderr != "" { print $result.stderr }
if $result.exit_code == 0 {
touch $marker
} else {
rm -f $marker
exit $result.exit_code
}
}
def cmd-promote [state_path: string] {
sudo schelk --state-path $state_path promote -y --kill
touch (clean-marker $state_path)
}
def usage [] {
print "Usage:"
print " nu bench-schelk.nu detect"
print " nu bench-schelk.nu restore <state-path> <mount-point>"
print " nu bench-schelk.nu mark-dirty <state-path>"
print " nu bench-schelk.nu cleanup <state-path>"
print " nu bench-schelk.nu promote <state-path>"
}
def main [command?: string, arg1?: string, arg2?: string] {
match $command {
"detect" => { cmd-detect },
"restore" => {
if $arg1 == null or $arg2 == null {
usage
exit 2
}
cmd-restore $arg1 $arg2
},
"mark-dirty" => {
if $arg1 == null {
usage
exit 2
}
cmd-mark-dirty $arg1
},
"cleanup" => {
if $arg1 == null {
usage
exit 2
}
cmd-cleanup $arg1
},
"promote" => {
if $arg1 == null {
usage
exit 2
}
cmd-promote $arg1
},
_ => {
usage
exit 2
},
}
}