Debian Codegraph @

bin/sbtool

raw
1 #!/usr/bin/perl
2 # sbtool
3 # The MySQL Sandbox
4 # Copyright (C) 2006-2018 Giuseppe Maxia
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 use strict;
19 use warnings;
20 use Carp;
21
22 use English qw( -no_match_vars );
23 use Data::Dumper;
24 use Getopt::Long qw(:config no_ignore_case );
25 use File::Copy qw/cp/;
26 use File::Find;
27 use MySQL::Sandbox qw(
28 runs_as_root
29 is_a_sandbox
30 is_sandbox_running
31 get_sandbox_params
32 get_ports
33 get_ranges
34 get_option_file_contents
35 sbinstr
36 deprecation_notice
37 get_sb_info);
38
39 use MySQL::Sandbox::Scripts;
40
41 runs_as_root();
42
43 my $DEBUG = $MySQL::Sandbox::DEBUG;
44
45 my $sandbox_options_file = $MySQL::Sandbox::sandbox_options_file;
46 # my $sandbox_current_options = $MySQL::Sandbox::sandbox_current_options;
47
48 my %supported_operations = %MySQL::Sandbox::Scripts::sbtool_supported_operations;
49 # ports => 'lists ports used by the Sandbox',
50 # range => 'finds N consecutive ports not yet used by the Sandbox',
51 # info => 'returns configuration options from a Sandbox',
52 # tree => 'creates a replication tree',
53 # copy => 'copies data from one Sandbox to another',
54 # move => 'moves a Sandbox to a different location',
55 # port => 'Changes a Sandbox port',
56 # delete => 'removes a sandbox completely',
57 # preserve => 'makes a sandbox permanent',
58 # unpreserve => 'makes a sandbox not permanent',
59 # plugin => adds plugin support to a sandbox (innodb,semisynch)
60 #);
61
62 my %supported_formats = %MySQL::Sandbox::Scripts::sbtool_supported_formats;
63 # text => 'plain text dump of requested information',
64 # perl => 'fully structured information in Perl code',
65 #);
66
67 my $msb = MySQL::Sandbox->new();
68
69 $msb->parse_options(MySQL::Sandbox::Scripts::parse_options_sbtool());
70 # my %{$msb->{options}} = map { $_, $msb->{parse_options}{$_}{value} } keys %{$msb->{parse_options}};
71
72 GetOptions( map { $msb->{parse_options}{$_}{parse}, \$msb->{options}{$_} }
73 keys %{$msb->{parse_options}} )
74 or get_help();
75
76 get_help() if $msb->{options}{help} or ! $msb->{options}{operation};
77
78 if ($msb->{options}{verbose}) {
79 $DEBUG = $msb->{options}{verbose} unless $DEBUG;
80 }
81
82 sub croak_die {
83 my ($msg) = @_;
84 if ($DEBUG) {
85 croak $msg;
86 }
87 else {
88 die $msg;
89 }
90 }
91
92
93 for my $op ( keys %{$msb->{parse_options}} ) {
94 if ( $msb->{parse_options}{$op}{accepted} ) {
95 my %accepted = %{ $msb->{parse_options}{$op}{accepted} };
96 for my $ak ( keys %accepted ) {
97 unless ( exists $accepted{ $msb->{options}{$op} } ) {
98 croak_die "invalid value '$msb->{options}{$op}' for option <$op>\n";
99 }
100 }
101 }
102 }
103
104 sbinstr( ' operation ' . $msb->{options}{operation} );
105 for my $dir (qw(source_dir dest_dir tree_dir search_path)) {
106 if ($msb->{options}{$dir}) {
107 $msb->{options}{$dir} =~ s/^\s*~/$ENV{HOME}/;
108 }
109 }
110
111 if ( $msb->{options}{operation} eq 'ports' ) {
112 get_ports(\%{$msb->{options}});
113 }
114 elsif ( $msb->{options}{operation} eq 'info' ) {
115 $msb->{options}{all_info} = 1;
116 $msb->{options}{format} = 'perl';
117 get_ports(\%{$msb->{options}});
118 }
119 elsif ( $msb->{options}{operation} eq 'range' ) {
120 get_ranges(\%{$msb->{options}})
121 }
122 elsif ( $msb->{options}{operation} eq 'tree' ) {
123 make_tree($msb->{options}{tree_dir} )
124 }
125 elsif ( $msb->{options}{operation} eq 'move' ) {
126 move_sandbox( $msb->{options}{source_dir}, $msb->{options}{dest_dir} );
127 }
128 elsif ( $msb->{options}{operation} eq 'port' ) {
129 unless ($msb->{options}{new_port}) {
130 croak_die "operation 'port' requires new_port option\n";
131 }
132 move_sandbox( $msb->{options}{source_dir}, $msb->{options}{source_dir}, 'alreday_moved' );
133 }
134 elsif ( $msb->{options}{operation} eq 'copy' ) {
135 copy_single_sandbox( $msb->{options}{source_dir}, $msb->{options}{dest_dir} );
136 }
137 elsif ( $msb->{options}{operation} eq 'preserve' ) {
138 preserve_sandbox($msb->{options}{source_dir})
139 }
140 elsif ( $msb->{options}{operation} eq 'unpreserve' ) {
141 unpreserve_sandbox($msb->{options}{source_dir})
142 }
143 elsif ( $msb->{options}{operation} eq 'delete' ) {
144 for my $opt (qw(dest_dir new_port only_used all_info )) {
145 if ( $msb->{options}{$opt} ) {
146 croak_die "option <$opt> is incompatible with the requested operation (delete)\n";
147 }
148 }
149 delete_sandbox( $msb->{options}{source_dir});
150 }
151 elsif ($msb->{options}{operation} eq 'plugin') {
152 add_plugin(
153 $msb->{options}{source_dir},
154 $msb->{options}{plugin},
155 $msb->{options}{plugin_file}
156 );
157 }
158 else {
159 croak_die "unsupported operation ($msb->{options}{operation})\n";
160 }
161
162 sub get_help {
163 my ($msg) = @_;
164 print MySQL::Sandbox::credits();
165 deprecation_notice();
166 if ($msg) {
167 print '*' x 50;
168 print "\n", $msg, "\n";
169 print '*' x 50;
170 print "\n";
171 }
172 print "usage: $PROGRAM_NAME [options] \n";
173 for my $op ( sort { $msb->{parse_options}{$a}{so} <=> $msb->{parse_options}{$b}{so} }
174 keys %{$msb->{parse_options}} )
175 {
176 my $val = $msb->{options}{$op};
177 my $parse = $msb->{parse_options}{$op}{parse};
178 my $expected = '-';
179 if ( $parse =~ s/=(.+)// ) {
180 $expected = $1;
181 }
182 my ( $short, $long ) = split /\|/, $parse;
183 unless ($long) {
184 $long = $short ;
185 $short = '';
186 }
187 printf "\t%s%-5s --%-15s (%s) <%s> - %s\n",
188 ($short? '-' : ' '),
189 $short,
190 $long,
191 $expected,
192 $val || '',
193 $msb->{parse_options}{$op}{help} || '';
194 if ( $msb->{parse_options}{$op}{accepted} ) {
195 my %accepted = %{ $msb->{parse_options}{$op}{accepted} };
196 for my $ao ( keys %accepted ) {
197 printf "\t\t %-10s %s\n", "'$ao'", $accepted{$ao};
198 }
199 }
200 }
201 exit 1;
202 }
203
204 sub make_tree {
205 my ($dir) = @_;
206 unless ( $dir) {
207 croak_die "you must set the directory using the 'tree_dir' option\n";
208 }
209 unless ( -d $dir ) {
210 croak_die "directory ($dir) does not exist\n";
211 }
212 my $master = $msb->{options}{master_node} || 1;
213 if ($msb->{options}{tree_nodes} ) {
214 my ($m, $mid, $leaf) = split /-/, $msb->{options}{tree_nodes};
215 if ($m) {
216 $master = $m;
217 }
218 else {
219 croak_die " master not defined in tree_nodes\n";
220 }
221 if ($mid) {
222 $msb->{options}{mid_nodes} = $mid;
223 }
224 else {
225 croak_die " middle nodes not defined in tree_nodes\n";
226 }
227 if ($leaf) {
228 $msb->{options}{leaf_nodes} = $leaf;
229 }
230 else {
231 croak_die " leaf nodes not defined in tree_nodes\n";
232 }
233
234 }
235 my @MID_NODES = split ' ', $msb->{options}{mid_nodes}
236 or croak_die "no mid nodes selected. Use the --mid_nodes option\n";
237 for my $mid (@MID_NODES) {
238 croak_die "middle nodes must be numeric" unless $mid =~ /^\d+$/;
239 }
240 my @LEAF_NODES = ();
241
242 my @chunks = split /\|/, $msb->{options}{leaf_nodes}
243 or croak_die "no leaf nodes selected. Use the --leaf_nodes option\n";
244 # print Data::Dumper->Dump([\@chunks], ['chunks']);
245 for my $c (@chunks) {
246 my @leaf = split ' ', $c;
247 croak_die "empty leaf node\n" unless @leaf;
248 for my $ln (@leaf) {
249 croak_die "leaf nodes must be numeric" unless $ln =~ /^\d+$/;
250 }
251 push @LEAF_NODES, [@leaf];
252 }
253
254 # print Data::Dumper->Dump([\@MID_NODES], ['MID_NODES']);
255 # print Data::Dumper->Dump([\@LEAF_NODES], ['LEAF_NODES']);
256 if ( @LEAF_NODES != @MID_NODES) {
257 croak_die "you must specify at least one leaf node for each middle node\n";
258 }
259
260 for my $node (( $master, @MID_NODES, map {@$_} @LEAF_NODES)) {
261 if ( ! -d "$dir/node$node" ) {
262 croak_die "node $node does not exist\n";
263 }
264 }
265
266 my ($N1INFO, $N1PORT)=get_node_info($dir, $master);
267
268 unless ($N1PORT) {
269 croak_die "can't get the port for node$master\n"
270 . "make sure the node is running\n";
271 }
272
273 my $CHANGE_MASTER_Q= "CHANGE MASTER TO master_host='127.0.0.1', "
274 . "master_user='msandbox', master_password='msandbox',"
275 . "master_port=";
276
277 print "$dir/use_all 'reset master'\n";
278 system "$dir/use_all 'reset master'";
279 print "$dir/stop_all\n";
280 system "$dir/stop_all";
281
282 print "node $master is master\n";
283 unless ( -e $N1INFO->{opt}{socket}) {
284 system "$dir/node$master/start"
285 }
286
287 system qq(echo "$dir/use_all 'stop slave'" > $dir/clear_all);
288 system qq(echo "$dir/use_all 'stop slave'" > $dir/stop_all);
289 system qq(echo "" > $dir/send_kill_all);
290 system qq(echo "$dir/node$master/start" > $dir/start_all);
291 for my $mid_node ( @MID_NODES ) {
292
293 my ($MID_NODE_INFO, $MID_NODE_PORT)=get_node_info($dir, $mid_node);
294 unless ( -e $MID_NODE_INFO->{opt}{socket}) {
295 system "$dir/node$mid_node/start";
296 }
297 my $HAS_UPDATES=`grep log_slave_updates $dir/node$mid_node/my.sandbox.cnf`;
298 my $HAS_REPORT=`grep "report-host" $dir/node$mid_node/my.sandbox.cnf`;
299
300 unless ($HAS_REPORT) {
301 system qq( echo "report-host=node$mid_node" >> $dir/node$mid_node/my.sandbox.cnf) ;
302 system qq( echo "report-port=$MID_NODE_PORT" >> $dir/node$mid_node/my.sandbox.cnf) ;
303 }
304 unless ( $HAS_UPDATES) {
305 print "enabling node $mid_node to relay updates\n";
306 system qq(echo "log_slave_updates" >> $dir/node$mid_node/my.sandbox.cnf) ;
307 system qq($dir/node$mid_node/restart) ;
308 }
309
310 system qq($dir/n$mid_node -e "stop slave") ;
311 system qq($dir/n$mid_node -e "$CHANGE_MASTER_Q $N1PORT") ;
312 system qq($dir/n$mid_node -e "start slave") ;
313 print " node $mid_node is slave of node $master\n";
314 my $l_nodes = shift @LEAF_NODES;
315 system qq(echo "$dir/node$mid_node/start" >> $dir/start_all);
316 for my $leaf_node (@$l_nodes) {
317 my ($LEAF_NODE_INFO, $LN_PORT) = get_node_info($dir, $leaf_node);
318 unless ( -e $LEAF_NODE_INFO->{opt}{socket}) {
319 system "$dir/node$leaf_node/start";
320 }
321 check_report($dir,$leaf_node, 1, $MID_NODE_PORT, $LN_PORT);
322 system qq($dir/n$leaf_node -e "stop slave");
323 system qq($dir/n$leaf_node -e "$CHANGE_MASTER_Q $MID_NODE_PORT");
324 system qq($dir/n$leaf_node -e "start slave");
325 print " node $leaf_node is slave of node $mid_node\n";
326 system qq(echo "$dir/node$leaf_node/stop" >> $dir/stop_all);
327 system qq(echo "$dir/node$leaf_node/clear" >> $dir/clear_all);
328 system qq(echo "$dir/node$leaf_node/send_kill" >> $dir/send_kill_all);
329 system qq(echo "$dir/node$leaf_node/start" >> $dir/start_all);
330 }
331 system qq(echo "$dir/node$mid_node/stop" >> $dir/stop_all);
332 system qq(echo "$dir/node$mid_node/clear" >> $dir/clear_all);
333 system qq(echo "$dir/node$mid_node/send_kill" >> $dir/send_kill_all);
334 }
335 system qq(echo "$dir/node$master/stop" >> $dir/stop_all);
336 system qq(echo "$dir/node$master/clear" >> $dir/clear_all);
337 system qq(echo "$dir/node$master/send_kill" >> $dir/send_kill_all);
338 }
339
340 sub check_report {
341 my ($dir, $node, $restart, $master_port, $this_node_port) = @_;
342 my $HAS_REPORT=`grep "report-host" $dir/node$node/my.sandbox.cnf`;
343 unless ($HAS_REPORT) {
344 system qq(echo "report-host=node$node" >> $dir/node$node/my.sandbox.cnf);
345 system qq(echo "report-port=$this_node_port" >> $dir/node$node/my.sandbox.cnf);
346 if ($restart) {
347 system qq($dir/node$node/restart);
348 }
349 }
350 }
351
352 sub get_node_info {
353 my ($dir, $node) = @_;
354 my ( $info ) = get_sandbox_params("$dir/node$node");
355 # print Dumper($ports, $all_info);
356 confess "can't read port for node $node" unless $info;
357 return ($info, $info->{opt}{port});
358 }
359 sub move_sandbox {
360 my ($source, $dest) = @_;
361 unless ($source) {
362 croak_die "Need a source directory (--source_dir)\n";
363 }
364 unless ($dest) {
365 croak_die "Need a destination directory (--dest_dir)\n";
366 }
367 $dest =~ s/^\s//;
368 $dest =~ s/\s*$//;
369 $source =~ s/^\s//;
370 $source =~ s/\s*$//;
371 $source =~ s/^\s*~/$ENV{HOME}/;
372 $dest =~ s/^\s*~/$ENV{HOME}/;
373 unless (($source =~ m{^/}) && ($dest =~ m{^/}) ) {
374 croak_die "Source and destination directories must be absolute paths.\n";
375 }
376 unless ( -d $source ) {
377 croak_die "directory $source does not exist\n";
378 }
379 if ( -x "$source/start") {
380 if (( $source eq $dest) and $msb->{options}{new_port}) {
381 move_single_sandbox($source, $dest, "already_moved");
382 }
383 else {
384 move_single_sandbox($source, $dest);
385 }
386 }
387 elsif ( -x "$source/start_all") {
388 move_multiple_sandbox($source, $dest);
389 }
390 else {
391 croak_die "directory $source does not seem to be a sandbox\n";
392 }
393 }
394
395 sub move_multiple_sandbox {
396 my ($old_dir, $new_dir) = @_;
397 unless ( -d $old_dir ) {
398 croak_die " directory $old_dir doesn't exist\n";
399 }
400 if ( -d $new_dir ) {
401 croak_die "directory $new_dir already exists\n";
402 }
403 if ( -x "$old_dir/stop_all" ) {
404 system "$old_dir/stop_all";
405 my $timeout = 5;
406 while ( file_exists($old_dir, '\.pid$')) {
407 $timeout--;
408 sleep 1;
409 }
410 }
411 else {
412 croak_die "$old_dir does not seem to contain a multiple sandbox\n";
413 }
414 my @old_subdirs = grep { -d $_ } glob("$old_dir/*/");
415 for my $od (@old_subdirs) {
416 unless ( -x "$od/change_paths" ) {
417 croak_die "directory $od is not a sandbox created with version 2.0.15+\n";
418 }
419 }
420 my $result = system "mv $old_dir $new_dir";
421 if ($result) {
422 croak_die "unable to move sandbox $old_dir to $new_dir ($OS_ERROR)\n";
423 }
424 my @new_subdirs = ();
425 for my $od (@old_subdirs) {
426 my $nd = $od;
427 if (($nd =~ s/$old_dir/$new_dir/ ) && ( -d $nd )) {
428 push @new_subdirs, [$od, $nd];
429 }
430 else {
431 # reverting to old directory
432 system "mv $new_dir $old_dir";
433 croak_die "can't move directory $od to $nd\n";
434 }
435 }
436 for my $sd (@new_subdirs) {
437 move_single_sandbox( $sd->[0], $sd->[1], "already_moved" );
438 }
439 chdir $new_dir;
440 unless ($old_dir =~ m{/$} ) {
441 $old_dir .= '/';
442 }
443 unless ($new_dir =~ m{/$} ) {
444 $new_dir .= '/';
445 }
446 my @nodes = glob("n[0-9]*");
447 my @slaves = glob("s[0-9]*");
448 my @scripts = qw(m start_all stop_all clear_all send_kill_all
449 check_slaves use_all initialize_slaves);
450 for my $script (( @nodes, @slaves, @scripts ) ) {
451 if ( -x $script ) {
452 system q(perl -i.bak -pe 'BEGIN{$old=shift;$new=shift};s/$old/$new/g')
453 . " $old_dir $new_dir $script " ;
454 }
455 }
456 }
457
458 sub file_exists {
459 my ($dir,$pattern) = @_;
460 my $file_count =0;
461 find (
462 sub {
463 $file_count++ if $File::Find::name =~ /$pattern/;
464 },
465 $dir
466 );
467 return $file_count;
468 }
469
470 sub move_single_sandbox {
471 my ($old_dir, $new_dir, $already_moved) = @_;
472 unless ( $already_moved) {
473 unless ( -d $old_dir ) {
474 croak_die " directory $old_dir doesn't exist\n";
475 }
476 }
477 if ( -d $new_dir && (! $already_moved ) ) {
478 croak_die "directory $new_dir already exists\n";
479 }
480 unless ( (-e "$old_dir/change_paths")
481 or ( $already_moved && -e "$new_dir/change_paths") ) {
482 croak_die "script 'change_paths' not found. "
483 . "Please get it from any Sandbox installed with version 2.0.15+\n";
484 }
485 if ($already_moved) {
486 if (is_sandbox_running($new_dir)) {
487 stop_sandbox($new_dir);
488 }
489 }
490 else {
491 stop_sandbox($old_dir);
492 my $result = system qq(mv $old_dir $new_dir) ;
493 if ($result) {
494 croak_die "unable to move sandbox $old_dir to $new_dir ($OS_ERROR)\n";
495 }
496 }
497 chdir $new_dir;
498 unless ( $old_dir eq $new_dir) {
499 system "./change_paths $old_dir $new_dir";
500 }
501 if ($msb->{options}{new_port}) {
502 unless ($msb->{options}{new_port} =~ /^\d+$/) {
503 croak_die "new port must be numerical ($msb->{options}{new_port})\n";
504 }
505 if (($msb->{options}{new_port} <= 1024) or ( $msb->{options}{new_port} > 32000)) {
506 croak_die "new port out of range ($msb->{options}{new_port}) - "
507 . "it must be between 1025 and 32000\n";
508 }
509 unless ( -e "$new_dir/change_ports" ) {
510 croak_die "script 'change_ports' not found. "
511 . "Please get it from any Sandbox installed with version 2.0.18+\n";
512 }
513 system "./change_ports $msb->{options}{new_port}";
514 }
515 }
516
517 sub stop_sandbox {
518 my ($sbdir) = @_;
519 my ($info) = get_sandbox_params($sbdir);
520 if ( -x "$sbdir/stop" ) {
521 system "$sbdir/stop";
522 my $timeout = 5;
523 while ($timeout && ( -e $info->{opt}{socket} )) {
524 $timeout--;
525 sleep 1;
526 }
527 if ( -e $info->{opt}{socket} ) {
528 croak_die "sandbox in $sbdir is still running. Unable to stop it\n";
529 }
530 }
531 else {
532 croak_die "$sbdir does not seem to contain a sandbox\n";
533 }
534 }
535
536 sub clone_sandbox_data {
537 my ($source_dir, $dest_dir) = @_;
538 croak_die "source directory missing\n" unless $source_dir;
539 croak_die "destination directory missing\n" unless $dest_dir;
540 $source_dir =~ s{/\s*$}{};
541 unless (-d $source_dir) {
542 croak_die "<$source_dir> is not a valid directory\n";
543 }
544 unless (-d $dest_dir) {
545 croak_die "<$dest_dir> is not a valid directory\n";
546 }
547 # checking if it is a valid data directory
548 unless (-d "$source_dir/mysql") {
549 croak_die "<$source_dir> is not a valid data directory\n"
550 }
551 my @pids = glob( "$source_dir/*.pid" );
552 if (@pids) {
553 croak_die "it seems that your sandbox is running. Please stop it and try again\n";
554 }
555 my @skip_files = map {qr/$_/} (
556 '^relay-log\.info$',
557 '\.err$',
558 '-bin\.\d+$',
559 '-bin\.index$',
560 '-relay-bin\.\d+$',
561 '-relay-bin\.index+$',
562 );
563 find (
564 {
565 no_chdir => 1,
566 follow => 1,
567 wanted => sub {
568 my $dir = $File::Find::dir;
569 my $fname = $File::Find::name;
570 $dir =~ s{/$}{};
571 $dir =~ s{.*/}{};
572 $fname =~ s{.*/}{};
573 # print "<$File::Find::name><$File::Find::dir> [$dir] [$fname]\n";
574 return if $dir =~ /^\./;
575 return if $File::Find::name eq $source_dir;
576 for my $skip (@skip_files) {
577 return if $fname =~ $skip ;
578 }
579 if ( -d $File::Find::name ) {
580 if ( -d "$dest_dir/$fname" ) {
581 return;
582 }
583 elsif ( -f "$dest_dir/$fname" ) {
584 croak_die "<$dest_dir/$fname> already exists and it is not a directory\n";
585 }
586 print_debug( "creating $dest_dir/$fname\n");
587 my $result = mkdir "$dest_dir/$fname";
588 unless ($result) {
589 croak_die "error creating directory ($!)\n";
590 }
591 }
592 elsif ( -f $File::Find::name ) {
593 # print "$Find::File::dir eq $source_dir\n";
594 if ((! $File::Find::dir) or ($File::Find::dir eq $source_dir)) {
595 $dir = '';
596 }
597 print_debug( "$File::Find::name -> $dest_dir/$dir/$fname\n");
598 my $result = cp $File::Find::name, "$dest_dir/$dir/$fname";
599 unless ($result) {
600 croak_die "error copying file $!\n";
601 }
602 }
603 else {
604 croak_die "unhandled file $File::Find::name\n";
605 }
606 }
607 },
608 $source_dir
609 );
610 }
611
612 sub print_debug {
613 my ($msg, $level) = @_;
614 $level |= 1;
615 if ($DEBUG >= $level) {
616 print $msg;
617 }
618 }
619
620 sub copy_single_sandbox {
621 my ($source_dir, $dest_dir) = @_;
622 if ($msb->{options}{new_port}) {
623 croak_die "option 'new_port' is not supported with 'copy'\n";
624 }
625 unless ( $source_dir) {
626 croak_die " source directory missing\n";
627 }
628 unless ( -d $source_dir) {
629 croak_die " <$source_dir> not found\n";
630 }
631 unless ( $dest_dir) {
632 croak_die " destination directory missing\n";
633 }
634 unless ( -d $dest_dir) {
635 croak_die " destination directory <$dest_dir> not found\n";
636 }
637 my ($srunning, $ssboptions) = is_sandbox_running($source_dir);
638 my ($drunning, $dsboptions) = is_sandbox_running($dest_dir);
639 my ($source_version, $dest_version);
640 unless ($srunning) {
641 system "$source_dir/start";
642 unless ( -e $ssboptions->{opt}{'pid_file'} ) {
643 croak_die "unable to start source sandbox\n";
644 }
645 }
646 $source_version = `$source_dir/use -B -N -e "select version()"`;
647 system "$source_dir/stop";
648 if ( -e $ssboptions->{opt}{'pid_file'} ) {
649 system "$source_dir/send_kill";
650 }
651 unless ($drunning) {
652 system "$dest_dir/start";
653 unless ( -e $ssboptions->{opt}{'pid_file'} ) {
654 croak_die "unable to start destination sandbox\n";
655 }
656 }
657 $dest_version = `$dest_dir/use -B -N -e "select version()"`;
658 system "$dest_dir/stop";
659 if ( -e $dsboptions->{opt}{'pid_file'} ) {
660 system "$dest_dir/send_kill";
661 }
662 if (substr($source_version, 0,3) ne substr($dest_version,0,3)) {
663 croak_die "can't copy from $source_dir to $dest_dir. Not the same major version\n";
664 }
665 clone_sandbox_data( "$source_dir/data", "$dest_dir/data");
666 }
667
668 sub delete_sandbox {
669 my ($source) = @_;
670 unless ($source) {
671 croak_die "Need a source directory (--source_dir)\n";
672 }
673 $source =~ s/^\s//;
674 $source =~ s/\s*$//;
675 $source =~ s{/$}{};
676 $ENV{SANDBOX_HOME} =~ s{/$}{};
677 $source =~ s/^\s*~/$ENV{HOME}/;
678 unless ($source =~ m{^/} ) {
679 croak_die "Source directory must be an absolute path.\n";
680 }
681 if ($source eq $ENV{SANDBOX_HOME} ) {
682 croak_die "Source directory must not be the whole sandbox home ($ENV{SANDBOX_HOME}.)\n";
683 }
684 unless ( -d $source ) {
685 croak_die "directory $source does not exist\n";
686 }
687 my $cmd1 = undef;
688 my $cmd2 = undef;
689 if (( -e "$source/no_clear" ) or ( -e "$source/no_clear_all")) {
690 print "<no_clear> script found.\nDirectory <$source> can't be deleted. It's a permanent sandbox.\n";
691 exit 0;
692 }
693
694 $OS_ERROR = undef;
695 $CHILD_ERROR = 0;
696 if ( (-x "$source/clear") && (-x "$source/stop" )) {
697 $cmd1 = "$source/stop";
698 $cmd2 = "$source/clear";
699 }
700 elsif ( (-x "$source/clear_all") && (-x "$source/stop_all")) {
701 $cmd1 = "$source/stop_all";
702 $cmd2 = "$source/clear_all";
703 }
704 else {
705 my @sandbox_subdirectories = grep { -d $_ } glob("$source/*");
706 unless (@sandbox_subdirectories)
707 {
708 croak_die "Directory $source does not seem to be a sandbox\n";
709 }
710 print "# Incomplete sandbox found at <$source> - Attempting to remove subdirectories\n";
711 for my $subdir (@sandbox_subdirectories)
712 {
713 print "# Deleting $subdir \n";
714 delete_sandbox($subdir);
715 }
716 system("rm -rf $source");
717 if ($CHILD_ERROR or $OS_ERROR)
718 {
719 croak_die "error deleting sandbox at $source\n ($CHILD_ERROR - $OS_ERROR)";
720 }
721 print "# Upper directory at <$source> has been removed \n";
722 return ;
723 }
724 print "$cmd1\n" if $msb->{options}{verbose};
725 $OS_ERROR = undef;
726 $CHILD_ERROR = 0;
727 system($cmd1);
728 if ($CHILD_ERROR or $OS_ERROR)
729 {
730 croak_die "error stopping sandbox at $source\n ($CHILD_ERROR - $OS_ERROR)";
731 }
732 print "$cmd2\n" if $msb->{options}{verbose};
733 system($cmd2);
734 if ($CHILD_ERROR or $OS_ERROR) {
735 croak_die "error clearing sandbox at $source\n ($CHILD_ERROR - $OS_ERROR)";
736 }
737 system("rm -rf $source");
738 if ($CHILD_ERROR or $OS_ERROR)
739 {
740 croak_die "error deleting sandbox at $source\n ($CHILD_ERROR - $OS_ERROR)";
741 }
742 print "sandbox at <$source> has been removed\n";
743 }
744
745 sub preserve_sandbox {
746 my ($source) = @_;
747 unless ($source) {
748 croak_die "Need a source directory (--source_dir)\n";
749 }
750 $source =~ s/^\s//;
751 $source =~ s/\s*$//;
752 $source =~ s/^\s*~/$ENV{HOME}/;
753 unless ($source =~ m{^/} ) {
754 croak_die "Source directory must be an absolute path.\n";
755 }
756 unless ( -d $source ) {
757 croak_die "directory $source does not exist\n";
758 }
759 if (( -e "$source/no_clear") or (-e "$source/no_clear_all" ) ) {
760 croak_die "<no_clear> script found.\nDirectory <$source> is already a permanent sandbox.\n";
761 }
762 my $old_clear = undef;
763 if ( -x "$source/clear") {
764 $old_clear = 'clear';
765 }
766 elsif ( -x "$source/clear_all") {
767 $old_clear = 'clear_all';
768 }
769 else {
770 croak_die "directory $source does not seem to be a sandbox\n";
771 }
772 if ($old_clear eq "clear_all") {
773 my @dirs = grep { (-d $_) && ( -e "$_/clear" ) } glob("$source/*");
774 for my $dir (@dirs) {
775 preserve_sandbox($dir);
776 }
777 }
778 chdir $source;
779 rename $old_clear, "no_$old_clear";
780 open my $CLEAR, q{>}, $old_clear
781 or croak_die "can't create $old_clear";
782 print $CLEAR qq(echo "This sandbox is permanent."\n);
783 print $CLEAR qq(echo "The '$old_clear' command has been disabled."\n);
784 print $CLEAR qq(echo "The contents of the old '$old_clear' command are in the 'no_$old_clear' file"\n);
785 close $CLEAR;
786 chmod 0755, $old_clear;
787 chmod 0644, "no_$old_clear";
788 print "sandbox at <$source> is now permanent\n";
789 }
790
791 sub unpreserve_sandbox {
792 my ($source) = @_;
793 unless ($source) {
794 croak_die "Need a source directory (--source_dir)\n";
795 }
796 $source =~ s/^\s//;
797 $source =~ s/\s*$//;
798 $source =~ s/^\s*~/$ENV{HOME}/;
799 unless ($source =~ m{^/} ) {
800 croak_die "Source directory must be an absolute path.\n";
801 }
802 unless ( -d $source ) {
803 croak_die "directory $source does not exist\n";
804 }
805 if (( ! -e "$source/no_clear") and (! -e "$source/no_clear_all" ) ) {
806 croak_die "<no_clear> script not found.\nDirectory <$source> is not a permanent sandbox.\n";
807 }
808 my $old_clear = undef;
809 if ( -f "$source/no_clear") {
810 $old_clear = 'no_clear';
811 }
812 elsif ( -f "$source/no_clear_all") {
813 $old_clear = 'no_clear_all';
814 }
815 else {
816 croak_die "directory $source does not seem to be a sandbox\n";
817 }
818 my $new_clear = $old_clear;
819 $new_clear =~ s/^no_//;
820 unless (-f "$source/$new_clear") {
821 croak_die "Can't find the '$new_clear' script. This may not be a preserved sandbox.\n";
822 }
823 if ($old_clear eq "no_clear_all") {
824 my @dirs = grep { (-d $_) && ( -e "$_/no_clear" ) } glob("$source/*");
825 for my $dir (@dirs) {
826 unpreserve_sandbox($dir);
827 }
828 }
829 chdir $source;
830 system "rm -f $new_clear";
831 rename $old_clear, $new_clear;
832 chmod 0755, $new_clear;
833 print "sandbox at <$source> is now NOT PERMANENT\n";
834 }
835
836 #
837 # ---- Plugin
838 #
839
840 sub add_plugin {
841 my ($source_dir, $plugin, $plugin_file) = @_;
842 unless ($source_dir) {
843 croak_die "Need a source directory (--source_dir)\n";
844 }
845 unless ($plugin) {
846 croak_die "Need a plugin (--plugin)\n";
847 }
848 $plugin =~ s/^\s//;
849 $plugin =~ s/\s*$//;
850 $source_dir =~ s/^\s//;
851 $source_dir =~ s/\s*$//;
852 $source_dir =~ s/\/$//;
853 $source_dir =~ s/^\s*~/$ENV{HOME}/;
854 unless ( -d $source_dir ) {
855 croak_die "directory $source_dir does not exist\n";
856 }
857 if ( exist_all($source_dir, [qw(start stop restart use clear)]) ) {
858 add_plugin_single($source_dir, $plugin, $plugin_file);
859 }
860 elsif ( exist_all($source_dir, [
861 qw(start_all stop_all restart_all use_all clear_all)]) ) {
862 add_plugin_multiple($source_dir, $plugin, $plugin_file);
863 }
864 else {
865 croak_die "directory $source_dir does not seem to be a sandbox\n";
866 }
867 }
868
869 #
870 # Finds the plugin configuration file (or uses an explicit one)
871 #
872 # Gets the contents of the plugin configuration file
873 #
874 sub get_plugin_conf {
875 my ($directory, $plugin, $plugin_conf_file) = @_;
876 if ($plugin_conf_file) {
877 unless ( -f $plugin_conf_file) {
878 croak_die "could not find $plugin_conf_file \n";
879 }
880 }
881 else {
882 $plugin_conf_file = "$directory/plugin.conf";
883 unless ( -f $plugin_conf_file) {
884 $plugin_conf_file = "$ENV{'SANDBOX_HOME'}/plugin.conf";
885 }
886 unless ( -f $plugin_conf_file) {
887 croak_die "could not find plugin.conf "
888 . "in $directory or in $ENV{'SANDBOX_HOME'}\n";
889 }
890 }
891 our $plugin_definition;
892 open my $FH, '<', $plugin_conf_file
893 or croak_die "can't open $plugin_conf_file ($!)\n";
894 my $contents ='';
895 while (my $line = <$FH>) {
896 $contents .= $line;
897 }
898 close $FH;
899 eval $contents;
900 if ($@) {
901 croak_die "error processing plugin configuration file $plugin_conf_file\n"
902 . "$@\n";
903 }
904 unless (exists $plugin_definition->{$plugin}) {
905 croak_die "the required plugin ($plugin) was not found in the "
906 . "configuration file ($plugin_conf_file)\n";
907 }
908 unless (
909 ref($plugin_definition->{$plugin})
910 &&
911 (ref( $plugin_definition->{$plugin}) eq 'HASH'))
912 {
913 croak_die "the definition of plugin '$plugin' must be a hash ref\n";
914 }
915 return ($plugin_conf_file, $plugin_definition->{$plugin});
916 }
917
918 #
919 # Returns true if a given option is set in a hash.
920 # The option could be written with dashes ('-')
921 # or underscores ('_')
922 #
923 sub is_option_set {
924 my ($href, $value) = @_;
925 my $value1 = $value;
926 my $value2 = $value;
927 $value1 =~ s/[_-]/-/g;
928 $value2 =~ s/[_-]/_/g;
929 # print "<$value1> <$value2>\n";exit;
930 return (defined($href->{$value1}) or defined($href->{$value2})) ;
931 }
932
933 sub add_plugin_single {
934 my ($directory, $plugin, $plugin_conf_file) = @_;
935 my $is_slave = 0;
936 my $is_master = 0;
937 my $options_from_file = get_option_file_contents("$directory/my.sandbox.cnf");
938 # print Dumper $options_from_file;
939
940 #
941 # it is a slave if slave related options
942 # are found in the options file
943 #
944 if (($options_from_file->{prompt} =~ /slave/)
945 &&
946 is_option_set( $options_from_file, 'report-host')
947 )
948 {
949 $is_slave =1;
950 }
951 #
952 # covers circular replication where a slave can also be a master
953 #
954 if ($is_slave
955 &&
956 is_option_set($options_from_file, 'auto_increment_increment')
957 &&
958 is_option_set($options_from_file, 'auto_increment_offset')
959 &&
960 is_option_set($options_from_file, 'replicate-same-server-id')
961 &&
962 is_option_set($options_from_file, 'log-slave-updates')
963 )
964 {
965 $is_master = 1;
966 }
967 #
968 # The normal case: a master is such when its directory
969 # is called *master/ and there is a prompt named "master"
970 # something in its options file
971 #
972 elsif ( ( $directory =~ m{master/?} )
973 &&
974 ( $options_from_file->{prompt} =~ /master/ )
975 )
976 {
977 $is_master =1;
978 }
979 my $plugin_conf;
980 #
981 # reads the plugin configuration file
982 #
983 ($plugin_conf_file, $plugin_conf) = get_plugin_conf(
984 $directory,
985 $plugin,
986 $plugin_conf_file);
987 #
988 # Looks for plugin libraries inside the options
989 #
990 my %plugin_libs = ();
991 for my $mode (qw(all_servers master slave)) {
992 next unless defined $plugin_conf->{$mode};
993 for my $item (
994 qw(operation_sequence options_file sql_commands startup_file )) {
995 unless ($plugin_conf->{ $item }) {
996 $plugin_conf->{$item} = [];
997 }
998 unless (ref($plugin_conf->{$item}) eq 'ARRAY') {
999 croak_die "$item must be an array ref\n";
1000 }
1001 }
1002 for my $line ( @{$plugin_conf->{$mode}{options_file}},
1003 @{$plugin_conf->{$mode}{sql_commands}}) {
1004 while ( $line =~ /([-\w]+\.so)/g ) {
1005 $plugin_libs{ $1 }++;
1006 }
1007 }
1008 }
1009 #
1010 # looks for the plugin directory
1011 #
1012 my $basedir = get_basedir($directory);
1013 my $plugindir = "$basedir/lib/plugin";
1014 unless ( -d $plugindir ) {
1015 $plugindir = "$basedir/lib/mysql/plugin";
1016 croak_die "could not find $plugindir\n" unless -d $plugindir;
1017 }
1018 #
1019 # makes sure that the plugin libraries
1020 # mentioned in the configuration file exist
1021 #
1022 for my $lib (keys %plugin_libs) {
1023 unless ( -f "$plugindir/$lib" ) {
1024 croak_die "could not find plugin '$lib' in $plugindir\n";
1025 }
1026 }
1027 # All elements found. Now we can do the deed.
1028 #
1029 # Defines which installation modes to use
1030 #
1031 my @install_defs = ();
1032 if ($plugin_conf->{'all_servers'}) {
1033 push @install_defs, 'all_servers';
1034 }
1035 if ($is_master) {
1036 push @install_defs, 'master';
1037 }
1038 if ($is_slave) {
1039 push @install_defs, 'slave';
1040 }
1041 unless (@install_defs) {
1042 croak_die "No installation instructions found\n"
1043 . "You must define one or more of "
1044 . "'all_servers', 'master', or 'slave'\n";
1045 }
1046 #
1047 # Runs the installation parts
1048 #
1049 for my $inst_def (@install_defs) {
1050 next unless defined $plugin_conf->{$inst_def};
1051 my $opseq = $plugin_conf->{$inst_def}{operation_sequence};
1052 # print Dumper $plugin_conf;exit;
1053 #
1054 # Makes sure that the plugin configuration
1055 # contains a sequence of desired operations
1056 #
1057 unless ( defined $opseq ) {
1058 croak_die "plugin definition in $plugin_conf_file "
1059 . "($plugin, $inst_def) must "
1060 . "contain 'operation_sequence'\n";
1061 }
1062 #
1063 # Loops through the requested operations
1064 #
1065 for my $op (@$opseq) {
1066 #
1067 # If it is a script found in the sandbox, run it
1068 #
1069 if ( grep { $op eq $_} qw(start stop clear restart) ) {
1070 my $result = system "$directory/$op";
1071 croak_die "error executing $op\n" if $result;
1072 }
1073 #
1074 # Runs an update of the options file
1075 #
1076 elsif ( $op eq 'options_file' ) {
1077 #print "<<$plugindir>>\n";
1078 update_options_file(
1079 "$directory/my.sandbox.cnf",
1080 'mysqld',
1081 $plugin_conf->{$inst_def}{'options_file'},
1082 $plugindir
1083 );
1084 }
1085 #
1086 # Creates a startup file
1087 #
1088 elsif ( $op eq 'startup_file' ) {
1089 fill_startup_file(
1090 "$directory/data/startup.sql",
1091 $plugin_conf->{$inst_def}{'startup_file'}
1092 );
1093
1094 }
1095 #
1096 # Runs a batch of SQL commands
1097 #
1098 elsif ( $op eq 'sql_commands' ) {
1099 run_sql_commands ($directory,
1100 $plugin_conf->{$inst_def}{'sql_commands'}
1101 );
1102 }
1103 #
1104 # Nothing else is recognized.
1105 #
1106 else {
1107 croak_die "unrecognized command $op in operation_sequence\n";
1108 }
1109 }
1110 }
1111 }
1112
1113 sub run_sql_commands {
1114 my ($directory, $queries) = @_;
1115 unless ( -x "$directory/use" ) {
1116 croak_die "can't execute sql queries in $directory "
1117 . "('./use' script not found)\n";
1118 }
1119 my $tmp_file = "$directory/tmpqueries0";
1120 while (-f $tmp_file) {
1121 $tmp_file =~ s/(\d+)$/$1 + 1/e;
1122 }
1123 #
1124 # Creates a temporary files, filled with queries
1125 #
1126 open my $FH, '>', $tmp_file
1127 or croak_die "can't open $tmp_file";
1128 my $semicolon_found = 0;
1129 for my $query (@$queries) {
1130 print $FH $query;
1131 if ($query =~ /\s*;$/) {
1132 $semicolon_found =1;
1133 }
1134 }
1135 close $FH;
1136 if ($semicolon_found) {
1137 #
1138 # Runs the queries
1139 #
1140 my $result = system "$directory/use -vv -e 'source $tmp_file' ";
1141 unlink $tmp_file;
1142 if ($result) {
1143 croak_die "error running queries\n";
1144 }
1145 }
1146 else {
1147 unlink $tmp_file;
1148 croak_die "Could not find any semicolon in the SQL list.\n"
1149 . "Please add at least one semicolon at the end of the"
1150 . " last line\n";
1151 }
1152 }
1153
1154 sub update_options_file {
1155 my ( $fname, $section, $contents, $plugindir ) = @_;
1156 # print "<<* $plugindir>>\n";
1157 my @old_contents ;
1158 unless (-f $fname) {
1159 croak_die "can't find file $fname\n";
1160 }
1161 open my $FH, '<', $fname
1162 or croak_die "can't open $fname\n";
1163 @old_contents = <$FH>;
1164 close $FH;
1165 my $found_section = 0;
1166 my $found_plugindir = 0;
1167 #
1168 # Checks for the wanted section to be updated
1169 # And for already existing commands in the options file
1170 #
1171 for my $old_line (@old_contents) {
1172 chomp $old_line;
1173 if ($old_line =~ /\[$section\]/) {
1174 $found_section =1;
1175 }
1176 for my $new_line (@$contents) {
1177 chomp $new_line;
1178 if ($old_line =~ /^\s*(plugin[_-]dir)\s*=\s*(.*)/) {
1179 my $key = $1;
1180 my $old_plugindir = $2;
1181 $old_plugindir =~ s/\s*$//;
1182 $old_plugindir =~ s/\/$//;
1183 $plugindir =~ s/\s*$//;
1184 $plugindir =~ s/\/$//;
1185 if ($old_plugindir ne $plugindir) {
1186 $old_line = "$key = $plugindir";
1187 }
1188 $found_plugindir = 1;
1189 next;
1190 }
1191 if ($old_line eq $new_line) {
1192 croak_die "option file already contains the line <$old_line>\n";
1193 }
1194 }
1195 }
1196 unless ($found_section) {
1197 croak_die "could not find section [$section] in $fname\n";
1198 }
1199 rename $fname, "$fname.bak"
1200 or croak_die "could not rename $fname to $fname.bak ($!)\n";
1201 open $FH, '>', $fname
1202 or croak_die "can't create $fname\n";
1203 #
1204 # If there was no plugin_dir in the old file
1205 # the we add it to the new options
1206 #
1207 unless ($found_plugindir && $plugindir) {
1208 unshift @$contents, "plugin_dir = $plugindir";
1209 }
1210 my $in_wanted_section = 0;
1211 #
1212 # Writes the updated options file
1213 #
1214 for my $old_line (@old_contents) {
1215 if ($old_line =~ /^\s*\[$section\]/) {
1216 $in_wanted_section =1;
1217 }
1218 #
1219 # If a new section starts,
1220 # the new options are inserted
1221 #
1222 elsif ($old_line =~ /^\s*\[/) {
1223 if ($in_wanted_section) {
1224 $in_wanted_section = 0;
1225 insert_new_options ($FH, $contents);
1226 }
1227 }
1228 print $FH $old_line, "\n";
1229 }
1230 #
1231 # If we reached the end of the file without
1232 # new sections, it means that we are still in the
1233 # wanted section and we add the new options here.
1234 if ($in_wanted_section) {
1235 $in_wanted_section =0;
1236 insert_new_options($FH, $contents);
1237 }
1238 close $FH;
1239 }
1240
1241 sub insert_new_options {
1242 my ($FH, $contents) = @_;
1243 print $FH "#\n# Lines added on ", scalar(localtime), "\n#\n";
1244 for my $nl (@$contents) {
1245 print $FH $nl, "\n";
1246 }
1247 print $FH "#\n# --- END \n#\n";
1248 }
1249
1250 sub fill_startup_file {
1251 my ( $fname, $contents ) = @_;
1252 my @old_contents ;
1253 if (-f $fname) {
1254 open my $FH, '<', $fname
1255 or croak_die "can't open $fname\n";
1256 @old_contents = <$FH>;
1257 close $FH;
1258 for my $old_line (@old_contents) {
1259 chomp $old_line;
1260 for my $new_line (@$contents) {
1261 chomp $new_line;
1262 if ($old_line eq $new_line) {
1263 croak_die "startup file already contains the line <$old_line>\n";
1264 }
1265 }
1266 }
1267 rename $fname, "$fname.bak"
1268 or croak_die "could not rename $fname to $fname.bak ($!)\n";
1269 }
1270 open my $FH, '>>', $fname
1271 or croak_die "can't create $fname\n";
1272 insert_new_options($FH, $contents);
1273 close $FH;
1274 }
1275
1276 sub add_plugin_multiple {
1277 my ($directory, $plugin, $plugin_file) = @_;
1278 my @dirs = grep { -d $_ } glob( "$directory/*/" ) ;
1279 my $result = system "$directory/stop_all";
1280 croak_die "error running stop_all in $directory\n" if $result;
1281 for my $dir (@dirs) {
1282 if ( exist_all($dir, [qw(start stop restart use clear)]) ) {
1283 print "Installing <$plugin> in <$dir>\n";
1284 add_plugin_single($dir, $plugin, $plugin_file);
1285 }
1286 else {
1287 print STDERR "WARNING: directory $dir is not a sandbox\n";
1288 }
1289 }
1290 }
1291
1292 sub get_basedir {
1293 my ($dir) = @_;
1294 unless ( -f "$dir/start" ) {
1295 croak_die "can't find file 'start' in $dir\n";
1296 }
1297 open my $FH, '<', "$dir/start"
1298 or croak_die "can't open $dir/start ($!)\n";
1299 my $basedir = undef;
1300 while ( (!$basedir) && (my $line =<$FH>)) {
1301 chomp $line;
1302 if ($line =~ /^BASEDIR=(\S+)/) {
1303 $basedir = $1;
1304 $basedir =~ s/^['" ]//;
1305 $basedir =~ s{['"/ ]+$}{};
1306 }
1307 }
1308 close $FH;
1309 croak_die "could not find BASEDIR in $dir/start\n" unless $basedir;
1310 return $basedir;
1311 }
1312
1313 sub exist_all {
1314 my ($directory, $files) = @_;
1315 for my $file ( @$files ) {
1316 unless ( -x "$directory/$file" ) {
1317 return 0;
1318 }
1319 }
1320 return 1;
1321 }
1322
1323