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 | my $force = 0; |
---|
15 | |
---|
16 | GetOptions( |
---|
17 | #"redo-delete" => \$redodelete, |
---|
18 | # "redo-add" => \$redoadd, |
---|
19 | # "redo-replace" => \$redoreplace, |
---|
20 | # "redo-diff" => \$redodiff, |
---|
21 | # "redo-all" => sub {$redodelete = $redoadd = $redoreplace = $redodiff = 1;}, |
---|
22 | "dev" => sub {$scriptsdev = "dev";}, |
---|
23 | "force" => \$force, |
---|
24 | ); |
---|
25 | |
---|
26 | if (@ARGV < 3) { |
---|
27 | print STDERR "Usage: $0 package oldversion newversion\n"; |
---|
28 | exit(1); |
---|
29 | } |
---|
30 | |
---|
31 | my ($package, $oldversion, $newversion) = @ARGV; |
---|
32 | my ($old, $new, $updatename) = ($package.'-'.$oldversion, $package.'-'.$newversion, $package.'-'.$oldversion.'-to-'.$newversion); |
---|
33 | |
---|
34 | my $proposaldir = $updatename.".proposal"; |
---|
35 | my $outdir = catdir("/mit/scripts/deploy$scriptsdev/updates/", $updatename); |
---|
36 | |
---|
37 | (-d $proposaldir) or die "Can't find $proposaldir, did you run propose-update?\n"; |
---|
38 | if (-e $outdir) { |
---|
39 | if ($force) { |
---|
40 | print STDERR "Clobbering $outdir.\n"; |
---|
41 | `rm -rf '$outdir'`; |
---|
42 | } else { |
---|
43 | die "Output directory $outdir exists; try --force.\n"; |
---|
44 | } |
---|
45 | } |
---|
46 | ((! -e $outdir) && mkdir($outdir)) or die "mkdir($outdir) failed: $!"; |
---|
47 | |
---|
48 | my $olddir = catdir($proposaldir,$old); |
---|
49 | my $newdir = catdir($proposaldir,$new); |
---|
50 | (-d $olddir && -d $newdir) or die "Packages not unpacked?\n"; |
---|
51 | |
---|
52 | sub readFileList($) { |
---|
53 | my ($list) = @_; |
---|
54 | open(LIST, $list) or die "Can't read $list: $!\n"; |
---|
55 | my @files = map { chomp; s|$newdir\/?||g; [split(' ', $_, 2)] } <LIST>; |
---|
56 | close(LIST); |
---|
57 | return @files; |
---|
58 | } |
---|
59 | |
---|
60 | my @todelete = readFileList(catfile($proposaldir, "files.delete")); |
---|
61 | my @toadd = readFileList(catfile($proposaldir, "files.add")); |
---|
62 | my @toreplace = readFileList(catfile($proposaldir, "files.replace")); |
---|
63 | |
---|
64 | open(CHECKMD5, ">", catfile($outdir, "check.md5")) or die "Can't open check.md5: $!"; |
---|
65 | print CHECKMD5 map { join(" ", @{$_})."\n" } @todelete, @toreplace; |
---|
66 | close(CHECKMD5); |
---|
67 | |
---|
68 | open(ABSENT, ">", catfile($outdir, "oldfiles.absent")) or die "Can't open oldfiles.absent: $!"; |
---|
69 | print ABSENT map { $_->[1]."\n" } @toadd; |
---|
70 | close(ABSENT); |
---|
71 | |
---|
72 | open(DELETE, ">", catfile($outdir, "files.delete")) or die "Can't open files.delete: $!"; |
---|
73 | print DELETE map { $_->[1]."\n" } @todelete, @toreplace; |
---|
74 | close(DELETE); |
---|
75 | |
---|
76 | my $outfiletarball = catfile($outdir, "newfiles.tar.gz"); |
---|
77 | my $cwd = getcwd(); |
---|
78 | |
---|
79 | chdir($newdir); |
---|
80 | my $tar = Archive::Tar->new; |
---|
81 | $tar->add_files(map { $_->[1] } @toadd, @toreplace); |
---|
82 | $tar->write($outfiletarball, 1); |
---|
83 | chdir($cwd) or die "Couldn't chdir back to $cwd: $!\n"; |
---|
84 | |
---|
85 | if (0) { |
---|
86 | my $outfiledir = catdir($outdir, "newfiles"); |
---|
87 | (-d $outfiledir || mkdir($outfiledir)) or die "Can't mkdir($outfiledir)\n"; |
---|
88 | |
---|
89 | foreach my $file (@toadd, @toreplace) { |
---|
90 | my $filename = $file->[1]; |
---|
91 | my $src = catfile($newdir, $filename); |
---|
92 | my $dest = catfile($outfiledir, $filename); |
---|
93 | my (undef, $dir, undef) = splitpath($dest); |
---|
94 | mkpath($dir); |
---|
95 | copy($src, $dest) or die "Couldn't copy $filename from $src to $dest: $!"; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | copy(catfile($proposaldir, "update.diff"), catfile($outdir, "update.diff")) or die "Couldn't copy update.diff: $!"; |
---|
100 | |
---|
101 | open (SCRIPT, ">", catfile($outdir, "update")) or die "Couldn't write update: $!"; |
---|
102 | printf SCRIPT <<'EOF', catfile("/mit/scripts/deploy/", $old), catfile("/mit/scripts/deploy/", $new); |
---|
103 | #!/bin/bash |
---|
104 | set -e -o noclobber |
---|
105 | |
---|
106 | die () { echo "$1" >&2; rm .scripts-security-upd-lock; exit 1; } |
---|
107 | |
---|
108 | patchdir=$(dirname "$0") |
---|
109 | |
---|
110 | >.scripts-security-upd-lock || die "error: Unable to obtain update lock." |
---|
111 | |
---|
112 | packages=$(tail -n 1 .scripts-version) |
---|
113 | |
---|
114 | echo "[$PWD] begin dry run" |
---|
115 | |
---|
116 | found="" |
---|
117 | newpackages="" |
---|
118 | for package in $packages; do |
---|
119 | if [ "$package" = "%s" ]; then |
---|
120 | found="yes" |
---|
121 | newpackages="$newpackages%s " |
---|
122 | else |
---|
123 | newpackages="$newpackages$package " |
---|
124 | fi |
---|
125 | done |
---|
126 | |
---|
127 | [ "$found" = "yes" ] || die "error: Correct version not found." |
---|
128 | xargs -n1 test ! -e <"$patchdir/oldfiles.absent" || die "error: Conflicting files exist." |
---|
129 | 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 |
---|
135 | patch -stN --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || : |
---|
136 | xargs rm -f <"$patchdir/files.delete" |
---|
137 | tar -xzf "$patchdir/newfiles.tar.gz" |
---|
138 | ( |
---|
139 | cat .scripts-version.old |
---|
140 | echo |
---|
141 | date "+%%F %%T %%z" |
---|
142 | echo "$USER@$(hostname)" |
---|
143 | echo "$patchdir" |
---|
144 | echo "$newpackages" |
---|
145 | ) >.scripts-version |
---|
146 | rm -f .scripts-version.old |
---|
147 | |
---|
148 | rm -f .scripts-security-upd-lock |
---|
149 | |
---|
150 | echo "[$PWD] done" |
---|
151 | |
---|
152 | exit 0 |
---|
153 | EOF |
---|
154 | chmod 0755, catfile($outdir, "update"); |
---|