source: locker/sbin/build-update @ 977

Last change on this file since 977 was 746, checked in by price, 16 years ago
dang shell scripting
  • Property svn:executable set to *
File size: 4.7 KB
Line 
1#!/usr/bin/env perl
2
3use File::Spec::Functions qw(:ALL);
4use File::Copy;
5use File::Path;
6use Data::Dumper;
7use Getopt::Long;
8use Archive::Tar;
9use Cwd;
10use strict;
11use warnings;
12
13my $scriptsdev = "";
14
15GetOptions(
16#"redo-delete" => \$redodelete,
17#          "redo-add" => \$redoadd,
18#          "redo-replace" => \$redoreplace,
19#          "redo-diff" => \$redodiff,
20#          "redo-all" => sub {$redodelete = $redoadd = $redoreplace = $redodiff = 1;},
21           "dev" => sub {$scriptsdev = "dev";},
22                  );
23
24if (@ARGV < 3) {
25  print STDERR "Usage: $0 package oldversion newversion\n";
26  exit(1);
27}
28
29my ($package, $oldversion, $newversion) = @ARGV;
30my ($old, $new, $updatename) = ($package.'-'.$oldversion, $package.'-'.$newversion, $package.'-'.$oldversion.'-to-'.$newversion);
31
32my $proposaldir = $updatename.".proposal";
33my $outdir = catdir("/mit/scripts/deploy$scriptsdev/updates/", $updatename);
34
35(-d $proposaldir) or die "Can't find $proposaldir, did you run propose-update?\n";
36((! -e $outdir) && mkdir($outdir)) or die "mkdir($outdir) failed: $!";
37
38my $olddir = catdir($proposaldir,$old);
39my $newdir = catdir($proposaldir,$new);
40(-d $olddir && -d $newdir) or die "Packages not unpacked?\n";
41
42sub readFileList($) {
43  my ($list) = @_;
44  open(LIST, $list) or die "Can't read $list: $!\n";
45  my @files = map { chomp; s|$newdir\/?||g; [split(' ', $_, 2)] } <LIST>;
46  close(LIST);
47  return @files;
48}
49
50my @todelete = readFileList(catfile($proposaldir, "files.delete"));
51my @toadd = readFileList(catfile($proposaldir, "files.add"));
52my @toreplace = readFileList(catfile($proposaldir, "files.replace"));
53
54open(CHECKMD5, ">", catfile($outdir, "check.md5")) or die "Can't open check.md5: $!";
55print CHECKMD5 map { join("  ", @{$_})."\n" } @todelete, @toreplace;
56close(CHECKMD5);
57
58open(ABSENT, ">", catfile($outdir, "oldfiles.absent")) or die "Can't open oldfiles.absent: $!";
59print ABSENT map { $_->[1]."\n" } @toadd;
60close(ABSENT);
61
62open(DELETE, ">", catfile($outdir, "files.delete")) or die "Can't open files.delete: $!";
63print DELETE map { $_->[1]."\n" } @todelete, @toreplace;
64close(DELETE);
65
66my $outfiletarball = catfile($outdir, "newfiles.tar.gz");
67my $cwd = getcwd();
68
69chdir($newdir);
70my $tar = Archive::Tar->new;
71$tar->add_files(map { $_->[1] } @toadd, @toreplace);
72$tar->write($outfiletarball, 1);
73chdir($cwd) or die "Couldn't chdir back to $cwd: $!\n";
74
75if (0) {
76    my $outfiledir = catdir($outdir, "newfiles");
77    (-d $outfiledir || mkdir($outfiledir)) or die "Can't mkdir($outfiledir)\n";
78   
79    foreach my $file (@toadd, @toreplace) {
80        my $filename = $file->[1];
81        my $src = catfile($newdir, $filename);
82        my $dest = catfile($outfiledir, $filename);
83        my (undef, $dir, undef) = splitpath($dest);
84        mkpath($dir);
85        copy($src, $dest) or die "Couldn't copy $filename from $src to $dest: $!";
86    }
87}
88
89copy(catfile($proposaldir, "update.diff"), catfile($outdir, "update.diff")) or die "Couldn't copy update.diff: $!";
90if (-d catdir($proposaldir, "extra")) {
91    system('cp', '-r', catdir($proposaldir, "extra"), $outdir) && die "Couldn't copy extra: $!";
92}
93
94open (SCRIPT, ">", catfile($outdir, "update")) or die "Couldn't write update: $!";
95printf SCRIPT <<'EOF', catfile("/afs/athena.mit.edu/contrib/scripts/deploy/", $old), catfile("/afs/athena.mit.edu/contrib/scripts/deploy/", $new);
96#!/bin/bash
97set -e -o noclobber
98
99die () { echo "$1" >&2; rm .scripts-security-upd-lock; exit 1; }
100
101patchdir=$(dirname "$0")
102
103>.scripts-security-upd-lock || die "error: Unable to obtain update lock."
104
105packages=$(tail -1 .scripts-version)
106
107echo "[$PWD] begin dry run"
108
109found=""
110newpackages=""
111for package in $packages; do
112    if [ "$package" = "%s" ]; then
113    found="yes"
114    newpackages="$newpackages%s "
115    else
116    newpackages="$newpackages$package "
117    fi
118done
119
120[ "$found" = "yes" ] || die "error: Correct version not found."
121
122if [ -e "$patchdir/extra/prepatch.sh" ]; then
123  . "$patchdir/extra/prepatch.sh" || die "error: prepatch failed"
124fi
125
126[ -s "$patchdir/oldfiles.absent" ] && (xargs -n1 test ! -e <"$patchdir/oldfiles.absent" || die "error: Conflicting files exist." )
127[ -s "$patchdir/check.md5" ] && (md5sum -c "$patchdir/check.md5" || die "error: MD5 check failed.")
128patch -stN --dry-run --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || die "error: Patch dry run failed."
129
130echo "[$PWD] dry run OK, applying update"
131
132mv .scripts-version .scripts-version.old
133patch -stN --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || :
134xargs rm -f <"$patchdir/files.delete"
135tar -xzf "$patchdir/newfiles.tar.gz"
136[ -e "$patchdir/extra/postpatch.sh" ] && . "$patchdir/extra/postpatch.sh"
137(
138    cat .scripts-version.old
139    echo
140    date "+%%F %%T %%z"
141    echo "$USER@$(hostname)"
142    echo "$patchdir"
143    echo "$newpackages"
144) >.scripts-version
145rm -f .scripts-version.old
146
147rm -f .scripts-security-upd-lock
148
149echo "[$PWD] done"
150
151exit 0
152EOF
153chmod 0755, catfile($outdir, "update");
Note: See TracBrowser for help on using the repository browser.