-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.sh
executable file
·122 lines (110 loc) · 2.88 KB
/
client.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
#!/usr/local/bin/zsh
set -e
TMUX="$(command -v tmux)"
DOCKER="$(command -v docker)"
run_client() {
# <hostname> <dbname>
$DOCKER exec -it "$1" cdb2sql "$2"
}
# TODO: At some point, I should create one function which runs a given command
# on all host panes in a new window. For now, I'll just duplication would do
open_tmux() {
# <hostnames> <dbname>
IFS=',' read -rA HOSTS <<<$1
DBNAME="$2"
TMSESS="$DBNAME-cluster"
WINDOW="clients"
$TMUX has-session -t "$TMSESS" 2>/dev/null && $TMUX kill-session -t "$TMSESS"
$TMUX new -d -c "$(pwd)" -s "$TMSESS" -n "$WINDOW"
$TMUX send-keys -t "$TMSESS:$WINDOW" "./client.sh -a -d '$2' -n ${HOSTS[1]}" Enter
for host in "${HOSTS[@]:1}"; do
$TMUX split-window
$TMUX send-keys "./client.sh -a -d '$2' -n '$host'" Enter
done
$TMUX split-window
$TMUX send-keys "$DOCKER exec -it client zsh" Enter
$TMUX select-layout tiled
open_logs "$1" "$2"
open_term "$1" "$2"
$TMUX a -t "$TMSESS"
}
open_logs() {
IFS=',' read -rA HOSTS <<<$1
DBNAME="$2"
TMSESS="$DBNAME-cluster"
WINDOW="logs"
if $TMUX has-session -t "$TMSESS" 2>/dev/null; then
$TMUX new-window -c "$(pwd)" -t "$TMSESS" -n "$WINDOW"
else
$TMUX new -d -c "$(pwd)" -s "$TMSESS" -n "$WINDOW"
fi
$TMUX send-keys -t "$TMSESS:$WINDOW" "$DOCKER compose logs -f comdb2-${HOSTS[1]}" Enter
for host in "${HOSTS[@]:1}"; do
$TMUX split-window
$TMUX send-keys "$DOCKER compose logs -f comdb2-$host" Enter
$TMUX select-layout tiled
done
# $TMUX a -t "$TMSESS:$WINDOW"
}
open_term() {
IFS=',' read -rA HOSTS <<<$1
DBNAME="$2"
TMSESS="$DBNAME-cluster"
WINDOW="shell"
if $TMUX has-session -t "$TMSESS" 2>/dev/null; then
$TMUX new-window -c "$(pwd)" -t "$TMSESS" -n "$WINDOW"
else
$TMUX new -d -c "$(pwd)" -s "$TMSESS" -n "$WINDOW"
fi
$TMUX send-keys -t "$TMSESS:$WINDOW" "$DOCKER exec -it "${HOSTS[1]}" /bin/zsh" Enter
for host in "${HOSTS[@]:1}"; do
$TMUX split-window
$TMUX send-keys "$DOCKER exec -it "$host" /bin/zsh" Enter
$TMUX select-layout tiled
done
# $TMUX a -t "$TMSESS:$WINDOW"
}
opt_attach=0
opt_logs=0
opt_db=''
opt_hosts=''
while getopts ahld:n: name; do
case $name in
d)
opt_db="$OPTARG"
;;
n)
opt_hosts="$OPTARG"
;;
a)
opt_attach=1
;;
l) opt_logs=1 ;;
h | ?)
cat <<EOF >&2
Usage: ./client.sh {-a | -d <database> -n <host1,host2,host3>}
-a run client for the given host
-d database name
-n hostnames
Example:
./client.sh -a -d testdb -h host1 # runs client in host1
./client.sh -d testdb -h host1,host2,host3 # opens a tmux session for each host
# and runs a client there
EOF
exit 1
;;
esac
done
shift $(expr $OPTIND - 1)
if [ -z $opt_db ] && [ -z $opt_db ]; then
echo "dbname and/or hostname not passed"
$0 -h
exit 1
fi
if [[ $opt_logs != 0 ]]; then
open_logs $opt_hosts $opt_db
elif [[ $opt_attach != 0 ]]; then
run_client $opt_hosts $opt_db
else
open_tmux $opt_hosts $opt_db
fi