-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpack
executable file
·192 lines (156 loc) · 5.04 KB
/
pack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/perl
# vim:ft=perl:ts=2:sts=2:sw=2
use strict;
use warnings;
use Digest::SHA;
use MIME::Base64 qw(encode_base64);
use POSIX qw(strftime);
######################################################################
if (grep {/^(-h|--help)$/} @ARGV) {
printf STDERR "usage: ./pack [<version> [<output path/file>]]\n\n";
exit 0;
}
system "perl -c -Ilib bin/genesis";
die "Perl compilation failed, not packing\n" if $?;
# ---
# Build package structure
# ---
system 'rm -rf pkg/ pkg.tar.gz && mkdir pkg';
die "Could not create a clean package staging area\n" if $?;
system 'cp -a lib pkg/';
die "Could not copy library into the package staging area\n" if $?;
system 'for f in $(find pkg/lib -name ".*.sw?") ; do rm "$f"; done';
die "Could not remove swap files in the package staging area\n" if $?;
# ---
# Version Handling
# ---
my $stub_out = ($ENV{GENESIS_PACK_PATH}||".")."/genesis-";
my $VERSION = $ARGV[0] || '';
chomp(my $sha1 = qx(git rev-parse HEAD));
$sha1 = substr($sha1, 0, 10);
if ($VERSION) {
$VERSION =~ s/^v//i;
$stub_out .= $VERSION;
} else {
$VERSION = '2.x.x-dev';
$stub_out .= "dev-$sha1";
}
if (qx(git status --porcelain bin/genesis lib/)) {
$sha1 .= '+';
$stub_out .= '-dirty';
}
$stub_out = $ARGV[1] if $ARGV[1];
my $BUILD = "($sha1) build ".strftime("%Y%m%d.%H%M%S", gmtime);
# ---
# Brand script with version
# ---
open my $out, ">", "pkg/genesis"
or die "Failed to open pkg/genesis for writing: $!\n";
open my $in, "<", "bin/genesis"
or die "Failed to open bin/genesis for reading: $!\n";
while (<$in>) {
s/^(\$Genesis::VERSION) = .*$/$1 = \"$VERSION\";/;
s/^(\$Genesis::BUILD) = .*$/$1 = \" $BUILD\";/;
print $out $_;
}
close $out;
close $in;
chmod 0755, "pkg/genesis";
# ---
# Assemble the tarball (with embedded checksum)
# ---
my $fh;
system('tar -cf pkg.tar --no-xattrs -C pkg .') == 0
or die "Failed to compile runtime archive: $!\n";
open $fh, "<", "pkg.tar"
or die "Failed to reopen runtime archive for reading: $!\n";
my $sha = Digest::SHA->new('sha1');
$sha->addfile($fh);
my $checksum = $sha->hexdigest;
open $fh, ">", "pkg/checksum"
or die "Failed to open checksum file for writing: $!\n";
print $fh "$checksum\n";
close $fh;
open $fh, ">", "pkg/version"
or die "Failed to open version file for writing: $!\n";
print $fh "$VERSION $BUILD\n";
close $fh;
system('tar -rf pkg.tar --no-xattrs -C pkg ./checksum ./version') == 0
or die "Failed to append checksum file to runtime archive: $!\n";
system('gzip -9 pkg.tar') == 0
or die "Failed to compress runtime archive: $!\n";
open my $o, ">", $stub_out
or die "Failed to open $stub_out for writing: $!\n";
my $first_line = <DATA>;
print $o $first_line;
print $o "\n# Genesis $VERSION $BUILD\n\n";
print $o $_ for <DATA>;
print $o "\n__DATA__\n$checksum\n";
my $buf;
open my $tbfh, "< :raw :bytes", "pkg.tar.gz"
or die "Could not open tarball for reading: $!\n";
while (read($tbfh, $buf, 60*57)) {
print $o encode_base64($buf);
}
close $o;
chmod 0755, $stub_out;
print "GEESE RUNTIME ARCHIVE:\n";
system("tar -tzvf pkg.tar.gz");
print "\n---\n";
print "packaged v$VERSION $BUILD as $stub_out\n\n";
# clean up after the geese
system 'rm -rf pkg/ pkg.tar.gz';
__DATA__
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Base64 qw/decode_base64/;
use Cwd qw/abs_path/;
use FindBin;
# Genesis Extraction and Execution Stub Engine (GEESE)
#
# This perl script contains the entire runtime environment needed to execute
# Genesis. It unpacks the compressed contents into your ~/.geese/ directory
# then passes execution to the ~/.genesis/bin Perl script that was
# extracted, using the libraries located in ~/.genesis/lib.
#
# Once extracted, further execution of this stub will use the previously
# extracted library after checking that the checksum is identical. This way,
# new versions will be properly handled.
# NOTE: BOSH will not work without $HOME set; so ...
die "You have no \$HOME. Please set one and re-run.\n"
unless $ENV{HOME};
$ENV{GENESIS_HOME} ||= $ENV{HOME};
my $root = "$ENV{GENESIS_HOME}/.genesis";
mkdir $root unless -d $root;
# TODO: need to separate tight loop callbacks (call .genesis/genesis directly) vs
# calling/referencing this extractor in the case of embed, upgrade, and
# output command examples in info and post-deploy. For now, just leave
# it to use the extractor as it doesn't impose that much overhead.
use FindBin;
$ENV{GENESIS_CALLBACK_BIN} = $FindBin::Bin.'/'.$FindBin::Script;
# check the checksum to see if we should untar
my ($fh, $want, $have) = (undef, "", "");
chomp($want = <DATA>);
open $fh, "<", "$root/checksum" or $fh = undef;
if ($fh) {
chomp($have = <$fh>);
close $fh;
}
if ($want ne $have) {
system("/bin/bash", "-c", "rm -rf $root/lib $root/genesis $root/checksum $root/version");
my $action = $have ? "updating" : "installing";
# extract the payload
print STDERR "[1;30m[$action genesis runtime][0m\n";
open my $tar, "|-", "tar -xzf - -C $root"
or die "Failed to do stuff\n";
while (<DATA>) {
chomp;
print $tar decode_base64($_);
}
close $tar;
}
# run it!
chmod 0755, "$root/genesis";
exec "$root/genesis", @ARGV;
exit(1);