1
package MySQL::Sandbox::Scripts;
8
our @ISA = qw/Exporter/;
10
our @EXPORT_OK = qw( scripts_in_code);
11
our @EXPORT = @EXPORT_OK;
30
# 'current_options.conf',
34
##'low_level_make_sandbox',
35
##'make_replication_sandbox',
36
##'make_multiple_sandbox',
37
##'make_multiple_custom_sandbox',
40
#'tests/test_sandbox.pl',
50
my %parse_options_low_level_make_sandbox = (
52
value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},
53
parse => 'upper_directory=s',
56
"The directory that will contain the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
60
sandbox_directory => {
62
parse => 'd|sandbox_directory=s',
65
'Where to install the sandbox, under home-directory'
70
parse => 'P|sandbox_port=i',
73
'The port number to use for the sandbox server.',
79
parse => 'datadir_from=s' ,
82
'Where to get datadir files from. Available options are',
83
'archive will be taken from the archived data',
84
' provided with the package. They include',
85
' default username and passwords',
87
'script the script mysql_install_db is called, with',
88
' default users, no passwords.',
89
'dir:name will be copied from an existing mysql directory',
95
parse => 'i|install_version=s',
98
'Which version to install (3.23, 4.0, 4.1, 5.0 or 5.1) default: none'
102
value => '/usr/local/mysql',
103
parse => 'b|basedir=s' ,
106
'Base directory for MySQL (default: /usr/local/mysql)'
111
parse => 'm|my_file=s',
114
'which sample my-{small|large|huge}.cnf file should be used',
115
'for additional configuration',
116
'You may enter either the label (small|large|huge) or a full',
117
'file name. (default: none)',
122
parse => 'f|conf_file=s',
125
'Configuration file containing options like the ones',
126
'you can give on the command line (without dashes)',
129
operating_system_user => {
130
value => $ENV{'USER'},
131
parse => 'U|operating_system_user=s',
134
'Operating system user (for mysql installation)',
135
"default: \$USER ($ENV{USER})",
140
parse => 'u|db_user=s' ,
143
'user for global access to mysql (Default: sandbox)'
148
parse => 'p|db_password=s' ,
151
'password for global access to mysql (Default: sandbox)'
156
parse => 'c|my_clause=s@',
159
'option to be inserted in a my.cnf file',
160
'it may be used several times',
165
parse => 'prompt_prefix=s',
168
'prefix to use in CLI prompt (default: mysql)',
173
value => q/ [\h] {\u} (\d) > '/,
174
parse => 'prompt_body=s',
177
'options to use in CLI prompt (default: [\h] {\u} (\d) > )',
185
'Use this option if you want to overwrite existing directories',
186
'and files during the installation. (Default: disabled)',
189
no_ver_after_name => {
191
parse => 'no_ver_after_name',
194
'Do not add version number after sandbox directory name (default: disabled)'
199
parse => 'v|verbose',
202
'Use this option to see installation progress (default: disabled)'
207
parse => 'load_grants',
210
'Loads the predefined grants from a SQL file.',
211
'Useful when installing from script.',
212
'(default: disabled)'
217
parse => 'no_load_grants',
220
'Does not loads the predefined grants from a SQL file.',
221
'(default: disabled)'
228
parse => 't|interactive',
231
'Use this option to be guided through the installation process (default: disabled)'
234
more_options => {value => q{}, parse => undef, so => 20},
235
help => {value => q{}, parse => 'help', so => 25},
238
parse => 'no_confirm',
241
'suppress the confirmation request from user',
246
my %parse_options_replication = (
248
value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},
249
parse => 'upper_directory=s',
252
"The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
255
replication_directory => {
257
parse => 'r|replication_directory=s',
260
'Where to install the sandbox replication system, under upper-directory',
261
'default: (rsandbox)'
266
parse => 'server_version=s',
269
'which version to install'
272
sandbox_base_port => {
274
parse => 'sandbox_base_port=i',
277
'The port number to use for the sandbox replication system.',
278
'(Default: 11000 + version )',
284
parse => 'how_many_nodes|how_many_slaves=i',
287
'The number of slaves to create.',
294
parse => 't|topology=s',
297
'Sets a replication topology.',
298
'Available: {standard|circular} (default: standard)'
303
parse => 'circular=i',
306
'Sets circular replication with N nodes.',
314
parse => 'master_master',
317
'set exactly two nodes in circular replication'
323
parse => 'v|verbose',
326
'Use this option to see installation progress (default: disabled)'
334
'show this help (default: disabled)'
340
my %parse_options_many = (
342
value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},
343
parse => 'upper_directory=s',
346
"The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
351
parse => 'r|group_directory=s',
354
'Where to install the sandbox group, under home-directory',
355
'default: (multi_msb)'
360
parse => 'server_version=s',
363
'which version to install'
366
sandbox_base_port => {
368
parse => 'sandbox_base_port=i',
371
'The port number to use for the sandbox multiple system.',
372
'(Default: 7000 + version )',
378
parse => 'how_many_nodes=i',
381
'The number of nodes to create.',
387
parse => 'master_master',
390
'set exactly two nodes in circular replication'
399
'set the nodes in circular replication'
404
parse => 'v|verbose',
407
'Use this option to see installation progress (default: disabled)'
416
'show this help (default: disabled)'
421
my %parse_options_custom_many = (
423
value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},
424
parse => 'upper_directory=s',
427
"The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
432
parse => 'r|group_directory=s',
435
'Where to install the sandbox group, under home-directory',
436
'default: (multi_msb)'
441
parse => 'server_version=s',
444
'which version to install'
447
sandbox_base_port => {
449
parse => 'sandbox_base_port=i',
452
'The port number to use for the sandbox custom system.',
453
'(Default: 5000 + version )',
459
parse => 'v|verbose',
462
'Use this option to see installation progress (default: disabled)'
470
'show this help (default: disabled)'
475
my %scripts_in_code = (
476
'start.sh' => <<'START_SCRIPT',
480
export LD_LIBRARY_PATH=$BASEDIR/lib:$BASEDIR/lib/mysql:$LD_LIBRARY_PATH
481
export DYLD_LIBRARY_PATH=$BASEDIR_/lib:$BASEDIR/lib/mysql:$DYLD_LIBRARY_PATH
482
MYSQLD_SAFE="$BASEDIR/bin/_MYSQLDSAFE_"
483
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
484
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
488
echo "sandbox server already started (found pid file $PIDFILE)"
494
$MYSQLD_SAFE --defaults-file=$SBDIR/my.sandbox.cnf $@ > /dev/null 2>&1 &
496
$MYSQLD_SAFE --defaults-file=$SBDIR/my.sandbox.cnf $@ > "$SBDIR/start.log" 2>&1 &
500
while [ ! -f $PIDFILE ]
502
ATTEMPTS=$(( $ATTEMPTS + 1 ))
504
if [ $ATTEMPTS = $TIMEOUT ]
514
echo " sandbox server started"
516
echo " sandbox server not started yet"
520
'restart.sh' => <<'RESTART_SCRIPT',
524
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
529
'stop.sh' => <<'STOP_SCRIPT',
532
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
534
export LD_LIBRARY_PATH=$BASEDIR/lib:$BASEDIR/lib/mysql:$LD_LIBRARY_PATH
535
export DYLD_LIBRARY_PATH=$BASEDIR/lib:$BASEDIR/lib/mysql:$DYLD_LIBRARY_PATH
536
MYSQL_ADMIN="$BASEDIR/bin/mysqladmin"
537
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
541
if [ -f $SBDIR/data/master.info ]
543
echo "stop slave" | $SBDIR/use -u root
545
# echo "$MYSQL_ADMIN --defaults-file=$SBDIR/my.sandbox.cnf shutdown"
546
$MYSQL_ADMIN --defaults-file=$SBDIR/my.sandbox.cnf shutdown
551
# use the send_kill script if the server is not responsive
556
'send_kill.sh' => <<'KILL_SCRIPT',
559
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
560
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
565
echo "Attempting normal termination --- kill -15 $MYPID"
567
# give it a chance to exit peacefully
569
while [ -f $PIDFILE ]
571
ATTEMPTS=$(( $ATTEMPTS + 1 ))
572
if [ $ATTEMPTS = $TIMEOUT ]
580
echo "SERVER UNRESPONSIVE --- kill -9 $MYPID"
588
'use.sh' => <<'USE_SCRIPT',
591
export LD_LIBRARY_PATH=_BASEDIR_/lib:_BASEDIR_/lib/mysql:$LD_LIBRARY_PATH
592
export DYLD_LIBRARY_PATH=_BASEDIR_/lib:_BASEDIR_/lib/mysql:$DYLD_LIBRARY_PATH
593
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
595
MYSQL="$BASEDIR/bin/mysql"
596
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
599
$MYSQL --defaults-file=$SBDIR/my.sandbox.cnf $MYCLIENT_OPTIONS "$@"
601
# echo "PID file $PIDFILE not found "
605
'clear.sh' => <<'CLEAR_SCRIPT',
608
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
610
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
612
# attempt to drop databases gracefully
616
for D in `echo "show databases " | ./use -B -N | grep -v "^mysql$" | grep -v "^information_schema$"`
618
echo 'drop database `'$D'`' | ./use
620
VERSION=`./use -N -B -e 'select version()'`
621
if [ `perl -le 'print $ARGV[0] ge "5.1" ? "1" : "0" ' "$VERSION"` = "1" ]
623
./use -e "truncate mysql.general_log"
624
./use -e "truncate mysql.slow_log"
630
rm -f data/`hostname`*
634
rm -f data/mysql-bin*
635
rm -f data/*relay-bin*
643
# remove all databases if any
645
for D in `ls -d data/*/ | grep -w -v mysql `
652
'my.sandbox.cnf' => <<'MY_SANDBOX_SCRIPT',
660
password = _DBPASSWORD_
662
socket = /tmp/mysql_sandbox_SERVERPORT_.sock
667
socket = /tmp/mysql_sandbox_SERVERPORT_.sock
669
datadir = _HOME_DIR_/_SANDBOXDIR_/data
670
pid-file = _HOME_DIR_/_SANDBOXDIR_/data/mysql_sandbox_SERVERPORT_.pid
671
#log-slow-queries = _HOME_DIR_/_SANDBOXDIR_/data/msandbox-slow.log
672
#log = _HOME_DIR_/_SANDBOXDIR_/data/msandbox.log
677
'USING' => <<'USING_SCRIPT',
678
Currently using _INSTALL_VERSION_ with basedir _BASEDIR_
682
'grants.mysql' => <<'GRANTS_MYSQL',
685
set password=password('_DBPASSWORD_');
686
grant all on *.* to _DBUSER_ identified by '_DBPASSWORD_';
687
delete from user where password='';
688
delete from db where user='';
693
'load_grants.sh' => << 'LOAD_GRANTS_SCRIPT',
696
SBDIR=_HOME_DIR_/_SANDBOXDIR_
697
echo "source $SBDIR/grants.mysql" | $SBDIR/use -u root --password=
700
'my.sh' => <<'MY_SCRIPT',
706
echo "syntax my sql{dump|binlog|admin} arguments"
710
SBDIR=_HOME_DIR_/_SANDBOXDIR_
712
export LD_LIBRARY_PATH=$BASEDIR/lib:$BASEDIR/lib/mysql:$LD_LIBRARY_PATH
713
export DYLD_LIBRARY_PATH=$BASEDIR/lib:$BASEDIR/lib/mysql:$DYLD_LIBRARY_PATH
714
MYSQL=$BASEDIR/bin/mysql
719
MYSQLCMD="$BASEDIR/bin/my$SUFFIX"
723
$MYSQLCMD --defaults-file=$SBDIR/my.sandbox.cnf "$@"
725
echo "$MYSQLCMD not found "
729
'proxy_start.sh' => <<'PROXY_START_SCRIPT',
733
PROXY_BIN=/usr/local/sbin/mysql-proxy
736
$PROXY_BIN --proxy-backend-addresses=$HOST:_SERVERPORT_ "$@"
740
'change_ports.sh' => <<'CHANGE_PORTS_SCRIPT',
744
OLD_PORT=_SERVERPORT_
748
echo "new port required"
754
if [ $OLD_PORT = $NEW_PORT ]
756
echo Old port and new port must be different.
760
PERL_SCRIPT1='BEGIN{$old=shift;$new=shift};'
761
PERL_SCRIPT2='s/sandbox$old/sandbox$new/g;'
762
PERL_SCRIPT3='s/\b$old\b/$new/'
763
PERL_SCRIPT="$PERL_SCRIPT1 $PERL_SCRIPT2 $PERL_SCRIPT3"
765
SCRIPTS1="start stop send_kill clear restart my.sandbox.cnf "
766
SCRIPTS2="load_grants my use current_options.conf $0"
767
SCRIPTS="$SCRIPTS1 $SCRIPTS2"
768
for SCRIPT in $SCRIPTS
770
perl -i.port.bak -pe "$PERL_SCRIPT" $OLD_PORT $NEW_PORT $SCRIPT
772
echo "($PWD) The old scripts have been saved as filename.port.bak"
776
'change_paths.sh' => <<'CHANGE_PATHS_SCRIPT',
781
OLD_SB_LOCATION=_HOME_DIR_/_SANDBOXDIR_
793
if [ $OLD_SB_LOCATION = $NEW_SB_LOCATION ]
795
echo Old location and new location must be different.
796
echo Move the sandbox to the new location and then run this script.
800
if [ ! -d "$NEW_SB_LOCATION" ]
802
echo "new location must be a directory"
806
PERL_SCRIPT1='BEGIN{$old=shift;$new=shift};'
807
PERL_SCRIPT2='s/\b$old\b/$new/g'
808
PERL_SCRIPT="$PERL_SCRIPT1 $PERL_SCRIPT2"
810
SCRIPTS1="start stop send_kill clear restart my.sandbox.cnf "
811
SCRIPTS2="load_grants my use current_options.conf $0"
812
SCRIPTS="$SCRIPTS1 $SCRIPTS2"
814
for SCRIPT in $SCRIPTS
816
perl -i.bak -pe "$PERL_SCRIPT" $OLD_SB_LOCATION $NEW_SB_LOCATION $SCRIPT
818
echo "($PWD) The old scripts have been saved as filename.path.bak"
820
'sandbox_action.pl' => <<'SANDBOX_ACTION_SCRIPT',
827
my $DEBUG = $MySQL::Sandbox::DEBUG;
830
or die "action required {use|start|stop|clear}\n";
831
$action =~/^(use|start|stop|clear|send_kill)$/
832
or die "action must be one of {use|start|stop|clear|send_kill}\n";
834
$sandboxdir =~ s{[^/]+$}{};
835
$sandboxdir =~ s{/$}{};
837
if ($action eq 'use' and !$command) {
838
die "action 'use' requires a command\n";
841
my @dirs = glob("$sandboxdir/*");
843
for my $dir (@dirs) {
845
if ($action eq "use") {
846
if ( -x "$dir/use_all" ) {
847
print "executing -- $dir/use_all $command\n" if $DEBUG;
848
system(qq($dir/use_all "$command"));
850
elsif ( -x "$dir/use" ) {
851
print "executing -- $dir/use $command\n" if $DEBUG;
852
system(qq(echo "$command" | $dir/use));
855
elsif ( -x "$dir/${action}_all") {
856
print "-- executing $dir/${action}_all\n" if $DEBUG;
857
system("$dir/${action}_all")
859
elsif ( -x "$dir/$action") {
860
print "-- executing $dir/$action\n" if $DEBUG;
861
system("$dir/$action")
866
SANDBOX_ACTION_SCRIPT
870
my $license_text = <<'LICENSE';
872
# Copyright (C) 2006,2007,2008 Giuseppe Maxia, MySQL AB
873
# Contacts: http://datacharmer.org
875
# This program is free software; you can redistribute it and/or modify
876
# it under the terms of the GNU General Public License as published by
877
# the Free Software Foundation; version 2 of the License
879
# This program is distributed in the hope that it will be useful,
880
# but WITHOUT ANY WARRANTY; without even the implied warranty of
881
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
882
# GNU General Public License for more details.
884
# You should have received a copy of the GNU General Public License
885
# along with this program; if not, write to the Free Software
886
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
890
return $license_text;
893
sub parse_options_low_level_make_sandbox {
894
return \%parse_options_low_level_make_sandbox;
897
sub parse_options_replication {
898
return \%parse_options_replication;
901
sub parse_options_many {
902
return \%parse_options_many;
905
sub parse_options_custom_many {
906
return \%parse_options_custom_many;
909
sub scripts_in_code {
910
return \%scripts_in_code;