SiteConfig/Deploy.pm

filter: Statementstmt 24.0% 39 / 162 Branchbran 1.8% 1 / 54 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 35.4% 11 / 31 Podpod 0.0% 0 / 17 totaltotal 17.2% 51 / 295 CC 37 SCAR 43.3
CC
37
Cov
17%
deploy
272.0
preprocess_meta
42.0
update
42.0
CRAP 76.0
↑ Summary SiteConfig/Template.pm »
1 package Apache::SiteConfig::Deploy;
2 1 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(mkpath rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Apache::SiteConfig::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # TODO: support template variable for paths and meta data.
11  
12 1 our $Single;
13  
14 1 ▶END {
Subroutine END: called CC=2 SCAR=7.6
15 1 ▶ $Single->execute_task( @ARGV ) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 1 1
16 }
17  
18 1 ▶sub import {
Subroutine import: called CC=2 SCAR=7.3
19 1 my ($class) = @_;
20 1 $Single = $class->new;
21 1 $Single->{args} = {};
22 1 $Single->{tasks} = {};
23  
24 # built-in tasks
25 0 ▶ $Single->{tasks}->{deploy} = sub { $Single->deploy( @_ ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶ $Single->{tasks}->{update} = sub { $Single->update( @_ ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 0 ▶ $Single->{tasks}->{clean} = sub { $Single->clean( @_ ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
28  
29 # setup accessors to main::
30 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 for my $key ( qw(su chown name domain domain_alias webroot source deploy task) ) {
32 9 ▶ *{ 'main::' . $key } = sub {
Subroutine __ANON__: not called CC=1 SCAR=2.6
33 0 ${ $class .'::'}{ $key }->( $Single , @_ );
34 9 };
35 }
36  
37 # Exporter->import( @_ );
38 1 return 1;
39 }
40  
41 1 ▶sub new { bless {} , shift; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
42  
43 0 ▶sub execute_task {
Subroutine execute_task: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my ($self,$task_name,@args) = @_;
45 0 my $task = $self->{tasks}->{ $task_name };
46 0 ▶ if ( $task ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task) { } 0 0 0
47 0 $task->( $self , @args );
48 } else {
49 0 print "Task $task_name not found.\n";
50 }
51 }
52  
53 0 ▶sub execute_command {
Subroutine execute_command: not called CC=4 SCAR=30.0
Pod undocumented
54 0 my ($self,$cmd, $abort_on_failure) = @_;
55  
56 0 ▶ if( $self->{args}->{su} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"args"}{"su"}) 0 0 0
57 0 $cmd = sprintf( 'sudo -u %s %s', $self->{args}->{su} , $cmd );
58 }
59  
60 0 say $cmd;
61 0 ▶ if( $abort_on_failure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abort_on_failure) { } 0 0 0
62 0 ▶ system( $cmd ) == 0 or die $!;
Branch: 1 branch
BranchTrueFalseTotal
system($cmd) == 0 or die $! 0 0 0
63 } else {
64 0 system( $cmd );
65 }
66 }
67  
68 0 ▶sub chown {
Subroutine chown: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my $self = shift;
70 0 $self->{args}->{chown} = $_[0];
71 }
72  
73 0 ▶sub su {
Subroutine su: not called CC=1 SCAR=6.9
Pod undocumented
74 0 my $self = shift;
75 0 $self->{args}->{su} = $_[0];
76 }
77  
78 0 ▶sub name ($) {
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $self = shift;
80 0 $self->{args}->{name} = $_[0];
81 }
82  
83 0 ▶sub domain {
Subroutine domain: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my $self = shift;
85 0 $self->{args}->{domain} = $_[0];
86 }
87  
88 0 ▶sub domain_alias {
Subroutine domain_alias: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $self = shift;
90 0 $self->{args}->{domain_alias} = $_[0];
91 }
92  
93 0 ▶sub source {
Subroutine source: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ($self,$type,$uri) = @_;
95 0 ▶ $self->{args}->{source} ||= {};
Condition0%0/2
$self->{"args"}{"source"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"args"}{"source"} ||= {}
$self->{"args"}{"source"}
Truth table0%0/2
A
A= $self->{"args"}{"source"}
Aresult
11
00
96 0 $self->{args}->{source}->{ $type } = $uri;
97 }
98  
99 0 ▶sub webroot {
Subroutine webroot: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my ($self,$path) = @_;
101 0 $self->{args}->{webroot} = $path;
102 }
103  
104 0 ▶sub task ($&) {
Subroutine task: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self,$name,$closure) = @_;
106 0 $self->{tasks}->{ $name } = $closure;
107 }
108  
109 0 ▶sub preprocess_meta {
Subroutine preprocess_meta: not called CC=6 SCAR=37.4
Pod undocumented
110 0 my $self = shift;
111 0 my $args = { %{ $self->{args} } }; # copy args
112 0 ▶ $args->{sites_dir} ||= File::Spec->join( '/var/sites' );
Condition0%0/3
$args->{"sites_dir"} ||= "File::Spec"->join("/var/sites")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"sites_dir"} ||= "File::Spec"->join("/var/sites")
$args->{"sites_dir"} "File::Spec"->join("/var/sites")
Truth table0%0/3
A ||= B
A= $args->{"sites_dir"}B= "File::Spec"->join("/var/sites")
ABresult
1X1
011
000
113 0 ▶ $args->{site_dir} ||= File::Spec->join( $args->{sites_dir} , $args->{name} );
Condition0%0/3
$args->{"site_dir"} ||= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"site_dir"} ||= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"})
$args->{"site_dir"} "File::Spec"->join($args->{"sites_dir"}, $args->{"name"})
Truth table0%0/3
A ||= B
A= $args->{"site_dir"}B= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"})
ABresult
1X1
011
000
114 $args->{document_root} = File::Spec->join(
115 0 $args->{site_dir} , $args->{webroot} );
116  
117 $args->{log_dir} ||=
118 0 ▶ File::Spec->join( $args->{sites_dir} , $args->{name} , 'apache2' , 'logs' );
Condition0%0/3
$args->{"log_dir"} ||= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"}, "apache2", "logs")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"log_dir"} ||= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"}, "apache2", "logs")
$args->{"log_dir"} "File::Spec"->join($args->{"sites_dir"}, $args->{"name"}, "apache2", "logs")
Truth table0%0/3
A ||= B
A= $args->{"log_dir"}B= "File::Spec"->join($args->{"sites_dir"}, $args->{"name"}, "apache2", "logs")
ABresult
1X1
011
000
119 # File::Spec->join( '/var/log/sites/' , $args->{name} , 'apache2' , 'logs' )
120  
121 0 ▶ $args->{access_log} ||= File::Spec->join( $args->{log_dir} , 'access.log' );
Condition0%0/3
$args->{"access_log"} ||= "File::Spec"->join($args->{"log_dir"}, "access.log")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"access_log"} ||= "File::Spec"->join($args->{"log_dir"}, "access.log")
$args->{"access_log"} "File::Spec"->join($args->{"log_dir"}, "access.log")
Truth table0%0/3
A ||= B
A= $args->{"access_log"}B= "File::Spec"->join($args->{"log_dir"}, "access.log")
ABresult
1X1
011
000
122 0 ▶ $args->{error_log} ||= File::Spec->join( $args->{log_dir} , 'error.log' );
Condition0%0/3
$args->{"error_log"} ||= "File::Spec"->join($args->{"log_dir"}, "error.log")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"error_log"} ||= "File::Spec"->join($args->{"log_dir"}, "error.log")
$args->{"error_log"} "File::Spec"->join($args->{"log_dir"}, "error.log")
Truth table0%0/3
A ||= B
A= $args->{"error_log"}B= "File::Spec"->join($args->{"log_dir"}, "error.log")
ABresult
1X1
011
000
123 0 return $args;
124 }
125  
126 0 ▶sub prepare_paths {
Subroutine prepare_paths: not called CC=4 SCAR=30.0
Pod undocumented
127 0 my ($self,$args) = @_;
128 0 for my $path ( qw(sites_dir site_dir document_root) ) {
129 0 ▶ next unless $args->{ $path };
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$path} 0 0 0
130 0 ▶ mkpath [ $args->{ $path } ] unless -e $args->{ $path };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $args->{$path} 0 0 0
131 }
132 }
133  
134 0 ▶sub prepare_log_path {
Subroutine prepare_log_path: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my ($self,$args) = @_;
136 0 ▶ mkpath [ $args->{log_dir} ] unless -e $args->{log_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $args->{"log_dir"} 0 0 0
137  
138 }
139  
140 0 ▶sub clean {
Subroutine clean: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $self = shift;
142 0 my $args = $self->preprocess_meta;
143 0 say "Cleanning up $args->{site_dir}";
144 0 rmtree( $args->{site_dir} );
145 }
146  
147 0 ▶sub update {
Subroutine update: not called CC=6 SCAR=37.4
Pod undocumented
148 0 my $self = shift;
149 0 my $args = $self->preprocess_meta;
150  
151 0 ▶ if( $args->{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"source"}) 0 0 0
152 0 chdir $args->{site_dir};
153  
154 0 ▶ if( $args->{source}->{git} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'source'}{'git'}) { } 0 0 0
elsif ($args->{'source'}{'hg'}) { } 0 0 0
155 0 ▶ my $branch = $args->{source}->{branch} || 'master';
Condition0%0/2
$args->{'source'}{'branch'} || 'master'
l!l
00
MC/DC0%0/1
$args->{'source'}{'branch'} || 'master'
$args->{'source'}{'branch'}
Truth table0%0/2
A
A= $args->{'source'}{'branch'}
Aresult
11
00
156 0 ▶ $self->execute_command("git pull origin $branch") if $branch eq 'master';
Branch: 1 branch
BranchTrueFalseTotal
if $branch eq "master" 0 0 0
157 }
158 elsif ( $args->{source}->{hg} ) {
159 0 $self->execute_command("hg pull -u");
160 }
161 }
162  
163 }
164  
165 0 ▶sub deploy {
Subroutine deploy: not called CC=16 SCAR=56.1
Pod undocumented
166 0 my ($self) = @_;
167 0 my $args = $self->preprocess_meta;
168 0 $self->prepare_paths( $args );
169  
170 SKIP_SOURCE_CLONE:
171 0 ▶ if( $args->{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"source"}) 0 0 0
172  
173 0 ▶ if( $args->{source}->{git} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'source'}{'git'}) { } 0 0 0
elsif ($args->{'source'}{'hg'}) { } 0 0 0
174 0 ▶ last SKIP_SOURCE_CLONE if -e File::Spec->join( $args->{site_dir} , '.git' );
Branch: 1 branch
BranchTrueFalseTotal
if -e "File::Spec"->join($args->{"site_dir"}, ".git") 0 0 0
175 0 say "Cloning git repository from $args->{source}->{git} to $args->{site_dir}";
176  
177 0 $self->execute_command("git clone $args->{source}->{git} $args->{site_dir}",1);
178  
179 # if branch is specified, then check the branch out.
180 0 my $branch = $args->{source}->{branch};
181 0 ▶ $self->execute_command("git checkout -t origin/$branch",1) if $branch;
Branch: 1 branch
BranchTrueFalseTotal
if $branch 0 0 0
182  
183 # if tag is specified, then check the tag out.
184 0 my $tag = $args->{source}->{tag};
185 0 ▶ $self->execute_command("git checkout $tag -b $tag",1) if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
186  
187 }
188 elsif( $args->{source}->{hg} ) {
189 0 ▶ last SKIP_SOURCE_CLONE if -e File::Spec->join( $args->{site_dir} , '.git' );
Branch: 1 branch
BranchTrueFalseTotal
if -e "File::Spec"->join($args->{"site_dir"}, ".git") 0 0 0
190  
191 0 say "Cloning hg repository from $args->{source}->{hg} to $args->{site_dir}";
192 0 ▶ $self->execute_command("hg clone $args->{source}->{hg} $args->{site_dir}") == 0 or die($?);
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_command(qq[hg clone $args->{"source"}{"hg"} $args->{"site_dir"}]) == 0 or die $? 0 0 0
193 }
194  
195 }
196  
197 0 $self->prepare_log_path( $args );
198  
199 0 ▶ if( $args->{chown} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"chown"}) 0 0 0
200 0 say "Changing owner to $args->{site_dir}";
201 0 $self->execute_command( sprintf( 'chown -R %s: ' , $args->{site_dir} ) ,1);
202 }
203
204  
205  
206 # Default template
207 0 my $template = Apache::SiteConfig::Template->new; # apache site config template
208 my $context = $template->build(
209 ServerName => $args->{domain},
210 ServerAlias => $args->{domain_alias},
211 DocumentRoot => $args->{document_root},
212 CustomLog => $args->{access_log} ,
213 ErrorLog => $args->{error_log}
214 0 );
215 0 my $config_content = $context->to_string;
216  
217 # get site config directory
218 0 my $apache_dir_debian = '/etc/apache2/sites-available';
219 0 ▶ if( -e $apache_dir_debian ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $apache_dir_debian) { } 0 0 0
220 0 say "Apache Site Config Dir Found.";
221  
222 0 my $config_file = File::Spec->join( $apache_dir_debian , $args->{name} );
223  
224 0 ▶ if ( -e $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_file) { } 0 0 0
225 0 say "$config_file exists, skipped.";
226 } else {
227 0 say "Writing site config to $config_file.";
228 0 open my $fh , ">", $config_file;
229 0 print $fh $config_content;
230 0 close $fh;
231  
232 0 say "Enabling $args->{name}";
233 0 system("a2ensite $args->{name}");
234  
235 0 say "Reloading apache";
236 0 system("/etc/init.d/apache2 reload");
237 }
238 }
239 else {
240  
241 # try to find where apachectl is located.
242 0 my $apachectl_bin = qx(which apachectl);
243 0 chomp( $apachectl_bin );
244  
245 0 my $apache_dir = dirname(dirname( $apachectl_bin ));
246 0 my $apache_conf_dir = File::Spec->join( $apache_dir , 'conf' );
247  
248 0 ▶ if( -e $apache_conf_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $apache_conf_dir) { } 0 0 0
249 0 say "Found apache configuration directory: $apache_conf_dir";
250 # prepare site config dir
251 0 mkpath [ File::Spec->join( $apache_conf_dir , 'sites' ) ];
252  
253 0 my $httpd_conf = File::Spec->join( $apache_conf_dir , 'httpd.conf' );
254  
255 # write site config to apache conf dir
256 0 my $config_file = File::Spec->join( $apache_conf_dir , 'sites' , $args->{name} . '.conf' );
257 0 say "Writing config file: $config_file";
258 0 ▶ open my $fh , ">" , $config_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $config_file or die $! 0 0 0
259 0 print $fh $config_content;
260 0 close $fh;
261  
262 0 say "Appending Include statement to $httpd_conf";
263 0 ▶ open my $fh2 , ">>" , $httpd_conf or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, ">>", $httpd_conf or die $! 0 0 0
264 0 print $fh2 "###### Apache Site Configurations \n";
265 0 print $fh2 "Include conf/sites/$args->{name}.conf\n";
266 0 close $fh2;
267  
268 } else {
269 # apachectl not found
270 0 mkpath [ 'apache2' ];
271  
272 # TODO: run apache configtest here
273 # /opt/local/apache2/bin/apachectl -t -f /path/to/config file
274 0 my $config_file = File::Spec->join( 'apache2' , 'sites' , $args->{name} ); # apache config
275 0 mkpath [ File::Spec->join('apache2','sites') ];
276  
277 0 say "Writing site config file: $config_file";
278 0 ▶ open my $fh , ">", $config_file or die "Can not write config file $config_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $config_file or die "Can not write config file ${config_file}: $!" 0 0 0
279 0 print $fh $config_content;
280 0 close $fh;
281 }
282  
283 }
284  
285  
286 }
287  
288  
289 1 1;
290 __END__
↑ Summary SiteConfig/Template.pm »