Debian Codegraph @

bin/make_sandbox

raw
1 #!/usr/bin/perl
2 # 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 Data::Dumper;
21 use English qw( -no_match_vars );
22 use MySQL::Sandbox qw( runs_as_root sbinstr exists_in_path deprecation_notice);
23 use Getopt::Long ;
24
25
26 my $default_binary_base = $ENV{SANDBOX_BINARY} || ($ENV{HOME} . '/opt/mysql');
27 unless ( -d $default_binary_base ) {
28 $default_binary_base = '/opt/mysql';
29 }
30 my $binary_base = $ENV{'BINARY_BASE'} || $default_binary_base;
31
32 my @prefixes = ('mysql', 'percona', 'mariadb');
33 my $default_prefix= 'mysql';
34 my %options = (
35 export_binaries => 0,
36 add_prefix => '',
37
38 );
39
40 my $msb = MySQL::Sandbox->new();
41
42 runs_as_root();
43
44
45 GetOptions(
46 export_binaries => \$options{export_binaries},
47 'add_prefix=s' => \$options{add_prefix},
48 # 'c|comment=s@' => \$options{comments} ,
49 'help' => \$options{help},
50 ) or get_help('+++');
51
52 get_help() if $options{help};
53
54 my $where_to_install = shift
55 or get_help("Binary tarball or expanded tarball directory needed\n");
56
57
58 my $version;
59
60 my ($major, $minor, $release);
61
62 if ($where_to_install =~ s{^(URL|WEB|GET|FETCH):}{}i)
63 {
64 if ( exists_in_path("make_sandbox_from_url"))
65 {
66 if (@ARGV)
67 {
68 exec ( "make_sandbox_from_url", $where_to_install, '--', @ARGV) ;
69 }
70 else
71 {
72 exec ( "make_sandbox_from_url", $where_to_install) ;
73 }
74 }
75 elsif ( $where_to_install =~ m{^https://})
76 {
77 die "URL requested, but make_sandbox_from_url not found in PATH"
78 }
79 }
80
81 $where_to_install =~ s{ / \s* $ }{}x;
82
83 $where_to_install =~ s/^\s*\~/$ENV{HOME}/x;
84
85 if ( $where_to_install =~ m{\.\./}) # it is a tricky path
86 {
87 die "relative paths must not contain '../' \n";
88 }
89
90
91 # explicit current directory relative path
92 if ( ((-f $where_to_install) # the file exists
93 and ( $where_to_install =~ m{^\./}) # it identifies the current directory
94 )
95 ) {
96 $where_to_install =~ s[^\./][$ENV{PWD}/] ;
97 unless ( ( -f $where_to_install) or (-d $where_to_install) ) {
98 die "file not found in current directory ($where_to_install)\n";
99 }
100 }
101
102 # relative path
103 if ( (( (-f $where_to_install) or ( -d $where_to_install)) # the file exists
104 and ( $where_to_install !~ m{^/}) # it is not an absolute path
105 )
106 ) {
107 $where_to_install = $ENV{PWD} . '/' . $where_to_install;
108 unless ( ( -f $where_to_install) or (-d $where_to_install) ) {
109 die "file not found in current directory ($where_to_install)\n";
110 }
111 }
112
113 my $prefix_regex = join('|', @prefixes );
114 $prefix_regex = qr/(?:$prefix_regex)/i;
115
116 #my $ignore_leading_number = 0;
117 sub basename
118 {
119 my ($path) = @_;
120 $path =~ s{.*/}{};
121 $path;
122 }
123
124 my $barename = basename($where_to_install);
125 if ($barename =~ /^\d+\..*\.tar\.gz/)
126 {
127 # $ignore_leading_number = 1;
128 if ($barename eq $where_to_install)
129 {
130 $where_to_install="$ENV{PWD}/$barename";
131 }
132 }
133
134 # current directory
135 if ( $where_to_install =~ m{^(?:\d+\.)?$prefix_regex\D+\d+\.\d+\.\d+.*\.tar\.gz$ }x ) {
136 $where_to_install = $ENV{PWD} . '/' . $where_to_install;
137 unless ( -f $where_to_install ) {
138 die "file not found in current directory ($where_to_install)\n";
139 }
140 }
141
142 # bare version directory under $HOME/opt/bin
143 if ( $where_to_install =~ /^(?:[-_a-zAZ]+)?(\d+)\.(\d+)\.(\d+)/x ) {
144 ($major, $minor, $release) = ($1, $2, $3);
145 $version = $where_to_install;
146 }
147 # absolute version directory
148 elsif ( $where_to_install =~ m{^(/.+)/(\d+)\.(\d+)\.(\d+)$}x ) {
149 ($binary_base, $major, $minor, $release) = ($1, $2, $3, $4);
150 $version = "$major.$minor.$release";
151 unless ( -d $where_to_install ) {
152 die "directory not found ($where_to_install)\n";
153 }
154 }
155
156 # absolute version directory with prefix
157 elsif (( -d "$ENV{SANDBOX_BINARY}/$where_to_install") && ($where_to_install =~ m{(\d+)\.(\d+)\.(\d+)$}x )) {
158 ($binary_base, $major, $minor, $release) = ($ENV{SANDBOX_BINARY}, $1, $2, $3, $4);
159 $version = "$major.$minor.$release";
160 }
161
162 # absolute version directory
163 elsif ( $where_to_install =~ m{^(/.+)/.*(\d+)\.(\d+)\.(\d+)$}x ) {
164 ($binary_base, $major, $minor, $release) = ($1, $2, $3, $4);
165 # print ">>$binary_base, $major, $minor, $release\n"; exit;
166 $version = "$major.$minor.$release";
167 unless ( -d $where_to_install ) {
168 die "directory not found ($where_to_install)\n";
169 }
170 }
171
172 # full path to tarball
173 elsif ( $where_to_install =~ m{^(/.+)/(?:\d+\.)?($prefix_regex\D+(\d+)\.(\d+)\.(\d+)[^/]*)\.tar\.gz$ }x ) {
174 my $new_dir;
175 unless ( -f $where_to_install ) {
176 die "tarball file not found ($where_to_install)\n";
177 }
178 ($binary_base, $new_dir, $major, $minor, $release) = ($1, $2, $3, $4, $5);
179 print "unpacking $where_to_install\n";
180 $version = "$major.$minor.$release";
181 my $tar_executable='no_such_program';
182 my $full_new_dir = "$binary_base/$options{add_prefix}$version";
183 if ( -d $full_new_dir ) {
184 $where_to_install = $full_new_dir;
185 }
186 else {
187 my $original_dir = $ENV{PWD};
188 my @recognized_tar_executables = qw(gtar gnutar bsdtar);
189 my $which_tar;
190 my $tar_found = 0;
191 for my $tar_exec (@recognized_tar_executables) {
192 $which_tar = exists_in_path($tar_exec);
193 if ((!$which_tar) or ($which_tar =~ /^no/i)) {
194 next
195 }
196 else {
197 $tar_found = 1; last
198 }
199 }
200 unless ($tar_found ) {
201 $which_tar = exists_in_path( 'tar' );
202 if ((!$which_tar) or ($which_tar =~ /^no/i)) {
203 $which_tar = undef;
204 }
205 }
206 if ($which_tar) {
207 chomp $which_tar;
208 $tar_executable = $which_tar;
209 my $tar_version = qx($tar_executable --version)
210 or die "can't find tar program\n";
211 unless ($tar_version =~ /(?:bsdtar|gnu\s+tar)/i) {
212 die "this version of tar is not supported\n";
213 }
214 }
215 else {
216 die "tar program not found\n";
217 }
218 chdir $binary_base
219 or die "can't change directory to $binary_base\n";
220 my $tar_file = $where_to_install;
221 $tar_file =~ s{^.*/}{};
222 my $result = system("gunzip -c $tar_file | $tar_executable -xf -");
223 if ($result) {
224 die "error unpacking $tar_file ($!)\n";
225 }
226 my $new_name = $options{add_prefix} . $version;
227 if ($options{export_binaries} && ( -d $default_binary_base)) {
228 $new_name = "$default_binary_base/$options{add_prefix}$version";
229 if ( -d $new_name) {
230 warn "can't export to $binary_base. Directory $version already exists!\n";
231 $new_name = $options{add_prefix} . $version;
232 }
233 else {
234 $full_new_dir = $new_name;
235 $binary_base = $default_binary_base;
236 }
237 }
238 my $rename_result = system "mv $new_dir $new_name";
239 if ($rename_result) {
240 die "can't rename $new_dir to $new_name";
241 }
242 # in case it is a debug build
243 if (-f "$new_name/bin/mysqld-debug" && ! -f "$new_name/bin/mysqld") {
244 system "cd $new_name/bin && ln -s mysqld-debug mysqld";
245 }
246 # some versions of Perl and OS can't perform a rename across file systems
247 # Bug #504789
248 #rename $new_dir, $new_name
249 # or die "can't rename $new_dir to $new_name";
250
251 system "chmod -R ogu+r $new_name "; # Not sandbox related.
252 # It is needed if the user wants
253 # to run the test suite
254
255 $where_to_install = $full_new_dir;
256 chdir $original_dir;
257 }
258 }
259 # not a full path
260 elsif ($where_to_install =~ m{[^/].*\.tar\.gz$} ) {
261 print "<$where_to_install>\n";
262 get_help( "You must enter a full path to the tarball. Relative paths are not supported.\n");
263 }
264 # not a tarball
265 elsif ($where_to_install =~ m{^/.*(?<!\.tar\.gz)$} ) {
266 get_help( "Not a tarball ($where_to_install).\n"
267 . "Accepted paramethers are: {VERSION|tarball_full_name} [options].\n");
268 }
269 # nothing was recognized. Print help
270 else {
271 get_help("Failed to parse arguments\n");
272 }
273
274 sub get_help {
275 my ($msg) = @_;
276 print $msb->credits(), "\n";
277 if ($msg) {
278 my $len = length($msg);
279 $len = 80 if $len > 80;
280 print '*' x $len, "\n",
281 $msg,
282 '*' x $len, "\n";
283 }
284 deprecation_notice();
285 print <<SYNTAX;
286 usage: ./make_sandbox full_version [options] {VERSION|tarball_full_name} -- [more options]
287 or : ./make_sandbox GET:short_version -- [options]
288
289 --export_binaries exports the new binary to $binary_base
290 --add_prefix=NAME add given prefix to renamed tarball directory
291
292 Additionally, you can pass any option accepted by "low_level_make_sandbox" after '--'
293 SYNTAX
294
295 print "You should provide either a version from '$binary_base' \n",
296 "or an absolute path to the tarball to extract.\n",
297 "See ./low_level_make_sandbox --help for more detail\n";
298 print "full_version should be provided as #.#.#\n";
299 print "short_version should be provided as #.#\n";
300 exit ($msg ? 1 : 0);
301 #print q{-} x 50, "\n";
302 #exec "$install_dir/low_level_make_sandbox --help ";
303 }
304
305 my ($bare_basedir) = (split /\//, $where_to_install)[-1];
306
307 # print "<$bare_basedir>\n";exit;
308
309 unless ( -d $binary_base ) {
310 die "$binary_base does not exist\n";
311 }
312
313 my @supported_versions = @{ MySQL::Sandbox::supported_versions() };
314
315 my $simple_version = "$major.$minor";
316
317 unless ( grep { $simple_version eq $_ } @supported_versions) {
318 die "unsupported version $simple_version\n";
319 }
320
321 if ($release < 10) {
322 $release = '0' . $release;
323 }
324
325 my $port = ($major . $minor. $release) ;
326
327 if ($port < 1024) {
328 $port .= '0';
329 }
330
331 if ( -d "$binary_base/$bare_basedir" ) {
332
333 my $text_version = $version;
334 # $text_version =~ tr/./_/d; ## no critic
335 $text_version =~ s/\./_/g;
336 my @install_options = (
337 "--basedir=$binary_base/$bare_basedir",
338 "--sandbox_directory=msb_$options{add_prefix}$text_version",
339 "--install_version=$simple_version",
340 "--sandbox_port=$port",
341 "--no_ver_after_name",
342 @ARGV
343 );
344 if ($major >=4) {
345 push(@install_options, qq(--my_clause=log-error=msandbox.err));
346 }
347 unless (grep {$_ eq '--no_show'} @install_options) {
348 print "Executing low_level_make_sandbox ", join( " \\\n\t", @install_options ), "\n";
349 }
350 sbinstr( " low_level_make_sandbox called with params <"
351 . join(";", @install_options) . ">");
352 exec "low_level_make_sandbox", @install_options;
353 }
354 else {
355 die "there is no $bare_basedir directory under $binary_base\n";
356 }
357