summaryrefslogtreecommitdiff
path: root/Build-tools/Do-pkg
blob: 4d0f120c6e3ef70660491a1dbf4f910ea8626cdd (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/perl -w
#
# Do-pkg - convert a binary distribution into a Mac OS X PKG and put it
# inside a Disk Image (.dmg). Additionally, add a separate package,
# including the required Startup Item to automatically start MySQL on
# bootup.
#
# The script currently assumes the following environment (which should exist
# like that, if the Do-compile script was used to build the binary
# distribution)
#
# - there must be a binary distribution (*.tar.gz) in the directory
#   `hostname` of the current directory
# - the extracted and compiled source tree should be located in the
#   `hostname` directory, too
#
# Use the "--help" option for more info!
#
# written by Lenz Grimmer <lenz@mysql.com>
#

use Cwd;
use File::Basename;
use File::Copy;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sys::Hostname;

$opt_dry_run= undef;
$opt_help= undef;
$opt_log= undef;
$opt_mail= "";
$opt_skip_dmg= undef;
$opt_skip_si= undef;
$opt_suffix= undef;
$opt_verbose= undef;
$opt_version= undef;

GetOptions(
	"dry-run",
	"help|h",
	"log|l:s",
	"mail|m=s",
	"skip-dmg|skip-disk-image|s",
	"skip-si|skip-startup-item",
	"suffix=s",
	"verbose|v",
	"version=s",
) || &print_help;

# Include helper functions
$PWD= cwd();
$LOGGER= "$PWD/logger.pm";
if (-f "$LOGGER")
{
	do "$LOGGER";
}
else
{
	die "ERROR: $LOGGER cannot be found!\n";
}

$PM= "/Developer/Applications/PackageMaker.app/Contents/MacOS/PackageMaker";
$TMP= $ENV{TMPDIR};
$TMP eq "" ?  $TMP= $TMP . "/PKGBUILD.$$": $TMP= "/tmp/PKGBUILD.$$";
$PKGROOT= "$TMP/PMROOT";
$PKGDEST= "$TMP/PKG";
$RESOURCE_DIR= "$TMP/Resources";
$SUFFIX= $opt_suffix;
$VERSION= $opt_version;
($MAJOR, $MINOR, $RELEASE)= split(/\./, $VERSION);
$NAME= "mysql$SUFFIX-$VERSION";
$HOST= hostname();
$ID= getpwuid($>);
$HOST=~ /^([^.-]*)/;
$HOST= $1;
$LOGFILE= "$PWD/Logs/$HOST-$MAJOR.$MINOR$SUFFIX.log";
$BUILDDIR= "$PWD/$HOST";
$SRCBASEDIR= <$BUILDDIR/mysql*-$VERSION>;
$SUPFILEDIR= <$SRCBASEDIR/support-files/MacOSX>;
$TAR= <$BUILDDIR/$NAME-apple-darwin*-powerpc.tar.gz>;
$INFO= <$SUPFILEDIR/Info.plist>;
$DESC= <$SUPFILEDIR/Description.plist>;
$SI_INFO= <$SUPFILEDIR/StartupItem.Info.plist>;
$SI_DESC= <$SUPFILEDIR/StartupItem.Description.plist>;
$SI_PARAMS= <$SUPFILEDIR/StartupParameters.plist>;
$SI_POST= <$SUPFILEDIR/StartupItem.postinstall>;
$SI_NAME= "MySQLStartupItem";
$SI_SCRIPT= <$SUPFILEDIR/MySQL>;
@RESOURCES= qw/ ReadMe.txt postinstall preinstall /;
@LICENSES= ("$SRCBASEDIR/COPYING","$SRCBASEDIR/MySQLEULA.txt");

&print_help("") if ($opt_help || !$opt_suffix || !$opt_version);

#
# Override predefined Log file name
#
if (defined $opt_log)
{
	if ($opt_log ne "")
	{
		if ($opt_log =~ /^\/.*/)
		{
			$LOGFILE= $opt_log;
		}
		else
		{
			$LOGFILE= $PWD . "/" . $opt_log;
		}
	}
}

# Creating the UFS disk image requires root privileges
die("You must be root to run this script!") if ($ID ne "root" && !$opt_dry_run);

@files= ($TAR, $INFO, $DESC);
@files= (@files, $SI_INFO, $SI_DESC, $SI_POST, $SI_SCRIPT) unless $opt_skip_si;
foreach $file (@files)
{
	&abort("Unable to find $file!") unless (-f "$file");
}

# Remove old temporary build directories first
&logger("Cleaning up temporary build directories");
&run_command("rm -rf $TMP", "Could not clean up $TMP!");
&logger("Creating temp directories");
foreach $dir ($TMP, $PKGROOT, $PKGDEST, $RESOURCE_DIR)
{
	if (!-d $dir)
	{
		&logger("Creating directory $dir!");
		unless($opt_dry_run)
		{
			mkdir($dir) or &abort("Could not make directory $dir!");
		}
	}
}

foreach $resfile (@RESOURCES)
{
	&logger("Copying $SUPFILEDIR/$resfile to $RESOURCE_DIR");
	unless($opt_dry_run)
	{
		copy("$SUPFILEDIR/$resfile", "$RESOURCE_DIR") or
		&abort("Error while copying $SUPFILEDIR/$resfile to $RESOURCE_DIR");
	}
}

# Search for license file
foreach $license (@LICENSES)
{
	if (-f "$license")
	{
		&logger("Copy $license to $RESOURCE_DIR/License.txt");
		unless($opt_dry_run)
		{
			copy("$license", "$RESOURCE_DIR/License.txt") or
			&abort("Error while copying $license to $RESOURCE_DIR");
		}
	}
}

&abort("Could not find a license file!")
unless (-f "$RESOURCE_DIR/License.txt");

# Extract the binary tarball and create the "mysql" symlink
&logger("Extracting $TAR to $PKGROOT");
&run_command("gnutar zxf $TAR -C $PKGROOT", "Unable to extract $TAR!");
&run_command("cd $PKGROOT ; ln -s mysql* ./mysql", "Unable to create symlink!");
&run_command("chown -R root.wheel $PKGROOT/*", "Cannot chown $PKGROOT!");

# Now build the PGK using PackageMaker
# The "|| true" is a nasty hack to work around a problem with Package Maker
# returning a non-zero value, even though the package was created correctly
&logger("Running PackageMaker");
$command= "$PM -build -p $PKGDEST/$NAME.pkg -f $PKGROOT -r $RESOURCE_DIR -i $INFO -d $DESC || true";
&run_command($command, "Error while building package $NAME.pkg!");

#
# Build the Startup Item PKG
#
unless ($opt_skip_si)
{
	&logger("Cleaning up $PKGROOT");
	&run_command("rm -rf $PKGROOT/*", "Unable to clean up $PKGROOT!");
	&logger("Cleaning up $RESOURCE_DIR");
	&run_command("rm -rf $RESOURCE_DIR/*", "Unable to clean up $RESOURCE_DIR!");

	&logger("Installing MySQL StartupItem files into $PKGROOT/MySQL");
	unless($opt_dry_run)
	{
		mkdir("$PKGROOT/MySQL") or &abort("Error creating $PKGROOT/MySQL");
		copy("$SI_SCRIPT", "$PKGROOT/MySQL/")
			or &abort("Error copying $SI_SCRIPT!");
		chmod(0755, "$PKGROOT/MySQL/" . basename("$SI_SCRIPT"));
		copy("$SI_PARAMS", "$PKGROOT/MySQL/")
			or &abort("Error copying $SI_PARAMS!");
		chmod(0644, "$PKGROOT/MySQL/" . basename("$SI_PARAMS"));
		&run_command("chown -R root.wheel $PKGROOT/*", "Cannot chown $PKGROOT!");
		copy("$SI_POST", "$RESOURCE_DIR/postinstall")
			or &abort("Error copying $SI_POST!");
		chmod(0644, "$RESOURCE_DIR/postinstall");
	}

	&logger("Building $SI_NAME.pkg using PackageMaker");
	$command= "$PM -build -p $PKGDEST/$SI_NAME.pkg -f $PKGROOT -r $RESOURCE_DIR -i $SI_INFO -d $SI_DESC || true";
	&run_command($command, "Error while building package $SI_NAME.pkg!");
}

if ($opt_skip_dmg)
{
	&logger("SUCCESS: Package $PKGDEST/$NAME.pkg created");
	exit 0;
}

# Determine the size of the Disk image to be created and add a 5% safety
# margin for filesystem overhead
&logger("Determining required disk image size for $PKGDEST");
unless($opt_dry_run)
{
	chomp($_= `du -sk $PKGDEST`);
	@size= split();
	$size= int($size[0]+($size[0]*0.05));
	&logger("Disk image size: $size KB");
}

unless($opt_dry_run)
{
	&abort("Zero bytes? Something is wrong here!") if ($size == 0);
}

# Now create and mount the disk image
$TMPNAME= $NAME . ".tmp";
&logger("Creating temporary Disk image $TMPNAME.dmg");
$command= "hdiutil create $TMPNAME -size ${size}k -ov -fs UFS -volname $NAME";
&run_command($command, "Unable to create disk image $TMPNAME.dmg!");
&logger("Attaching Disk image $TMPNAME.dmg");
&run_command("hdid $TMPNAME.dmg", "Unable to attach $TMPNAME.dmg!");

# Install the PKG into the .dmg
chomp($mountpoint=`mount | grep "\/Volumes\/$NAME" | cut -f3 -d" "`) if (!$opt_dry_run);
&logger("Copying $PKGDEST/$NAME.pkg to Disk image /Volumes/$NAME");
&run_command("ditto $PKGDEST /Volumes/$NAME", "Could not copy $PKGDEST to /Volumes/$NAME!");
&run_command("ditto $SUPFILEDIR/ReadMe.txt /Volumes/$NAME", "Could not copy $SPFILEDIR/ReadMe.txt to /Volumes/$NAME!");
chomp($mountpoint=`mount | grep "\/Volumes\/$NAME" | cut -f1 -d" "`) if (!$opt_dry_run);
&abort("/Volumes/$NAME not attached!") if (!$mountpoint && !$opt_dry_run);
&logger("Unmounting $mountpoint");
&run_command("hdiutil detach $mountpoint", "Unable to detach $mountpoint");
&run_command("rm -f $NAME.dmg", "Unable to remove $NAME.dmg!") if (-f "$NAME.dmg");
&logger("Compressing disk image");
$command= "hdiutil convert $TMPNAME.dmg -format UDZO -imagekey zlib-level=9 -o $NAME.dmg";
&run_command($command, "Unable to compress disk image!");

# Final cleanups
&logger("Removing $TMPNAME.dmg");
&run_command("rm -f $TMPNAME.dmg", "Unable to remove $TMPNAME.dmg!");
&logger("Removing $TMP");
&run_command("rm -rf $TMP", "Unable to remove $TMP!");

&logger("SUCCESS: $NAME.dmg created.") if (!$opt_dry_run);
exit 0;

sub print_help
{
	my $message= $_[0];
	if ($message ne "")
	{
		print "\n";
		print "ERROR: $message\n";
	}
	print <<EOF;

Usage: Do-pkg <options> --suffix=<suffix> --version=<version>

Creates a Mac OS X installation package (PKG) and stores it inside
a Disk Image (.dmg) file. You need to create a binary distribution
tarball with scripts/make_binary_distribution first!

NOTE: You need to run this script with root privileges (required
      to create the disk image)

Options:

    --dry-run                        Dry run without executing
-h, --help                           Print this help
-l, --log[=<filename>]               Write a log file [to <filename>]
                                     (default is "$LOGFILE")
-m, --mail=<address>                 Mail a failure report to the given
                                     address (and include a log file snippet,
                                     if logging is enabled)
                                     Note that the \@-Sign needs to be quoted!
                                     Example: --mail=user\\\@domain.com
-s, --skip-disk-image, --skip-dmg    Just build the PKGs, don't put it into a
                                     disk image afterwards
    --skip-startup-item, --skip-si   Skip the creation of the StartupItem PKG
    --suffix=<suffix>                The package suffix
                                     (e.g. "-standard" or "-pro)
    --version=<version>              The MySQL version number
                                     (e.g. 4.0.11-gamma)
-v, --verbose                        Verbose execution

EOF
	exit 1;
}