blib/lib/MYDan/Deploy.pm untested

filter: Statementstmt 0.0% 0 / 119 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 170 CC 9 SCAR 32.8
CC
9
Cov
0%
run
72.0
new
6.0
BEGIN
2.0
CRAP 26.7
1 package MYDan::Deploy;
2 use strict;
3 use warnings;
4  
5 use Carp;
6 use POSIX;
7 use YAML::XS;
8  
9 use Data::Dumper;
10  
11 use MYDan::Deploy::Conf;
12 use MYDan::Deploy::Code;
13 use MYDan::Deploy::Ctrl;
14 use MYDan::Deploy::Jobs;
15  
16 use MYDan::Deploy::Cache;
17  
18 use MYDan::Util::ProcLock;
19 use MYDan::Node;
20  
21 sub new
22 {
23 my ( $class, %self ) = splice @_;
24  
25 my $name = $self{name};
26  
27 my ( $conf, $maint, $code ) = MYDan::Deploy::Conf->new(
28 $name, +{ mould => $self{mould}, conf => $self{conf} }
29 )->dump( $self{macro} );
30  
31 bless +{
32 name => $name,
33 conf => $conf,
34 maint => $maint,
35  
36 code => MYDan::Deploy::Code->new( $self{code} => $code ),
37 lock => MYDan::Util::ProcLock->new( "$self{lock}/$name" ),
38 ctrl => MYDan::Deploy::Ctrl->new( $name => "$self{ctrl}" ),
39  
40 cache => "$self{cache}/$name",
41 global => [ map{ $_->{global} ? 1 : 0 }@$maint ],
42 }, ref $class || $class;
43 }
44  
45 sub run
46 {
47  
48 my $self = shift;
49 my ( $name, $lock, $conf, $code, $maint, $ctrl, $cache_path, $global, $cache )
50 = @$self{ qw( name lock conf code maint ctrl cache global ) };
51  
52 unless( $lock->lock() )
53 {
54 printf "$name is already running in pid: %s\n", $lock->check();
55 exit 1;
56 }
57  
58 $SIG{TERM} = $SIG{INT} = sub
59 {
60 print "deploy: killed.\n";
61 exit 1;
62 };
63 $SIG{PIPE} = sub
64 {
65 print "deploy: Broken pipe.\n";
66 exit 1;
67 };
68  
69 $SIG{QUIT} = sub
70 {
71 print "deploy: quit.\n";
72 exit 1;
73 };
74  
75 my @batch;
76 if( -e $cache_path )
77 {
78 $cache = MYDan::Deploy::Cache->load( $cache_path, $maint );
79  
80 @batch = @{$cache->{node}};
81
82 $ctrl->pause( 'error', 'init', 'cache', 'load cache ok' );
83 print "[WARN] load info from cache,you need to manually resume.\n";
84 }
85 else
86 {
87 my $batch = $conf->{batch};
88 @batch = $code->run(
89 $batch->{code}, param => $batch->{param}, cache => $cache
90 );
91 $cache->{node} = \@batch;
92
93 system sprintf "ln -fsn '%s' '%s'",
94 POSIX::strftime( "$name.%Y-%m-%d_%H:%M:%S", localtime ), $cache_path;
95 }
96  
97 $ctrl->pause( 'error', 'init', 'batch', 'node is null' ) unless @batch;
98  
99 sleep 3 while $ctrl->stuck( 'init' );
100  
101 my ( $i, $index, @job ) = ( 0, 0 );
102
103 for ( @$global )
104 {
105 push @{$job[$index]}, $i ++;
106 $index ++ if $_ == 1 || ( $global->[$i] && $global->[$i] == 1 );
107 }
108
109 my ( @node, @jobs, @index );
110 for my $job ( @job )
111 {
112 push @node, $global->[$job->[0]] == 1 ? \@batch : @batch;
113 my $len = $global->[$job->[0]] == 1 ? 1: scalar @batch;
114 map{ push @jobs, $job; push @index, $_; } 1 .. $len;
115 }
116  
117
118 $cache->{step} = [ map{ $_->{title} }@$maint ];
119 $cache->{glob} = [ map{ $_->{global}||0 }@$maint ];
120  
121 YAML::XS::DumpFile $cache_path, $cache;
122  
123  
124 my $range = MYDan::Node->new();
125 for my $id ( 0 .. @jobs -1 )
126 {
127 for my $j ( @{$jobs[$id]} )
128 {
129  
130 my ( $title, $step ) = ( $maint->[$j]{title}, $index[$id]);
131  
132 $cache->{todo} = [ $title, $step];
133 $cache->{time}{$title}{$step }
134 = POSIX::strftime( "%T", localtime );
135 YAML::XS::DumpFile $cache_path, $cache;
136  
137  
138 if( $cache->{done}{$title}{$step} )
139 {
140 print 'x' x 75, "\n";
141 print "skip|title:$title step:$step\n";
142 print 'x' x 75, "\n";
143 next;
144 }
145
146 my %succ =
147 MYDan::Deploy::Jobs->new(
148 name => $name,
149 step => $step,
150 conf => $maint->[$j],
151 ctrl => $ctrl,
152 code => $code->{$maint->[$j]->{code}},
153 cache => $cache,
154 )->run( @{$node[$id]} );
155  
156 map{ $cache->{succ}{$maint->[$j]{title}}{$_} = $succ{$_} }keys %succ;
157  
158 printf "succ[%d]:%s\n"
159 ,scalar $range->load( [ keys %succ ] )->list
160 , $range->load( [ keys %succ ] )->dump;
161  
162 $cache->{done}{$title}{$step} = 1;
163 $cache->{todo} = [];
164 YAML::XS::DumpFile $cache_path, $cache;
165 }
166 }
167  
168 unlink $cache_path;
169  
170 return 0;
171 }
172  
173 1;