| 1 |
#!/usr/bin/perl |
| 2 |
# make_sandbox_from_installed |
| 3 |
# The MySQL Sandbox |
| 4 |
# Copyright (C) 2006-2016 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 exists_in_path greater_version); |
| 21 |
my $repository=$ENV{SANDBOX_REPO} || 'https://github.com/datacharmer/mysql-docker-minimal/blob/master/dbdata'; |
| 22 |
my $sandbox_binary=$ENV{SANDBOX_BINARY} || "$ENV{HOME}/opt/mysql"; |
| 23 |
my $pound_line = '#' x 80; |
| 24 |
my %supported_versions = |
| 25 |
( |
| 26 |
'5.0' => ['5.0.96'], |
| 27 |
'5.1' => ['5.1.72'], |
| 28 |
'5.5' => ['5.5.51', '5.5.52' ], |
| 29 |
'5.6' => ['5.6.32', '5.6.33' ], |
| 30 |
'5.7' => ['5.7.14', '5.7.15' ], |
| 31 |
'8.0' => ['8.0.0' ] |
| 32 |
); |
| 33 |
|
| 34 |
runs_as_root(); |
| 35 |
# --------------------------------------------------------------------------------- |
| 36 |
# main |
| 37 |
# --------------------------------------------------------------------------------- |
| 38 |
|
| 39 |
my $remote_version = shift |
| 40 |
or get_help("version needed"); |
| 41 |
|
| 42 |
my $wget = exists_in_path ('wget'); |
| 43 |
unless ($wget ) |
| 44 |
{ |
| 45 |
get_help( 'wget was not found in PATH' ); |
| 46 |
} |
| 47 |
|
| 48 |
unless ($^O =~ /linux/i) |
| 49 |
{ |
| 50 |
get_help("This operating system ($^O) is not supported"); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
%supported_versions = update_supported_versions(); |
| 55 |
|
| 56 |
if (exists $supported_versions{$remote_version}) |
| 57 |
{ |
| 58 |
$remote_version = get_latest($remote_version); |
| 59 |
} |
| 60 |
elsif ($remote_version =~ /^(\d+\.\d+)/) |
| 61 |
{ |
| 62 |
my $short_version = $1; |
| 63 |
if ((exists $supported_versions{$short_version} ) |
| 64 |
&& grep { $remote_version eq $_} @{ $supported_versions{$short_version}} ) |
| 65 |
{ |
| 66 |
# Do nothing. The request version exists |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
get_help( "Could not find $remote_version"); |
| 71 |
} |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
get_help( "Unsupported version $remote_version") |
| 76 |
} |
| 77 |
if ($remote_version =~ /^(-h|--help)$/) |
| 78 |
{ |
| 79 |
get_help(); |
| 80 |
} |
| 81 |
|
| 82 |
my $download_file="$repository/$remote_version.tar.gz"; |
| 83 |
|
| 84 |
if ( ! -d $sandbox_binary ) |
| 85 |
{ |
| 86 |
system "mkdir -p $sandbox_binary"; |
| 87 |
if ($?) |
| 88 |
{ |
| 89 |
die "Can't create $sandbox_binary ($!)\n"; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! -d $sandbox_binary ) |
| 94 |
{ |
| 95 |
get_help("Unable to use or create $sandbox_binary") |
| 96 |
} |
| 97 |
|
| 98 |
chdir $sandbox_binary; |
| 99 |
|
| 100 |
if ( -d $remote_version) |
| 101 |
{ |
| 102 |
get_help( "\n$pound_line\n" |
| 103 |
. "### Version $remote_version already exists in $sandbox_binary\n" |
| 104 |
. "### Run 'make_sandbox $remote_version' to use it\n" |
| 105 |
. $pound_line |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
my $local_file = "$remote_version.tar.gz"; |
| 110 |
print "wget -nv -O $local_file '$download_file?raw=true' \n"; |
| 111 |
system "wget -nv -O $local_file '$download_file?raw=true' "; |
| 112 |
if ($?) |
| 113 |
{ |
| 114 |
die "Error downloading $download_file\n"; |
| 115 |
} |
| 116 |
|
| 117 |
if ( ! -f $local_file) |
| 118 |
{ |
| 119 |
die "Error downloading $download_file. Can't find '$local_file'\n"; |
| 120 |
} |
| 121 |
|
| 122 |
system "tar -xzf $local_file"; |
| 123 |
if ($?) |
| 124 |
{ |
| 125 |
die "Error extracting from $local_file\n"; |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! -d $remote_version) |
| 129 |
{ |
| 130 |
die "Directory $remote_version was not created\n"; |
| 131 |
} |
| 132 |
|
| 133 |
$ENV{SANDBOX_BINARY} = $sandbox_binary; |
| 134 |
$ENV{PWD} = $sandbox_binary; |
| 135 |
|
| 136 |
exec "make_sandbox $remote_version @ARGV"; |
| 137 |
|
| 138 |
# --------------------------------------------------------------------------------- |
| 139 |
# Functions |
| 140 |
# --------------------------------------------------------------------------------- |
| 141 |
sub get_help |
| 142 |
{ |
| 143 |
my ($msg) = @_; |
| 144 |
|
| 145 |
print MySQL::Sandbox::credits(); |
| 146 |
if ($msg) |
| 147 |
{ |
| 148 |
print "### $msg\n"; |
| 149 |
} |
| 150 |
print<<END_HELP1; |
| 151 |
|
| 152 |
This script tries to create a sandbox using binaries downloaded from the web. |
| 153 |
The success of this scripts depends on good connectivity |
| 154 |
and availability of downloading tools. |
| 155 |
########################## |
| 156 |
# IT ONLY WORKS ON LINUX |
| 157 |
########################## |
| 158 |
The repository of available binaries is at $repository . |
| 159 |
Supported: |
| 160 |
END_HELP1 |
| 161 |
|
| 162 |
for my $major (sort keys %supported_versions ) |
| 163 |
{ |
| 164 |
print "# $major -> [ @{ [ @{$supported_versions{$major}} ]} ] \n"; |
| 165 |
} |
| 166 |
|
| 167 |
print<<END_HELP2; |
| 168 |
The binaries are imported into $sandbox_binary . |
| 169 |
|
| 170 |
make_sandbox_from_url X.X.XX [options] |
| 171 |
|
| 172 |
where X.X.XX is the version number. |
| 173 |
You can then pass any options accepted by make_sandbox. |
| 174 |
END_HELP2 |
| 175 |
|
| 176 |
if ($msg) |
| 177 |
{ |
| 178 |
exit 1; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
exit 0; |
| 183 |
} |
| 184 |
} |
| 185 |
sub get_latest |
| 186 |
{ |
| 187 |
my ($wanted) = @_; |
| 188 |
my @versions = @{ $supported_versions{$wanted}}; |
| 189 |
return $versions[-1]; |
| 190 |
} |
| 191 |
|
| 192 |
sub update_supported_versions |
| 193 |
{ |
| 194 |
my $catalog = "$repository/available.txt"; |
| 195 |
my $return_self =0; |
| 196 |
unless ($ENV{GETLOCAL} ) |
| 197 |
{ |
| 198 |
system "wget -nv -O /tmp/available.txt '$catalog?raw=true' "; |
| 199 |
if ($?) |
| 200 |
{ |
| 201 |
warn "Error downloading $catalog\n"; |
| 202 |
$return_self =1; |
| 203 |
} |
| 204 |
} |
| 205 |
if ( ! -f '/tmp/available.txt' ) |
| 206 |
{ |
| 207 |
warn "Error downloading $catalog. Can't find 'available.txt'\n"; |
| 208 |
$return_self = 1; |
| 209 |
} |
| 210 |
if ($return_self) |
| 211 |
{ |
| 212 |
return %supported_versions; |
| 213 |
} |
| 214 |
|
| 215 |
open my $FH , '<', '/tmp/available.txt' |
| 216 |
or die "Error opening /tmp/available.txt\n"; |
| 217 |
my %latest_versions; |
| 218 |
while (my $line = readline($FH)) |
| 219 |
{ |
| 220 |
chomp $line; |
| 221 |
next if $line =~ /^\s*$/; |
| 222 |
next if $line =~ /^\s*#/; |
| 223 |
if ($line =~ /(\S+)\s*=\s*(.*)/) |
| 224 |
{ |
| 225 |
my $key = $1; |
| 226 |
my $value = $2; |
| 227 |
my @value_list = split ' ', $value; |
| 228 |
$latest_versions{$key} = \@value_list; |
| 229 |
} |
| 230 |
} |
| 231 |
close $FH; |
| 232 |
return %latest_versions; |
| 233 |
} |
| 234 |
|