| 1 |
#!/usr/bin/perl |
| 2 |
# low_level_make_sandbox |
| 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 English qw( -no_match_vars ); |
| 21 |
use Data::Dumper; |
| 22 |
use Getopt::Long qw(:config no_ignore_case ); |
| 23 |
use MySQL::Sandbox qw( find_safe_port_and_directory exists_in_path runs_as_root mylogin_cnf_exists use_env sbinstr fix_server_uuid greater_version split_version); |
| 24 |
use MySQL::Sandbox::Scripts; |
| 25 |
|
| 26 |
my $DEBUG = $MySQL::Sandbox::DEBUG; |
| 27 |
|
| 28 |
#my $install_dir; |
| 29 |
#$install_dir = $PROGRAM_NAME; |
| 30 |
#$install_dir =~ s{/\w+(\.pl)?$}{}; |
| 31 |
my $msb = MySQL::Sandbox->new(); |
| 32 |
|
| 33 |
runs_as_root(); |
| 34 |
mylogin_cnf_exists(); |
| 35 |
|
| 36 |
check_sandbox_dir(); |
| 37 |
check_tmp_dir(); |
| 38 |
my %scripts_in_code = %{ MySQL::Sandbox::Scripts::scripts_in_code() }; |
| 39 |
my $license_text = MySQL::Sandbox::Scripts::license_text(); |
| 40 |
|
| 41 |
$msb->parse_options(MySQL::Sandbox::Scripts::parse_options_low_level_make_sandbox()); |
| 42 |
# print Dumper $msb;exit; |
| 43 |
|
| 44 |
# my %{$msb->{options}} = map { $_ , $msb->{parse_options}{$_}{'value'}} keys %{$msb->{parse_options}}; |
| 45 |
my %default_values = %{ $msb->{options}}; |
| 46 |
my %user_values; |
| 47 |
#print Dumper \%{$msb->{options}}; exit; |
| 48 |
|
| 49 |
GetOptions ( |
| 50 |
map { $msb->{parse_options}{$_}{parse}, \$msb->{options}{$_} } |
| 51 |
grep { $msb->{parse_options}{$_}{parse}} keys %{$msb->{parse_options}} |
| 52 |
) or $msb->get_help(); |
| 53 |
|
| 54 |
# |
| 55 |
# We are not expecting bare arguments |
| 56 |
# |
| 57 |
if (@ARGV){ |
| 58 |
die "unexpected arguments <@ARGV>\n"; |
| 59 |
} |
| 60 |
|
| 61 |
$msb->get_help() if $msb->{options}{'help'}; |
| 62 |
|
| 63 |
# print Dumper(\%{$msb->{options}}, \@ARGV); exit; |
| 64 |
|
| 65 |
my $with_space=''; |
| 66 |
for my $option (qw(upper_directory basedir datadir)) |
| 67 |
{ |
| 68 |
next unless (defined $msb->{options}{$option}); |
| 69 |
if ($msb->{options}{$option} =~ / /) |
| 70 |
{ |
| 71 |
$with_space .= " $option"; |
| 72 |
} |
| 73 |
} |
| 74 |
if ($with_space) |
| 75 |
{ |
| 76 |
die "# Options <$with_space> contain space. Aborting\n"; |
| 77 |
} |
| 78 |
|
| 79 |
$msb->{options}{globaltmpdir} = $ENV{TMPDIR}; |
| 80 |
$msb->{options}{globaltmpdir} =~ s{/$}{}; |
| 81 |
|
| 82 |
|
| 83 |
for my $opt (keys %{$msb->{options}} ) { |
| 84 |
#unless (defined $default_values{$opt} and defined $msb->{options}{$opt} ) { |
| 85 |
# print Data::Dumper->Dump([ $default_values{$opt}, |
| 86 |
# $msb->{options}{$opt}, $opt], ['default','option', 'opt']); |
| 87 |
# die "undefined option\n"; |
| 88 |
#} |
| 89 |
# next unless $default_values{$opt}; |
| 90 |
if ((not defined $default_values{$opt}) |
| 91 |
or ($default_values{$opt} ne $msb->{options}{$opt} )) { |
| 92 |
$user_values{$opt} = $msb->{options}{$opt}; |
| 93 |
} |
| 94 |
} |
| 95 |
%default_values = (); |
| 96 |
|
| 97 |
$msb->{options}{'my_clause'} = [] unless $msb->{options}{'my_clause'} ; |
| 98 |
# print Dumper($msb->{options}{'my_clause'});exit; |
| 99 |
|
| 100 |
if ( $msb->{options}{'high_performance'}) |
| 101 |
{ |
| 102 |
my @perf_clauses = ( |
| 103 |
'innodb-thread-concurrency=0', |
| 104 |
'sync_binlog=0', |
| 105 |
'innodb-log-buffer-size=100M', |
| 106 |
# 'innodb-additional-mem-pool-size=50M', # deprecated (removed in MySQL 5.7.4) |
| 107 |
'max-connections=350', |
| 108 |
'max_allowed_packet=48M', |
| 109 |
'innodb_buffer_pool_size=512M', |
| 110 |
'innodb-log-file-size=50M', |
| 111 |
'innodb-flush-method=O_DIRECT' |
| 112 |
); |
| 113 |
|
| 114 |
for my $clause (@perf_clauses) |
| 115 |
{ |
| 116 |
unshift @{ $msb->{options}{'my_clause'}}, $clause; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ($msb->{options}{check_port} and (! $msb->{options}{no_check_port}) ) { |
| 121 |
|
| 122 |
( $msb->{options}{'sandbox_port'}, |
| 123 |
$msb->{options}{'sandbox_directory'}) = |
| 124 |
find_safe_port_and_directory( |
| 125 |
$msb->{options}{'sandbox_port'}, |
| 126 |
$msb->{options}{'sandbox_directory'}, |
| 127 |
$msb->{options}{'upper_directory'}) ; |
| 128 |
} |
| 129 |
|
| 130 |
if ($msb->{options}{'slaveof'} or $msb->{options}{'master'}) |
| 131 |
{ |
| 132 |
for my $clause (('log-bin=mysql-bin', 'server-id=' . $msb->{options}{sandbox_port}, 'relay-log=mysql-relay', 'relay-log-index=mysql-relay' )) |
| 133 |
{ |
| 134 |
unshift @{ $msb->{options}{'my_clause'}}, $clause; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if ($msb->{options}{plugin_mysqlx}) |
| 139 |
{ |
| 140 |
my $mysqlx_port=$msb->{options}{mysqlx_port}; |
| 141 |
if ($mysqlx_port < 1000) |
| 142 |
{ |
| 143 |
$mysqlx_port=$msb->{options}{sandbox_port} + 10_000; |
| 144 |
} |
| 145 |
$msb->{options}{mysqlx_port} = $mysqlx_port; |
| 146 |
push @{ $msb->{options}{'my_clause'} }, 'plugin_load=mysqlx:mysqlx.so'; |
| 147 |
push @{ $msb->{options}{'my_clause'} }, "mysqlx_port=$mysqlx_port"; |
| 148 |
} |
| 149 |
|
| 150 |
my $detected_mysql_version = qx($msb->{options}{basedir}/bin/mysql_config --version); |
| 151 |
if ($detected_mysql_version) |
| 152 |
{ |
| 153 |
chomp $detected_mysql_version; |
| 154 |
$msb->{options}{detected_mysql_version} = $detected_mysql_version; |
| 155 |
my ($major, $minor, $rev) = split_version($detected_mysql_version); |
| 156 |
$msb->{options}{mysql_version}{major} = $major; |
| 157 |
$msb->{options}{mysql_version}{minor} = $minor; |
| 158 |
$msb->{options}{mysql_version}{rev} = $rev; |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
die "# Can't get MySQL version from mysql_config\n"; |
| 163 |
} |
| 164 |
|
| 165 |
if ($msb->{options}{expose_dd_tables}) |
| 166 |
{ |
| 167 |
my $mysql_version = $msb->{options}{detected_mysql_version}; |
| 168 |
my ( $major, undef, undef) = split_version($mysql_version); |
| 169 |
if ( $major != '8') |
| 170 |
{ |
| 171 |
die "The option 'expose_dd_tables' can only be used with version 8\n"; |
| 172 |
} |
| 173 |
my $executable = sprintf '%s/bin/mysqld-debug', $msb->{options}{basedir} ; |
| 174 |
unless ( -x $executable) |
| 175 |
{ |
| 176 |
die "mysqld-debug not found in $msb->{options}{basedir}/bin\n" |
| 177 |
} |
| 178 |
$msb->{options}{custom_mysqld} = 'mysqld-debug'; |
| 179 |
unless ($msb->{options}{post_grants_sql}) |
| 180 |
{ |
| 181 |
$msb->{options}{post_grants_sql} = ''; |
| 182 |
} |
| 183 |
|
| 184 |
# In MySQL 8.0.2 the "hidden" column was changed from integer (0=visible, 1=hidden) |
| 185 |
# to an ENUM (System, Visible, SE, DDL) |
| 186 |
$msb->{options}{post_grants_sql} = |
| 187 |
"set persist debug='+d,skip_dd_table_access_check'; |
| 188 |
set \@col_type=(select c.type from mysql.columns c inner join mysql.tables t where t.id=table_id and t.name='tables' and c.name='hidden'); |
| 189 |
set \@visible=(if(\@col_type = 'MYSQL_TYPE_ENUM', 'Visible', '0')); |
| 190 |
set \@hidden=(if(\@col_type = 'MYSQL_TYPE_ENUM', 'System', '1')); |
| 191 |
create table sys.dd_hidden_tables select id, name, schema_id from mysql.tables where hidden=\@hidden; |
| 192 |
update mysql.tables set hidden=\@visible where hidden=\@hidden and schema_id = 1; |
| 193 |
" |
| 194 |
. $msb->{options}{post_grants_sql}; |
| 195 |
} |
| 196 |
|
| 197 |
if ($msb->{options}{'gtid'}) |
| 198 |
{ |
| 199 |
my $mysql_version = $msb->{options}{detected_mysql_version}; |
| 200 |
|
| 201 |
my $major = $msb->{options}{mysql_version}{major}; |
| 202 |
my $minor = $msb->{options}{mysql_version}{minor}; |
| 203 |
my $rev = $msb->{options}{mysql_version}{rev}; |
| 204 |
$mysql_version=sprintf('%d.%d.%d', $major,$minor, $rev); |
| 205 |
|
| 206 |
if ((greater_version($mysql_version, '5.6.9') ) && ($major < 10)) # exclude MariaDB from this feature |
| 207 |
{ |
| 208 |
|
| 209 |
my @clauses = ( "master-info-repository=table ", |
| 210 |
"relay-log-info-repository=table", |
| 211 |
"gtid_mode=ON", |
| 212 |
"enforce-gtid-consistency"); |
| 213 |
|
| 214 |
unless ( grep {/log.bin/} @{ $msb->{options}{'my_clause'} }) |
| 215 |
{ |
| 216 |
push @clauses, 'log-bin' ; |
| 217 |
} |
| 218 |
unless ( grep {/server.id/} @{ $msb->{options}{'my_clause'} }) |
| 219 |
{ |
| 220 |
push @clauses, 'server-id=' . $msb->{options}{sandbox_port} ; |
| 221 |
} |
| 222 |
unless (greater_version($mysql_version, '5.7.9')) |
| 223 |
{ |
| 224 |
push @clauses , "log-slave-updates"; |
| 225 |
} |
| 226 |
for my $clause (@clauses) |
| 227 |
{ |
| 228 |
unshift @{ $msb->{options}{'my_clause'}}, $clause; |
| 229 |
} |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
print "# enabling GTID is supported for MySQL 5.6.10 + - Skipping \n" |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
# my $mysql_version = qx($msb->{options}{basedir}/bin/mysql_config --version); |
| 238 |
|
| 239 |
if ($msb->{options}{'interactive'}) { |
| 240 |
$msb->{options}{no_confirm} = 0; |
| 241 |
select_interactively() if $msb->{options}{'interactive'}; |
| 242 |
} |
| 243 |
|
| 244 |
$DEBUG = 1 if $msb->{options}{'verbose'}; |
| 245 |
$msb->{options}{'verbose'} = 1 if $DEBUG; |
| 246 |
|
| 247 |
if ( -f "$ENV{'HOME'}/.msandboxrc") { |
| 248 |
if ( $msb->{options}{'conf_file'}) { |
| 249 |
# conf file given. Neet to concatenate default file and |
| 250 |
# rc file |
| 251 |
my $tmp_conf_file = "$ENV{TMPDIR}/msandboxrc.cnf"; |
| 252 |
my $result = system (qq( cat $ENV{'HOME'}/.msandboxrc $msb->{options}{'conf_file'} > $tmp_conf_file )); |
| 253 |
if ($result) { |
| 254 |
die "can't create $tmp_conf_file ($CHILD_ERROR)\n"; |
| 255 |
} |
| 256 |
$msb->{options}{'conf_file'} = $tmp_conf_file; |
| 257 |
} |
| 258 |
else { |
| 259 |
$msb->{options}{'conf_file'} = "$ENV{'HOME'}/.msandboxrc"; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if ( $msb->{options}{'conf_file'}) { |
| 264 |
open my $CONF, q{<}, $msb->{options}{'conf_file'} |
| 265 |
or die "error opening $msb->{options}{'conf_file'}. ($ERRNO)\n"; |
| 266 |
|
| 267 |
print "reading configuration file ($msb->{options}{'conf_file'})\n" if $DEBUG; |
| 268 |
|
| 269 |
while (<$CONF>) { |
| 270 |
next if / ^ \s* $ /x; |
| 271 |
next if / ^ \s* [#] /x; |
| 272 |
chomp; |
| 273 |
my ($key, $value); |
| 274 |
if ( /^ \s* (\S+) \s* = \s* (.+)?/x) { |
| 275 |
($key, $value) = ($1, $2); |
| 276 |
} |
| 277 |
elsif ( /^ \s* ( \S+ ) \s* $/x) { |
| 278 |
($key, $value) = ($1, ''); |
| 279 |
next unless $msb->{parse_options}{$key}; |
| 280 |
# print "++ $msb->{parse_options}{$key}{'value'} \n"; |
| 281 |
if ($msb->{parse_options}{$key}{'value'} =~ /^\d+$/) { |
| 282 |
$value = $msb->{parse_options}{$key}{'value'} ? 0 : 1 ; |
| 283 |
} |
| 284 |
} |
| 285 |
else { |
| 286 |
next; |
| 287 |
} |
| 288 |
# print ">>> $key ($value)\n"; |
| 289 |
if (exists $user_values{$key}) { |
| 290 |
# skipping file options if they have been entered in the command line |
| 291 |
# print "skipping option $key\n"; |
| 292 |
next; |
| 293 |
} |
| 294 |
elsif (exists $msb->{options}{$key} ) { |
| 295 |
$value = q{} unless defined $value; |
| 296 |
if ( $value =~ s/^\$//x ) { |
| 297 |
$value = $ENV{$value}; |
| 298 |
} |
| 299 |
if ( $msb->{parse_options}{ $key }{'parse'} =~ /\@/) { |
| 300 |
$msb->{options}{ $key } = [ split /\s*;\s*/, $value ]; |
| 301 |
} |
| 302 |
else { |
| 303 |
# print "assigning <$key> = '$value'\n"; |
| 304 |
$msb->{options}{$key} = $value; |
| 305 |
} |
| 306 |
} |
| 307 |
else { |
| 308 |
die "Option '$key' not recognized - File $msb->{options}{'conf_file'} - Line $.\n"; |
| 309 |
} |
| 310 |
} |
| 311 |
close $CONF; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
# |
| 316 |
# options to skip from the imported my.cnf file |
| 317 |
# |
| 318 |
my %skip_options = ( |
| 319 |
'user' => 1, |
| 320 |
'port' => 1, |
| 321 |
'socket' => 1, |
| 322 |
'datadir' => 1, |
| 323 |
'basedir' => 1, |
| 324 |
'tmpdir' => 1, |
| 325 |
'pid-file' => 1, |
| 326 |
'server-id' => 1, |
| 327 |
); |
| 328 |
|
| 329 |
# |
| 330 |
# placeholders used in the installation files |
| 331 |
# with their corresponding values in %{$msb->{options}} |
| 332 |
# |
| 333 |
my %replace_options = ( |
| 334 |
_INSTALL_VERSION_ => 'install_version', |
| 335 |
_DBUSER_ => 'db_user', |
| 336 |
_REMOTE_ACCESS_ => 'remote_access', |
| 337 |
_DBUSERRO_ => 'ro_user', |
| 338 |
_DBUSERRW_ => 'rw_user', |
| 339 |
_DBUSERREPL_ => 'repl_user', |
| 340 |
_DBPASSWORD_ => 'db_password', |
| 341 |
_DB_REPL_PASSWORD_ => 'repl_password', |
| 342 |
_OSUSER_ => 'operating_system_user', |
| 343 |
_BASEDIR_ => 'basedir', |
| 344 |
_TMPDIR_ => 'tmpdir', |
| 345 |
_GLOBALTMPDIR_ => 'globaltmpdir', |
| 346 |
_BINBASH_ => 'binbash', |
| 347 |
_BINPERL_ => 'binperl', |
| 348 |
_HOME_DIR_ => 'upper_directory', |
| 349 |
_SANDBOXDIR_ => 'sandbox_directory', |
| 350 |
_SERVERPORT_ => 'sandbox_port', |
| 351 |
_MYSQLXPORT_ => 'mysqlx_port', |
| 352 |
_MORE_OPTIONS_ => 'more_options', |
| 353 |
_MYSQL_PROMPT_ => 'mysql_prompt', |
| 354 |
_MYSQLDSAFE_ => 'mysqld_safe', |
| 355 |
_EVENTS_OPTIONS_ => 'events_options', |
| 356 |
_SLOW_QUERY_LOG_ => 'slow_query_log', |
| 357 |
_GENERAL_LOG_ => 'general_log', |
| 358 |
_BIND_ADDRESS_ => 'bind_address', |
| 359 |
_MSB_VERSION_ => 'msb_version', |
| 360 |
_LOWER_CASE_TABLE_NAMES_ => 'lower_case_table_names', |
| 361 |
_HISTORY_DIR_ => 'history_dir', |
| 362 |
_CUSTOM_MYSQLD_ => 'custom_mysqld', |
| 363 |
); |
| 364 |
|
| 365 |
$msb->{options}{msb_version} = $MySQL::Sandbox::VERSION; |
| 366 |
|
| 367 |
if ($msb->{options}{db_user} =~ /\@/) { |
| 368 |
my ($user, $host) = split /\@/, $msb->{options}{db_user}; |
| 369 |
$msb->{options}{db_user} = $user; |
| 370 |
$msb->{options}{remote_access} = $host; |
| 371 |
} |
| 372 |
|
| 373 |
if ($OSNAME =~ /darwin/) |
| 374 |
{ |
| 375 |
$msb->{options}{lower_case_table_names} = 2; |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
$msb->{options}{lower_case_table_names} = 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if ( |
| 384 |
($msb->{options}{remote_access} eq $MySQL::Sandbox::default_users{remote_access}) |
| 385 |
&& ($msb->{options}{bind_address} eq '127.0.0.1') |
| 386 |
) |
| 387 |
{ |
| 388 |
# Do nothing. |
| 389 |
# Make sure that we honor the defaults |
| 390 |
# |
| 391 |
} |
| 392 |
elsif ( |
| 393 |
($msb->{options}{remote_access} ne $MySQL::Sandbox::default_users{remote_access}) |
| 394 |
&& ($msb->{options}{bind_address} ne '127.0.0.1') |
| 395 |
) |
| 396 |
{ |
| 397 |
# Do nothing. |
| 398 |
# Both defaults were changed. |
| 399 |
# Make sure that we let user change both values |
| 400 |
# |
| 401 |
} |
| 402 |
elsif ( |
| 403 |
($msb->{options}{remote_access} ne $MySQL::Sandbox::default_users{remote_access}) |
| 404 |
&& ($msb->{options}{remote_access} ne '127.0.0.1') |
| 405 |
&& ($msb->{options}{bind_address} eq '127.0.0.1') |
| 406 |
) |
| 407 |
{ |
| 408 |
# remote_access changed. Make sure that we can access the server |
| 409 |
$msb->{options}{bind_address} = '0.0.0.0'; |
| 410 |
} |
| 411 |
elsif ( |
| 412 |
($msb->{options}{bind_address} ne '0.0.0.0') |
| 413 |
&& ($msb->{options}{remote_access} ne '%') |
| 414 |
) |
| 415 |
{ |
| 416 |
# Customized bind address |
| 417 |
# We adjust the remote access to match it |
| 418 |
$msb->{options}{remote_access} = $msb->{options}{bind_address}; |
| 419 |
} |
| 420 |
elsif ($msb->{options}{bind_address} ne '127.0.0.1') { |
| 421 |
$msb->{options}{remote_access} = $msb->{options}{bind_address}; |
| 422 |
} |
| 423 |
|
| 424 |
for my $opt (qw(db_user remote_access ro_user rw_user repl_user db_password repl_password)) { |
| 425 |
if (defined $msb->{options}{$opt}) { |
| 426 |
$MySQL::Sandbox::default_users{$opt} = $msb->{options}{$opt}; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
unless ( |
| 431 |
($msb->{options}{'sandbox_port'} =~ /^ \d{3,} $ /x) |
| 432 |
&& ($msb->{options}{'sandbox_port'} > 1024 ) |
| 433 |
&& ($msb->{options}{'sandbox_port'} <= 64000)) { |
| 434 |
$msb->get_help( "sandbox_port option must be a port number between 1025 and 64000 (currently requested $msb->{options}{'sandbox_port'} ) \n"); |
| 435 |
} |
| 436 |
|
| 437 |
if ($msb->{options}{datadir_from} eq 'archive') { |
| 438 |
print "You have chosen to install with the 'archive' method.\n"; |
| 439 |
print "This option is now DEPRECATED.\n"; |
| 440 |
print "It will be removed soon. Use the 'script' method instead\n"; |
| 441 |
|
| 442 |
#chdir $sandbox_directory; |
| 443 |
#system "tar -xzf $curdir/datadir.$msb->{options}{'install_version'}.tar.gz "; |
| 444 |
#chdir $curdir; |
| 445 |
} |
| 446 |
|
| 447 |
if ($msb->{options}{datadir_from} eq 'script' and (not $msb->{options}{no_load_grants} ) ) { |
| 448 |
$msb->{options}{load_grants} = 1; |
| 449 |
} |
| 450 |
|
| 451 |
# In MySQL 8.0.8+, the default authentication was changed from mysql_native_password to |
| 452 |
# caching_sha2_password |
| 453 |
# The following code will restore the backward compatibility default. |
| 454 |
# To keep the new default, use --keep_auth_plugin |
| 455 |
if (greater_version($msb->{options}{detected_mysql_version},'8.0.3') |
| 456 |
and $msb->{options}{mysql_version}{major} < 10) |
| 457 |
{ |
| 458 |
unless ( $msb->{options}{keep_auth_plugin}) |
| 459 |
{ |
| 460 |
push @{$msb->{options}{my_clause}}, "default_authentication_plugin=mysql_native_password"; |
| 461 |
$msb->{options}{init_options} = "" unless $msb->{options}{init_options}; |
| 462 |
$msb->{options}{init_options} .= " --default_authentication_plugin=mysql_native_password"; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if ( @{ $msb->{options}{'my_clause'} } ) { |
| 467 |
$msb->{options}{'more_options'} .= qq(#\n# additional options passed through 'my_clause' \n#\n); |
| 468 |
for my $clause ( @{ $msb->{options}{'my_clause'} }) { |
| 469 |
$msb->{options}{'more_options'} .= $clause . "\n"; |
| 470 |
if ($clause =~ /server.id=(\d+)/) |
| 471 |
{ |
| 472 |
my $sid=$1; |
| 473 |
$msb->{options}{fix_server_uuid} = [ $sid, |
| 474 |
$msb->{options}{install_version}, |
| 475 |
$msb->{options}{sandbox_port}, |
| 476 |
"$msb->{options}{upper_directory}/$msb->{options}{sandbox_directory}"]; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if ($msb->{options}{my_file} ) { |
| 482 |
my $fname = $msb->{options}{'my_file'}; |
| 483 |
unless ( -f $fname ) { |
| 484 |
$fname = sprintf 'my-%s.cnf', $msb->{options}{my_file}; |
| 485 |
} |
| 486 |
my $extended_fname = sprintf '%s/support-files/%s', $msb->{options}{basedir}, $fname; |
| 487 |
my $share_extended_fname = sprintf '%s/share/mysql/%s', $msb->{options}{basedir}, $fname; |
| 488 |
if ( -f $fname ) { |
| 489 |
$msb->{options}{more_options} .= get_more_options($fname); |
| 490 |
} |
| 491 |
elsif ( -f $extended_fname ) { |
| 492 |
$msb->{options}{more_options} .= get_more_options($extended_fname); |
| 493 |
} |
| 494 |
elsif ( -f $share_extended_fname ) { |
| 495 |
$msb->{options}{more_options} .= get_more_options($share_extended_fname); |
| 496 |
} |
| 497 |
else { |
| 498 |
die "configuration file $fname not found\n"; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
# print Dumper(\%{$msb->{options}}); exit; |
| 503 |
|
| 504 |
my $bash = 'bash'; |
| 505 |
|
| 506 |
unless (exists_in_path($bash)) { |
| 507 |
$bash = 'tcsh'; |
| 508 |
} |
| 509 |
|
| 510 |
for my $cmd ( ($bash, 'tar', 'gzip') ) { |
| 511 |
unless ( exists_in_path($cmd) ) { |
| 512 |
die "$cmd not found. Can't continue\n"; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
my $bin_bash = exists_in_path($bash); |
| 517 |
if ((! $bin_bash) or ( $bin_bash =~ /^no/i) ) { |
| 518 |
die "can't find the 'bash' shell\n"; |
| 519 |
} |
| 520 |
|
| 521 |
chomp $bin_bash; |
| 522 |
$msb->{options}{'binbash'} = $bin_bash; |
| 523 |
|
| 524 |
my $bin_perl = exists_in_path('perl'); |
| 525 |
if ((! $bin_perl) or ( $bin_perl =~ /^no/i) ) { |
| 526 |
die "can't find the 'perl' interpreter\n"; |
| 527 |
} |
| 528 |
|
| 529 |
chomp $bin_perl; |
| 530 |
$msb->{options}{'binperl'} = $bin_perl; |
| 531 |
|
| 532 |
my @supported_versions = @{ MySQL::Sandbox::supported_versions() }; |
| 533 |
|
| 534 |
#$msb->{options}{'install_version'} = '5.0' unless $msb->{options}{'install_version'}; |
| 535 |
die "server version required.\n" unless $msb->{options}{'install_version'}; |
| 536 |
|
| 537 |
unless (grep {$_ eq $msb->{options}{'install_version'} } @supported_versions ) { |
| 538 |
get_help( "specified version ($msb->{options}{'install_version'}) not allowed. Currently accepted are [@supported_versions] "); |
| 539 |
} |
| 540 |
|
| 541 |
# |
| 542 |
# |
| 543 |
# |
| 544 |
# print "<<$msb->{options}{ 'install_version' }>>\n"; |
| 545 |
if ($msb->{options}{ 'install_version' } ge '5.5' ) { |
| 546 |
$msb->{options}{events_options} = ' --events '; |
| 547 |
} |
| 548 |
else |
| 549 |
{ |
| 550 |
$msb->{options}{events_options} = ''; |
| 551 |
} |
| 552 |
|
| 553 |
my $sb_datadir = qq($msb->{options}{upper_directory}/$msb->{options}{sandbox_directory}/data); |
| 554 |
if ($msb->{options}{ 'install_version' } ge '5.1' ) { |
| 555 |
$msb->{options}{'slow_query_log'} = |
| 556 |
qq(slow-query-log = on\n) |
| 557 |
. qq(# slow-query-log-file=$sb_datadir/msandbox-slow.log); |
| 558 |
$msb->{options}{'general_log'} = |
| 559 |
qq(general-log = on\n) |
| 560 |
. qq(# general-log-file=$sb_datadir/msandbox-general.log); |
| 561 |
} |
| 562 |
else { |
| 563 |
$msb->{options}{'slow_query_log'} = qq(log-slow-queries = $sb_datadir/msandbox-slow.log); |
| 564 |
$msb->{options}{'general_log'} = qq(log = $sb_datadir/msandbox-general.log) ; |
| 565 |
} |
| 566 |
|
| 567 |
if ($msb->{options}{ 'install_version' } eq '3.23' ) { |
| 568 |
$msb->{options}{'mysql_prompt'} = q{}; |
| 569 |
$msb->{options}{'mysqld_safe'} = q{safe_mysqld}; |
| 570 |
} |
| 571 |
else { |
| 572 |
$msb->{options}{'mysql_prompt'} = q/prompt=/ . $msb->{options}{prompt_prefix} . $msb->{options}{prompt_body}; |
| 573 |
$msb->{options}{'mysql_prompt'} =~ s/'//g; |
| 574 |
$msb->{options}{'mysql_prompt'} =~ s/=/='/; |
| 575 |
$msb->{options}{'mysql_prompt'} .= "'"; |
| 576 |
$msb->{options}{'mysqld_safe'} = q{mysqld_safe}; |
| 577 |
} |
| 578 |
|
| 579 |
if ( $msb->{options}{'sandbox_directory'} ) { |
| 580 |
if ($msb->{options}{'sandbox_directory'} =~ m{/} ) { |
| 581 |
die "<sandbox_directory> can't be a full path. " |
| 582 |
. "It's only the name of the directory " |
| 583 |
. "to create inside $msb->{options}{'upper_directory'}\n"; |
| 584 |
} |
| 585 |
unless ( $msb->{options}{ 'no_ver_after_name' } ) { |
| 586 |
my $ver = $msb->{options}{ 'install_version' }; |
| 587 |
# $ver =~ tr/./_/d; ## no critic |
| 588 |
$ver =~ s/\./_/g; |
| 589 |
unless ($msb->{options}{ 'sandbox_directory'} =~ /$ver$/) { |
| 590 |
$msb->{options}{'sandbox_directory'} .= $ver; |
| 591 |
$msb->{options}{ 'no_ver_after_name' } = 1; # 1.6 it will prevent |
| 592 |
# side effects when loading |
| 593 |
# options from defaults file |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
else { |
| 598 |
get_help ('sandbox directory not specified'); |
| 599 |
} |
| 600 |
|
| 601 |
print $msb->credits(); |
| 602 |
unless ($msb->{options}{no_show}) |
| 603 |
{ |
| 604 |
print "Installing with the following parameters:\n"; |
| 605 |
} |
| 606 |
#open my $CURRENT_CONF , q{>}, "$install_dir/current_options.conf" |
| 607 |
# or die "error creating current_options.conf ($ERRNO)\n"; |
| 608 |
|
| 609 |
if ($msb->{options}{no_show}) { |
| 610 |
$msb->{options}{no_confirm} =1; |
| 611 |
} |
| 612 |
|
| 613 |
unless ($msb->{options}{no_show}) { |
| 614 |
for my $key ( |
| 615 |
sort { $msb->{parse_options}{$a}{so} <=> $msb->{parse_options}{$b}{so} } |
| 616 |
grep { $msb->{parse_options}{$_}{parse}} keys %{$msb->{parse_options}} ) { |
| 617 |
next if grep { $key eq $_ } qw/more_options help interactive conf_file no_confirm /; |
| 618 |
my $is_array = ref($msb->{options}{$key}) && ref($msb->{options}{$key}) eq 'ARRAY'; |
| 619 |
printf "%-30s = %s\n", $key, $is_array ? join q{ ; }, |
| 620 |
@{ $msb->{options}{$key} } : ($msb->{options}{$key} || ''); |
| 621 |
next unless $msb->{options}{$key}; |
| 622 |
next if $msb->{options}{$key} =~ /^ \s* $/x; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
my $answer = 'y'; |
| 627 |
|
| 628 |
unless ($msb->{options}{no_confirm}) { |
| 629 |
print 'do you agree? ([Y],n) '; |
| 630 |
$answer = <>; |
| 631 |
chomp $answer; |
| 632 |
if ($answer =~ / ^ \s* $ /x) { |
| 633 |
$answer = 'y'; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
if (lc($answer) eq 'n') { |
| 638 |
print "Installation interrupted at user's request\n"; |
| 639 |
# "To repeat this installation with the same options,\n" |
| 640 |
# "use $PROGRAM_NAME --conf_file=current_options.conf\n"; |
| 641 |
exit; |
| 642 |
} |
| 643 |
|
| 644 |
my @file_list = (); |
| 645 |
# my @doc_file_list = ( ['README'], ['COPYING'], ['VERSION'] ); |
| 646 |
|
| 647 |
my @MANIFEST = MySQL::Sandbox::Scripts::manifest(); |
| 648 |
|
| 649 |
#open my $MANIFEST, q{<}, "$install_dir/MANIFEST" |
| 650 |
# or die "file MANIFEST not found\n"; |
| 651 |
|
| 652 |
my $install_section = 0; |
| 653 |
|
| 654 |
print "reading MANIFEST files \n" if $DEBUG; |
| 655 |
for my $filename ( @MANIFEST) { |
| 656 |
chomp $filename; |
| 657 |
next if $filename =~ /^ \s* $/x; |
| 658 |
unless ($install_section) { |
| 659 |
$install_section = 1 if $filename =~ /^ \s* [#] \s* INSTALL \s* FILES /x; |
| 660 |
} |
| 661 |
next if $filename =~ /^ \s* [#] /x; |
| 662 |
print "($install_section) ", $filename, qq(\n) if $DEBUG; |
| 663 |
my $destination = $filename; |
| 664 |
|
| 665 |
if ( ( $scripts_in_code{$filename} ) ) { |
| 666 |
unless ($install_section) { |
| 667 |
push @file_list, [$filename, $destination]; |
| 668 |
} |
| 669 |
} |
| 670 |
else { |
| 671 |
die "listed file $filename not found. Package is not complete\n" |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
# set_sandbox_dir_scripts(); |
| 676 |
|
| 677 |
my $sandbox_directory = sprintf('%s/%s', $msb->{options}{'upper_directory'}, $msb->{options}{'sandbox_directory'}); |
| 678 |
|
| 679 |
|
| 680 |
#if ($curdir eq $sandbox_directory) { |
| 681 |
# die "installation directory and destination directory are the same. Can't install\n"; |
| 682 |
#} |
| 683 |
|
| 684 |
if ( -d $sandbox_directory ) { |
| 685 |
if ($msb->{options}{'force'} ) { |
| 686 |
my $new_datadir = "$sandbox_directory/data"; |
| 687 |
my $old_datadir = "$sandbox_directory/old_data"; |
| 688 |
if ( -d $old_datadir) |
| 689 |
{ |
| 690 |
system "rm -rf $old_datadir"; |
| 691 |
} |
| 692 |
|
| 693 |
if ( -x "$sandbox_directory/stop") { |
| 694 |
system("$sandbox_directory/stop"); |
| 695 |
} |
| 696 |
system "mv $new_datadir $old_datadir"; |
| 697 |
warn "Directory $new_datadir already exists. Renamed to $old_datadir\n" if $DEBUG; |
| 698 |
|
| 699 |
#warn "$sandbox_directory already exists. Overwriting its contents\n" if $DEBUG; |
| 700 |
#if ( -f "$sandbox_directory/clear") { |
| 701 |
# system("$sandbox_directory/clear"); |
| 702 |
#} |
| 703 |
#if ( -d "$sandbox_directory/data/mysql") { |
| 704 |
# system("rm -rf $sandbox_directory/data/mysql"); |
| 705 |
#} |
| 706 |
} |
| 707 |
else { |
| 708 |
die "$sandbox_directory already exists.\n'--force' option not specified.\nInstallation halted\n"; |
| 709 |
} |
| 710 |
} |
| 711 |
else { |
| 712 |
print "creating $sandbox_directory\n" if $DEBUG; |
| 713 |
my $result = mkdir $sandbox_directory; |
| 714 |
if (! $result ) { |
| 715 |
die "error creating $sandbox_directory\n"; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
my $sandbox_tmpdir = sprintf '%s/tmp', $sandbox_directory; |
| 720 |
if ($msb->{options}{'tmpdir'}) { |
| 721 |
$sandbox_tmpdir = $msb->{options}{'tmpdir'}; |
| 722 |
} |
| 723 |
else { |
| 724 |
$msb->{options}{'tmpdir'} = $sandbox_tmpdir; |
| 725 |
} |
| 726 |
if ( -f $sandbox_tmpdir) { |
| 727 |
die "Can't create directory $sandbox_tmpdir (a file of the same name exists)\n"; |
| 728 |
} |
| 729 |
unless (-d $sandbox_tmpdir) { |
| 730 |
mkdir $sandbox_tmpdir |
| 731 |
or die "Error creating $sandbox_tmpdir ($!)\n"; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
set_sandbox_dir_scripts(); |
| 736 |
|
| 737 |
|
| 738 |
for my $file_spec ( @file_list) { #, @doc_file_list) { |
| 739 |
copy_and_replace($file_spec->[0], $sandbox_directory, $file_spec->[1]); |
| 740 |
} |
| 741 |
|
| 742 |
if ($msb->{options}{datadir_from} eq 'script' ) { |
| 743 |
my $additional_options = ""; |
| 744 |
my $script = sprintf '%s/scripts/mysql_install_db', $msb->{options}{basedir} ; |
| 745 |
my $mysql_version = $msb->{options}{detected_mysql_version} ; |
| 746 |
if ($mysql_version) |
| 747 |
{ |
| 748 |
chomp $mysql_version; |
| 749 |
my $major = $msb->{options}{mysql_version}{major}; |
| 750 |
my $minor = $msb->{options}{mysql_version}{minor}; |
| 751 |
my $rev = $msb->{options}{mysql_version}{rev}; |
| 752 |
$mysql_version=sprintf('%d.%d.%d', $major,$minor, $rev); |
| 753 |
print "#1 $mysql_version - $major - $minor - $rev - $script\n" if $DEBUG; |
| 754 |
if ((greater_version($mysql_version, '5.7.6') ) && ($major < 10)) # exclude MariaDB from this feature |
| 755 |
{ |
| 756 |
# mysql_install_db is deprecated. It is now replaced by 'mysqld --initialize' |
| 757 |
# https://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html |
| 758 |
# https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html |
| 759 |
# $additional_options .= ' --initialize-insecure --skip-ssl' ; |
| 760 |
$additional_options .= ' --initialize-insecure' ; |
| 761 |
$script = sprintf '%s/bin/mysqld', $msb->{options}{basedir} ; |
| 762 |
} |
| 763 |
elsif (greater_version($mysql_version, '5.7.4' ) && ($major < 10) ) |
| 764 |
{ |
| 765 |
$additional_options .= ' --insecure' ; |
| 766 |
} |
| 767 |
print "#2 $mysql_version - $major - $minor - $rev - $script\n" if $DEBUG; |
| 768 |
} |
| 769 |
else |
| 770 |
{ |
| 771 |
die "Can't determine MySQL version\n"; |
| 772 |
} |
| 773 |
|
| 774 |
if ($script =~ /mysql_install_db/) |
| 775 |
{ |
| 776 |
unless ( -x $script ) |
| 777 |
{ |
| 778 |
if ( -e $script ) { |
| 779 |
die "$script is not executable\n"; |
| 780 |
} |
| 781 |
$script = sprintf '%s/bin/mysql_install_db', $msb->{options}{basedir} ; |
| 782 |
} |
| 783 |
} |
| 784 |
if ( -x $script ) |
| 785 |
{ |
| 786 |
my $init_defaults='--no-defaults'; |
| 787 |
my $init_options=" --tmpdir=$sandbox_directory/tmp "; |
| 788 |
if ($msb->{options}{init_options}) |
| 789 |
{ |
| 790 |
$init_options .= " $msb->{options}{init_options}"; |
| 791 |
} |
| 792 |
if ($msb->{options}{init_my_cnf} && $msb->{options}{init_use_cnf}) |
| 793 |
{ |
| 794 |
die "Only one of --init_use_cnf or --init_my_cnf can be used\n"; |
| 795 |
} |
| 796 |
elsif ($msb->{options}{init_my_cnf}) |
| 797 |
{ |
| 798 |
$init_defaults="--defaults-file=$sandbox_directory/my.sandbox.cnf"; |
| 799 |
} |
| 800 |
elsif ($msb->{options}{init_use_cnf}) |
| 801 |
{ |
| 802 |
my $init_file=$msb->{options}{init_use_cnf}; |
| 803 |
unless (-f $init_file) |
| 804 |
{ |
| 805 |
die "File $init_file not found\n"; |
| 806 |
} |
| 807 |
$init_defaults="--defaults-file=$init_file"; |
| 808 |
} |
| 809 |
#my $cmd = sprintf '%s --no-defaults --user=%s --basedir=%s --datadir=%s/data --tmpdir=%s/tmp %s', |
| 810 |
my $cmd = sprintf "%s $init_defaults --user=%s --basedir=%s --datadir=%s/data %s $init_options", |
| 811 |
$script, |
| 812 |
$msb->{options}{operating_system_user}, |
| 813 |
$msb->{options}{basedir}, |
| 814 |
$sandbox_directory, |
| 815 |
# $sandbox_directory, |
| 816 |
$ENV{MYSQL_INSTALL_DB} || ''; |
| 817 |
if ($DEBUG) { |
| 818 |
print "$cmd $additional_options\n"; |
| 819 |
} |
| 820 |
if ($ENV{TRACE_INSTALL}) |
| 821 |
{ |
| 822 |
# This instruction will never execute unless the user variable is defined. |
| 823 |
# It is only needed for testing purposes |
| 824 |
system qq[echo "\$(date) $mysql_version - $cmd $additional_options" >> $sandbox_directory/trace_install.txt ]; |
| 825 |
} |
| 826 |
my $will_abort = 0; |
| 827 |
my $result = qx($cmd $additional_options 2>&1); |
| 828 |
if ($CHILD_ERROR) { |
| 829 |
$will_abort = 1; |
| 830 |
} |
| 831 |
$result = '' unless defined $result; |
| 832 |
my $abort_msg = "error while creating grant tables\n$result\n"; |
| 833 |
if ($result && ($result =~ /\berror\b/i)) |
| 834 |
{ |
| 835 |
# We look for the word 'error' in the output |
| 836 |
while ($result =~ /(.*\berror\b.*)/ig) |
| 837 |
{ |
| 838 |
my $error_line = $1; |
| 839 |
# If the line containing 'error' will also say [Warning], we skip it |
| 840 |
if ($error_line =~ /\[Warning\]/) |
| 841 |
{ |
| 842 |
print "# Found [Warning]: skipping line containing 'error'\n" if $DEBUG; |
| 843 |
} |
| 844 |
else |
| 845 |
{ |
| 846 |
# otherwise, we mark this operation as an error |
| 847 |
$will_abort++; |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
if ($DEBUG) |
| 852 |
{ |
| 853 |
while ($result =~/(.*warning.*)/ig) |
| 854 |
{ |
| 855 |
print "++ $1\n"; |
| 856 |
} |
| 857 |
} |
| 858 |
if ($will_abort) |
| 859 |
{ |
| 860 |
die $abort_msg; |
| 861 |
} |
| 862 |
if (greater_version($msb->{options}{detected_mysql_version},'8.0.1') and $msb->{options}{mysql_version}{major} < 10) |
| 863 |
{ |
| 864 |
my $ibd = "$sandbox_directory/data/mysql.ibd"; |
| 865 |
if ( -f $ibd) |
| 866 |
{ |
| 867 |
print "Table $ibd found\n" if $DEBUG; |
| 868 |
} |
| 869 |
else |
| 870 |
{ |
| 871 |
die "Table $ibd not found\n"; |
| 872 |
} |
| 873 |
} |
| 874 |
else |
| 875 |
{ |
| 876 |
for my $table_name (qw(user db tables_priv columns_priv)) { |
| 877 |
my $frm = "$sandbox_directory/data/mysql/$table_name.frm"; |
| 878 |
my $myd = "$sandbox_directory/data/mysql/$table_name.MYD"; |
| 879 |
my $ibd = "$sandbox_directory/data/mysql/$table_name.ibd"; |
| 880 |
if ( (-f $frm ) or ( -f $myd) or ( -f $ibd ) ) |
| 881 |
{ |
| 882 |
# do nothing |
| 883 |
print "Table $table_name found\n" if $DEBUG; |
| 884 |
} |
| 885 |
else |
| 886 |
{ |
| 887 |
die "Table $table_name not found\n"; |
| 888 |
} |
| 889 |
} |
| 890 |
} |
| 891 |
for my $logfile (qw(ib_logfile0 ib_logfile1)) |
| 892 |
{ |
| 893 |
if ( -f "$sandbox_directory/data/$logfile") |
| 894 |
{ |
| 895 |
unlink "$sandbox_directory/data/$logfile"; |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
else { |
| 900 |
if ( -e $script ) { |
| 901 |
die "$script is not executable\n"; |
| 902 |
} |
| 903 |
die "$script not found\n"; |
| 904 |
} |
| 905 |
} |
| 906 |
elsif ($msb->{options}{datadir_from} =~ /dir: (.+) /x ) { |
| 907 |
my $origin = $1; |
| 908 |
if (-d $origin ) { |
| 909 |
if ( -f sprintf '%s/mysql/user.MYD', $origin) { |
| 910 |
my $sandbox_datadir = sprintf '%s/data', $sandbox_directory; |
| 911 |
if (-d $sandbox_datadir) { |
| 912 |
if ($msb->{options}{'force'}) { |
| 913 |
warn "directory $sandbox_datadir already exists. Overwriting its contents\n" if $DEBUG; |
| 914 |
} |
| 915 |
else { |
| 916 |
die "directory $sandbox_datadir already exists. Option --force not specified. Installation halted\n" |
| 917 |
} |
| 918 |
} |
| 919 |
else { |
| 920 |
mkdir $sandbox_datadir |
| 921 |
or die "error creating $sandbox_datadir ($!)\n"; |
| 922 |
} |
| 923 |
system sprintf 'cp %s %s/* %s/', $msb->{options}{'verbose'} ? q{-vr} : q{-r}, $origin, $sandbox_datadir; |
| 924 |
} |
| 925 |
else { |
| 926 |
die "mysql user table not found in $origin\n"; |
| 927 |
} |
| 928 |
} |
| 929 |
else { |
| 930 |
die "directory $origin not found\n"; |
| 931 |
} |
| 932 |
} |
| 933 |
else { |
| 934 |
die "unrecognized value for option 'datadir_from' ($msb->{options}{datadir_from})\n"; |
| 935 |
} |
| 936 |
|
| 937 |
if ($msb->{options}{fix_server_uuid} && ( ! $msb->{options}{keep_uuid})) { |
| 938 |
if ( $DEBUG ) |
| 939 |
{ |
| 940 |
print Data::Dumper->Dump([ $msb->{options}{fix_server_uuid}], ['options_fix_server_uuid']); |
| 941 |
} |
| 942 |
fix_server_uuid( @{ $msb->{options}{fix_server_uuid} } ); |
| 943 |
} |
| 944 |
|
| 945 |
sub run_query |
| 946 |
{ |
| 947 |
my ($run_instructions) = @_; |
| 948 |
for my $key (qw(dir user type)) |
| 949 |
{ |
| 950 |
unless (defined $run_instructions->{$key}) |
| 951 |
{ |
| 952 |
die "# run_query : '$key' field missing in run_instructions\n"; |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
my $dir = $run_instructions->{dir} ; |
| 957 |
my $user = $run_instructions->{user} ; |
| 958 |
my $type = $run_instructions->{type} ; |
| 959 |
my $options = $run_instructions->{options} ; |
| 960 |
my $visual_options = ''; |
| 961 |
if ($run_instructions->{verbose}) |
| 962 |
{ |
| 963 |
$visual_options = '-v -t'; |
| 964 |
} |
| 965 |
unless ( -x "$dir/use" ) |
| 966 |
{ |
| 967 |
die "# Executable ./use not found in $dir\n"; |
| 968 |
} |
| 969 |
$options = '' unless $options; |
| 970 |
if ($run_instructions->{skip_password}) |
| 971 |
{ |
| 972 |
$ENV{NOPASSWORD}=1; |
| 973 |
} |
| 974 |
if ( $type eq 'file') |
| 975 |
{ |
| 976 |
my $file = $run_instructions->{file} |
| 977 |
or die "# run_query : 'file' field missing in run_instructions\n"; |
| 978 |
die "# run_query: file '$file' not found " unless -f $file; |
| 979 |
system qq( $dir/use $visual_options -u $user $options < $file ); |
| 980 |
} |
| 981 |
elsif ($type eq 'query') |
| 982 |
{ |
| 983 |
my $query = $run_instructions->{query} |
| 984 |
or die "# run_query : 'query' field missing in run_instructions\n"; |
| 985 |
my $qfile= "$dir/tmp/qfile.sql"; |
| 986 |
open my $QF , '>' , $qfile |
| 987 |
or die "# Can't create $qfile\n"; |
| 988 |
print $QF $query; |
| 989 |
close $QF; |
| 990 |
system qq( $dir/use $visual_options -u $user $options < $qfile ); |
| 991 |
} |
| 992 |
else |
| 993 |
{ |
| 994 |
die "# run_query: unrecognized type '$type'\n"; |
| 995 |
} |
| 996 |
delete $ENV{NOPASSWORD}; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
sub run_exec_conditionally |
| 1001 |
{ |
| 1002 |
my ($_msb, $cond_option) = @_; |
| 1003 |
if ($_msb->{options}{$cond_option}) |
| 1004 |
{ |
| 1005 |
local $ENV{SANDBOX_DIR} = $sandbox_directory; |
| 1006 |
local $ENV{BASEDIR} = $_msb->{options}{basedir}; |
| 1007 |
local $ENV{DB_DATADIR} = "$sandbox_directory/data"; |
| 1008 |
local $ENV{MY_CNF} = "$sandbox_directory/my.sandbox.cnf"; |
| 1009 |
local $ENV{DB_PORT} = $_msb->{options}{sandbox_port}; |
| 1010 |
local $ENV{DB_USER} = $_msb->{options}{db_user}; |
| 1011 |
local $ENV{DB_PASSWORD} = $_msb->{options}{db_password}; |
| 1012 |
local $ENV{DB_SOCKET} = "$_msb->{options}{globaltmpdir}/mysql_sandbox$_msb->{options}{sandbox_port}.sock"; |
| 1013 |
local $ENV{MYSQL_VERSION} = $detected_mysql_version; |
| 1014 |
local $ENV{MYSQL_MAJOR} = $msb->{options}{mysql_version}{major}; |
| 1015 |
local $ENV{MYSQL_MINOR} = $msb->{options}{mysql_version}{minor}; |
| 1016 |
local $ENV{MYSQL_REV} = $msb->{options}{mysql_version}{rev}; |
| 1017 |
local $ENV{EXEC_STAGE} = $cond_option; |
| 1018 |
print qq(# [$cond_option] system "$_msb->{options}{$cond_option}"\n); |
| 1019 |
system "$_msb->{options}{$cond_option}"; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
run_exec_conditionally($msb, 'pre_start_exec'); |
| 1024 |
|
| 1025 |
if ($msb->{options}{load_grants}) { |
| 1026 |
print "# Starting server\n"; |
| 1027 |
system "$sandbox_directory/start" |
| 1028 |
and die "can't start server\n"; |
| 1029 |
|
| 1030 |
## |
| 1031 |
# Run pre-grant instructions |
| 1032 |
# |
| 1033 |
# my $verbose_query = $msb->{options}{verbose}; |
| 1034 |
my $verbose_query = not $msb->{options}{no_show}; |
| 1035 |
if ($msb->{options}{load_plugin}) |
| 1036 |
{ |
| 1037 |
my $query = $msb->{options}{pre_grants_sql} || ''; |
| 1038 |
my @plugins = split /,/, $msb->{options}{load_plugin}; |
| 1039 |
for my $plugin (@plugins) |
| 1040 |
{ |
| 1041 |
my $plugin_name = $plugin; |
| 1042 |
my $plugin_file = "${plugin}.so"; |
| 1043 |
if ($plugin =~ /(\w+)[=:](\S+)/) |
| 1044 |
{ |
| 1045 |
$plugin_name = $1; |
| 1046 |
$plugin_file = $2; |
| 1047 |
} |
| 1048 |
$query .= ";\n" if $query; |
| 1049 |
$query .= "INSTALL PLUGIN $plugin_name soname '$plugin_file';\n"; |
| 1050 |
if ($verbose_query) |
| 1051 |
{ |
| 1052 |
$query .= "select plugin_name, plugin_version, plugin_status from information_schema.plugins where plugin_name = '$plugin_name';\n"; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
if ($query) |
| 1056 |
{ |
| 1057 |
$msb->{options}{pre_grants_sql} = $query; |
| 1058 |
print "# Added installation command for plugin 'plugin_name' to pre_grants_sql\n" if $DEBUG; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
run_exec_conditionally($msb, 'pre_grants_exec'); |
| 1063 |
|
| 1064 |
if ($msb->{options}{pre_grants_sql}) |
| 1065 |
{ |
| 1066 |
run_query({ |
| 1067 |
type => 'query', |
| 1068 |
dir => $sandbox_directory, |
| 1069 |
user => 'root', |
| 1070 |
skip_password => 1, |
| 1071 |
verbose => $verbose_query, |
| 1072 |
query => $msb->{options}{pre_grants_sql}, |
| 1073 |
}); |
| 1074 |
} |
| 1075 |
if ($msb->{options}{pre_grants_file}) |
| 1076 |
{ |
| 1077 |
run_query({ |
| 1078 |
type => 'file', |
| 1079 |
dir => $sandbox_directory, |
| 1080 |
user => 'root', |
| 1081 |
skip_password => 1, |
| 1082 |
verbose => $verbose_query, |
| 1083 |
file => $msb->{options}{pre_grants_file} |
| 1084 |
}); |
| 1085 |
} |
| 1086 |
|
| 1087 |
print "# Loading grants\n"; |
| 1088 |
system "$sandbox_directory/load_grants" |
| 1089 |
and die "can't load grants\n"; |
| 1090 |
if ($msb->{options}{master}) { |
| 1091 |
system "$sandbox_directory/use -e 'reset master'"; |
| 1092 |
} |
| 1093 |
|
| 1094 |
## |
| 1095 |
# Run post grant instructions |
| 1096 |
# |
| 1097 |
run_exec_conditionally($msb, 'post_grants_exec'); |
| 1098 |
|
| 1099 |
if ($msb->{options}{post_grants_sql}) |
| 1100 |
{ |
| 1101 |
run_query({ |
| 1102 |
type => 'query', |
| 1103 |
dir => $sandbox_directory, |
| 1104 |
user => 'root', |
| 1105 |
verbose => $verbose_query, |
| 1106 |
skip_password => 0, |
| 1107 |
query => $msb->{options}{post_grants_sql} |
| 1108 |
}); |
| 1109 |
} |
| 1110 |
if ($msb->{options}{post_grants_file}) |
| 1111 |
{ |
| 1112 |
run_query({ |
| 1113 |
type => 'file', |
| 1114 |
dir => $sandbox_directory, |
| 1115 |
user => 'root', |
| 1116 |
skip_password => 0, |
| 1117 |
verbose => $verbose_query, |
| 1118 |
file => $msb->{options}{post_grants_file} |
| 1119 |
}); |
| 1120 |
} |
| 1121 |
|
| 1122 |
if ($msb->{options}{slaveof}) { |
| 1123 |
my $cmt = 'CHANGE MASTER TO '; |
| 1124 |
my %cmt_clauses = ( |
| 1125 |
master_host => '127.0.0.1', |
| 1126 |
master_port => undef, |
| 1127 |
master_user => 'rsandbox', |
| 1128 |
master_password => 'rsandbox', |
| 1129 |
master_log_file => undef, |
| 1130 |
master_log_pos => undef, |
| 1131 |
); |
| 1132 |
my %apply_clauses = map { $_,$cmt_clauses{$_} } grep { $cmt_clauses{$_} } keys %cmt_clauses; |
| 1133 |
for my $clause ( keys %cmt_clauses) { |
| 1134 |
if ($msb->{options}{slaveof} =~ /$clause\s*=([^,]+)/i) |
| 1135 |
{ |
| 1136 |
$apply_clauses{$clause}=$1; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
my $count =0; |
| 1140 |
for my $clause (keys %apply_clauses) { |
| 1141 |
if ($count) { |
| 1142 |
$cmt .= ','; |
| 1143 |
} |
| 1144 |
else { |
| 1145 |
$count++; |
| 1146 |
} |
| 1147 |
$cmt .= ' ' . $clause . '=' . quote_clause($apply_clauses{$clause}); |
| 1148 |
} |
| 1149 |
if ($apply_clauses{master_port}) { |
| 1150 |
system "$sandbox_directory/use -e '$cmt; start slave'"; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
if ($msb->{options}{no_run}) { |
| 1154 |
print "stopping server\n"; |
| 1155 |
system "$sandbox_directory/stop" |
| 1156 |
and die "can't stop server\n"; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
print |
| 1161 |
# "installation options saved to current_options.conf.\n", |
| 1162 |
#"To repeat this installation with the same options,\n", |
| 1163 |
#"use $PROGRAM_NAME --conf_file=current_options.conf\n", |
| 1164 |
#('-' x 40), "\n", |
| 1165 |
"Your sandbox server was installed in ", use_env($sandbox_directory), "\n"; |
| 1166 |
sbinstr( "sandbox installed in " . use_env($sandbox_directory)); |
| 1167 |
if ($msb->{options}{plugin_mysqlx}) |
| 1168 |
{ |
| 1169 |
print "# mysqlx plugin installed, listening to port # $msb->{options}{mysqlx_port}\n" |
| 1170 |
} |
| 1171 |
|
| 1172 |
sub quote_clause { |
| 1173 |
my ($string) = @_; |
| 1174 |
if ($string =~ /^\d+$/) { |
| 1175 |
return $string; |
| 1176 |
} |
| 1177 |
if ($string =~ /^["'].*['"]$/) { |
| 1178 |
return $string; |
| 1179 |
} |
| 1180 |
return qq("$string"); |
| 1181 |
} |
| 1182 |
|
| 1183 |
sub copy_and_replace { |
| 1184 |
my ($filename, $destination_dir, $destination_filename) = @_ ; |
| 1185 |
$destination_filename = $filename unless $destination_filename; |
| 1186 |
my $newname = sprintf('%s/%s', $destination_dir, $destination_filename); |
| 1187 |
#unless ( $scripts_in_code{$filename} ) { |
| 1188 |
# $scripts_in_code{$filename} = ''; |
| 1189 |
# unless ( -f $filename) { |
| 1190 |
# $filename = "$install_dir/$filename"; |
| 1191 |
# } |
| 1192 |
# open my $FROM, q{<}, $filename |
| 1193 |
# or die "error opening $filename ($ERRNO)\n"; |
| 1194 |
# while (<$FROM>) { |
| 1195 |
# for my $key ( keys %replace_options) { |
| 1196 |
# s/ $key /$msb->{options}{$replace_options{$key}}/xg; |
| 1197 |
# } |
| 1198 |
# $scripts_in_code{$filename} .= $_ ; |
| 1199 |
# } |
| 1200 |
# close $FROM; |
| 1201 |
#} |
| 1202 |
open my $TO, q{>}, $newname |
| 1203 |
or die "error creating $newname ($ERRNO)\n"; |
| 1204 |
print "copying $filename into $destination_dir\n" if $DEBUG; |
| 1205 |
my $script_content = $scripts_in_code{$filename} |
| 1206 |
or die "invalid script content for <$filename>\n"; |
| 1207 |
|
| 1208 |
$script_content =~ s/__LICENSE__/$license_text/; |
| 1209 |
my $SBINSTR_SH_TEXT = $MySQL::Sandbox::SBINSTR_SH_TEXT; |
| 1210 |
|
| 1211 |
$script_content =~ s/__SBINSTR_SH__/$SBINSTR_SH_TEXT/; |
| 1212 |
|
| 1213 |
for my $key ( keys %replace_options) { |
| 1214 |
my $new_value = $msb->{options}{$replace_options{$key}}; |
| 1215 |
if ( ($key eq '_DBUSER_') |
| 1216 |
and ($destination_filename =~ /my\.sandbox.cnf/)) { |
| 1217 |
$new_value =~ s/\@.*//g; |
| 1218 |
} |
| 1219 |
unless (defined $new_value) |
| 1220 |
{ |
| 1221 |
die "can't find a value for '$key'\n"; |
| 1222 |
} |
| 1223 |
$script_content =~ s/ $key /$new_value/xg; |
| 1224 |
} |
| 1225 |
print $TO $script_content; |
| 1226 |
close $TO; |
| 1227 |
if ($newname =~ / \. (?: sh | pl ) $ /x) { |
| 1228 |
print "changing attributes to $newname\n" if $DEBUG; |
| 1229 |
chmod 0755, $newname; ## no critic |
| 1230 |
my $better_name = $newname; |
| 1231 |
$better_name =~ s/\.(?:sh|pl)$//; |
| 1232 |
rename $newname, $better_name; |
| 1233 |
} |
| 1234 |
elsif ($newname =~ /\.json$/ ) |
| 1235 |
{ |
| 1236 |
print "#validating $newname for JSON contents\n" if $DEBUG; |
| 1237 |
my $is_json = MySQL::Sandbox::validate_json_object($newname); |
| 1238 |
if ($is_json && ($is_json == -1)) |
| 1239 |
{ |
| 1240 |
warn "Can't validate JSON in $newname\n" if $DEBUG; |
| 1241 |
} |
| 1242 |
elsif (! $is_json) |
| 1243 |
{ |
| 1244 |
warn "# JSON object inside newname is not valid\n"; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
return; |
| 1248 |
} |
| 1249 |
|
| 1250 |
sub get_more_options { |
| 1251 |
my ($fname) = @_; |
| 1252 |
my $text = qq(#\n# additional options from $fname \n#\n); |
| 1253 |
open my $MYFILE, q{<}, $fname |
| 1254 |
or die "can't open $fname ($!)\n"; |
| 1255 |
my $start = 0; |
| 1256 |
while (<$MYFILE>) { |
| 1257 |
if ($start ) { |
| 1258 |
last if m/ ^ \s* \[ /x; |
| 1259 |
|
| 1260 |
my ($var) = m/^ \s* (\S+) \s* = /x; |
| 1261 |
next if ( $var && exists ($skip_options{$var} )); |
| 1262 |
$text .= $_; |
| 1263 |
} |
| 1264 |
else { |
| 1265 |
$start = m/ ^ \[mysqld\] /x; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
close $MYFILE; |
| 1269 |
return $text; |
| 1270 |
} |
| 1271 |
|
| 1272 |
sub select_interactively { |
| 1273 |
|
| 1274 |
print "Enter the values for each option\n", |
| 1275 |
"* To leave the interactive choice and accept default values \n", |
| 1276 |
" for the remaining options, enter 'default'\n", |
| 1277 |
"* To go to the previous item, enter 'back'\n", |
| 1278 |
"* To quit the installation without any action, enter 'quit'\n\n"; |
| 1279 |
|
| 1280 |
my @options_list = |
| 1281 |
sort { $msb->{parse_options}{$a}{'so'} <=> $msb->{parse_options}{$b}{'so'} } |
| 1282 |
grep { $_ !~ /(?:interactive|help|no_check_port|no_load_grants)/} |
| 1283 |
grep { $msb->{parse_options}{$_}{'parse'}} keys %{$msb->{parse_options}} ; |
| 1284 |
|
| 1285 |
my $opnum =0; |
| 1286 |
OPTION_CHOICE: |
| 1287 |
while( $opnum < @options_list ) { |
| 1288 |
my $op = $options_list[$opnum]; |
| 1289 |
my $original_default = $msb->{options}{$op} ; |
| 1290 |
my $default = $original_default; |
| 1291 |
$default = $msb->{parse_options}{$op}{'value'} unless defined $default; |
| 1292 |
my $is_array = 0; |
| 1293 |
if (ref($default) && ref($default) eq 'ARRAY') { |
| 1294 |
$default = join(q{ ; } , @{$default}); |
| 1295 |
$is_array=1; |
| 1296 |
} |
| 1297 |
my $chosen = q{}; |
| 1298 |
|
| 1299 |
my ($number_wanted) = $msb->{parse_options}{$op}{'parse'} =~ m/ = (i) /x; |
| 1300 |
|
| 1301 |
print q{-} x 65; |
| 1302 |
print "\n$op\n"; |
| 1303 |
my $text_items = $msb->{parse_options}{$op}{help}; |
| 1304 |
for my $titem (@{$text_items}) { |
| 1305 |
print " $titem \n" if $titem; |
| 1306 |
} |
| 1307 |
print "Your choice: (current value [$default]) "; |
| 1308 |
if ($is_array) { |
| 1309 |
print "(You can enter multiple values separated by ';') "; |
| 1310 |
} |
| 1311 |
$chosen = <>; |
| 1312 |
chomp($chosen); |
| 1313 |
if ($chosen eq 'quit') { |
| 1314 |
exit; |
| 1315 |
} |
| 1316 |
elsif ($chosen eq 'back') { |
| 1317 |
$opnum--; |
| 1318 |
if ($opnum < 0 ) { |
| 1319 |
$opnum = 0; |
| 1320 |
} |
| 1321 |
redo OPTION_CHOICE; |
| 1322 |
} |
| 1323 |
elsif ($chosen eq 'default') { |
| 1324 |
last; |
| 1325 |
} |
| 1326 |
elsif ($chosen =~ m/ ^ \s* $ /x) { |
| 1327 |
$msb->{options}{$op} = $original_default; |
| 1328 |
} |
| 1329 |
elsif ($number_wanted) { |
| 1330 |
if ($chosen =~ m/ ^ \d+ $ /x ) { |
| 1331 |
$msb->{options}{$op} = $chosen; |
| 1332 |
} |
| 1333 |
else { |
| 1334 |
print q{*} x 65, |
| 1335 |
"\n" , |
| 1336 |
"**** option $op requires a numeric value\n" , |
| 1337 |
q{*} x 65, |
| 1338 |
"\n"; |
| 1339 |
redo OPTION_CHOICE; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
else { |
| 1343 |
if($is_array){ |
| 1344 |
$msb->{options}{$op} = [ split /\s*;\s*/, $chosen ] ; |
| 1345 |
} |
| 1346 |
else { |
| 1347 |
$msb->{options}{$op} = $chosen; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
$opnum++; |
| 1351 |
} |
| 1352 |
return; |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
sub check_sandbox_dir { |
| 1357 |
my $sandboxdir = $ENV{'SANDBOX_HOME'} || "$ENV{'HOME'}/sandboxes"; |
| 1358 |
if (! -d $sandboxdir) { |
| 1359 |
mkdir $sandboxdir; |
| 1360 |
if ( $CHILD_ERROR) { |
| 1361 |
die ("can't set sandbox directory $sandboxdir ($CHILD_ERROR)\n"); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
sub check_tmp_dir { |
| 1367 |
my $tmpdir = $ENV{'TMPDIR'} || $ENV{'TEMPDIR'}; |
| 1368 |
# print ">>$tmpdir\n"; |
| 1369 |
if (! -d $tmpdir) { |
| 1370 |
die ("can't find tmp directory $tmpdir\n"); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
sub set_sandbox_dir_scripts { |
| 1375 |
my $sandboxdir = $ENV{'SANDBOX_HOME'} |
| 1376 |
or return; |
| 1377 |
if ( $ENV{'SANDBOX_HOME'} eq $ENV{'HOME'} ) { |
| 1378 |
return; |
| 1379 |
} |
| 1380 |
copy_and_replace('sandbox_action.pl', $sandboxdir); |
| 1381 |
copy_and_replace('test_replication.sh', $sandboxdir); |
| 1382 |
copy_and_replace('plugin.conf', $sandboxdir); |
| 1383 |
for my $name (qw(start stop clear use send_kill restart status )) { |
| 1384 |
open my $FH, q{>}, "$sandboxdir/${name}_all" |
| 1385 |
or die "can't create ${name}_all in $sandboxdir\n"; |
| 1386 |
print $FH "#!$msb->{options}{'binbash'}\n"; |
| 1387 |
print $FH qq($sandboxdir/sandbox_action $name "\$\@"\n); |
| 1388 |
close $FH; |
| 1389 |
chmod 0755, "$sandboxdir/${name}_all"; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
|