Skip to content

Commit 08bdf9e

Browse files
committed
Enable to use ARCHIVE and RECIPE.
1 parent 7a0a103 commit 08bdf9e

3 files changed

Lines changed: 76 additions & 7 deletions

File tree

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,26 @@ up-cgi: up-common
9090
up-psgi: MT_RUN_VIA=psgi
9191
up-psgi: up-common
9292

93+
94+
ifeq (${RECIPE},)
95+
ARCHIVE_FOR_SETUP=""
96+
else
97+
ARCHIVE_FOR_SETUP=${ARCHIVE}
98+
endif
99+
93100
up-common: down fixup
94101
${MAKE} down-mt-home-volume
95102
${DOCKER} volume create --label mt-dev-mt-home-tmp mt-dev-mt-home-tmp
96103

97104
ifneq (${ARCHIVE},)
105+
ifeq (${RECIPE},)
98106
# TBD: random name?
99107
$(eval MT_HOME_PATH=mt-dev-mt-home-tmp)
100108
${MAKEFILE_DIR}/bin/extract-archive ${BASE_ARCHIVE_PATH} ${MT_HOME_PATH} $(shell echo ${ARCHIVE} | tr ',' ' ')
109+
endif
101110
endif
102111

103-
$(eval export _ARGS=$(shell UPDATE_BRANCH=${UPDATE_BRANCH} ${MAKEFILE_DIR}/bin/setup-environment --recipe "$(shell echo ${RECIPE} | tr ',' ' ')" --repo "$(shell echo ${REPO} | tr ',' ' ')" --pr "$(shell echo ${PR} | tr ',' ' ')"))
112+
$(eval export _ARGS=$(shell UPDATE_BRANCH=${UPDATE_BRANCH} ${MAKEFILE_DIR}/bin/setup-environment --recipe "$(shell echo ${RECIPE} | tr ',' ' ')" --repo "$(shell echo ${REPO} | tr ',' ' ')" --pr "$(shell echo ${PR} | tr ',' ' ')" --archive "$(shell echo ${ARCHIVE_FOR_SETUP} | tr ',' ' ')"))
104113
ifneq (${RECIPE},)
105114
@perl -e 'exit(length($$ENV{_ARGS}) > 0 ? 0 : 1)'
106115
endif
@@ -128,6 +137,7 @@ endif
128137

129138
down:
130139
${_DC} down --remove-orphans ${DOWN_ARGS}
140+
${MAKEFILE_DIR}/bin/teardown-environment
131141
${MAKE} down-mt-home-volume
132142

133143
down-mt-home-volume:

bin/setup-environment

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@ use utf8;
77
use FindBin;
88
use lib "$FindBin::Bin/local/lib/perl5";
99

10-
use Cwd qw(realpath);
10+
use Cwd qw(realpath getcwd);
11+
use IO::File;
1112
use Data::Dumper;
13+
use Digest::MD5;
1214
use Getopt::Long;
1315
use HTTP::Tiny;
1416
use Config::Tiny;
1517
use YAML::Tiny qw(LoadFile DumpFile);
16-
use File::Basename qw(basename);
18+
use File::Basename qw(basename fileparse);
1719
use File::Spec::Functions qw(catfile);
18-
use File::Temp qw(tempfile tmpnam);
20+
use File::Temp qw(tempfile tmpnam tempdir);
1921

2022
GetOptions(
21-
'recipe=s' => \my $recipes,
22-
'repo=s' => \my $repos,
23-
'pr=s' => \my $pull_reqs,
23+
'recipe=s' => \my $recipes,
24+
'repo=s' => \my $repos,
25+
'pr=s' => \my $pull_reqs,
26+
'archive=s' => \my $archives,
2427
);
2528

2629
my $recipe_dir = realpath("$FindBin::Bin/../recipe");
2730
my $repo_dir = realpath("$FindBin::Bin/../repo");
2831
my $repo_base_dir = realpath("$FindBin::Bin/../..");
32+
my $archive_dir = realpath("$FindBin::Bin/../archive");
2933
my $git_clone_cmd = realpath("$FindBin::Bin/git-clone");
3034

3135
my $env = {};
@@ -221,6 +225,58 @@ for my $r ( split /\s+/, $repos ) {
221225
handle_recipe( { $r => { directory => $directory, }, } );
222226
}
223227

228+
for my $a ( split /\s+/, $archives ) {
229+
my $file_path;
230+
my ( $basename, $ext ) = ( fileparse( $a, qr/\.[a-zA-Z\.]+$/ ) )[ 0, 2 ];
231+
232+
if ( $a =~ m{^https?://} ) {
233+
234+
# download
235+
my $response = HTTP::Tiny->new->get($a);
236+
die "Failed: @{[Dumper($response)]}"
237+
unless $response->{success} && length $response->{content};
238+
239+
( my $fh, $file_path ) = tempfile( undef, SUFFIX => $ext );
240+
print $fh $response->{content};
241+
}
242+
elsif ( $a =~ m{^/} ) {
243+
$file_path = $a;
244+
}
245+
else {
246+
$file_path = catfile( $archive_dir, $a );
247+
}
248+
249+
die "File not found: $a" unless -f $file_path;
250+
251+
# extract archive
252+
my $cwd = getcwd();
253+
my $directory = tempdir( '/tmp/mt-dev-archive-temp-XXXXX', );
254+
255+
chdir $directory;
256+
my $extract_res
257+
= $file_path =~ m/\.zip$/i
258+
? `unzip -q $file_path`
259+
: `tar zxf $file_path`;
260+
if ($?) {
261+
die "Failed to extract: $a";
262+
}
263+
264+
my $md5_ctx = Digest::MD5->new;
265+
$md5_ctx->addfile( IO::File->new( $file_path, 'r' ) );
266+
print STDERR join( ':', $basename . $ext, $md5_ctx->hexdigest ) . "\n";
267+
268+
my @entries = grep {
269+
!m{^(addons|mt-static|php|alt-tmpl|plugins|default_templates|search_templates|extlib|themes|import|tmpl|tools|lib|.*\.cgi)$}
270+
} glob('*');
271+
if ( @entries == 1 ) {
272+
$directory = catfile( $directory, $entries[0] );
273+
}
274+
chdir $cwd;
275+
276+
$basename =~ s{\.}{_}g; # "." causes problems in yaml files
277+
handle_recipe( { $basename => { directory => $directory, }, } );
278+
}
279+
224280
if (@$yamls) {
225281
$env->{DOCKER_COMPOSE_YML_OVERRIDE} = join( ' ', map {"-f $_"} @$yamls );
226282
}

bin/teardown-environment

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash -e
2+
3+
rm -fr /tmp/mt-dev-archive-temp-*

0 commit comments

Comments
 (0)