blib/lib/MYDan/Project/Deploy/Ctrl.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 154 Conditioncond 0.0% 0 / 127 MC/DCmcdc 0.0% 0 / 75 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 570 CC 78 SCAR 53.2
CC
78
Cov
0%
stage
342.0
deploy
342.0
explain
210.0
CRAP 203.5
1 package MYDan::Project::Deploy::Ctrl;
2 use strict;
3 use warnings;
4 use Carp;
5 use YAML::XS;
6 use Data::UUID;
7 use File::Basename;
8 use Data::Dumper;
9  
10 sub new
11 {
12 my ( $class, %self ) = @_;
13  
14 confess "conf undef.\n" unless $self{conf} && ref $self{conf} eq 'HASH';
15 map{ die "$_ undef" unless $self{conf}->{$_} }qw( link path repo version );
16 # map{ $self{conf}{$_} .= $self{name} if $self{conf}{$_} =~ /\/$/ }qw( link path );
17  
18 my $tmp = $self{conf}{version};
19  
20 $self{conf}{repo} .= "/$tmp";
21
22 $self{packtype} = $tmp =~ s/\.tar\.gz$// ? 'tar.gz' : $tmp =~ s/\.tar$// ? 'tar' : 'raw';
23 $self{datatype} = $tmp =~ /\.patch$/ ? 'patch' : $tmp =~ /\.inc$/ ? 'inc' : 'full';
24  
25 $self{currpath} = "$self{conf}{path}/$self{conf}{version}";
26  
27 bless \%self, ref $class || $class;
28 }
29  
30 sub stage
31 {
32 my ( $this, %param ) = @_;
33 my ( $conf, $currpath ) = @$this{qw( conf currpath )};
34 my $repo = $conf->{repo};
35  
36 my ( $info, $succ ) = ( "$currpath/info", "$currpath/info/stage.succ" );
37  
38 return if -f "$currpath/pack" && -f $succ;
39  
40 $this->syscmd( 'stage' => "rm '$succ'" ) if -f $succ;
41 $this->syscmd( 'stage' => "mkdir -p '$info'" ) unless -d $info;
42  
43 my $limit = $param{mark} || $this->{limit};
44 $limit = $limit ? "--limit-rate=${limit}k" : '';
45  
46 my( $old_md5, $old_file_md5 );
47 my @try = ( split /\s+/, join " ",`cat '$currpath/pack.try'` ) if -f "$currpath/pack.try";
48 if( @try > 3 ) { map{ unlink "$currpath/pack$_" if -f "$currpath/pack$_" }( '', '.md5', '.try' ); }
49  
50 if( -f "$currpath/pack.md5" )
51 {
52 $old_md5 = eval{ YAML::XS::LoadFile "$currpath/pack.md5" };
53 unless( $old_md5 && $old_md5 =~ /^\w{32}$/ )
54 {
55 unlink "$currpath/pack.md5";
56 $old_md5 = undef;
57 }
58 }
59  
60 if( -f "$currpath/pack" )
61 {
62 my $tmp_file_md5 = `md5sum '$currpath/pack'`;
63 ( $old_file_md5 ) = split /\s+/, $tmp_file_md5 if $tmp_file_md5;
64 $old_file_md5 = undef unless $old_file_md5 && $old_md5 && $old_file_md5 eq $old_md5;
65 }
66  
67 system "echo 'download' >> '$currpath/pack.try'";
68 $this->syscmd( 'stage' => "wget -c -t 10 $limit -q -O '$currpath/pack.md5' '$repo.md5'" ) unless $old_md5;
69 $this->syscmd( 'stage' => "wget -c -t 10 $limit -q -O '$currpath/pack' '$repo'" ) unless $old_file_md5;
70  
71 my $m1 = eval{ YAML::XS::LoadFile "$currpath/pack.md5" };
72 die "md5 file error" unless $m1 && $m1 =~ /^\w{32}$/;
73 die "md5sum '$currpath/pack' fail" unless my $md5 = `md5sum '$currpath/pack'`;
74 my ( $m2 ) = split /\s+/, $md5;
75 die "md5 nomatch" unless $m1 && $m2 && $m1 eq $m2;
76  
77 $this->syscmd( 'stage' => "touch '$succ'" );
78 }
79  
80 sub explain
81 {
82 my ( $this, %param ) = @_;
83 my ( $conf, $currpath, $packtype, $datatype )= @$this{qw( conf currpath packtype datatype )};
84 my ( $repo, $path, $link, $version ) = @$conf{qw( repo path link version )};
85  
86 my @try = ( split /\s+/, join " ",`cat '$currpath/explain.try'` ) if -f "$currpath/explain.try";
87 if( @try > 3 ) { map{ unlink "$currpath/$_" if -f "$currpath/$_" }( 'pack', 'pack.md5', 'pack.try', 'explain.try', 'info/stage.succ' ); }
88  
89 $this->stage( %param, mark => undef )
90 unless -f "$currpath/pack" && -f "$currpath/info/stage.succ";
91  
92 my ( $succ, $uuid )= ( "$currpath/info/explain.succ", Data::UUID->new->create_str() );
93  
94 eval{ YAML::XS::DumpFile "$currpath/data/.deploy_uuid", $uuid }
95 if $datatype eq 'full' && -d "$currpath/data" && ! -f "$currpath/data/.deploy_uuid";
96  
97 die "[explain] dump uuid to '$currpath/data/.deploy_uuid' fail: $@\n" if $@;
98
99 return if -f $succ && -d "$currpath/data";
100  
101 $this->syscmd( 'explain' => "rm -f '$succ'") if -f $succ;
102  
103 $this->syscmd( 'explain' => "mkdir -p '$currpath/data'") unless -d "$currpath/data";
104 my $opt = ( $packtype eq 'tar.gz' || $packtype eq 'raw' ) ? 'z' : '';
105  
106 system "echo 'explain' >> '$currpath/explain.try'";
107  
108 $datatype eq 'patch' && $packtype eq 'raw'
109 ? $this->syscmd( 'explain' => "rsync '$currpath/pack' '$currpath/data/patch'")
110 : $this->syscmd( 'explain' => "tar -${opt}xvf '$currpath/pack' -C '$currpath/data'");
111  
112 eval{ YAML::XS::DumpFile "$currpath/data/.deploy_uuid", $uuid }
113 if $datatype eq 'full' && -d "$currpath/data" && !-f "$currpath/data/.deploy_uuid";
114 die "[explain] dump uuid to '$currpath/data/.deploy_uuid' fail: $@\n" if $@;
115  
116 $this->syscmd( 'explain' => "touch '$succ'");
117 }
118  
119 sub deploy
120 {
121 my ( $this, %param ) = @_;
122 my ( $conf, $currpath, $packtype, $datatype )= @$this{qw( conf currpath packtype datatype )};
123 my ( $repo, $path, $link, $version ) = @$conf{qw( repo path link version )};
124  
125 my $count = $param{mark} || $this->{keep};
126 $count ||= 5;
127  
128 $this->explain( %param, mark => undef ) if ! -f "$currpath/info/explain.succ"
129 || ! -e "$currpath/data"
130 || ( $datatype eq 'full' && ! -f "$currpath/data/.deploy_uuid" );
131  
132 $this->syscmd( 'deploy' => "touch '$currpath/info/$datatype'") unless -f "$currpath/info/$datatype";
133  
134 my $currlink = readlink $link;
135  
136 if( $datatype eq 'full' )
137 {
138 my $rollback = "$currpath/info/rollback";
139 my $rolllink = readlink $rollback;
140  
141 $this->syscmd( 'deploy' => "mv '$link' '$link.old.nices'" ) if -d $link && ! -l $link && ! -e "$link.old.nices";
142 $this->syscmd( 'deploy' => "ln -fsn '$link.old.nices' '$link'" ) if ! -l $link && -d "$link.old.nices" && ! -l "$link.old.nices";
143
144
145 $currlink = readlink $link unless $currlink;
146  
147 $this->syscmd( 'deploy' => "ln -fsn '$currlink' '$rollback'" )
148 if $currlink && (( $rolllink && $rolllink ne $currlink && $currlink ne "$currpath/data" ) || !$rolllink );
149 my $dirname = dirname $link;
150 $this->syscmd( 'deploy' => "mkdir -p '$dirname'" ) unless -e $dirname;
151 $this->syscmd( 'deploy' => "ln -fsn '$currpath/data' '$link'" )
152 unless $currlink && $currlink eq "$currpath/data";
153 }
154 else
155 {
156 die "The current full package does not take effect\n" unless $currlink && -d $currlink;
157
158 my $uuid = eval{ YAML::XS::LoadFile "$currlink/.deploy_uuid" } if -f "$currlink/.deploy_uuid";
159 die "[deploy] load full uuid fail: $@" if $@;
160 die "Did not find deployid\n" unless $uuid && $uuid =~ /^[\w-]+$/;
161  
162 my $privatepath = "$currpath/info/$uuid";
163 unless( -f "$privatepath/done" )
164 {
165 $this->syscmd( 'deploy' => "mkdir -p '$privatepath/backup'") unless -d "$privatepath/backup";
166  
167 if( $datatype eq 'patch' )
168 {
169 die "nofind file: $currpath/data/patch\n" unless -f "$currpath/data/patch";
170 $this->syscmd( 'deploy' => "patch -f -p1 --dry-run < '$currpath/data/patch'");
171 $this->syscmd( 'deploy' => "patch -f -p1 < '$currpath/data/patch'");
172 }
173 else
174 {
175 $this->syscmd( 'deploy' => "rsync -a -b --backup-dir '$privatepath/backup' '$currpath/data/' '$currlink/'");
176 }
177 $this->syscmd( 'deploy' => "touch '$privatepath/done'");
178 }
179 }
180  
181  
182 my %path = map{ $_ => ( stat $_ )[10] }glob "$path/*/info/$datatype";
183  
184 delete $path{"$currpath/info/$datatype"};
185 my @path = sort{ $path{$a} <=> $path{$b} }keys %path;
186 while( @path > $count )
187 {
188 my $p = shift @path;
189 $p =~ s#/info/$datatype$##;
190 $this->syscmd( 'deploy' => "rm -rf '$p'");
191 }
192  
193 };
194  
195 sub rollback
196 {
197 my ( $this, %param ) = @_;
198 my ( $conf, $currpath, $packtype, $datatype )= @$this{qw( conf currpath packtype datatype )};
199 my ( $repo, $path, $link, $version ) = @$conf{qw( repo path link version )};
200  
201 my $currlink = readlink $link;
202  
203 if( $datatype eq 'full' )
204 {
205 my ( $currlink, $rolllink )= map{ readlink $_ }( $link, "$currpath/info/rollback" );
206 die "no rollback data\n" unless $rolllink && -e $rolllink;
207  
208 $this->syscmd( 'deploy' => "ln -fsn '$rolllink' '$link'" )
209 unless $currlink && $currlink eq $rolllink;
210
211 $this->syscmd( 'deploy' => "rm -f '$link' && mv '$rolllink' '$link'" )
212 if $rolllink eq "$link.old.nices" && -d "$link.old.nices" && ! -l "$link.old.nices";
213 }
214 else
215 {
216 die "The current full package does not take effect\n" unless $currlink && -d $currlink;
217  
218 my $uuid = eval{ YAML::XS::LoadFile "$currlink/.deploy_uuid" } if -f "$currlink/.deploy_uuid";
219 die "[rollback] load uuid fail: $@\n" if $@;
220 die "Did not find deployid\n" unless $uuid && $uuid =~ /^[\w-]+$/;
221  
222 my $privatepath = "$currpath/info/$uuid";
223 die "no the backup to rollback\n" unless -f "$privatepath/done";
224 if( $datatype eq 'patch' )
225 {
226 die "nofind file: $currpath/data/patch\n" unless -f "$currpath/data/patch";
227 $this->syscmd( 'deploy' => "patch -f -R -p1 --dry-run < '$currpath/data/patch'");
228 $this->syscmd( 'deploy' => "patch -f -R -p1 < '$currpath/data/patch'");
229 }
230 else
231 {
232 $this->syscmd( 'deploy' => "rsync -a '$privatepath/backup/' '$path/data/'");
233 }
234 $this->syscmd( 'deploy' => "rm -rf '$privatepath'");
235  
236 }
237 }
238  
239 sub show
240 {
241 my ( $this, %param ) = @_;
242 my ( $conf, $packtype, $datatype )= @$this{qw( conf packtype datatype )};
243 my ( $repo, $path, $link, $version ) = @$conf{qw( repo path link version )};
244  
245  
246 my ( @pack, %data ) = glob "$path/*";
247  
248 if( $data{current} = readlink $link )
249 {
250 $data{rollback} = readlink "$data{current}/../info/rollback";
251 $data{rollback} = $data{rollback}
252 ? -e $data{rollback} ? $data{rollback} : "nvalid link":"no link";
253  
254 if( $datatype eq 'patch' || $datatype eq 'inc' )
255 {
256 my $uuid = eval{ YAML::XS::LoadFile "$data{currlink}/.deploy_uuid" } if -f "$data{currlink}/.deploy_uuid";
257 die "[show] load uuid fail: $@\n" if $@;
258 unless( $uuid && $uuid =~ /^[\w-]+$/ )
259 {
260 $data{error} = "Did not find uuid";
261 }
262 else
263 {
264 $data{$datatype} = -f "$path/info/$uuid/done"
265 ? "$datatype is in effect" : "$datatype is not used";
266 }
267 }
268 }
269 else { $data{error} = "Current link is empty"; }
270  
271 map{ print "$_: $data{$_}\n" if $data{$_}; }qw( current rollback patch inc error );
272 map{ printf "package: %s\n", basename $_; }@pack;
273 };
274  
275 our %ctrl =
276 (
277 stage => sub { shift->stage(@_); },
278 explain => sub { shift->explain(@_); },
279 deploy => sub { shift->deploy(@_); },
280 rollback => sub { shift->rollback(@_); },
281 show => sub { shift->show(@_); },
282 );
283  
284 sub do
285 {
286 my ( $this, @ctrl ) = @_;
287 my $conf = $this->{conf};
288  
289 print( YAML::XS::Dump $conf ) and return unless @ctrl;
290  
291 for my $ctrl ( @ctrl )
292 {
293 my $mark; ( $ctrl, $mark ) = ( $1, $2 ) if $ctrl =~ /^(.+):(\d+)$/;
294 die "no command $ctrl\n" unless $ctrl{$ctrl};
295 &{$ctrl{$ctrl}}( $this, mark => $mark );
296 }
297 return 0;
298 }
299  
300 sub syscmd
301 {
302 my ( $this, $ctrl ) = splice @_, 0, 2;
303 print( join( " ", "[$ctrl]:", @_ ), "\n" );# if $this->{verbose};
304 return system( @_ ) ? die "run $ctrl ERROR\n" : $this;
305 }
306  
307 1;
308 __END__