Line 0
Link Here
|
|
|
1 |
package Koha::AtomicUpdater; |
2 |
|
3 |
# Copyright Open Source Freedom Fighters |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Carp; |
22 |
use Scalar::Util qw(blessed); |
23 |
use Try::Tiny; |
24 |
use Data::Format::Pretty::Console qw(format_pretty); |
25 |
use Git; |
26 |
use YAML::XS; |
27 |
use File::Slurp; |
28 |
|
29 |
use C4::Installer; |
30 |
|
31 |
use Koha::Database; |
32 |
use Koha::Cache; |
33 |
use Koha::AtomicUpdate; |
34 |
|
35 |
use base qw(Koha::Objects); |
36 |
|
37 |
use Koha::Exceptions::File; |
38 |
use Koha::Exceptions::Parse; |
39 |
use Koha::Exceptions::BadParameter; |
40 |
use Koha::Exceptions::DuplicateObject; |
41 |
|
42 |
sub _type { |
43 |
return 'Atomicupdate'; |
44 |
} |
45 |
sub object_class { |
46 |
return 'Koha::AtomicUpdate'; |
47 |
} |
48 |
sub _get_castable_unique_columns { |
49 |
return ['atomicupdate_id']; |
50 |
} |
51 |
|
52 |
=head find |
53 |
@OVERLOADS |
54 |
my $$atomicUpdate = $atomicUpdater->find($issue_id || $atomicupdate_id); |
55 |
|
56 |
@PARAM1 Scalar, issue_id or atomicupdate_id |
57 |
@RETURNS Koha::AtomicUpdate |
58 |
@THROWS Koha::Exceptions::BadParameter, if @PARAM1 is not a scalar |
59 |
Koha::Exceptions::DuplicateObject, if @PARAM1 matches both the issue_id and atomicupdate_id, |
60 |
you should change your issue naming convention. |
61 |
=cut |
62 |
|
63 |
sub find { |
64 |
my ( $self, $id ) = @_; |
65 |
return unless $id; |
66 |
if (ref($id)) { |
67 |
return $self->SUPER::find($id); |
68 |
} |
69 |
|
70 |
my @results = $self->_resultset()->search({'-or' => [ |
71 |
{issue_id => $id}, |
72 |
{atomicupdate_id => $id} |
73 |
]}); |
74 |
return unless @results; |
75 |
if (scalar(@results > 1)) { |
76 |
my @cc1 = caller(1); |
77 |
my @cc0 = caller(0); |
78 |
Koha::Exceptions::DuplicateObject->throw(error => $cc1[3]."() -> ".$cc0[3]."():> Given \$id '$id' matches multiple issue_ids and atomicupdate_ids. Aborting because couldn't get a uniquely identifying AtomicUpdate."); |
79 |
} |
80 |
|
81 |
my $object = $self->object_class()->_new_from_dbic( $results[0] ); |
82 |
return $object; |
83 |
} |
84 |
|
85 |
my $updateOrderFilename = '_updateorder'; |
86 |
|
87 |
sub new { |
88 |
my ($class, $params) = @_; |
89 |
|
90 |
my $cache = Koha::Cache->new(); |
91 |
my $self = $cache->get_from_cache('Koha::AtomicUpdater') || {}; |
92 |
bless($self, $class); |
93 |
|
94 |
$self->{verbose} = $params->{verbose} || $self->{verbose} || 0; |
95 |
$self->{scriptDir} = $params->{scriptDir} || $self->{scriptDir} || C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/'; |
96 |
$self->{confFile} = $params->{confFile} || $self->{confFile} || C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate.conf'; |
97 |
$self->{gitRepo} = $params->{gitRepo} || $self->{gitRepo} || $ENV{KOHA_PATH}; |
98 |
$self->{dryRun} = $params->{dryRun} || $self->{dryRun} || 0; |
99 |
|
100 |
$self->_loadConfig(); |
101 |
return $self; |
102 |
} |
103 |
|
104 |
=head getAtomicUpdates |
105 |
|
106 |
my $atomicUpdates = $atomicUpdater->getAtomicUpdates(); |
107 |
|
108 |
Gets all the AtomicUpdate-objects in the DB. This result should be Koha::Cached. |
109 |
@RETURNS HASHRef of Koha::AtomicUpdate-objects, keyed with the issue_id |
110 |
=cut |
111 |
|
112 |
sub getAtomicUpdates { |
113 |
my ($self) = @_; |
114 |
|
115 |
my @au = $self->search({}); |
116 |
my %au; #HASHify the AtomicUpdate-objects for easy searching. |
117 |
foreach my $au (@au) { |
118 |
$au{$au->issue_id} = $au; |
119 |
} |
120 |
return \%au; |
121 |
} |
122 |
|
123 |
sub addAtomicUpdate { |
124 |
my ($self, $params) = @_; |
125 |
print "Adding atomicupdate '".($params->{issue_id} || $params->{filename})."'\n" if $self->{verbose} > 2; |
126 |
|
127 |
my $atomicupdate = Koha::AtomicUpdate->new($params); |
128 |
$atomicupdate->store(); |
129 |
$atomicupdate = $self->find($atomicupdate->issue_id); |
130 |
return $atomicupdate; |
131 |
} |
132 |
|
133 |
sub removeAtomicUpdate { |
134 |
my ($self, $issueId) = @_; |
135 |
print "Deleting atomicupdate '$issueId'\n" if $self->{verbose} > 2; |
136 |
|
137 |
my $atomicupdate = $self->find($issueId); |
138 |
if ($atomicupdate) { |
139 |
$atomicupdate->delete; |
140 |
print "Deleted atomicupdate '$issueId'\n" if $self->{verbose} > 2; |
141 |
} |
142 |
else { |
143 |
Koha::Exceptions::BadParameter->throw(error => __PACKAGE__."->removeIssueFromLog():> No such Issue '$issueId' stored to the atomicupdates-table"); |
144 |
} |
145 |
} |
146 |
|
147 |
sub listToConsole { |
148 |
my ($self) = @_; |
149 |
my @stringBuilder; |
150 |
|
151 |
my @atomicupdates = $self->search({}); |
152 |
foreach my $au (@atomicupdates) { |
153 |
push @stringBuilder, $au->unblessed(); |
154 |
} |
155 |
return Data::Format::Pretty::Console::format_pretty(\@stringBuilder); |
156 |
} |
157 |
|
158 |
sub listPendingToConsole { |
159 |
my ($self) = @_; |
160 |
my @stringBuilder; |
161 |
|
162 |
my $atomicUpdates = $self->getPendingAtomicUpdates(); |
163 |
foreach my $key (sort keys %$atomicUpdates) { |
164 |
my $au = $atomicUpdates->{$key}; |
165 |
push @stringBuilder, $au->unblessed(); |
166 |
} |
167 |
return Data::Format::Pretty::Console::format_pretty(\@stringBuilder); |
168 |
} |
169 |
|
170 |
sub getPendingAtomicUpdates { |
171 |
my ($self) = @_; |
172 |
|
173 |
my %pendingAtomicUpdates; |
174 |
my $atomicupdateFiles = $self->_getValidAtomicUpdateScripts(); |
175 |
my $atomicUpdatesDeployed = $self->getAtomicUpdates(); |
176 |
foreach my $key (keys(%$atomicupdateFiles)) { |
177 |
my $au = $atomicupdateFiles->{$key}; |
178 |
my $parsedissueId = $self->_parseIssueIds($au->issue_id); |
179 |
unless ($atomicUpdatesDeployed->{$au->issue_id} || $atomicUpdatesDeployed->{$parsedissueId}) { |
180 |
#This script hasn't been deployed. |
181 |
$pendingAtomicUpdates{$au->issue_id} = $au; |
182 |
} |
183 |
} |
184 |
return \%pendingAtomicUpdates; |
185 |
} |
186 |
|
187 |
=head applyAtomicUpdates |
188 |
|
189 |
my $atomicUpdater = Koha::AtomicUpdater->new(); |
190 |
my $appliedAtomicupdates = $atomicUpdater->applyAtomicUpdates(); |
191 |
|
192 |
Checks the atomicupdates/-directory for any not-applied update scripts and |
193 |
runs them in the order specified in the _updateorder-file in atomicupdate/-directory. |
194 |
|
195 |
@RETURNS ARRAYRef of Koha::AtomicUpdate-objects deployed on this run |
196 |
=cut |
197 |
|
198 |
sub applyAtomicUpdates { |
199 |
my ($self) = @_; |
200 |
|
201 |
my %appliedUpdates; |
202 |
|
203 |
my $atomicUpdates = $self->getPendingAtomicUpdates(); |
204 |
my $updateOrder = $self->getUpdateOrder(); |
205 |
foreach my $issueId ( @$updateOrder ) { |
206 |
my $atomicUpdate = $atomicUpdates->{$issueId}; |
207 |
next unless $atomicUpdate; #Not each ordered Git commit necessarily have a atomicupdate-script. |
208 |
|
209 |
$self->applyAtomicUpdate($atomicUpdate); |
210 |
$appliedUpdates{$issueId} = $atomicUpdate; |
211 |
} |
212 |
|
213 |
#Check that we have actually applied all the updates. |
214 |
my $stillPendingAtomicUpdates = $self->getPendingAtomicUpdates(); |
215 |
if (scalar(%$stillPendingAtomicUpdates)) { |
216 |
my @issueIds = sort keys %$stillPendingAtomicUpdates; |
217 |
print "Warning! After upgrade, the following atomicupdates are still pending '@issueIds'\n Try rebuilding the atomicupdate-scripts update order from the original Git repository.\n"; |
218 |
} |
219 |
|
220 |
return \%appliedUpdates; |
221 |
} |
222 |
|
223 |
sub applyAtomicUpdate { |
224 |
my ($self, $atomicUpdate) = @_; |
225 |
#Validate params |
226 |
unless ($atomicUpdate) { |
227 |
Koha::Exceptions::BadParameter->throw(error => __PACKAGE__."->applyAtomicUpdate($atomicUpdate):> Parameter must be a Koha::AtomicUpdate-object or a path to a valid atomicupdates-script!"); |
228 |
} |
229 |
if ($atomicUpdate && ref($atomicUpdate) eq '') { #We have a scalar, presumably a filepath to atomicUpdate-script. |
230 |
$atomicUpdate = Koha::AtomicUpdate->new({filename => $atomicUpdate}); |
231 |
} |
232 |
|
233 |
#$atomicUpdate = Koha::AtomicUpdater->cast($atomicUpdate); |
234 |
|
235 |
my $filename = $atomicUpdate->filename; |
236 |
print "Applying file '$filename'\n" if $self->{verbose} > 2; |
237 |
|
238 |
unless ($self->{dryRun}) { |
239 |
my $rv; |
240 |
if ( $filename =~ /\.sql$/ ) { |
241 |
my $installer = C4::Installer->new(); |
242 |
$rv = $installer->load_sql( $self->{scriptDir}.'/'.$filename ) ? 0 : 1; |
243 |
} elsif ( $filename =~ /\.(perl|pl)$/ ) { |
244 |
my $fileAndPath = $self->{scriptDir}.'/'.$filename; |
245 |
$rv = do $fileAndPath; |
246 |
unless ($rv) { |
247 |
warn "couldn't parse $fileAndPath: $@\n" if $@; |
248 |
warn "couldn't do $fileAndPath: $!\n" unless defined $rv; |
249 |
warn "couldn't run $fileAndPath\n" unless $rv; |
250 |
} |
251 |
} |
252 |
print 'AtomicUpdate '.$atomicUpdate->filename." done.\n" if $self->{verbose} > 0; |
253 |
$atomicUpdate->store(); |
254 |
} |
255 |
|
256 |
print "File '$filename' applied\n" if $self->{verbose} > 2; |
257 |
} |
258 |
|
259 |
=head _getValidAtomicUpdateScripts |
260 |
|
261 |
@RETURNS HASHRef of Koha::AtomicUpdate-objects, of all the files |
262 |
in the atomicupdates/-directory that can be considered valid. |
263 |
Validity is currently conforming to the naming convention. |
264 |
Keys are the issue_id of atomicupdate-scripts |
265 |
Eg. {'Bug8584' => Koha::AtomicUpdate, |
266 |
... |
267 |
} |
268 |
=cut |
269 |
|
270 |
sub _getValidAtomicUpdateScripts { |
271 |
my ($self) = @_; |
272 |
|
273 |
my %atomicUpdates; |
274 |
opendir( my $dirh, $self->{scriptDir} ); |
275 |
foreach my $file ( sort readdir $dirh ) { |
276 |
print "Looking at file '$file'\n" if $self->{verbose} > 2; |
277 |
|
278 |
my $atomicUpdate; |
279 |
try { |
280 |
$atomicUpdate = Koha::AtomicUpdate->new({filename => $file}); |
281 |
} catch { |
282 |
if (blessed($_)) { |
283 |
if ($_->isa('Koha::Exceptions::File') || $_->isa('Koha::Exceptions::Parse')) { |
284 |
print "File-error for file '$file': ".$_->error()." \n" if $self->{verbose} > 2; |
285 |
#We can ignore filename validation issues, since the directory has |
286 |
#loads of other types of files as well. Like README . .. |
287 |
} |
288 |
else { |
289 |
$_->rethrow(); |
290 |
} |
291 |
} |
292 |
else { |
293 |
die $_; #Rethrow the unknown Exception |
294 |
} |
295 |
}; |
296 |
next unless $atomicUpdate; |
297 |
|
298 |
$atomicUpdates{$atomicUpdate->issue_id} = $atomicUpdate; |
299 |
} |
300 |
return \%atomicUpdates; |
301 |
} |
302 |
|
303 |
=head getUpdateOrder |
304 |
|
305 |
$atomicUpdater->getUpdateOrder(); |
306 |
|
307 |
@RETURNS ARRAYRef of Strings, IssueIds ordered from the earliest to the newest. |
308 |
=cut |
309 |
|
310 |
sub getUpdateOrder { |
311 |
my ($self) = @_; |
312 |
|
313 |
my $updateOrderFilepath = $self->{scriptDir}."/$updateOrderFilename"; |
314 |
open(my $FH, "<:encoding(UTF-8)", $updateOrderFilepath) or die "Koha::AtomicUpdater->_saveAsUpdateOrder():> Couldn't open the updateOrderFile for reading\n$!\n"; |
315 |
my @updateOrder = map {chomp($_); $_;} <$FH>; |
316 |
close $FH; |
317 |
return \@updateOrder; |
318 |
} |
319 |
|
320 |
=head |
321 |
|
322 |
my $issueIdOrder = Koha::AtomicUpdater->buildUpdateOrderFromGit(10000); |
323 |
|
324 |
Creates a update order file '_updateorder' for atomicupdates to know which updates come before which. |
325 |
This is a simple way to make sure the atomicupdates are applied in the correct order. |
326 |
The update order file is by default in your $KOHA_PATH/installer/data/mysql/atomicupdate/_updateorder |
327 |
|
328 |
This requires a Git repository to be in the $ENV{KOHA_PATH} to be effective. |
329 |
|
330 |
@PARAM1 Integer, How many Git commits to include to the update order file, |
331 |
10000 is a good default. |
332 |
@RETURNS ARRAYRef of Strings, The update order of atomicupdates from oldest to newest. |
333 |
=cut |
334 |
|
335 |
sub buildUpdateOrderFromGit { |
336 |
my ($self, $gitCommitsCount) = @_; |
337 |
|
338 |
my %orderedCommits; #Store the commits we have ordered here, so we don't reorder any followups. |
339 |
my @orderedCommits; |
340 |
|
341 |
my $i = 0; #Index of array where we push issue_ids |
342 |
my $commits = $self->_getGitCommits($gitCommitsCount); |
343 |
foreach my $commit (reverse @$commits) { |
344 |
|
345 |
my ($commitHash, $commitTitle) = $self->_parseGitOneliner($commit); |
346 |
unless ($commitHash && $commitTitle) { |
347 |
next(); |
348 |
} |
349 |
|
350 |
my $issueId; |
351 |
try { |
352 |
$issueId = Koha::AtomicUpdate::getIssueIdentifier(undef, $commitTitle); |
353 |
} catch { |
354 |
if (blessed($_)) { |
355 |
if($_->isa('Koha::Exceptions::Parse')) { |
356 |
#Silently ignore parsing errors |
357 |
print "Koha::AtomicUpdater->buildUpdateOrderFromGit():> Couldn't parse issue_id from Git commit title '$commitTitle'.\n" |
358 |
if $self->{verbose} > 1; |
359 |
} |
360 |
else { |
361 |
$_->rethrow(); |
362 |
} |
363 |
} |
364 |
else { |
365 |
die $_; |
366 |
} |
367 |
}; |
368 |
next unless $issueId; |
369 |
|
370 |
if ($orderedCommits{ $issueId }) { |
371 |
next(); |
372 |
} |
373 |
else { |
374 |
$orderedCommits{ $issueId } = $issueId; |
375 |
$orderedCommits[$i] = $issueId; |
376 |
$i++; |
377 |
} |
378 |
} |
379 |
|
380 |
$self->_saveAsUpdateOrder(\@orderedCommits); |
381 |
return \@orderedCommits; |
382 |
} |
383 |
|
384 |
sub _parseIssueIds { |
385 |
my ($self, $issueId) = @_; |
386 |
|
387 |
my @keys = split /(-)/, $issueId; |
388 |
delete $keys[1]; |
389 |
@keys = grep defined, @keys; |
390 |
|
391 |
return join('', @keys); |
392 |
} |
393 |
|
394 |
sub _getGitCommits { |
395 |
my ($self, $count) = @_; |
396 |
my $repo = Git->repository(Directory => $self->{gitRepo}); |
397 |
|
398 |
#We can read and print 10000 git commits in less than three seconds :) good Git! |
399 |
my @commits = $repo->command('show', '--pretty=oneline', '--no-patch', '-'.$count); |
400 |
return \@commits; |
401 |
} |
402 |
|
403 |
sub _parseGitOneliner { |
404 |
my ($self, $gitLiner) = @_; |
405 |
|
406 |
my ($commitHash, $commitTitle) = ($1, $2) if $gitLiner =~ /^(\w{40}) (.+)$/; |
407 |
unless ($commitHash && $commitTitle) { |
408 |
print "Koha::AtomicUpdater->parseGitOneliner():> Couldn't parse Git commit '$gitLiner' to hash and title.\n" |
409 |
if $self->{verbose} > 1; |
410 |
return(); |
411 |
} |
412 |
return ($commitHash, $commitTitle); |
413 |
} |
414 |
|
415 |
sub _saveAsUpdateOrder { |
416 |
my ($self, $orderedUpdates) = @_; |
417 |
|
418 |
my $updateOrderFilepath = $self->{scriptDir}."/$updateOrderFilename"; |
419 |
my $text = join("\n", @$orderedUpdates); |
420 |
open(my $FH, ">:encoding(UTF-8)", $updateOrderFilepath) or die "Koha::AtomicUpdater->_saveAsUpdateOrder():> Couldn't open the updateOrderFile for writing\n$!\n"; |
421 |
print $FH $text; |
422 |
close $FH; |
423 |
} |
424 |
|
425 |
=head %config |
426 |
Package static variable to the configurations Hash. |
427 |
=cut |
428 |
|
429 |
my $config; |
430 |
|
431 |
sub _loadConfig { |
432 |
my ($self) = @_; |
433 |
|
434 |
if (-e $self->{confFile}) { |
435 |
my $yaml = File::Slurp::read_file( $self->{confFile}, { binmode => ':utf8' } ) ; |
436 |
$config = YAML::XS::Load($yaml); |
437 |
} |
438 |
} |
439 |
|
440 |
1; |