-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·280 lines (237 loc) · 5.88 KB
/
entrypoint.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
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
#!/bin/zsh
set -e
# The script runs the comdb dev container and persists
# using three directories
# comdb2-opt-bb-bin:/opt/bb/bin/
# comdb2-dbs:/home/heisengarg/dbs
# binds the comdb2 top level directory to /home/heisengarg/comdb2
#
# The binding is only relevant for building
USER="heisengarg"
HOMEDIR="/home/$USER/"
DBSDIR="$HOMEDIR/dbs/"
CLUSTVOLDIR="$HOMEDIR/volumes/" # directory to copy the database to when building a cluster
DBNAME="testdb"
# Environment for ssh
# host password for the containers
PASSWD="docker"
KEYLOC="$HOMEDIR/.ssh"
KEYFILE="id_clust"
build() {
mkdir -p build && cd build &&
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug ..
ninja && sudo ninja install
}
clean_build() {
[[ -d build ]] && rm -rf build && mkdir build
build
}
new_db() {
if ! COMDB2="$(command -v comdb2)"; then
echo "Failed to find comdb2"
exit 1
fi
if [ ! -d "$DBSDIR" ]; then
echo "The $DBSDIR doesn't exist. Forgot to mount?"
exit 2
fi
# when we mount a directory, the current user
# might not have permissions to write to the directory
# since it is mounted with root access
if [ -w "$DBSDIR" ]; then
sudo chown -R $(whoami) "$DBSDIR"
fi
if [ -z "$1" ]; then
echo "no dbname passed using testdb"
else
DBNAME="$1"
fi
# make a directory for logs
sudo mkdir -p /opt/bb/var/log/cdb2 && sudo chown -R $(whoami) /opt/bb/
sudo mkdir -p "$DBSDIR/$DBNAME" && sudo chown -R $(whoami) "$DBSDIR/$DBNAME"
$COMDB2 --create --dir "$DBSDIR/$DBNAME" "$DBNAME"
# Add extra lrl options if we want by copying the lrl.options file
# , and setting the $LRLFILEOPTSPATH file path
if [ -n "$LRLFILEOPTSPATH" ]; then
if [ -e "$LRLFILEOPTSPATH" ]; then
echo "Appending lrl.options to "$DBNAME".lrl"
cat "$LRLFILEOPTSPATH" >>"$DBSDIR/$DBNAME/$DBNAME.lrl"
else
echo "An lrl.options file path: $LRLFILEOPTSPATH given but file doesn't exist"
fi
fi
}
clusterize() {
# <dbname> <hosts> <skip_copy_db=1>
if [ -z "$1" ]; then
echo "no dbname passed using testdb"
else
DBNAME="$1"
fi
if [ -z "$2" ]; then
echo "No hosts passed. Pass hosts as mach1,mach2,..,machn"
exit 1
fi
IFS=',' read -rA hosts <<<"$2"
[[ ! -d "$CLUSTVOLDIR" ]] && echo "volumes directory not found. Forgot to mount?" && exit 2
[[ ! -w "$CLUSTVOLDIR" ]] && sudo chown -R $(whoami) "$CLUSTVOLDIR"
echo "Copying binary files to $CLUSTVOLDIR/bin"
for file in $(ls /opt/bb/bin/); do
if [ -x "$CLUSTVOLDIR/bin" ]; then
[[ -n "$(diff /opt/bb/bin/$file $CLUSTVOLDIR/bin/$file)" ]] && echo "$file is changed" || echo "latest $file present"
else
echo "New binary $file"
fi
done
[[ -d "$CLUSTVOLDIR/bin" ]] && rm -r "$CLUSTVOLDIR/bin"
cp -R /opt/bb/bin "$CLUSTVOLDIR/bin"
if [[ "$3" -eq "1" ]]; then
exit 0
fi
if ! CPCOMDB2="$(command -v copycomdb2)"; then
echo "Failed to find comdb2"
exit 1
fi
echo "cluster nodes $(IFS=" " echo "${hosts[@]}")
" >>"$DBSDIR/$DBNAME/$DBNAME.lrl"
declare -a COPYINGPIDS
for host in ${hosts[@]}; do
{
echo "Copying $DBNAME from $DBSDIR/$DBNAME/$DBNAME.lrl to $CLUSTVOLDIR/$host-dbs/$DBNAME"
$CPCOMDB2 "$DBSDIR/$DBNAME/$DBNAME.lrl" "$CLUSTVOLDIR/$host-dbs/$DBNAME"
# Hack: I don't want the lrl dir path modified to $HOME/volumes/node1-dbs/dbname etc since
# $HOME/volumes/node1-dbs is just a mount and this would be mounted to $HOME/dbs/dbname
cp "$DBSDIR/$DBNAME/$DBNAME.lrl" "$CLUSTVOLDIR/$host-dbs/$DBNAME/$DBNAME.lrl"
} &
COPYINGPIDS+=($!)
done
for pid in ${COPYINGPIDS[@]}; do
wait $pid
sleep 0.5
done
}
run_db() {
if ! COMDB2="$(command -v comdb2)"; then
echo "Failed to find comdb2"
exit 1
fi
if [ -z "$1" ]; then
echo "no dbname passed using testdb"
else
DBNAME="$1"
fi
if [ ! -f "$DBSDIR/$DBNAME/$DBNAME.lrl" ]; then
echo "$DBNAME.lrl file doesn't exist. Did you build db?"
exit 2
fi
# make a directory for logs
[[ ! -d /opt/bb/var/log/cdb2 ]] && sudo mkdir -p /opt/bb/var/log/cdb2
sudo chown -R $(whoami) /opt/bb/
pmux -n && $COMDB2 --lrl "$DBSDIR/$DBNAME/$DBNAME.lrl" "$DBNAME"
}
copy_run_db() {
if ! CPCOMDB2="$(command -v copycomdb2)"; then
echo "Failed to find comdb2"
exit 1
fi
if [ -z "$1" ]; then
echo "no dbname passed using testdb"
else
DBNAME="$1"
fi
FROMHOST="$2"
$CPCOMDB2 "$FROMHOST:$DBSDIR/$DBNAME/$DBNAME.lrl"
run_db "$1"
}
run_client() {
if ! CDB2SQL="$(command -v cdb2sql)"; then
echo "Failed to find comdb2"
exit 1
fi
if [ -z "$1" ]; then
echo "no dbname passed using testdb"
else
DBNAME="$1"
fi
"$CDB2SQL" "$DBNAME"
}
distribute_ssh_keys() {
keep_running=0
if [ -z "$1" ]; then
echo "No hosts passed. Pass hosts as mach1,mach2,..,machn"
exit 1
fi
IFS=',' read -rA hosts <<<"$1"
if [ -n "$2" ]; then
keep_running=1
fi
if [ -e "$KEYLOC/$KEYFILE" ] && [ -e "$KEYLOC/$KEYFILE.pub" ]; then
# Restarting from a stopped container
# No need to run again
echo "$KEYLOC already exists."
else
ssh-keygen -b 2048 -t rsa -f "$KEYLOC/$KEYFILE" -q -N ""
for host in ${hosts[@]}; do
echo "$USER"
sshpass -p "$PASSWD" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEYLOC/$KEYFILE" "$USER"@"$host"
done
cat >>"$KEYLOC/config" <<EOF
IdentityFile $KEYLOC/$KEYFILE
EOF
fi
for host in ${hosts[@]}; do
if [ "$(sudo ssh -i $KEYLOC/$KEYFILE -o StrictHostKeyChecking=no $USER@"$host" hostname)" != $host ]; then
echo "Couldn't reach out to $host" >&2
exit 1
else
echo "Success ssh'ing to $host"
fi
done
[[ "$keep_running" != 0 ]] && watch uptime
}
sudo service ssh restart 2>&1 >/dev/null
sudo sysctl -w kernel.randomize_va_space=0 2>&1 >/dev/null ||
echo "[WARNING] Not running in privileged mode. Address randomization is still enabled"
case "$1" in
build)
build
;;
clean)
clean_build
;;
shell)
figlet "$hostname"
/bin/zsh
;;
db)
shift
new_db $*
;;
run)
shift
run_db $*
;;
cprun)
shift
copy_run_db $*
;;
client)
shift
run_client $*
;;
clust)
shift
clusterize $*
;;
clustbin)
shift
clusterize $* 1
;;
diskeys)
shift
distribute_ssh_keys $* 1
;;
*)
exec "$@"
;;
esac