-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.sh
executable file
·104 lines (87 loc) · 2.61 KB
/
cli.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
#!/usr/bin/env bash
set -euo pipefail
export FDB_CLUSTER_FILE="${FDB_CLUSTER_FILE:-"$(dirname "$(realpath "$0")")/.fdb/cluster.file"}"
start_ephemeral_fdb_server() {
local background=${1:-"background"}
local data_dir pid
# shellcheck disable=SC2317
cleanup() {
local exit_code=$?
if [[ -n "${pid:-}" ]] && kill -0 "$pid" 2>/dev/null; then
echo "Stopping fdbserver (PID: $pid)" >&2
kill -15 "$pid" || true
fi
if [[ -n "${data_dir:-}" ]] && [[ -d "$data_dir" ]]; then
rm -rf "$data_dir"
fi
exit "$exit_code"
}
trap cleanup EXIT
if [[ -z "${FDB_CLUSTER_FILE:-}" ]]; then
echo "Error: FDB_CLUSTER_FILE environment variable not set" >&2
exit 1
fi
data_dir="$(mktemp -d)" || {
echo "Error: Failed to create temporary directory" >&2
exit 1
}
chmod 700 "$data_dir"
mkdir -p "$data_dir/data" "$data_dir/trace" || {
echo "Error: Failed to create data directories" >&2
exit 1
}
chmod 700 "$data_dir/data" "$data_dir/trace"
local fdb_port=4500
while { echo >/dev/tcp/127.0.0.1/"$fdb_port"; } 2>/dev/null; do
echo "Port $fdb_port is in use, trying another port..." >&2
fdb_port=$((fdb_port + 1))
done
local cluster_dir
cluster_dir="$(dirname "$FDB_CLUSTER_FILE")"
mkdir -p "$cluster_dir" || {
echo "Error: Failed to create cluster file directory" >&2
exit 1
}
local connection_string="t17x3130g3ju1xwxnnwaal6e029grtel:[email protected]:$fdb_port"
echo "Generated connection string: $connection_string" >&2
echo "$connection_string" >"$FDB_CLUSTER_FILE"
chmod 600 "$FDB_CLUSTER_FILE"
local fdbserver_path
fdbserver_path="$(command -v fdbserver)"
local -a cmd=(
"$fdbserver_path"
--cluster_file "$FDB_CLUSTER_FILE"
--datadir "$data_dir/data"
--logdir "$data_dir/trace"
--listen_address "127.0.0.1:$fdb_port"
--public_address "127.0.0.1:$fdb_port"
)
if [[ "$(uname -s)" =~ [dD]arwin ]]; then
echo "macOS detected, requesting sudo access..." >&2
if ! sudo -v; then
echo "Error: Failed to validate sudo access" >&2
exit 1
fi
sudo "${cmd[@]}" >&2 &
else
"${cmd[@]}" >&2 &
fi
pid=$!
echo "FDB server process pid=$pid" >&2
if ! timeout 15 fdbcli --exec "status" >&2; then
echo "Timed out after 15s checking for FDB status" >&2
exit 1
fi
echo "Configuring the cluster.." >&2
if ! fdbcli --exec "configure new single ssd-2" >&2; then
echo "Error: Failed to configure cluster" >&2
exit 1
fi
echo "FDB server started successfully (PID: $pid)" >&2
if [[ "$background" == "background" ]]; then
echo "$pid"
else
wait "$pid"
fi
}
"$@"