| 1 |
#!/usr/bin/perl |
| 2 |
# make_sandbox_from_source |
| 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 |
runs_as_root(); |
| 23 |
|
| 24 |
my $msb = MySQL::Sandbox->new(); |
| 25 |
|
| 26 |
my $source_directory = shift |
| 27 |
or get_help('source directory missing'); |
| 28 |
|
| 29 |
my $sandbox_type = shift |
| 30 |
or get_help('sandbox_type missing'); |
| 31 |
|
| 32 |
my %sandbox_apps = ( |
| 33 |
single => 'make_sandbox', |
| 34 |
replication => 'make_replication_sandbox', |
| 35 |
circular => 'make_replication_sandbox', |
| 36 |
multiple => 'make_multiple_sandbox', |
| 37 |
); |
| 38 |
|
| 39 |
unless ( -d $source_directory) { |
| 40 |
die "<$source_directory> is not a directory\n"; |
| 41 |
} |
| 42 |
|
| 43 |
$source_directory =~ s{/$}{}; |
| 44 |
|
| 45 |
unless ( -x "$source_directory/scripts/make_binary_distribution") { |
| 46 |
die "can't find executable script 'make_binary_distribution'\n"; |
| 47 |
} |
| 48 |
|
| 49 |
unless ( -x "$source_directory/sql/mysqld") { |
| 50 |
die "Can't find executable mysqld. Sources don't seem to have been compiled.\n"; |
| 51 |
} |
| 52 |
|
| 53 |
unless ($sandbox_apps{$sandbox_type}) { |
| 54 |
get_help("unsupported sandbox type ($sandbox_type)"); |
| 55 |
} |
| 56 |
|
| 57 |
chdir $source_directory |
| 58 |
or die "can't change path to <$source_directory>\n"; |
| 59 |
|
| 60 |
my $source_version = get_source_version(); |
| 61 |
|
| 62 |
my $old_tarball = last_tarball(); |
| 63 |
|
| 64 |
my $new_tarball; |
| 65 |
|
| 66 |
if ( $old_tarball ) { |
| 67 |
my @tgz = file_age($old_tarball); |
| 68 |
my @mysqld = file_age("./sql/mysqld"); |
| 69 |
my @dir = file_age($source_version) ; |
| 70 |
unless (@dir) { |
| 71 |
@dir = ('---', 0); |
| 72 |
} |
| 73 |
if (($mysqld[1] > $tgz[1]) or ($mysqld[1] > $dir[1])) { |
| 74 |
print " new build. The tarball needs to be redone\n"; |
| 75 |
$new_tarball = build_tarball(); |
| 76 |
# removing the directory |
| 77 |
if ( -d $source_version ) { |
| 78 |
remove_source_dir($source_version); |
| 79 |
} |
| 80 |
} |
| 81 |
elsif ($tgz[1] > $dir[1]) { |
| 82 |
print "tarball newer than the version directory\n"; |
| 83 |
# removing the directory |
| 84 |
if ( ( -d $source_version) && (( $tgz[1] - $dir[1] ) > 60 )) { |
| 85 |
remove_source_dir($source_version); |
| 86 |
} |
| 87 |
$new_tarball = $old_tarball; |
| 88 |
} |
| 89 |
elsif ( $dir[1] >= $tgz[1] ) { |
| 90 |
print "tarball older than directory\n"; |
| 91 |
# Nothing to be done |
| 92 |
$new_tarball = $source_version; |
| 93 |
} |
| 94 |
else { |
| 95 |
print "dir @dir\n"; |
| 96 |
print "mysqld @mysqld\n"; |
| 97 |
print "tgz @tgz\n"; |
| 98 |
die "unhandled \n"; |
| 99 |
} |
| 100 |
} |
| 101 |
else { |
| 102 |
print "no old tarball found\n"; |
| 103 |
$new_tarball = build_tarball(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
my $options = ''; |
| 108 |
if ( $sandbox_type eq 'circular') { |
| 109 |
$options = '--topology=circular'; |
| 110 |
} |
| 111 |
|
| 112 |
system "$sandbox_apps{$sandbox_type} $source_directory/$new_tarball $options @ARGV"; |
| 113 |
|
| 114 |
# |
| 115 |
# functions |
| 116 |
# |
| 117 |
|
| 118 |
sub build_tarball { |
| 119 |
my $result = system "./scripts/make_binary_distribution"; |
| 120 |
|
| 121 |
if ($result or $? ) { |
| 122 |
die "error creating binary tarball. ($result - $? - $! )"; |
| 123 |
} |
| 124 |
|
| 125 |
my $tarball = last_tarball() |
| 126 |
or die "can't find a tarball\n"; |
| 127 |
return $tarball; |
| 128 |
} |
| 129 |
|
| 130 |
sub get_source_version { |
| 131 |
# |
| 132 |
# Parses 'Makefile' for server version. |
| 133 |
# |
| 134 |
# 'Makefile' exists only after the server is compiled |
| 135 |
# but this is just as well in our case, since we only |
| 136 |
# need to run this script after the server is fully built. |
| 137 |
# This method should be more robust than parsing |
| 138 |
# configure.in |
| 139 |
# |
| 140 |
|
| 141 |
my $version; |
| 142 |
my $version_file; |
| 143 |
if ( -f 'VERSION' ) |
| 144 |
{ |
| 145 |
$version_file = 'VERSION'; |
| 146 |
} |
| 147 |
# when build tree differs from source tree |
| 148 |
elsif ( -f 'VERSION.dep' ) |
| 149 |
{ |
| 150 |
$version_file = 'VERSION.dep'; |
| 151 |
} |
| 152 |
if ( $version_file ) |
| 153 |
{ |
| 154 |
open my $VFH, q{<}, $version_file |
| 155 |
or die "can't open $version_file\n"; |
| 156 |
while (my $line = <$VFH>) |
| 157 |
{ |
| 158 |
if ($line =~ /MYSQL_VERSION_(?:MAJOR|MINOR|PATCH)=(\d+)/) |
| 159 |
{ |
| 160 |
if (defined $version) |
| 161 |
{ |
| 162 |
$version .= '.'; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
$version = ''; |
| 167 |
} |
| 168 |
$version .= $1; |
| 169 |
} |
| 170 |
} |
| 171 |
unless ($version =~ /\d+\.\d+\.\d+/) |
| 172 |
{ |
| 173 |
die "could not find a version number in $version_file ($version)\n"; |
| 174 |
} |
| 175 |
return $version; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
print "# There is no VERSION or VERSION.dep file in this directory.\n# Now trying with Makefile\n" if $MySQL::Sandbox::DEBUG; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
open my $MAKEFILE, q{<}, 'Makefile' |
| 184 |
or die "can't find Makefile\n"; |
| 185 |
while (my $line = <$MAKEFILE>) { |
| 186 |
if ($line =~ / |
| 187 |
^ # start of line |
| 188 |
\s* # optional whitespace |
| 189 |
MYSQL_NO_DASH_VERSION # literal |
| 190 |
\s* # optional whitespace |
| 191 |
= # equals sign |
| 192 |
\s* # optional whitespace |
| 193 |
(\d\.\d.\d+) # capture the version |
| 194 |
\s* # optional whitespace |
| 195 |
$ # end of line |
| 196 |
/x) { |
| 197 |
$version = $1; |
| 198 |
last; |
| 199 |
} |
| 200 |
} |
| 201 |
close $MAKEFILE; |
| 202 |
if ($version) { |
| 203 |
return $version; |
| 204 |
} |
| 205 |
else { |
| 206 |
die "can't find a version in Makefile\n"; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
#sub get_source_version { |
| 212 |
# open my $CONFIG, q{<}, 'configure.in' |
| 213 |
# or die "can't find configure.in\n"; |
| 214 |
# my $version; |
| 215 |
# while (my $line = <$CONFIG>) { |
| 216 |
# if ($line =~ / |
| 217 |
# (?:AM_INIT_AUTOMAKE |
| 218 |
# | |
| 219 |
# AC_INIT) # either of these macros |
| 220 |
# \D+ # followed by one or more non digit |
| 221 |
# (?:(?i)mysql) # followed by "mysql" in any case |
| 222 |
# \D+ # followed by one or more non digit |
| 223 |
# (\d\.\d.\d+) # capture the version |
| 224 |
# /x) { |
| 225 |
# $version = $1; |
| 226 |
# last; |
| 227 |
# } |
| 228 |
# } |
| 229 |
# close $CONFIG; |
| 230 |
# if ($version) { |
| 231 |
# return $version; |
| 232 |
# } |
| 233 |
# else { |
| 234 |
# die "can't find a version in configure.in\n"; |
| 235 |
# } |
| 236 |
#} |
| 237 |
|
| 238 |
sub last_tarball { |
| 239 |
my %files = map {file_age($_)} |
| 240 |
grep {$_ !~ /^mysql-\d\.\d\.\d+\.tar\.gz$/} |
| 241 |
glob("*.tar.gz"); |
| 242 |
return unless keys %files; |
| 243 |
return (sort { $files{$b} <=> $files{$a} } keys %files)[0]; |
| 244 |
} |
| 245 |
|
| 246 |
sub file_age { |
| 247 |
my ($filename) = @_; |
| 248 |
return unless -e $filename; |
| 249 |
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
| 250 |
$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); |
| 251 |
return ($filename, $atime); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
sub get_help{ |
| 256 |
my ($msg) = @_; |
| 257 |
print $msb->credits(), "\n"; |
| 258 |
if ($msg) { |
| 259 |
print "*** $msg\n"; |
| 260 |
} |
| 261 |
print <<END_HELP; |
| 262 |
Syntax: $0 source_directory sandbox_type [options] |
| 263 |
|
| 264 |
source directory is where you have successfully |
| 265 |
run ./configure && make |
| 266 |
|
| 267 |
sandbox_type is one of |
| 268 |
single |
| 269 |
replication |
| 270 |
circular |
| 271 |
multiple |
| 272 |
|
| 273 |
options is anything that is needed by the sandbox |
| 274 |
application of your choice |
| 275 |
|
| 276 |
END_HELP |
| 277 |
exit 1; |
| 278 |
} |
| 279 |
|
| 280 |
sub remove_source_dir { |
| 281 |
my ($dir) = @_; |
| 282 |
system ("rm -rf $dir"); |
| 283 |
} |
| 284 |
|
| 285 |
__END__ |