| 1 |
#!/usr/bin/perl |
| 2 |
# msb |
| 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 MySQL::Sandbox qw(runs_as_root); |
| 21 |
|
| 22 |
my $DEBUG = $MySQL::Sandbox::DEBUG; |
| 23 |
|
| 24 |
runs_as_root(); |
| 25 |
|
| 26 |
my $msb = MySQL::Sandbox->new(); |
| 27 |
|
| 28 |
my $version = shift |
| 29 |
or get_help(); |
| 30 |
|
| 31 |
my $group_prefix = ''; |
| 32 |
my $tarball = ''; |
| 33 |
|
| 34 |
if ( $version =~ /^[rm]$/) { |
| 35 |
$group_prefix = $version; |
| 36 |
$version = shift |
| 37 |
or get_help(); |
| 38 |
} |
| 39 |
|
| 40 |
$ENV{SANDBOX_HOME} = $ENV{SANDBOX_HOME} || "$ENV{HOME}/sandboxes"; |
| 41 |
#unless ( -d $ENV{SANDBOX_HOME} ) { |
| 42 |
# die "\$SANDBOX_HOME not found at $ENV{SANDBOX_HOME}\n"; |
| 43 |
#} |
| 44 |
|
| 45 |
$version =~ s/\s*//g; |
| 46 |
|
| 47 |
if ($version =~ /(\d)\.(\d)\.(\d+).*tar\.gz$/) { |
| 48 |
$tarball = $version; |
| 49 |
$version = "$group_prefix$1.$2.$3"; |
| 50 |
unless (-f $tarball) { |
| 51 |
die "file not found $tarball\n"; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( $version =~ /^(\d)\.(\d)\.(\d+)$/ ) { # install single |
| 56 |
install_and_restart('single', $version, "$1$2$3"); |
| 57 |
} |
| 58 |
elsif ( $version =~ /^(r|m)(\d)\.(\d)\.(\d+)$/ ) { # install group |
| 59 |
my $group = $1; |
| 60 |
my $simple_version = "$2$3$4"; |
| 61 |
my %groupsb = ( |
| 62 |
r => 'replication', |
| 63 |
m => 'multiple', |
| 64 |
); |
| 65 |
install_and_restart( $groupsb{$group}, $version, $simple_version); |
| 66 |
} |
| 67 |
|
| 68 |
if ($version =~ /^r(\d+)$/i) { # replication |
| 69 |
open_replication($1); |
| 70 |
} |
| 71 |
elsif ($version =~ /^m(\d+)$/i) { # multiple |
| 72 |
open_multi($1); |
| 73 |
} |
| 74 |
elsif ($version =~ /^\d+$/) { # |
| 75 |
open_single($version); |
| 76 |
} |
| 77 |
elsif (-d "$ENV{SANDBOX_HOME}/$version") { |
| 78 |
open_single($version, "nosearch"); |
| 79 |
} |
| 80 |
else { |
| 81 |
# this should fail when the directory is not found |
| 82 |
} |
| 83 |
|
| 84 |
sub find_sb { |
| 85 |
my ($prefix, $version) = @_; |
| 86 |
my @dirs = glob ("$ENV{SANDBOX_HOME}/$prefix*"); |
| 87 |
$prefix =~ s/_//g; |
| 88 |
for my $dir (@dirs) { |
| 89 |
my $simple_dir = $dir; |
| 90 |
$simple_dir =~ s{.*/}{}; |
| 91 |
$simple_dir =~ s/_//g; |
| 92 |
if ("$prefix$version" eq $simple_dir) { |
| 93 |
return $dir; |
| 94 |
} |
| 95 |
} |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
sub open_single { |
| 100 |
my ($version, $nosearch) = @_; |
| 101 |
my $sb_dir = find_sb('msb', $version); |
| 102 |
if ($nosearch) { |
| 103 |
$sb_dir= "$ENV{SANDBOX_HOME}/$version"; |
| 104 |
} |
| 105 |
else { |
| 106 |
$sb_dir = find_sb('msb', $version); |
| 107 |
} |
| 108 |
if ($sb_dir) { |
| 109 |
my ($pid) = glob("$sb_dir/data/*.pid"); |
| 110 |
unless ($pid) { |
| 111 |
system "$sb_dir/start"; |
| 112 |
} |
| 113 |
exec "$sb_dir/use", @ARGV; |
| 114 |
} |
| 115 |
else { |
| 116 |
die "can't find a matching sandbox for $version\n"; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub open_replication { |
| 121 |
my ($version) = @_; |
| 122 |
my $sb_dir = find_sb('rsandbox', $version); |
| 123 |
# print "replication ($sb_dir)\n"; |
| 124 |
if ($sb_dir) { |
| 125 |
my ($pid) = glob("$sb_dir/master/data/*.pid"); |
| 126 |
unless ($pid) { |
| 127 |
system "$sb_dir/start_all"; |
| 128 |
} |
| 129 |
my $item = 'm'; |
| 130 |
if ($ARGV[0] =~ /^\d+$/ ) { |
| 131 |
my $node = shift @ARGV; |
| 132 |
$item = "s$node"; |
| 133 |
} |
| 134 |
exec "$sb_dir/$item", @ARGV; |
| 135 |
} |
| 136 |
else { |
| 137 |
die "can't find a matching sandbox for $version\n"; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
sub open_multi { |
| 142 |
my ($version) = @_; |
| 143 |
my $sb_dir = find_sb('multi_msb', $version); |
| 144 |
if ($sb_dir) { |
| 145 |
my ($pid) = glob("$sb_dir/node1/data/*.pid"); |
| 146 |
unless ($pid) { |
| 147 |
system "$sb_dir/start_all"; |
| 148 |
} |
| 149 |
my $node = 1; |
| 150 |
if ($ARGV[0] =~ /^\d+$/ ) { |
| 151 |
$node = shift @ARGV; |
| 152 |
} |
| 153 |
exec "$sb_dir/n$node", @ARGV; |
| 154 |
} |
| 155 |
else { |
| 156 |
die "can't find a matching sandbox for $version\n"; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
sub get_help { |
| 161 |
my ($msg) = @_; |
| 162 |
if ($msg) { |
| 163 |
print "**** $msg\n"; |
| 164 |
} |
| 165 |
print $msb->credits(); |
| 166 |
print "MySQL Sandbox shortcut\n", |
| 167 |
"Invokes a single or group sandbox easily.\n", |
| 168 |
"(It will start the sandbox if it is not active).\n", |
| 169 |
"\n", |
| 170 |
"Usage: msb [r|m]version [node] [options]\n", |
| 171 |
#"\n", |
| 172 |
"\tr -> replication\tm -> multiple\n", |
| 173 |
"version could be a plain number to open a sandbox\n", |
| 174 |
"or a dotted number, to install it if it doesn't exist\n", |
| 175 |
"or the full path to a tarball\n", |
| 176 |
"\t[node] is used for r|m\n", |
| 177 |
"\t[options] are what you would pass to the sandbox app\n"; |
| 178 |
print "Examples:\n", |
| 179 |
"msb 5135\n", |
| 180 |
"# runs \$SANDBOX_HOME/msb_5_1_35/use\n", |
| 181 |
"\n", |
| 182 |
"msb 5.1.35\n", |
| 183 |
"# runs make_sandbox 5.1.35 and \$SANDBOX_HOME/msb_5_1_35/use\n", |
| 184 |
"\n", |
| 185 |
"msb r5135\n", |
| 186 |
"# runs \$SANDBOX_HOME/rsandbox_5_1_35/m\n", |
| 187 |
"\n", |
| 188 |
"msb r5.1.35\n", |
| 189 |
"# runs make_replication_sandbox 5.1.35 and \$SANDBOX_HOME/msb_5_1_35/use\n", |
| 190 |
"\n"; |
| 191 |
exit 1; |
| 192 |
} |
| 193 |
|
| 194 |
sub install_and_restart { |
| 195 |
my ($application, $version, $simple_version) = @_; |
| 196 |
my $cmd; |
| 197 |
my $prefix = ''; |
| 198 |
if ($version =~ /^([rm])/) { |
| 199 |
$prefix = $1; |
| 200 |
$version =~ s/^[rm]//; |
| 201 |
} |
| 202 |
my $uversion = $version; |
| 203 |
$uversion =~ s/\./_/g; |
| 204 |
my $run_installer = 1; |
| 205 |
if ($application eq 'single') { |
| 206 |
$cmd = 'make_sandbox ' . ($tarball || $version) . ' -- --no_confirm'; |
| 207 |
if ( -d "$ENV{SANDBOX_HOME}/msb_$uversion") { |
| 208 |
$run_installer = 0; |
| 209 |
} |
| 210 |
} |
| 211 |
elsif ($application eq 'replication') { |
| 212 |
$cmd = 'make_replication_sandbox ' . ($tarball || $version); |
| 213 |
if ( -d "$ENV{SANDBOX_HOME}/rsandbox_$uversion") { |
| 214 |
$run_installer = 0; |
| 215 |
} |
| 216 |
} |
| 217 |
elsif ($application eq 'multiple') { |
| 218 |
$cmd = 'make_multiple_sandbox ' . ($tarball || $version); |
| 219 |
if ( -d "$ENV{SANDBOX_HOME}/multi_msb_$uversion") { |
| 220 |
$run_installer = 0; |
| 221 |
} |
| 222 |
} |
| 223 |
else { |
| 224 |
die "unknown applicationn $application\n"; |
| 225 |
} |
| 226 |
if ($run_installer) { |
| 227 |
print "EXECUTING $cmd\n" if $DEBUG; |
| 228 |
my $result = system($cmd); |
| 229 |
if ($result) { |
| 230 |
die "installation error ($!)\n"; |
| 231 |
} |
| 232 |
} |
| 233 |
exec $0, "$prefix$simple_version", @ARGV; |
| 234 |
} |