~giuseppe-maxia/mysql-sandbox/mysql-sandbox-3

« back to all changes in this revision

Viewing changes to lib/MySQL/Sandbox/Scripts.pm

  • Committer: Giuseppe Maxia
  • Date: 2009-03-29 09:38:55 UTC
  • Revision ID: g.maxia@gmail.com-20090329093855-uie8syy1r12x6brj
- Preparation for version 3.0
- refactoring code to use with ExtUtilis::MakeMaker and install a proper Perl module 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package MySQL::Sandbox::Scripts;
 
2
use strict;
 
3
use warnings;
 
4
use MySQL::Sandbox;
 
5
 
 
6
require Exporter;
 
7
 
 
8
our @ISA = qw/Exporter/;
 
9
 
 
10
our @EXPORT_OK = qw( scripts_in_code);
 
11
our @EXPORT = @EXPORT_OK;
 
12
 
 
13
our @MANIFEST = (
 
14
'clear.sh',
 
15
'my.sandbox.cnf',
 
16
'start.sh',
 
17
'restart.sh',
 
18
'stop.sh',
 
19
'send_kill.sh',
 
20
'load_grants.sh',
 
21
'use.sh',
 
22
'proxy_start.sh',
 
23
'my.sh',
 
24
'change_paths.sh',
 
25
'change_ports.sh',
 
26
#'COPYING',
 
27
#'README',
 
28
'USING',
 
29
'grants.mysql',
 
30
# 'current_options.conf',
 
31
'# INSTALL FILES',
 
32
# 'sandbox',
 
33
##'make_sandbox',
 
34
##'low_level_make_sandbox',
 
35
##'make_replication_sandbox',
 
36
##'make_multiple_sandbox',
 
37
##'make_multiple_custom_sandbox',
 
38
##'sbtool',
 
39
'sandbox_action.pl',
 
40
#'tests/test_sandbox.pl',
 
41
#'tests/README',
 
42
#'sandbox.conf',
 
43
##'Changelog',
 
44
 
 
45
);
 
46
 
 
47
sub manifest {
 
48
    return @MANIFEST;
 
49
}
 
50
my %parse_options_low_level_make_sandbox = (
 
51
    upper_directory        => {
 
52
                                value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},       
 
53
                                parse => 'upper_directory=s',         
 
54
                                so    =>  10,
 
55
                                help  => [
 
56
                                            "The directory that will contain the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
 
57
                                         ]
 
58
                            },
 
59
 
 
60
    sandbox_directory     => {
 
61
                                value => 'msb',    
 
62
                                parse => 'd|sandbox_directory=s',      
 
63
                                so    =>  20,
 
64
                                help  => [
 
65
                                            'Where to install the sandbox, under home-directory'
 
66
                                         ] 
 
67
                             },
 
68
    sandbox_port          => {
 
69
                                value => 3310,               
 
70
                                parse => 'P|sandbox_port=i',           
 
71
                                so    =>  30,
 
72
                                help  => [
 
73
                                            'The port number to use for the sandbox server.', 
 
74
                                            '(Default: 3310)',
 
75
                                         ]
 
76
                            },
 
77
    datadir_from          => {
 
78
                                value => 'script',          
 
79
                                parse => 'datadir_from=s' ,            
 
80
                                so    =>  40,
 
81
                                help  => [
 
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',
 
86
                                            '          ( DEPRECATED )',
 
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', 
 
90
                                            '(Default: script)',
 
91
                                         ]
 
92
                             },
 
93
    install_version       => {
 
94
                                value => '',              
 
95
                                parse => 'i|install_version=s',        
 
96
                                so    =>  50,
 
97
                                help  => [
 
98
                                            'Which version to install (3.23, 4.0, 4.1, 5.0 or 5.1) default: none'
 
99
                                         ]
 
100
                            },
 
101
   basedir               => {
 
102
                                value => '/usr/local/mysql', 
 
103
                                parse => 'b|basedir=s' ,               
 
104
                                so    =>  60,
 
105
                                help  => [
 
106
                                            'Base directory for MySQL (default: /usr/local/mysql)'
 
107
                                         ]
 
108
                            },
 
109
    my_file               => {
 
110
                                value => q{},                 
 
111
                                parse => 'm|my_file=s',                
 
112
                                so    =>  70,
 
113
                                help  => [
 
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)',
 
118
                                         ]
 
119
                            },
 
120
    conf_file             => {
 
121
                                value => q{},                 
 
122
                                parse => 'f|conf_file=s',              
 
123
                                so    =>  80,
 
124
                                help  =>  [
 
125
                                            'Configuration file containing options like the ones',
 
126
                                            'you can give on the command line (without dashes)',
 
127
                                          ]
 
128
                            },
 
129
    operating_system_user => {
 
130
                                value => $ENV{'USER'},       
 
131
                                parse => 'U|operating_system_user=s',  
 
132
                                so    =>  90,
 
133
                                help  => [
 
134
                                            'Operating system user (for mysql installation)',
 
135
                                            "default: \$USER ($ENV{USER})",
 
136
                                         ]
 
137
                            },
 
138
    db_user               => {
 
139
                                value => 'msandbox',      
 
140
                                parse => 'u|db_user=s'  ,              
 
141
                                so    => 100,
 
142
                                help  => [
 
143
                                            'user for global access to mysql (Default: sandbox)'
 
144
                                         ]
 
145
                            },
 
146
    db_password           => {
 
147
                                value => 'msandbox',      
 
148
                                parse => 'p|db_password=s'  ,              
 
149
                                so    => 110,
 
150
                                help  => [
 
151
                                            'password for global access to mysql (Default: sandbox)'
 
152
                                         ]
 
153
                            },
 
154
    my_clause             => {
 
155
                                value => '',      
 
156
                                parse => 'c|my_clause=s@', 
 
157
                                so    => 120,
 
158
                                help  => [
 
159
                                            'option to be inserted in a my.cnf file',
 
160
                                            'it may be used several times',
 
161
                                         ]
 
162
                            },
 
163
    prompt_prefix           => {
 
164
                                value => 'mysql',      
 
165
                                parse => 'prompt_prefix=s', 
 
166
                                so    => 130,
 
167
                                help  => [
 
168
                                            'prefix to use in CLI prompt (default: mysql)',
 
169
                                         ]
 
170
                            },
 
171
 
 
172
    prompt_body          => {
 
173
                                value => q/ [\h] {\u} (\d) > '/,      
 
174
                                parse => 'prompt_body=s', 
 
175
                                so    => 135,
 
176
                                help  => [
 
177
                                            'options to use in CLI prompt (default:  [\h] {\u} (\d) > )',
 
178
                                         ]
 
179
                            },
 
180
    force                 => {
 
181
                                value => 0,                  
 
182
                                parse => 'force',                      
 
183
                                so    => 140,
 
184
                                help  => [
 
185
                                            'Use this option if you want to overwrite existing directories',
 
186
                                            'and files during the installation. (Default: disabled)', 
 
187
                                         ] 
 
188
                            },
 
189
    no_ver_after_name     => {
 
190
                                value => 0,                  
 
191
                                parse => 'no_ver_after_name',                  
 
192
                                so    => 150,
 
193
                                help  => [
 
194
                                            'Do not add version number after sandbox directory name (default: disabled)'
 
195
                                         ] 
 
196
                            },
 
197
    verbose               => {
 
198
                                value => 0,                  
 
199
                                parse => 'v|verbose',                  
 
200
                                so    => 160,
 
201
                                help  => [
 
202
                                            'Use this option to see installation progress (default: disabled)'
 
203
                                         ] 
 
204
                            },
 
205
   load_grants           => {
 
206
                                value => 0,                  
 
207
                                parse => 'load_grants',                  
 
208
                                so    => 170,
 
209
                                help  => [
 
210
                                            'Loads the predefined grants from a SQL file.',
 
211
                                            'Useful when installing from script.',
 
212
                                            '(default: disabled)'
 
213
                                         ] 
 
214
                            },
 
215
   no_load_grants         => {
 
216
                                value => 0,                  
 
217
                                parse => 'no_load_grants',                  
 
218
                                so    => 175,
 
219
                                help  => [
 
220
                                            'Does not loads the predefined grants from a SQL file.',
 
221
                                            '(default: disabled)'
 
222
                                         ] 
 
223
                            },
 
224
 
 
225
 
 
226
    interactive           => {
 
227
                                value => 0,                  
 
228
                                parse => 't|interactive',                  
 
229
                                so    => 180,
 
230
                                help  => [
 
231
                                            'Use this option to be guided through the installation process (default: disabled)'
 
232
                                         ] 
 
233
                            },
 
234
    more_options          => {value => q{},                 parse => undef,                        so => 20},
 
235
    help                  => {value => q{},                 parse => 'help',                       so => 25},
 
236
    no_confirm            => {
 
237
                                value => 0, 
 
238
                                parse => 'no_confirm',
 
239
                                so    => 190,
 
240
                                help  => [
 
241
                                            'suppress the confirmation request from user',
 
242
                                         ],
 
243
                            },
 
244
);
 
245
 
 
246
my %parse_options_replication = (
 
247
    upper_directory        => {
 
248
                                value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},       
 
249
                                parse => 'upper_directory=s',         
 
250
                                so    =>  10,
 
251
                                help  => [
 
252
                                            "The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
 
253
                                         ]
 
254
                            },
 
255
    replication_directory  => {
 
256
                                value => undef,
 
257
                                parse => 'r|replication_directory=s',      
 
258
                                so    =>  20,
 
259
                                help  => [
 
260
                                            'Where to install the sandbox replication system, under upper-directory',
 
261
                                            'default: (rsandbox)'
 
262
                                         ] 
 
263
                             },
 
264
    server_version  => {
 
265
                                value => undef,    
 
266
                                parse => 'server_version=s',      
 
267
                                so    =>  30,
 
268
                                help  => [
 
269
                                            'which version to install'
 
270
                                         ] 
 
271
                             },
 
272
    sandbox_base_port      => {
 
273
                                value => undef,               
 
274
                                parse => 'sandbox_base_port=i',           
 
275
                                so    =>  40,
 
276
                                help  => [
 
277
                                            'The port number to use for the sandbox replication system.', 
 
278
                                            '(Default: 11000  + version )',
 
279
                                         ]
 
280
                            },
 
281
 
 
282
    how_many_slaves      => {
 
283
                                value => 2,               
 
284
                                parse => 'how_many_nodes|how_many_slaves=i',           
 
285
                                so    =>  50,
 
286
                                help  => [
 
287
                                            'The number of slaves to create.', 
 
288
                                            '(Default: 2)',
 
289
                                         ]
 
290
                            },
 
291
 
 
292
    topology               => {
 
293
                                value => 'standard',                  
 
294
                                parse => 't|topology=s',                  
 
295
                                so    => 55,
 
296
                                help  => [
 
297
                                            'Sets a replication topology.',
 
298
                                            'Available: {standard|circular} (default: standard)'
 
299
                                         ] 
 
300
                            },
 
301
    circular               => {
 
302
                                value => 0,
 
303
                                parse => 'circular=i',                  
 
304
                                so    => 56,
 
305
                                help  => [
 
306
                                            'Sets circular replication with N nodes.',
 
307
                                            '(default: 0)'
 
308
                                         ] 
 
309
                            },
 
310
 
 
311
 
 
312
    master_master        => {
 
313
                                value => 0,                  
 
314
                                parse => 'master_master',                  
 
315
                                so    => 57,
 
316
                                help  => [
 
317
                                            'set exactly two nodes in circular replication'
 
318
                                         ] 
 
319
                            },
 
320
 
 
321
    verbose               => {
 
322
                                value => 0,                  
 
323
                                parse => 'v|verbose',                  
 
324
                                so    => 60,
 
325
                                help  => [
 
326
                                            'Use this option to see installation progress (default: disabled)'
 
327
                                         ] 
 
328
                            },
 
329
    help               => {
 
330
                                value => 0,                  
 
331
                                parse => 'help',                  
 
332
                                so    => 70,
 
333
                                help  => [
 
334
                                            'show this help (default: disabled)'
 
335
                                         ] 
 
336
                            },
 
337
);
 
338
 
 
339
 
 
340
my %parse_options_many = (
 
341
    upper_directory        => {
 
342
                                value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},       
 
343
                                parse => 'upper_directory=s',         
 
344
                                so    =>  10,
 
345
                                help  => [
 
346
                                            "The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
 
347
                                         ]
 
348
                            },
 
349
    group_directory  => {
 
350
                                value => undef,
 
351
                                parse => 'r|group_directory=s',      
 
352
                                so    =>  20,
 
353
                                help  => [
 
354
                                            'Where to install the sandbox group, under home-directory',
 
355
                                            'default: (multi_msb)'
 
356
                                         ] 
 
357
                             },
 
358
    server_version  => {
 
359
                                value => undef,    
 
360
                                parse => 'server_version=s',      
 
361
                                so    =>  30,
 
362
                                help  => [
 
363
                                            'which version to install'
 
364
                                         ] 
 
365
                             },
 
366
    sandbox_base_port      => {
 
367
                                value => undef,               
 
368
                                parse => 'sandbox_base_port=i',           
 
369
                                so    =>  40,
 
370
                                help  => [
 
371
                                            'The port number to use for the sandbox multiple system.', 
 
372
                                            '(Default: 7000  + version )',
 
373
                                         ]
 
374
                            },
 
375
 
 
376
    how_many_nodes      => {
 
377
                                value => 3,               
 
378
                                parse => 'how_many_nodes=i',           
 
379
                                so    =>  50,
 
380
                                help  => [
 
381
                                            'The number of nodes to create.', 
 
382
                                            '(Default: 3)',
 
383
                                         ]
 
384
                            },
 
385
    master_master        => {
 
386
                                value => 0,                  
 
387
                                parse => 'master_master',                  
 
388
                                so    => 65,
 
389
                                help  => [
 
390
                                            'set exactly two nodes in circular replication'
 
391
                                         ] 
 
392
                            },
 
393
 
 
394
    circular             => {
 
395
                                value => 0,                  
 
396
                                parse => 'circular',                  
 
397
                                so    => 60,
 
398
                                help  => [
 
399
                                            'set the nodes in circular replication'
 
400
                                         ] 
 
401
                            },
 
402
    verbose               => {
 
403
                                value => 0,                  
 
404
                                parse => 'v|verbose',                  
 
405
                                so    => 70,
 
406
                                help  => [
 
407
                                            'Use this option to see installation progress (default: disabled)'
 
408
                                         ] 
 
409
                            },
 
410
 
 
411
    help               => {
 
412
                                value => 0,                  
 
413
                                parse => 'help',                  
 
414
                                so    => 80,
 
415
                                help  => [
 
416
                                            'show this help (default: disabled)'
 
417
                                         ] 
 
418
                            },
 
419
); 
 
420
 
 
421
my %parse_options_custom_many = (
 
422
    upper_directory        => {
 
423
                                value => $ENV{'SANDBOX_HOME'} || $ENV{'HOME'},       
 
424
                                parse => 'upper_directory=s',         
 
425
                                so    =>  10,
 
426
                                help  => [
 
427
                                            "The directory containing the sandbox. (default: \$SANDBOX_HOME ($ENV{SANDBOX_HOME}))"
 
428
                                         ]
 
429
                            },
 
430
    group_directory  => {
 
431
                                value => undef,
 
432
                                parse => 'r|group_directory=s',      
 
433
                                so    =>  20,
 
434
                                help  => [
 
435
                                            'Where to install the sandbox group, under home-directory',
 
436
                                            'default: (multi_msb)'
 
437
                                         ] 
 
438
                             },
 
439
    server_version  => {
 
440
                                value => undef,    
 
441
                                parse => 'server_version=s',      
 
442
                                so    =>  30,
 
443
                                help  => [
 
444
                                            'which version to install'
 
445
                                         ] 
 
446
                             },
 
447
    sandbox_base_port      => {
 
448
                                value => undef,               
 
449
                                parse => 'sandbox_base_port=i',           
 
450
                                so    =>  40,
 
451
                                help  => [
 
452
                                            'The port number to use for the sandbox custom system.', 
 
453
                                            '(Default: 5000  + version )',
 
454
                                         ]
 
455
                            },
 
456
 
 
457
    verbose               => {
 
458
                                value => 0,                  
 
459
                                parse => 'v|verbose',                  
 
460
                                so    => 60,
 
461
                                help  => [
 
462
                                            'Use this option to see installation progress (default: disabled)'
 
463
                                         ] 
 
464
                            },
 
465
    help               => {
 
466
                                value => 0,                  
 
467
                                parse => 'help',                  
 
468
                                so    => 70,
 
469
                                help  => [
 
470
                                            'show this help (default: disabled)'
 
471
                                         ] 
 
472
                            },
 
473
); 
 
474
 
 
475
my %scripts_in_code = (
 
476
    'start.sh' => <<'START_SCRIPT',
 
477
#!_BINBASH_
 
478
__LICENSE__
 
479
BASEDIR='_BASEDIR_'
 
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"
 
485
TIMEOUT=20
 
486
if [ -f $PIDFILE ]
 
487
then
 
488
    echo "sandbox server already started (found pid file $PIDFILE)"
 
489
else
 
490
    CURDIR=`pwd`
 
491
    cd $BASEDIR
 
492
    if [ "$DEBUG" = "" ]
 
493
    then
 
494
        $MYSQLD_SAFE --defaults-file=$SBDIR/my.sandbox.cnf $@ > /dev/null 2>&1 &
 
495
    else
 
496
        $MYSQLD_SAFE --defaults-file=$SBDIR/my.sandbox.cnf $@ > "$SBDIR/start.log" 2>&1 &
 
497
    fi
 
498
    cd $CURDIR
 
499
    ATTEMPTS=1
 
500
    while [ ! -f $PIDFILE ] 
 
501
    do
 
502
        ATTEMPTS=$(( $ATTEMPTS + 1 ))
 
503
        echo -n "."
 
504
        if [ $ATTEMPTS = $TIMEOUT ]
 
505
        then
 
506
            break
 
507
        fi
 
508
        sleep 1
 
509
    done
 
510
fi
 
511
 
 
512
if [ -f $PIDFILE ]
 
513
then
 
514
    echo " sandbox server started"
 
515
else
 
516
    echo " sandbox server not started yet"
 
517
fi
 
518
 
 
519
START_SCRIPT
 
520
'restart.sh' => <<'RESTART_SCRIPT',
 
521
#!_BINBASH_
 
522
__LICENSE__
 
523
 
 
524
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
 
525
$SBDIR/stop
 
526
$SBDIR/start $@
 
527
 
 
528
RESTART_SCRIPT
 
529
    'stop.sh' => <<'STOP_SCRIPT',
 
530
#!_BINBASH_
 
531
__LICENSE__
 
532
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
 
533
BASEDIR=_BASEDIR_
 
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"
 
538
 
 
539
if [ -f $PIDFILE ]
 
540
then
 
541
    if [ -f $SBDIR/data/master.info ]
 
542
    then
 
543
        echo "stop slave" | $SBDIR/use -u root
 
544
    fi
 
545
    # echo "$MYSQL_ADMIN --defaults-file=$SBDIR/my.sandbox.cnf shutdown"
 
546
    $MYSQL_ADMIN --defaults-file=$SBDIR/my.sandbox.cnf shutdown
 
547
    sleep 1
 
548
fi
 
549
if [ -f $PIDFILE ]
 
550
then
 
551
    # use the send_kill script if the server is not responsive
 
552
    $SBDIR/send_kill
 
553
fi
 
554
STOP_SCRIPT
 
555
 
 
556
    'send_kill.sh' => <<'KILL_SCRIPT',
 
557
#!_BINBASH_
 
558
__LICENSE__
 
559
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
 
560
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
 
561
TIMEOUT=10
 
562
if [ -f $PIDFILE ]
 
563
then
 
564
    MYPID=`cat $PIDFILE`
 
565
    echo "Attempting normal termination --- kill -15 $MYPID"
 
566
    kill -15 $MYPID
 
567
    # give it a chance to exit peacefully
 
568
    ATTEMPTS=1
 
569
    while [ -f $PIDFILE ]
 
570
    do
 
571
        ATTEMPTS=$(( $ATTEMPTS + 1 ))
 
572
        if [ $ATTEMPTS = $TIMEOUT ]
 
573
        then
 
574
            break
 
575
        fi
 
576
        sleep 1
 
577
    done
 
578
    if [ -f $PIDFILE ]
 
579
    then
 
580
        echo "SERVER UNRESPONSIVE --- kill -9 $MYPID"
 
581
        kill -9 $MYPID
 
582
        rm -f $PIDFILE
 
583
    fi
 
584
fi
 
585
 
 
586
KILL_SCRIPT
 
587
 
 
588
    'use.sh' => <<'USE_SCRIPT',
 
589
#!_BINBASH_
 
590
__LICENSE__
 
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_"
 
594
BASEDIR=_BASEDIR_
 
595
MYSQL="$BASEDIR/bin/mysql"
 
596
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
 
597
if [ -f $PIDFILE ]
 
598
then
 
599
    $MYSQL --defaults-file=$SBDIR/my.sandbox.cnf $MYCLIENT_OPTIONS "$@"
 
600
#else
 
601
#    echo "PID file $PIDFILE not found "
 
602
fi
 
603
USE_SCRIPT
 
604
 
 
605
    'clear.sh' => <<'CLEAR_SCRIPT', 
 
606
#!_BINBASH_
 
607
__LICENSE__
 
608
SBDIR="_HOME_DIR_/_SANDBOXDIR_"
 
609
cd $SBDIR
 
610
PIDFILE="$SBDIR/data/mysql_sandbox_SERVERPORT_.pid"
 
611
#
 
612
# attempt to drop databases gracefully
 
613
#
 
614
if [ -f $PIDFILE ]
 
615
then
 
616
    for D in `echo "show databases " | ./use -B -N | grep -v "^mysql$" | grep -v "^information_schema$"` 
 
617
    do
 
618
        echo 'drop database `'$D'`' | ./use 
 
619
    done
 
620
    VERSION=`./use -N -B  -e 'select version()'`
 
621
    if [ `perl -le 'print $ARGV[0] ge "5.1" ? "1" : "0" ' "$VERSION"` = "1" ]
 
622
    then
 
623
        ./use -e "truncate mysql.general_log"
 
624
        ./use -e "truncate mysql.slow_log"
 
625
    fi
 
626
fi
 
627
 
 
628
./stop
 
629
#./send_kill
 
630
rm -f data/`hostname`*
 
631
rm -f data/log.0*
 
632
rm -f data/*.log
 
633
rm -f data/falcon*
 
634
rm -f data/mysql-bin*
 
635
rm -f data/*relay-bin*
 
636
rm -f data/ib*
 
637
rm -f data/*.info
 
638
rm -f data/*.err
 
639
rm -f data/*.err-old
 
640
# rm -rf data/test/*
 
641
 
 
642
#
 
643
# remove all databases if any
 
644
#
 
645
for D in `ls -d data/*/ | grep -w -v mysql ` 
 
646
do
 
647
    rm -rf $D
 
648
done
 
649
mkdir data/test
 
650
 
 
651
CLEAR_SCRIPT
 
652
    'my.sandbox.cnf'  => <<'MY_SANDBOX_SCRIPT',
 
653
__LICENSE__
 
654
[mysql]
 
655
_MYSQL_PROMPT_
 
656
#
 
657
 
 
658
[client]
 
659
user            = _DBUSER_
 
660
password        = _DBPASSWORD_
 
661
port            = _SERVERPORT_
 
662
socket          = /tmp/mysql_sandbox_SERVERPORT_.sock
 
663
 
 
664
[mysqld]
 
665
user                            = _OSUSER_
 
666
port                            = _SERVERPORT_
 
667
socket                          = /tmp/mysql_sandbox_SERVERPORT_.sock
 
668
basedir                         = _BASEDIR_
 
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
 
673
_MORE_OPTIONS_
 
674
 
 
675
MY_SANDBOX_SCRIPT
 
676
 
 
677
    'USING' => <<'USING_SCRIPT',
 
678
Currently using _INSTALL_VERSION_ with basedir _BASEDIR_
 
679
 
 
680
USING_SCRIPT
 
681
 
 
682
    'grants.mysql'  => <<'GRANTS_MYSQL',
 
683
 
 
684
use 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='';
 
689
flush privileges;
 
690
 
 
691
GRANTS_MYSQL
 
692
 
 
693
    'load_grants.sh' => << 'LOAD_GRANTS_SCRIPT',
 
694
#!_BINBASH_
 
695
__LICENSE__
 
696
SBDIR=_HOME_DIR_/_SANDBOXDIR_
 
697
echo "source $SBDIR/grants.mysql" | $SBDIR/use -u root --password= 
 
698
LOAD_GRANTS_SCRIPT
 
699
 
 
700
    'my.sh'    => <<'MY_SCRIPT',
 
701
#!_BINBASH_
 
702
__LICENSE__
 
703
 
 
704
if [ "$1" = "" ]
 
705
then
 
706
    echo "syntax my sql{dump|binlog|admin} arguments"
 
707
    exit
 
708
fi
 
709
 
 
710
SBDIR=_HOME_DIR_/_SANDBOXDIR_
 
711
BASEDIR=_BASEDIR_
 
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
 
715
 
 
716
SUFFIX=$1
 
717
shift
 
718
 
 
719
MYSQLCMD="$BASEDIR/bin/my$SUFFIX"
 
720
 
 
721
if [ -f $MYSQLCMD ]
 
722
then
 
723
    $MYSQLCMD --defaults-file=$SBDIR/my.sandbox.cnf "$@"
 
724
else
 
725
    echo "$MYSQLCMD not found "
 
726
fi
 
727
 
 
728
MY_SCRIPT
 
729
    'proxy_start.sh'    => <<'PROXY_START_SCRIPT',
 
730
#!_BINBASH_
 
731
__LICENSE__
 
732
 
 
733
PROXY_BIN=/usr/local/sbin/mysql-proxy
 
734
HOST='127.0.0.1'
 
735
 
 
736
$PROXY_BIN --proxy-backend-addresses=$HOST:_SERVERPORT_ "$@"
 
737
 
 
738
PROXY_START_SCRIPT
 
739
 
 
740
'change_ports.sh' => <<'CHANGE_PORTS_SCRIPT',
 
741
#!_BINBASH_
 
742
__LICENSE__
 
743
 
 
744
OLD_PORT=_SERVERPORT_
 
745
 
 
746
if [ "$1" = "" ]
 
747
then
 
748
    echo "new port required"
 
749
    exit
 
750
else
 
751
    NEW_PORT=$1
 
752
fi
 
753
 
 
754
if [ $OLD_PORT = $NEW_PORT ]
 
755
then
 
756
    echo Old port and new port must be different.
 
757
    exit
 
758
fi
 
759
 
 
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"
 
764
 
 
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
 
769
do
 
770
    perl -i.port.bak -pe "$PERL_SCRIPT" $OLD_PORT $NEW_PORT $SCRIPT
 
771
done
 
772
echo "($PWD) The old scripts have been saved as filename.port.bak"
 
773
 
 
774
CHANGE_PORTS_SCRIPT
 
775
 
 
776
    'change_paths.sh' => <<'CHANGE_PATHS_SCRIPT',
 
777
#!_BINBASH_
 
778
__LICENSE__
 
779
if [ "$1" = "" ]
 
780
then
 
781
    OLD_SB_LOCATION=_HOME_DIR_/_SANDBOXDIR_
 
782
else
 
783
    OLD_SB_LOCATION=$1
 
784
fi
 
785
 
 
786
if [ "$2" = "" ]
 
787
then
 
788
    NEW_SB_LOCATION=$PWD
 
789
else
 
790
    NEW_SB_LOCATION=$2
 
791
fi
 
792
 
 
793
if [ $OLD_SB_LOCATION = $NEW_SB_LOCATION ]
 
794
then
 
795
    echo Old location and new location must be different.
 
796
    echo Move the sandbox to the new location and then run this script.
 
797
    exit
 
798
fi
 
799
 
 
800
if [ ! -d "$NEW_SB_LOCATION" ]
 
801
then
 
802
    echo "new location must be a directory"
 
803
    exit
 
804
fi
 
805
 
 
806
PERL_SCRIPT1='BEGIN{$old=shift;$new=shift};'
 
807
PERL_SCRIPT2='s/\b$old\b/$new/g' 
 
808
PERL_SCRIPT="$PERL_SCRIPT1 $PERL_SCRIPT2"
 
809
 
 
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"
 
813
 
 
814
for SCRIPT in $SCRIPTS
 
815
do
 
816
    perl -i.bak -pe "$PERL_SCRIPT" $OLD_SB_LOCATION $NEW_SB_LOCATION $SCRIPT
 
817
done
 
818
echo "($PWD) The old scripts have been saved as filename.path.bak"
 
819
CHANGE_PATHS_SCRIPT
 
820
    'sandbox_action.pl' => <<'SANDBOX_ACTION_SCRIPT',
 
821
#!/usr/bin/perl
 
822
__LICENSE__
 
823
use strict;
 
824
use warnings;
 
825
use MySQL::Sandbox;
 
826
 
 
827
my $DEBUG = $MySQL::Sandbox::DEBUG;
 
828
 
 
829
my $action = shift
 
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";
 
833
my $sandboxdir = $0;
 
834
$sandboxdir =~ s{[^/]+$}{};
 
835
$sandboxdir =~ s{/$}{};
 
836
my $command = shift;
 
837
if ($action eq 'use' and !$command) {
 
838
    die "action 'use' requires a command\n";
 
839
}
 
840
 
 
841
my @dirs = glob("$sandboxdir/*");
 
842
 
 
843
for my $dir (@dirs) {
 
844
    if (-d $dir) {
 
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"));
 
849
            }           
 
850
            elsif ( -x "$dir/use" ) {
 
851
                print "executing -- $dir/use $command\n" if $DEBUG;
 
852
                system(qq(echo "$command" | $dir/use));
 
853
            }           
 
854
        } 
 
855
        elsif ( -x "$dir/${action}_all") {
 
856
            print "-- executing  $dir/${action}_all\n" if $DEBUG;
 
857
            system("$dir/${action}_all")
 
858
        }
 
859
        elsif ( -x "$dir/$action") {
 
860
            print "-- executing  $dir/$action\n" if $DEBUG;
 
861
            system("$dir/$action")
 
862
        }
 
863
    }
 
864
}
 
865
 
 
866
SANDBOX_ACTION_SCRIPT
 
867
 
 
868
);
 
869
 
 
870
my $license_text = <<'LICENSE';
 
871
#    The MySQL Sandbox
 
872
#    Copyright (C) 2006,2007,2008 Giuseppe Maxia, MySQL AB
 
873
#    Contacts: http://datacharmer.org
 
874
#
 
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
 
878
#
 
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.
 
883
#
 
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
 
887
LICENSE
 
888
 
 
889
sub license_text {
 
890
    return $license_text;
 
891
}
 
892
 
 
893
sub parse_options_low_level_make_sandbox {
 
894
    return \%parse_options_low_level_make_sandbox;
 
895
}
 
896
 
 
897
sub parse_options_replication {
 
898
    return \%parse_options_replication;
 
899
}
 
900
 
 
901
sub parse_options_many {
 
902
    return \%parse_options_many;
 
903
}
 
904
 
 
905
sub parse_options_custom_many {
 
906
    return \%parse_options_custom_many;
 
907
}
 
908
 
 
909
sub scripts_in_code {
 
910
    return \%scripts_in_code;
 
911
}
 
912
 
 
913
1;
 
914