blib/script/narada-install

filter: Statementstmt 94.9% 150 / 158 Branchbran 87.9% 51 / 58 Conditioncond 88.8% 24 / 27 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 96.0% 24 / 25 Podpod n/a totaltotal 91.8% 260 / 283 CC 40 SCAR 19.4
CC
40
Cov
93%
main
15.0
ask
12.0
check_path
10.0
CRAP 6.9
« blib/lib/Narada/Config.pm ↑ Summary blib/lib/Narada.pm »
1 #!/usr/bin/env perl
2 10 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = 'v2.3.4';
8  
9 10 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use lib "$FindBin::Bin/../lib/perl5";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Narada::Lock qw( exclusive_lock unlock_new shared_lock unlock );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use App::migrate;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 ▶use constant INITIAL_VERSION => '0.0.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use constant FULL_BACKUP => path('.backup/full.tar');
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use constant USAGE => <<'EOUSAGE';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 Usage:
21 narada-install [--allow-downgrade|-D] [--allow-restore|-R] [-f <file>] <ver>
22 narada-install [-f <file>] --path|-p <ver1> <ver2> <ver3> ...
23 narada-install --check|-c <file>
24 narada-install --help|-h
25 EOUSAGE
26  
27 10 my $Last_backup; # OPTIMIZATION delay copying last backup to drop exclusive_lock faster
28  
29  
30 10 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 10 10
31  
32  
33 9 ▶sub err { die "narada-install: @_\n" };
Subroutine err: called CC=1 SCAR=0.0
34  
35 sub main {
36 ## no critic (RequireCarping)
37 62 ▶ my ($allow_downgrade, $allow_restore, @files) = (0, 0);
Subroutine main: called CC=15 SCAR=27.1
38 62 my ($is_path, $check, $is_help);
39 62 my ($prev_version, $next_version, $path);
40 62 ▶ GetOptionsFromArray(\@_,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "D|allow-downgrade", \$allow_downgrade, "R|allow-restore", \$allow_restore, "f=s\@", \@files, "p|path", \$is_path, "c|check=s", \$check, "h|help", \$is_help) or die "Usage:\n narada-install [--allow-downgrade|-D] [--allow-restore|-R] [-f <file>] <ver>\n narada-install [-f <file>] --path|-p <ver1> <ver2> <ver3> ...\n narada-install --check|-c <file>\n narada-install --help|-h\n" 1 61 62
41 'D|allow-downgrade' => \$allow_downgrade,
42 'R|allow-restore' => \$allow_restore,
43 'f=s@' => \@files,
44 'p|path' => \$is_path,
45 'c|check=s' => \$check,
46 'h|help' => \$is_help,
47 ) or die USAGE;
48 61 ▶ if ($is_help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_help) 4 57 61
49 4 print USAGE;
50 4 return;
51 }
52 57 ▶ if (defined $check) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check) 12 45 57
53 12 ▶ die USAGE if @_ || $is_path || $allow_downgrade || $allow_restore || @files;
Branch: 1 branch
BranchTrueFalseTotal
if @_ or $is_path or $allow_downgrade or $allow_restore or @files 2 7 9
Condition100%12/12
@_ or $is_path or $allow_downgrade or $allow_restore or @files
l!l&&r!l&&!r
417
@_ or $is_path or $allow_downgrade or $allow_restore
l!l&&r!l&&!r
318
@_ or $is_path or $allow_downgrade
l!l&&r!l&&!r
219
@_ or $is_path
l!l&&r!l&&!r
1110
MC/DC100%5/5
@_ or $is_path or $allow_downgrade or $allow_restore or @files
@_ $is_path $allow_downgrade $allow_restore @files
Truth table100%6/6
A or B or C or D or E
A= @_B= $is_pathC= $allow_downgradeD= $allow_restoreE= @files
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
54 7 say "Checking $check";
55 7 App::migrate->new->load($check);
56 2 return;
57 }
58 45 ▶ if ($is_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_path) { } 7 38 45
59 7 ▶ die USAGE if @_ <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ <= 2 4 3 7
60 3 $path = [@_];
61 3 $prev_version = $path->[0];
62 3 $next_version = $path->[-1];
63 3 $allow_downgrade= 1;
64 3 $allow_restore = 1;
65 }
66 else {
67 38 ▶ die USAGE if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 4 34 38
68 34 $next_version = $_[0];
69 34 $prev_version = INITIAL_VERSION;
70 34 ▶ if (path(q{.})->children(qr/\A(?![.]release\z|[.]backup\z|[.]lock)/ms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (path(".")->children(qr/\A(?![.]release\z|[.]backup\z|[.]lock)/ms)) 23 11 34
71 23 Narada::detect('narada');
72 23 ($prev_version) = path('VERSION')->lines({chomp=>1});
73 }
74 }
75 37 ▶ if ($next_version eq $prev_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_version eq $prev_version) 5 32 37
76 5 return;
77 }
78  
79 32 my $migrate = load($prev_version, $next_version, @files);
80 32 ▶ $path ||= get_path($migrate, $prev_version, $next_version);
Condition66%2/3
$path ||= get_path($migrate, $prev_version, $next_version)
l!l&&r!l&&!r
1310
MC/DC0%0/2
$path ||= get_path($migrate, $prev_version, $next_version)
$path get_path($migrate, $prev_version, $next_version)
Truth table66%2/3
A ||= B
A= $pathB= get_path($migrate, $prev_version, $next_version)
ABresult
1X1
011
000
81 32 check_path($migrate, $path, $allow_downgrade, $allow_restore);
82 31 migrate($migrate, $path);
83  
84 25 return;
85 }
86  
87 sub load {
88 38 ▶ my ($prev_version, $next_version, @files) = @_;
Subroutine load: called CC=4 SCAR=13.9
89 38 my $migrate = App::migrate->new;
90 38 ▶ if ($next_version ne INITIAL_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_version ne "0.0.0") 23 15 38
91 23 push @files, ".release/$next_version.migrate";
92 }
93 38 ▶ if (-f ".release/$prev_version.migrate") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".release/$prev_version.migrate") 27 11 38
94 27 push @files, ".release/$prev_version.migrate";
95 }
96 38 for (@files) {
97 70 say "Loading $_";
98 70 $migrate->load($_);
99 }
100 37 return $migrate;
101 }
102  
103 sub get_path {
104 33 ▶ my ($migrate, $prev_version, $next_version) = @_;
Subroutine get_path: called CC=3 SCAR=11.0
105 33 my @paths = $migrate->find_paths($prev_version, $next_version);
106 33 ▶ if (0 == @paths) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == @paths) { } 1 32 33
elsif (1 != @paths) { } 1 31 32
107 1 err "Unable to find migration path from $prev_version to $next_version";
108 }
109 elsif (1 != @paths) {
110 err join "\n",
111 'Found more than one upgrade path, run one of these commands to choose a path:',
112 1 map {"\tnarada-install --path @{$_}"} @paths;
113 }
114 31 return $paths[0];
115 }
116  
117 sub check_path {
118 39 ▶ my ($migrate, $path, $allow_downgrade, $allow_restore) = @_;
Subroutine check_path: called CC=10 SCAR=23.0
119 39 ▶ if ($allow_restore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_restore) 15 24 39
120 15 $allow_downgrade = 1;
121 }
122 39 for my $step ($migrate->get_steps($path)) {
123 759 my $t = $step->{type};
124 759 ▶ my $is_down = $t eq 'downgrade' || $t eq 'after_downgrade';
Condition100%3/3
$t eq 'downgrade' || $t eq 'after_downgrade'
l!l&&r!l&&!r
39017352
MC/DC100%2/2
$t eq 'downgrade' || $t eq 'after_downgrade'
$t eq 'downgrade' $t eq 'after_downgrade'
Truth table100%3/3
A || B
A= $t eq 'downgrade'B= $t eq 'after_downgrade'
ABresult
1X1
011
000
125 759 ▶ err 'Downgrade required, use --allow-downgrade to continue'
Branch: 1 branch
BranchTrueFalseTotal
if $is_down and not $allow_downgrade 2 757 759
Condition100%3/3
$is_down and not $allow_downgrade
!ll&&!rl&&r
3524052
MC/DC100%2/2
$is_down and not $allow_downgrade
$is_down not $allow_downgrade
Truth table100%3/3
A and not(B)
A= $is_downB= not $allow_downgrade
ABresult
0X0
100
111
126 if $is_down && !$allow_downgrade;
127 757 ▶ err 'Restore from backup required, use --allow-restore to continue'
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "RESTORE" and not $allow_restore 3 754 757
Condition100%3/3
$t eq "RESTORE" and not $allow_restore
!ll&&!rl&&r
74773
MC/DC100%2/2
$t eq "RESTORE" and not $allow_restore
$t eq "RESTORE" not $allow_restore
Truth table100%3/3
A and not(B)
A= $t eq "RESTORE"B= not $allow_restore
ABresult
0X0
100
111
128 if $t eq 'RESTORE' && !$allow_restore;
129 754 ▶ if ($t eq 'RESTORE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "RESTORE") 7 747 754
130 7 my $f = path(".backup/full-$step->{next_version}.tar");
131 7 ▶ err "Required backup not found: $f" if !$f->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_file 2 5 7
132 }
133 }
134 31 return;
135 }
136  
137 sub migrate {
138 28 ▶ my ($migrate, $path) = @_;
Subroutine migrate: called CC=3 SCAR=11.1
139  
140 28 exclusive_lock();
141 {
142 28 local $ENV{NARADA_SKIP_LOCK} = 1;
143 28 $Last_backup = undef; # tests may call main() many times
144 28 $migrate->on(BACKUP => \&_backup)
145 ->on(RESTORE => \&_restore)
146 ->on(VERSION => \&_version)
147 ->on(error => \&_error)
148 ->run($path);
149 }
150 22 unlock_new();
151  
152 22 shared_lock();
153 22 ▶ if ($Last_backup && !FULL_BACKUP->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Last_backup and not bless( [".backup/full.tar",".backup/full.tar"], 'Path::Tiny' )->exists) 22 0 22
Condition33%1/3
$Last_backup and not bless( [".backup/full.tar",".backup/full.tar"], 'Path::Tiny' )->exists
!ll&&!rl&&r
0022
MC/DC0%0/2
$Last_backup and not bless( [".backup/full.tar",".backup/full.tar"], 'Path::Tiny' )->exists
$Last_backup not bless( [".backup/full.tar",".backup/full.tar"], 'Path::Tiny' )->exists
Truth table33%1/3
A and not(B)
A= $Last_backupB= not bless( [".backup/full.tar",".backup/full.tar"], 'Path::Tiny' )->exists
ABresult
0X0
100
111
154 22 path($Last_backup)->copy(FULL_BACKUP);
155 }
156 22 unlock();
157  
158 22 return;
159 }
160  
161 sub ask {
162 0 ▶ my ($msg, $match) = @_;
Subroutine ask: not called CC=3 SCAR=24.8
163 0 print $msg;
164 # TODO try IO::Prompter
165 ## no critic (ProhibitExplicitStdin)
166 0 ▶ while (<STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 0 0 0
167 0 chomp;
168 0 ▶ return $_ if /$match/ms;
Branch: 1 branch
BranchTrueFalseTotal
if /$match/ms 0 0 0
169 0 print $msg;
170 }
171 0 die "Interrupted by EOF\n";
172 }
173  
174 sub _backup {
175 76 ▶ my ($step) = @_;
Subroutine _backup: called CC=4 SCAR=13.9
176 76 ▶ if ($step->{version} ne INITIAL_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{"version"} ne "0.0.0") 66 10 76
177 66 ▶ if ($Last_backup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Last_backup) 37 29 66
178 37 path($Last_backup)->copy(FULL_BACKUP);
179 }
180 66 my $file = ".backup/full-$step->{version}.tar";
181 66 say "Backuping to $file";
182 66 ▶ system('narada-backup') == 0 or die "BACKUP failed\n";
Branch: 1 branch
BranchTrueFalseTotal
system("narada-backup") == 0 or die "BACKUP failed\n" 4 62 66
183 62 $Last_backup = $file;
184 62 FULL_BACKUP->move($Last_backup);
185 }
186 72 return;
187 }
188  
189 sub _restore {
190 9 ▶ my ($step) = @_;
Subroutine _restore: called CC=2 SCAR=6.9
191 9 my $file = ".backup/full-$step->{version}.tar";
192 9 say "Restoring from $file";
193 9 ▶ system('narada-restore', $file) == 0 or die "RESTORE failed\n";
Branch: 1 branch
BranchTrueFalseTotal
system("narada-restore", $file) == 0 or die "RESTORE failed\n" 3 6 9
194 6 return;
195 }
196  
197 sub _version {
198 72 ▶ my ($step) = @_;
Subroutine _version: called CC=2 SCAR=6.9
199 72 say "Migration to $step->{version} completed";
200 72 ▶ if ($step->{version} eq INITIAL_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{'version'} eq '0.0.0') { } 13 59 72
201 13 path('VERSION')->remove;
202 }
203 else {
204 59 path('VERSION')->spew_utf8("$step->{version}\n");
205 }
206 72 return;
207 }
208  
209 sub _error {
210 10 ▶ say q{};
Subroutine _error: called CC=3 SCAR=11.1
211 10 say 'YOU NEED TO MANUALLY FIX THIS ISSUE RIGHT NOW';
212 10 my $prompt
213 = "Please choose what to do:\n"
214 . " shell - run $ENV{SHELL} (exit from it to return to this menu)\n"
215 . " continue - continue migration (use if you have fixed this issue)\n"
216 . " restore - interrupt migration and RESTORE previous version from backup\n"
217 . 'Enter action [shell]: '
218 ;
219 10 while (1) {
220 10 my $ans = lc ask($prompt, qr/\A(?:shell|continue|restore|\s*)\z/msi);
221 10 ▶ if ($ans eq 'restore') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ans eq 'restore') { } 6 4 10
elsif ($ans eq 'continue') { } 4 0 4
222 6 die "Migration failed\n";
223 }
224 elsif ($ans eq 'continue') {
225 4 last;
226 }
227 else {
228 0 system $ENV{SHELL};
229 }
230 }
231 4 return;
232 }
233  
234  
235 10 1; # Magic true value required at end of module
236 __END__
237  
238 =encoding utf8
239  
240 =head1 NAME
241  
242 narada-install - migrate project to given version
243  
244  
245 =head1 VERSION
246  
247 This document describes narada-install version v2.3.4
248  
249  
250 =head1 USAGE
251  
252 narada-install [--allow-downgrade|-D] [--allow-restore|-R] [-f <file>] <ver>
253 narada-install [-f <file>] --path|-p <ver1> <ver2> <ver3> ...
254 narada-install --check|-c <file>
255 narada-install --help|-h
256  
257  
258 =head1 DESCRIPTION
259  
260 Should be executed in project deploy directory.
261  
262 Upgrade/downgrade your project in safe way:
263  
264 =over
265  
266 =item *
267  
268 It use Narada's exclusive lock to make sure no other code will run while
269 migrating project's files or databases (to make this works as expected you
270 must use Narada's shared lock around all your code which read or write any
271 project's files or databases).
272  
273 =item *
274  
275 Before every migration it will automatically create new backup
276 C<< .backup/full-<current_version>.tar >>.
277  
278 =item *
279  
280 If migration fail it will automatically restore previous version from backup.
281  
282 =item *
283  
284 Use L<App::migrate> file format to describe project migrations, which let you
285 downgrade fast and without losing any data whenever possible.
286  
287 =back
288  
289 When called with option C<--check|-c> will check syntax of given migrate
290 file and exit.
291  
292 It will try to load several migrate files, in this order:
293  
294 =over
295  
296 =item *
297  
298 file given using C<-f> option (if any)
299  
300 =item *
301  
302 C<< .release/<next_ver>.migrate >> where C<< <next_ver> >> is last
303 parameter (if this file exists)
304  
305 =item *
306  
307 C<< .release/<prev_ver>.migrate >> where C<< <prev_ver> >> is current
308 project version if called without C<--path> option and file C<VERSION>
309 exists or first parameter after C<--path> option (if this file exists)
310  
311 =back
312  
313 Then it will try to find migration path from current version to given and
314 execute it (or just execute given migration path if called with C<--path>
315 option).
316  
317  
318 =head1 CONFIGURATION AND ENVIRONMENT
319  
320 .lock
321 .lock.new
322 .lock.bg
323 $NARADA_SKIP_LOCK
324 .backup/full.tar
325 .backup/full-*.tar
326 .release/*.migrate
327  
328  
329 =head1 SUPPORT
330  
331 =head2 Bugs / Feature Requests
332  
333 Please report any bugs or feature requests through the issue tracker
334 at L<https://github.com/powerman/Narada/issues>.
335 You will be notified automatically of any progress on your issue.
336  
337 =head2 Source Code
338  
339 This is open source software. The code repository is available for
340 public review and contribution under the terms of the license.
341 Feel free to fork the repository and submit pull requests.
342  
343 L<https://github.com/powerman/Narada>
344  
345 git clone https://github.com/powerman/Narada.git
346  
347 =head2 Resources
348  
349 =over
350  
351 =item * MetaCPAN Search
352  
353 L<https://metacpan.org/search?q=Narada>
354  
355 =item * CPAN Ratings
356  
357 L<http://cpanratings.perl.org/dist/Narada>
358  
359 =item * AnnoCPAN: Annotated CPAN documentation
360  
361 L<http://annocpan.org/dist/Narada>
362  
363 =item * CPAN Testers Matrix
364  
365 L<http://matrix.cpantesters.org/?dist=Narada>
366  
367 =item * CPANTS: A CPAN Testing Service (Kwalitee)
368  
369 L<http://cpants.cpanauthors.org/dist/Narada>
370  
371 =back
372  
373  
374 =head1 AUTHOR
375  
376 Alex Efros E<lt>powerman@cpan.orgE<gt>
377  
378  
379 =head1 COPYRIGHT AND LICENSE
380  
381 This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>.
382  
383 This is free software, licensed under:
384  
385 The MIT (X11) License
386  
387  
388 =cut
« blib/lib/Narada/Config.pm ↑ Summary blib/lib/Narada.pm »