-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptb_interactive.sh
executable file
·286 lines (269 loc) · 7.97 KB
/
ptb_interactive.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
281
282
283
284
285
286
#!/bin/bash
set -f
set +e
################################################################################
# Percona Test Bench (c) 2013 Percona Ireland Ltd
################################################################################
# Originally Created 04/2013 George Ormond Lorch III
# Written by George Ormond Lorch III
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Launchpad homepage:
# * https://launchpad.net/percona-test-bench
#
# Bazaar code repository:
# * lp:percona-test-bench
#
# LICENSE
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. Please see the
# LICENSE file for information about licensing and use restrictions of
# this software.
################################################################################
. ./include/ptb_core.inc
########################################
# Percona Test Bench Interactive shell #
########################################
#
# The location of the saved history file
#
PTB_HISTORYFILE=
#
# The current history buffer
#
PTB_HISTORY[0]=
################################################################################
# Loads the saved history file for the current sandbox if there is one.
function ptb_load_history()
{
PTB_HISTORYFILE="$PTB_DATADIR/.ptb.history"
local line=
if [ -e "$PTB_HISTORYFILE" ]; then
while read line; do
PTB_HISTORY[${#PTB_HISTORY[@]}]=$line
history -s $line
done < "$PTB_HISTORYFILE"
fi
}
################################################################################
# Shows the current history
function ptb_show_history()
{
local count=${#PTB_HISTORY[@]};
for (( line=1; line < count; line++ )); do
echo "$line: ${PTB_HISTORY[$line]}"
done
}
################################################################################
# Saves the current history to the history file for the current sandbox.
function ptb_save_history()
{
if [ -e "$PTB_HISTORYFILE" ]; then
rm -f "$PTB_HISTORYFILE"
fi
local count="${#PTB_HISTORY[@]}"
local line=
for (( line=1; line < count; line++ )); do
echo "${PTB_HISTORY[$line]}" >> "$PTB_HISTORYFILE"
done
}
################################################################################
# Adds a command to the current history.
#
# $1 - required, command to add to history
function ptb_add_history()
{
local count="${#PTB_HISTORY[@]}"
local command="$@"
if [ "$count" -le "1" ]; then
PTB_HISTORY[0]=""
PTB_HISTORY[1]="$command"
history -s "$command"
else
if [ "${PTB_HISTORY[`expr $count - 1`]}" != "$command" ]; then
PTB_HISTORY[$count]="$command"
history -s "$command"
fi
fi
}
################################################################################
# Obtains the command stored at a specific history ordinal
#
# $1 - required, position to return
# $2 - required, variable to store it in
function ptb_history_at()
{
eval "$2="
local count="${#PTB_HISTORY[@]}"
local max=`expr "$count" - 1`
if [ "$max" -eq "0" ]; then
ptb_report_error "There is no history."
fi
if [ -z "$1" ]; then
ptb_report_error "You must specify a valid position from 1-$max..."
fi
if ! ptb_is_integer "$1"; then
ptb_report_error "You must specify a valid position from 1-$max..."
else
if [[ "$1" -lt "1" || "$1" -gt "$max" ]]; then
ptb_report_error "You must specify a valid position from 1-$max..."
else
eval "$2=\"${PTB_HISTORY[$1]}\""
fi
fi
}
################################################################################
# Clears the current history
function ptb_clear_history()
{
unset PTB_HISTORY
history -c
}
################################################################################
# Shows task status
#
# $1 - optional, instance id of task to display status, if NULL, tasks will be
# enumerated and status shown for each
function ptb_show_task_status()
{
local instanceid=$1
local count=${#S_TASKPID[@]}
if [ -z "$instanceid" ]; then
echo "Showing task status for `expr ${count} -1` task instances:"
local instance=
for (( instance=1; instance < count; instance++ )); do
ptb_show_task_status $instance
done
elif ! ptb_is_integer "$instanceid"; then
ptb_report_error "ptb_show_task_status($1) - Invalid task id specified."
else
local res="Task[$instanceid] :"
if [ -n "${T_PID[$instanceid]}" ]; then
res="$res pid[${T_PID[$instanceid]}] command[${T_COMMAND[$instanceid]}]"
if [ -n "`ps -p ${T_PID[$instanceid]} -o cmd --no-headers`" ]; then
res="$res appears to be running."
else
res="$res appears to have stopped."
fi
else
res="$res is empty."
fi
echo -e $res
fi
return $PTB_RET_SUCCESS
}
################################################################################
# Shows server status
#
# $1 - optional, instance id of server to display status, if NULL, servers will be
# enumerated and status shown for each
function ptb_show_server_status()
{
local instanceid=$1
local count=${#S_BASEDIR[@]}
if [ -z "$instanceid" ]; then
echo "Showing server status for `expr ${count} - 1` server instances:"
local instance=
for (( instance=1; instance < count; instance++ )); do
ptb_show_server_status $instance
done
elif ! ptb_is_integer "$instanceid"; then
ptb_report_error "ptb_show_server_status($1) - Invalid server id specified."
else
local res="Server[${instanceid}] :"
if [ -n "${S_BINDIR[$instanceid]}" ]; then
res="${res} bindir[${S_BINDIR[$instanceid]}] port[${S_PORT[$instanceid]}] socket[${S_SOCKET[$instanceid]}]"
if [ -f "${S_PIDFILE[$instanceid]}" ]; then
local pid=`cat ${S_PIDFILE[$instanceid]}`
res="${res} pid[${pid}]"
if [ -n "`ps -p ${pid} -o cmd --no-headers`" ]; then
res="${res} appears to be running."
else
res="${res} appears to have crashed."
fi
else
res="${res} is not running."
fi
local file="${S_OPTIONSFILE[$instanceid]}"
local line=
if [ -e "$file" ]; then
res="${res}\n\tServer options:"
while read line; do
res="${res}\n\t\t$line"
done < "$file"
else
res="${res}\n\tNo server options specified."
fi
else
res="${res} is empty."
fi
echo -e $res
fi
return $PTB_RET_SUCCESS
}
################################################################################
# Shows usage
function ptb_show_usage()
{
echo "Usage:"
echo " interactive (-bsv)"
echo "Where: "
echo " -s (required) Path to sandbox root."
echo " -v (optional) Output verbosity filter: ERROR=4; WARNING=3; INFO=2; DEBUG=1; IDEBUG=0. Default=3 (INFO)."
exit 1
}
################################################################################
# Executes command specified by user
function ptb_execute_command()
{
local command=$1
shift
case "$command" in
"quit" | "q" ) return 1;;
* ) ptb_${command} $@;;
esac
ptb_add_history "${command} $@"
return 0
}
################################################################################
# Interactive shell main
I_SANDBOXDIR=
I_VERBOSITY=$PTB_RPT_INFO
if [ $# -eq 0 ]; then
ptb_show_usage
exit 1
fi
while getopts ":s:v:" opt; do
case $opt in
s ) I_SANDBOXDIR=${OPTARG};;
v ) I_VERBOSITY=${OPTARG};;
* ) echo "ERROR: Unknown option."; usage;;
esac
done
ptb_init $I_SANDBOXDIR $I_VERBOSITY "" ptb_save_history
if [ $? -ne 0 ]; then
ptb_report_error "Unable to initialize ptb core with $?"
exit 1
fi
ptb_load_history
I_COMMAND=
while read -e -p "> " I_COMMAND; do
ptb_execute_command $I_COMMAND
if [ $? -ne 0 ]; then
break;
fi
done
ptb_cleanup 1