-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate-deploy.pl
executable file
·144 lines (122 loc) · 3.64 KB
/
update-deploy.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
#!/usr/bin/env perl
#
# This script checks for updates to either the mapper codebase or to
# the mapping files. It then rebuilds the mapper if necessary and
# unpacks the deployment package into the specified base
# directory. The base directory will contain directories named after
# git hash codes and a symlink 'current' pointing to the most recent
# one.
#
# When using the mapper, always use the 'current' directory. That way
# the version can be updated and rolled back by simply changing the
# symlink.
#
# To run this script:
#
# update-deploy.pl [-f] [targetdir] [mapdir]
#
# Default values are targetdir="../mapper", mapdir="../md-mapping".
#
# The flag -f forces rebuilding even if there have been no updates.
#
# This script requires git, Maven, JDK >= 1.7, and tar.
#
# This script returns a value, which may be examined by other scripts:
# 0 - nothing has changed
# 1 - something changed (existing records should be re-mapped)
# 2 - an error occurred
#
# Lari Lampen (MPI-PL), 2013-2014.
use strict;
use warnings;
use Cwd qw(getcwd abs_path);
# Get hash of the current commit (HEAD) in either the current
# directory or, if specified, another directory.
sub git_hash(;$) {
my $dir;
if (scalar @_ > 0) {
$dir = getcwd();
chdir($_[0]);
}
my $rev = `git rev-parse HEAD`;
if (scalar @_ > 0) {
chdir($dir);
}
chomp $rev;
return $rev;
}
# Update directory via git. Return 0 if no changes were made, 1 if an
# update was received. If a directory is passed as parameter, do the
# update there (and return to the intial working directory
# afterwards).
sub git_pull(;$) {
my $dir;
if (scalar @_ > 0) {
$dir = getcwd();
chdir($_[0]);
}
my $res = `git pull`;
if (scalar @_ > 0) {
chdir($dir);
}
if ($res =~ /Already up-to-date/) {
return 0;
}
return 1;
}
my $force;
if (scalar @ARGV>0 && $ARGV[0] eq "-f") {
$force=1;
shift @ARGV;
}
my $targetdir = (scalar @ARGV > 0) ? $ARGV[0] : "../mapper";
my $mapdir = (scalar @ARGV > 1) ? $ARGV[1] : "../md-mapping";
die "Error: directory $mapdir does not exist" if !-d $mapdir;
my $upd_mapper = git_pull();
my $upd_mappings = git_pull($mapdir);
# Check situations where rebuilding is not needed.
unless ($force || $upd_mapper) {
if ($upd_mappings) {
print "Only mappings have changed.\n";
exit 1;
}
print "No changes.\n";
exit 0;
}
# If we got this far, we'll need to rebuild the mapper.
mkdir $targetdir unless -d $targetdir;
my $hash_mapper = git_hash();
my $curr = $targetdir . '/current';
unlink $curr if -d $curr;
my $target = $targetdir . '/' . $hash_mapper;
print "Rebuilding needed; running Maven.\n";
if (system("mvn -Dmaven.test.skip=true clean package assembly:assembly") != 0) {
print "Error: Maven build failed. Deployed mapper has not been modified.\n";
exit 2;
}
my $glob = getcwd() . '/target/md-mapper-*.tar.gz';
my @files = glob $glob;
if (scalar @files != 1) {
print "Confused trying to find deployment package. Stop.\n";
exit 2;
}
my $pack = $files[0];
if (-d $target) {
print "Removing existing $target\n";
system("rm -rf $target");
}
mkdir $target;
system("tar xfz $pack -C $targetdir/$hash_mapper") == 0 || die "Error: tar failed";
# Find the only directory under $hash_mapper.
$glob = "$targetdir/$hash_mapper/*";
@files = glob $glob;
if (scalar @files != 1) {
print "Error: Unexpected directory structure. Symlink not created. Report this as a bug!\n";
exit 2;
}
symlink $files[0], "$targetdir/current";
print "New version $hash_mapper deployed successfully; see $targetdir/current.\n";
unless (-f "$target/mapfiles") {
symlink abs_path("$mapdir/mapfiles"), "$targetdir/current/mapfiles";
}
exit 1;