-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemodulerpm.pl
executable file
·463 lines (430 loc) · 13.3 KB
/
makemodulerpm.pl
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/perl
# makemodulerpm.pl
# Create an RPM for a webmin or usermin module or theme
use strict;
use warnings;
use 5.010;
# Colors!
use Term::ANSIColor qw(:constants);
my $basedir;
# Does any system still have a redhat dir?
if (-d "$ENV{'HOME'}" . "/redhat") {
$basedir = "$ENV{'HOME'}" . "/redhat";
}
elsif (-d "$ENV{'HOME'}" . "/rpmbuild") {
$basedir = "$ENV{'HOME'}" . "/rpmbuild";
}
else {
$basedir = "/usr/src/redhat";
}
my $target_dir = "$basedir" . "/RPMS/noarch"; # where to copy the RPM to
my $licence = "BSD";
my $release = 1;
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin";
my $allow_overwrite = 0;
my ($force_theme, $rpmdepends, $no_prefix, $vendor, $provides, $url,
$force_usermin, $final_mod, $sign, $epoch, $dir, $ver, @extrareqs,
@exclude);
# Parse command-line args
while(@ARGV) {
# XXX Untainting isn't needed when running as non-root?
my $a = &untaint(shift(@ARGV));
if ($a eq "--force-theme") {
$force_theme = 1;
}
elsif ($a eq "--rpm-dir") {
$basedir = &untaint(shift(@ARGV));
}
elsif ($a eq "--licence" || $a eq "--license") {
$licence = &untaint(shift(@ARGV));
}
elsif ($a eq "--rpm-depends") {
$rpmdepends = 1;
}
elsif ($a eq "--no-prefix") {
$no_prefix = 1;
}
elsif ($a eq "--vendor") {
$vendor = &untaint(shift(@ARGV));
}
elsif ($a eq "--provides") {
$provides = &untaint(shift(@ARGV));
}
elsif ($a eq "--url") {
$url = shift(@ARGV);
}
elsif ($a eq "--release") {
$release = &untaint(shift(@ARGV));
}
elsif ($a eq "--usermin") {
$force_usermin = 1;
}
elsif ($a eq "--target-dir") {
$target_dir = &untaint(shift(@ARGV));
}
elsif ($a eq "--dir") {
$final_mod = &untaint(shift(@ARGV));
}
elsif ($a eq "--requires") {
push(@extrareqs, shift(@ARGV));
}
elsif ($a eq "--allow-overwrite") {
$allow_overwrite = 1;
}
elsif ($a eq "--sign") {
$sign = 1;
}
elsif ($a eq "--epoch") {
$epoch = shift(@ARGV);
}
elsif ($a eq "--exclude") {
push(@exclude, shift(@ARGV));
}
elsif ($a =~ /^\-\-/) {
print STDERR "Unknown option $a\n";
exit(1);
}
else {
if (!defined($dir)) {
$dir = $a;
}
else {
$ver = $a;
}
}
}
# Validate args
if (!$dir) {
print "usage: ";
print CYAN, "makemodulerpm.pl ";
print YELLOW, "[--force-theme]\n";
print " [--rpm-dir directory]\n";
print " [--rpm-depends]\n";
print " [--no-prefix]\n";
print " [--vendor name]\n";
print " [--licence name]\n";
print " [--url url]\n";
print " [--provides provides]\n";
print " [--usermin]\n";
print " [--release number]\n";
print " [--epoch number]\n";
print " [--target-dir directory]\n";
print " [--dir directory-in-package]\n";
print " [--allow-overwrite]\n";
print CYAN, " <module> ";
print YELLOW, "[version]\n", RESET;
exit(1);
}
my $par;
chop($par = `/usr/bin/dirname $dir`);
$par = &untaint($par);
my $source_mod;
chop($source_mod = `/bin/basename $dir`);
$source_mod = &untaint($source_mod);
my $source_dir = "$par/$source_mod";
my (%minfo, %tinfo);
&read_file("$source_dir/module.info", \%minfo);
&read_file("$source_dir/theme.info", \%tinfo);
my $mod = $final_mod || $minfo{'default_dir'} || $tinfo{'default_dir'} || $source_mod;
if (!-d $basedir) {
die "RPM directory $basedir does not exist";
}
if ($mod eq "." || $mod eq "..") {
die "directory must be an actual directory (module) name, not \"$mod\"";
}
my $spec_dir = "$basedir/SPECS";
my $rpm_source_dir = "$basedir/SOURCES";
my $rpm_dir = "$basedir/RPMS/noarch";
my $source_rpm_dir = "$basedir/SRPMS";
if (!-d $spec_dir || !-d $rpm_source_dir || !-d $rpm_dir) {
die "RPM directory $basedir is not valid";
}
# Is this actually a module or theme directory?
-d $source_dir || die "$dir is not a directory";
my ($depends, $prefix, $desc, $prog, $iver, $istheme, $post_config);
if ($minfo{'desc'}) {
$depends = join(" ", map { s/\/[0-9\.]+//; $_ }
grep { !/^[0-9\.]+$/ }
split(/\s+/, $minfo{'depends'}));
if ($minfo{'usermin'} && (!$minfo{'webmin'} || $force_usermin)) {
$prefix = "usm-";
$desc = "Usermin module for '$minfo{'desc'}'";
$prog = "usermin";
}
else {
$prefix = "wbm-";
$desc = "Webmin module for '$minfo{'desc'}'";
$prog = "webmin";
}
$iver = $minfo{'version'};
$post_config = 1;
}
elsif ($tinfo{'desc'}) {
if ($tinfo{'usermin'} && (!$tinfo{'usermin'} || $force_usermin)) {
$prefix = "ust-";
$desc = "Usermin theme '$tinfo{'desc'}'";
$prog = "usermin";
}
else {
$prefix = "wbt-";
$desc = "Webmin theme '$tinfo{'desc'}'";
$prog = "webmin";
}
$iver = $tinfo{'version'};
$istheme = 1;
$post_config = 0;
}
else {
die "$source_dir does not appear to be a webmin module or theme";
}
$prefix = "" if ($no_prefix);
my $ucprog = ucfirst($prog);
# Copy the directory to a temp location for tarring
system("/bin/mkdir -p /tmp/makemodulerpm");
system("cd $par && /bin/cp -rpL $source_mod /tmp/makemodulerpm/$mod");
system("/usr/bin/find /tmp/makemodulerpm -name .git | xargs rm -rf");
system("/usr/bin/find /tmp/makemodulerpm -name RELEASE | xargs rm -rf");
system("/usr/bin/find /tmp/makemodulerpm -name RELEASE.sh | xargs rm -rf");
system("/usr/bin/find /tmp/makemodulerpm -name t | xargs rm -rf");
if (-r "/tmp/makemodulerpm/$mod/EXCLUDE") {
system("cd /tmp/makemodulerpm/$mod && cat EXCLUDE | xargs rm -rf");
system("rm -f /tmp/makemodulerpm/$mod/EXCLUDE");
}
foreach my $e (@exclude) {
system("/usr/bin/find /tmp/makemodulerpm -name ".quotemeta($e)." | xargs rm -rf");
}
# Set version in .info file to match command line, if given
if ($ver) {
if ($minfo{'desc'}) {
$minfo{'version'} = $ver;
&write_file("/tmp/makemodulerpm/$mod/module.info", \%minfo);
}
elsif ($tinfo{'desc'}) {
$tinfo{'version'} = $ver;
&write_file("/tmp/makemodulerpm/$mod/theme.info", \%tinfo);
}
}
else {
$ver ||= $iver; # Use module.info version, or 1
$ver ||= 1;
}
# Tar up the directory
system("cd /tmp/makemodulerpm && tar czhf $rpm_source_dir/$mod.tar.gz $mod");
system("/bin/rm -rf /tmp/makemodulerpm");
# Build list of dependencies on other RPMs, for inclusion as an RPM
# Requires: header
my $rdeps;
if ($rpmdepends && defined($minfo{'depends'})) {
my @rdeps;
foreach my $d (split(/\s+/, $minfo{'depends'})) {
my ($dwebmin, $dmod, $dver);
if ($d =~ /^[0-9\.]+$/) {
# Depends on a version of Webmin
$dwebmin = $d;
}
elsif ($d =~ /^(\S+)\/([0-9\.]+)$/) {
# Depends on some version of a module
$dmod = $1;
$dver = $2;
}
else {
# Depends on any version of a module
$dmod = $d;
}
# If the module is part of Webmin, we don't need to depend on it
if ($dmod) {
my %dinfo;
&read_file("$dmod/module.info", \%dinfo);
next if ($dinfo{'longdesc'});
}
push(@rdeps, $dwebmin ? ("webmin", ">=", $dwebmin) :
$dver ? ($prefix.$dmod, ">=", $dver) :
($prefix.$dmod));
}
$rdeps = join(" ", @rdeps, @extrareqs);
}
# Create the SPEC file
my $providesheader = $provides ? "Provides: $provides" : "";
my $vendorheader = $vendor ? "Vendor: $vendor" : "";
my $urlheader = $url ? "URL: $url" : "";
my $epochheader = $epoch ? "Epoch: $epoch" : "";
$force_theme //= "";
$istheme //= "";
$rdeps //= "";
$depends //= "";
open(my $SPEC, ">", "$spec_dir/$prefix$mod.spec");
print $SPEC <<EOF;
%define __spec_install_post %{nil}
Summary: $desc
Name: $prefix$mod
Version: $ver
Release: $release
Requires: /bin/sh /usr/bin/perl /usr/libexec/$prog $rdeps
Autoreq: 0
Autoprov: 0
License: $licence
Group: System/Tools
Source: $mod.tar.gz
BuildRoot: /tmp/%{name}-%{version}
BuildArchitectures: noarch
$epochheader
$providesheader
$vendorheader
$urlheader
%description
$desc in RPM format
%prep
%setup -n $mod
%build
(find . -name '*.cgi' ; find . -name '*.pl') | perl -ne 'chop; open(F,\$_); \@l=<F>; close(F); \$l[0] = "#\!/usr/bin/perl\$1\n" if (\$l[0] =~ /#\!\\S*perl\\S*(.*)/); open(F,">\$_"); print F \@l; close(F)'
%install
mkdir -p %{buildroot}/usr/libexec/$prog/$mod
cp -rp * %{buildroot}/usr/libexec/$prog/$mod
echo rpm >%{buildroot}/usr/libexec/$prog/$mod/install-type
%clean
[ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
%files
/usr/libexec/$prog/$mod/
%pre
# Check if webmin/usermin is installed
if [ ! -r /etc/$prog/config -o ! -d /usr/libexec/$prog ]; then
echo "$ucprog does not appear to be installed on your system."
echo "This RPM cannot be installed unless the RPM version of $ucprog"
echo "is installed first."
exit 1
fi
if [ "$depends" != "" -a "$rpmdepends" != 1 ]; then
# Check if depended webmin/usermin modules are installed
for d in $depends; do
if [ ! -r /usr/libexec/$prog/\$d/module.info ]; then
echo "This $ucprog module depends on the module \$d, which is"
echo "not installed on your system."
exit 1
fi
done
fi
# Check if this module is already installed
if [ -d /usr/libexec/$prog/$mod -a "\$1" = "1" -a "$allow_overwrite" != "1" ]; then
echo "This $ucprog module is already installed on your system."
exit 1
fi
%post
if [ "$post_config" = "1" ]; then
# Copy config file to /etc/webmin or /etc/usermin
os_type=`grep "^os_type=" /etc/$prog/config | sed -e 's/os_type=//g'`
os_version=`grep "^os_version=" /etc/$prog/config | sed -e 's/os_version=//g'`
real_os_type=`grep "^real_os_type=" /etc/$prog/config | sed -e 's/real_os_type=//g'`
real_os_version=`grep "^real_os_version=" /etc/$prog/config | sed -e 's/real_os_version=//g'`
/usr/bin/perl /usr/libexec/$prog/copyconfig.pl "\$os_type/\$real_os_type" "\$os_version/\$real_os_version" /usr/libexec/$prog /etc/$prog $mod
# Update the ACL for the root user, or the first user in the ACL
grep "^root:" /etc/$prog/webmin.acl >/dev/null
if [ "\$?" = "0" ]; then
user=root
else
user=`head -1 /etc/$prog/webmin.acl | cut -f 1 -d :`
fi
mods=`grep \$user: /etc/$prog/webmin.acl | cut -f 2 -d :`
echo \$mods | grep " $mod" >/dev/null
if [ "\$?" != "0" ]; then
grep -v ^\$user: /etc/$prog/webmin.acl > /tmp/webmin.acl.tmp
echo \$user: \$mods $mod > /etc/$prog/webmin.acl
cat /tmp/webmin.acl.tmp >> /etc/$prog/webmin.acl
rm -f /tmp/webmin.acl.tmp
fi
fi
if [ "$force_theme" != "" -a "$istheme" = "1" ]; then
# Activate this theme
grep -v "^preroot=" /etc/$prog/miniserv.conf >/etc/$prog/miniserv.conf.tmp
(cat /etc/$prog/miniserv.conf.tmp ; echo preroot=$mod) > /etc/$prog/miniserv.conf
rm -f /etc/$prog/miniserv.conf.tmp
grep -v "^theme=" /etc/$prog/config >/etc/$prog/config.tmp
(cat /etc/$prog/config.tmp ; echo theme=$mod) > /etc/$prog/config
rm -f /etc/$prog/config.tmp
(/etc/$prog/stop && /etc/$prog/start) >/dev/null 2>&1
fi
rm -f /etc/$prog/module.infos.cache
rm -f /var/$prog/module.infos.cache
# Run post-install function
if [ "$prog" = "webmin" ]; then
cd /usr/libexec/$prog
WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/run-postinstalls.pl $mod
fi
# Run post-install shell script
if [ -r "/usr/libexec/$prog/$mod/postinstall.sh" ]; then
cd /usr/libexec/$prog
WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/$mod/postinstall.sh
fi
%preun
# De-activate this theme, if in use and if we are not upgrading
if [ "$istheme" = "1" -a "\$1" = "0" ]; then
grep "^preroot=$mod" /etc/$prog/miniserv.conf >/dev/null
if [ "\$?" = "0" ]; then
grep -v "^preroot=$mod" /etc/$prog/miniserv.conf >/etc/$prog/miniserv.conf.tmp
(cat /etc/$prog/miniserv.conf.tmp) > /etc/$prog/miniserv.conf
rm -f /etc/$prog/miniserv.conf.tmp
grep -v "^theme=$mod" /etc/$prog/config >/etc/$prog/config.tmp
(cat /etc/$prog/config.tmp) > /etc/$prog/config
rm -f /etc/$prog/config.tmp
(/etc/$prog/stop && /etc/$prog/start) >/dev/null 2>&1
fi
fi
# Run the pre-uninstall script, if we are not upgrading
if [ "$prog" = "webmin" -a "\$1" = "0" -a -r "/usr/libexec/$prog/$mod/uninstall.pl" ]; then
cd /usr/libexec/$prog
WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/run-uninstalls.pl $mod
fi
/bin/true
%postun
EOF
close($SPEC);
# Build the actual RPM
my $cmd = -x "/usr/bin/rpmbuild" ? "/usr/bin/rpmbuild" : "/bin/rpm";
system("$cmd -ba $spec_dir/$prefix$mod.spec") && exit;
unlink("$rpm_source_dir/$mod.tar.gz");
# Sign if requested
if ($sign) {
system("echo | rpm --resign $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $source_rpm_dir/$prefix$mod-$ver-$release.src.rpm");
}
if ($target_dir =~ /:/) {
# scp to dest
system("scp $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $target_dir/$prefix$mod-$ver-$release.noarch.rpm");
}
elsif ($rpm_dir ne $target_dir) {
# Just copy
system("/bin/cp $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $target_dir/$prefix$mod-$ver-$release.noarch.rpm");
}
# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
open(ARFILE, $_[0]) || return 0;
while(<ARFILE>) {
s/\r|\n//g;
if (!/^#/ && /^([^=]+)=(.*)$/) {
$_[1]->{$_[3] ? lc($1) : $1} = $2;
push(@{$_[2]}, $1) if ($_[2]);
}
}
close(ARFILE);
return 1;
}
# write_file(file, array)
# Write out the contents of an associative array as name=value lines
sub write_file
{
my (%old, @order);
&read_file($_[0], \%old, \@order);
open(ARFILE, ">$_[0]");
foreach my $k (@order) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (exists($_[1]->{$k}));
}
foreach my $k (keys %{$_[1]}) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
}
close(ARFILE);
}
sub untaint
{
$_[0] =~ /^(.*)$/;
return $1;
}