1 | #!/usr/bin/env perl |
---|
2 | |
---|
3 | use File::Spec::Functions qw(:ALL); |
---|
4 | use File::Copy; |
---|
5 | use File::Path; |
---|
6 | use Data::Dumper; |
---|
7 | use Getopt::Long; |
---|
8 | use Archive::Tar; |
---|
9 | use Cwd; |
---|
10 | use strict; |
---|
11 | use warnings; |
---|
12 | |
---|
13 | my $scriptsdev = ""; |
---|
14 | |
---|
15 | GetOptions( |
---|
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 | |
---|
24 | if (@ARGV < 3) { |
---|
25 | print STDERR "Usage: $0 package oldversion newversion\n"; |
---|
26 | exit(1); |
---|
27 | } |
---|
28 | |
---|
29 | my ($package, $oldversion, $newversion) = @ARGV; |
---|
30 | my ($old, $new, $updatename) = ($package.'-'.$oldversion, $package.'-'.$newversion, $package.'-'.$oldversion.'-to-'.$newversion); |
---|
31 | |
---|
32 | my $proposaldir = $updatename.".proposal"; |
---|
33 | my $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 | |
---|
38 | my $olddir = catdir($proposaldir,$old); |
---|
39 | my $newdir = catdir($proposaldir,$new); |
---|
40 | (-d $olddir && -d $newdir) or die "Packages not unpacked?\n"; |
---|
41 | |
---|
42 | sub 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 | |
---|
50 | my @todelete = readFileList(catfile($proposaldir, "files.delete")); |
---|
51 | my @toadd = readFileList(catfile($proposaldir, "files.add")); |
---|
52 | my @toreplace = readFileList(catfile($proposaldir, "files.replace")); |
---|
53 | |
---|
54 | open(CHECKMD5, ">", catfile($outdir, "check.md5")) or die "Can't open check.md5: $!"; |
---|
55 | print CHECKMD5 map { join(" ", @{$_})."\n" } @todelete, @toreplace; |
---|
56 | close(CHECKMD5); |
---|
57 | |
---|
58 | open(ABSENT, ">", catfile($outdir, "oldfiles.absent")) or die "Can't open oldfiles.absent: $!"; |
---|
59 | print ABSENT map { $_->[1]."\n" } @toadd; |
---|
60 | close(ABSENT); |
---|
61 | |
---|
62 | open(DELETE, ">", catfile($outdir, "files.delete")) or die "Can't open files.delete: $!"; |
---|
63 | print DELETE map { $_->[1]."\n" } @todelete, @toreplace; |
---|
64 | close(DELETE); |
---|
65 | |
---|
66 | my $outfiletarball = catfile($outdir, "newfiles.tar.gz"); |
---|
67 | my $cwd = getcwd(); |
---|
68 | |
---|
69 | chdir($newdir); |
---|
70 | my $tar = Archive::Tar->new; |
---|
71 | $tar->add_files(map { $_->[1] } @toadd, @toreplace); |
---|
72 | $tar->write($outfiletarball, 1); |
---|
73 | chdir($cwd) or die "Couldn't chdir back to $cwd: $!\n"; |
---|
74 | |
---|
75 | if (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 | |
---|
89 | copy(catfile($proposaldir, "update.diff"), catfile($outdir, "update.diff")) or die "Couldn't copy update.diff: $!"; |
---|
90 | if (-d catdir($proposaldir, "extra")) { |
---|
91 | system('cp', '-r', catdir($proposaldir, "extra"), $outdir) && die "Couldn't copy extra: $!"; |
---|
92 | } |
---|
93 | |
---|
94 | open (SCRIPT, ">", catfile($outdir, "update")) or die "Couldn't write update: $!"; |
---|
95 | printf SCRIPT <<'EOF', catfile("/afs/athena.mit.edu/contrib/scripts/deploy/", $old), catfile("/afs/athena.mit.edu/contrib/scripts/deploy/", $new); |
---|
96 | #!/bin/bash |
---|
97 | set -e -o noclobber |
---|
98 | |
---|
99 | die () { echo "[$PWD] die: $1" >&2; rm .scripts-security-upd-lock; exit 1; } |
---|
100 | aiee () { echo "[$PWD] AIEE: $1" >&2; exit 2; } |
---|
101 | |
---|
102 | patchdir=$(dirname "$0") |
---|
103 | lockername="${1:-$USER}" |
---|
104 | |
---|
105 | >.scripts-security-upd-lock || die "error: Unable to obtain update lock." |
---|
106 | |
---|
107 | packages=$(tail -1 .scripts-version) |
---|
108 | |
---|
109 | echo "[$PWD] begin dry run" |
---|
110 | |
---|
111 | found="" |
---|
112 | newpackages="" |
---|
113 | for package in $packages; do |
---|
114 | if [ "$package" = "%s" ]; then |
---|
115 | found="yes" |
---|
116 | newpackages="$newpackages%s " |
---|
117 | else |
---|
118 | newpackages="$newpackages$package " |
---|
119 | fi |
---|
120 | done |
---|
121 | |
---|
122 | [ "$found" = "yes" ] || die "error: Correct version not found." |
---|
123 | |
---|
124 | if [ -e "$patchdir/extra/prepatch.sh" ]; then |
---|
125 | . "$patchdir/extra/prepatch.sh" || die "error: prepatch failed" |
---|
126 | fi |
---|
127 | |
---|
128 | [ -s "$patchdir/oldfiles.absent" ] && (xargs -n1 test ! -e <"$patchdir/oldfiles.absent" || die "error: Conflicting files exist." ) |
---|
129 | [ -s "$patchdir/check.md5" ] && (md5sum -c "$patchdir/check.md5" || die "error: MD5 check failed.") |
---|
130 | patch -stN --dry-run --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || die "error: Patch dry run failed." |
---|
131 | |
---|
132 | echo "[$PWD] dry run OK, applying update" |
---|
133 | |
---|
134 | mv .scripts-version .scripts-version.old || aiee "Failed to back up scripts-version" |
---|
135 | patch -stN --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || aiee "patch did not apply" |
---|
136 | xargs rm -f <"$patchdir/files.delete" || aiee "Failed to remove files" |
---|
137 | /afs/athena.mit.edu/contrib/scripts/bin/gtar -xzf "$patchdir/newfiles.tar.gz" || aiee "Failed to extract new files" |
---|
138 | [ -e "$patchdir/extra/postpatch.sh" ] && . "$patchdir/extra/postpatch.sh" |
---|
139 | ( |
---|
140 | cat .scripts-version.old |
---|
141 | echo |
---|
142 | date "+%%F %%T %%z" |
---|
143 | echo "$USER@$(hostname)" |
---|
144 | echo "$patchdir" |
---|
145 | echo "$newpackages" |
---|
146 | ) >.scripts-version |
---|
147 | rm -f .scripts-version.old || aiee "Failed to remove .scripts-version.old" |
---|
148 | |
---|
149 | rm -f .scripts-security-upd-lock || aiee "Failed to remove .scripts-security-upd-lock" |
---|
150 | |
---|
151 | echo "[$PWD] done" |
---|
152 | |
---|
153 | exit 0 |
---|
154 | EOF |
---|
155 | chmod 0755, catfile($outdir, "update"); |
---|