source: locker/update-system/bin/build-update @ 692

Last change on this file since 692 was 692, checked in by price, 14 years ago
Make updte development cycle shorter. - Remove lock file when fail, so we can run again - Accept --force to clobber the existing built-updater directory
  • Property svn:executable set to *
File size: 4.5 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 = "";
14my $force = 0;
15
16GetOptions(
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
26if (@ARGV < 3) {
27  print STDERR "Usage: $0 package oldversion newversion\n";
28  exit(1);
29}
30
31my ($package, $oldversion, $newversion) = @ARGV;
32my ($old, $new, $updatename) = ($package.'-'.$oldversion, $package.'-'.$newversion, $package.'-'.$oldversion.'-to-'.$newversion);
33
34my $proposaldir = $updatename.".proposal";
35my $outdir = catdir("/mit/scripts/deploy$scriptsdev/updates/", $updatename);
36
37(-d $proposaldir) or die "Can't find $proposaldir, did you run propose-update?\n";
38if (-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
48my $olddir = catdir($proposaldir,$old);
49my $newdir = catdir($proposaldir,$new);
50(-d $olddir && -d $newdir) or die "Packages not unpacked?\n";
51
52sub 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
60my @todelete = readFileList(catfile($proposaldir, "files.delete"));
61my @toadd = readFileList(catfile($proposaldir, "files.add"));
62my @toreplace = readFileList(catfile($proposaldir, "files.replace"));
63
64open(CHECKMD5, ">", catfile($outdir, "check.md5")) or die "Can't open check.md5: $!";
65print CHECKMD5 map { join("  ", @{$_})."\n" } @todelete, @toreplace;
66close(CHECKMD5);
67
68open(ABSENT, ">", catfile($outdir, "oldfiles.absent")) or die "Can't open oldfiles.absent: $!";
69print ABSENT map { $_->[1]."\n" } @toadd;
70close(ABSENT);
71
72open(DELETE, ">", catfile($outdir, "files.delete")) or die "Can't open files.delete: $!";
73print DELETE map { $_->[1]."\n" } @todelete, @toreplace;
74close(DELETE);
75
76my $outfiletarball = catfile($outdir, "newfiles.tar.gz");
77my $cwd = getcwd();
78
79chdir($newdir);
80my $tar = Archive::Tar->new;
81$tar->add_files(map { $_->[1] } @toadd, @toreplace);
82$tar->write($outfiletarball, 1);
83chdir($cwd) or die "Couldn't chdir back to $cwd: $!\n";
84
85if (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
99copy(catfile($proposaldir, "update.diff"), catfile($outdir, "update.diff")) or die "Couldn't copy update.diff: $!";
100
101open (SCRIPT, ">", catfile($outdir, "update")) or die "Couldn't write update: $!";
102printf SCRIPT <<'EOF', catfile("/mit/scripts/deploy/", $old), catfile("/mit/scripts/deploy/", $new);
103#!/bin/bash
104set -e -o noclobber
105
106die () { echo "$1" >&2; rm .scripts-security-upd-lock; exit 1; }
107
108patchdir=$(dirname "$0")
109
110>.scripts-security-upd-lock || die "error: Unable to obtain update lock."
111
112packages=$(tail -n 1 .scripts-version)
113
114echo "[$PWD] begin dry run"
115
116found=""
117newpackages=""
118for package in $packages; do
119    if [ "$package" = "%s" ]; then
120    found="yes"
121    newpackages="$newpackages%s "
122    else
123    newpackages="$newpackages$package "
124    fi
125done
126
127[ "$found" = "yes" ] || die "error: Correct version not found."
128xargs -n1 test ! -e <"$patchdir/oldfiles.absent" || die "error: Conflicting files exist."
129md5sum -c "$patchdir/check.md5" || die "error: MD5 check failed."
130patch -stN --dry-run --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || die "error: Patch dry run failed."
131
132echo "[$PWD] dry run OK, applying update"
133
134mv .scripts-version .scripts-version.old
135patch -stN --no-backup-if-mismatch -p2 <"$patchdir/update.diff" || :
136xargs rm -f <"$patchdir/files.delete"
137tar -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
146rm -f .scripts-version.old
147
148rm -f .scripts-security-upd-lock
149
150echo "[$PWD] done"
151
152exit 0
153EOF
154chmod 0755, catfile($outdir, "update");
Note: See TracBrowser for help on using the repository browser.