OpenInteract2/Manage/Website/Create.pm

filter: Statementstmt 68.0% 81 / 119 Branchbran 38.4% 10 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 7 totaltotal 63.5% 110 / 173 CC 20 SCAR 16.8
CC
20
Cov
63%
validate_param
13.8
_set_nowrite_files
12.0
_create_directories
6.6
CRAP 5.4
« OpenInteract2/Config/PackageChanges.pm ↑ Summary OpenInteract2/Controller/ManageTemplates.pm »
1 package OpenInteract2::Manage::Website::Create;
2  
3 # $Id: Create.pm,v 1.31 2005/03/17 14:58:03 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( OpenInteract2::Manage::Website );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use File::Spec::Functions qw( catdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use File::Path qw( rmtree );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Brick;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Config::Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use OpenInteract2::Manage qw( SYSTEM_PACKAGES );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 ▶use OpenInteract2::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 87 ▶use OpenInteract2::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 87 $OpenInteract2::Manage::Website::Create::VERSION = sprintf("%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/);
18  
19 # think about merging this with info from OI2::Manage::Website::SetDirectoryPermissions
20  
21 87 my @WEBSITE_SUBDIR = (
22 [ 0770, 'cache' ],
23 [ 0770, 'cache', 'tt' ],
24 [ 0770, 'cache', 'content' ],
25 [ 0770, 'cache', 'sessions' ],
26 [ 0770, 'cache', 'sessions_lock' ],
27 [ 0777, 'cgi-bin' ],
28 [ 0777, 'conf' ],
29 [ 0777, 'error' ],
30 [ 0777, 'html' ],
31 [ 0777, 'html', 'images' ],
32 [ 0777, 'logs' ],
33 [ 0777, 'mail' ],
34 [ 0777, 'msg' ],
35 [ 0777, 'overflow' ],
36 [ 0777, 'pkg' ],
37 [ 0777, 'template' ],
38 [ 0777, 'uploads' ],
39 );
40  
41 # METADATA
42  
43 87 ▶sub get_name {
Subroutine get_name: called CC=1 SCAR=0.0
Pod undocumented
44 87 return 'create_website';
45 }
46  
47 0 ▶sub get_brief_description {
Subroutine get_brief_description: not called CC=1 SCAR=6.9
Pod undocumented
48 0 return 'Create a new website from sample files shipped with OI2';
49 }
50  
51 2 ▶sub get_parameters {
Subroutine get_parameters: called CC=1 SCAR=0.0
Pod undocumented
52 2 my ( $self ) = @_;
53 return {
54 2 website_dir => {
55 description =>
56 "Directory where website will be created. It must " .
57 "not already exist or be empty when you run the task.",
58 is_required => 'yes',
59 do_validate => 'yes',
60 },
61 };
62 }
63  
64 # VALIDATE
65  
66 # Use a different check for 'website_dir' than our parent, since we
67 # want to ensure it DOES NOT exist
68  
69 1 ▶sub validate_param {
Subroutine validate_param: called CC=5 SCAR=26.2
Pod undocumented
70 1 my ( $self, $name, $value ) = @_;
71 1 ▶ if ( $name eq 'website_dir' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "website_dir") 1 0 1
72 1 ▶ return unless ( -d $value );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $value 1 0 1
73  
74 # Now do a quick check to see if any of our directories are
75 # already there -- if so we're trying to install over another
76 # site and will bail
77  
78 0 foreach my $dir_info ( @WEBSITE_SUBDIR ) {
79 0 my ( $mode, $dir ) = @{ $dir_info };
80 0 my $check_dir = catdir( $value, $dir );
81 0 ▶ if ( -d $check_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $check_dir) 0 0 0
82 0 return "Website directory '$value' already exists and " .
83 "contains directory '$dir'; cannot create website " .
84 "over an existing site";
85 }
86 }
87 0 return;
88  
89 }
90 0 return $self->SUPER::validate_param( $name, $value );
91 }
92  
93 # RUN
94  
95 # Define empty method so parent doesn't create context
96  
97 1 ▶sub setup_task {}
Subroutine setup_task: called CC=1 SCAR=0.0
Pod undocumented
98  
99  
100 # If anything bails in run_task, this cleans it up
101  
102 1 ▶sub tear_down_task {
Subroutine tear_down_task: called CC=5 SCAR=17.3
Pod undocumented
103 1 my ( $self ) = @_;
104 1 ▶ return unless ( $self->param( 'task_failed' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("task_failed") 0 1 1
105 1 my $website_dir = $self->param( 'website_dir' );
106 1 $self->notify_observers(
107 progress => 'Some part of the task failed, cleaning up directory' );
108 1 ▶ if ( -d $website_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $website_dir) 1 0 1
109 1 ▶ eval { rmtree( $website_dir ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
rmtree($website_dir) or die $! 1 0 1
110 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
111 0 oi_error "Cannot cleanup website directory [$website_dir]: $@";
112 }
113 }
114 }
115  
116  
117 1 ▶sub run_task {
Subroutine run_task: called CC=1 SCAR=1.4
Pod undocumented
118 1 my ( $self ) = @_;
119 1 my $website_dir = $self->param( 'website_dir' );
120  
121 1 $self->_create_directories( $website_dir );
122 1 $self->notify_observers( progress => 'Directories created' );
123  
124 1 $self->_copy_from_bricks( $website_dir,
125 'apache', 'apache2', 'cgi', 'daemon',
126 'messages', 'website_config', 'widgets' );
127 1 $self->notify_observers(
128 progress => 'All files from sample website copied' );
129  
130 # This will initialize the context to our new website...
131 1 $self->_setup_context({ skip => 'read repository' });
132  
133 0 my $repository = OpenInteract2::Repository->new();
134 0 $repository->website_dir( $website_dir );
135 0 CTX->repository( $repository );
136  
137 0 $self->notify_observers( progress => 'Installing packages',
138 { long => 'yes' } );
139 0 $self->_install_packages_from_bricks( $website_dir, SYSTEM_PACKAGES );
140 0 $self->notify_observers( progress => 'Packages installed' );
141  
142 0 $self->_set_nowrite_files( $website_dir );
143 0 $self->_add_status_head({
144 action => 'create website',
145 is_ok => 'yes',
146 message => 'All directories and files created, all ' .
147 'packages installed',
148 });
149 }
150  
151  
152 # Create the main website directory and subdirectories
153  
154 1 ▶sub _create_directories {
Subroutine _create_directories: called CC=6 SCAR=18.8
155 1 my ( $self, $website_dir ) = @_;
156 1 ▶ unless ( -d $website_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $website_dir) 1 0 1
157 1 ▶ mkdir( $website_dir, 0775 )
Branch: 1 branch
BranchTrueFalseTotal
mkdir $website_dir, 509 or oi_error("Cannot create website directory '${website_dir}': $!") 1 0 1
158 || oi_error "Cannot create website directory '$website_dir': $!";
159 }
160  
161 1 foreach my $sub_dir_info ( @WEBSITE_SUBDIR ) {
162 17 my ( $perm, @subdirs ) = @{ $sub_dir_info };
163 17 my $full_subdir = catdir( $website_dir, @subdirs );
164 17 ▶ eval { mkdir( $full_subdir, $perm ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
mkdir $full_subdir, $perm or die $! 17 0 17
165 17 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
166 0 oi_error "Cannot create subdirectory in '$full_subdir': $@";
167 }
168 $self->_add_status({
169 17 is_ok => 'yes',
170 action => 'create subdirectory',
171 filename => $full_subdir,
172 message => 'Directory created',
173 });
174 }
175 }
176  
177 1 ▶sub _copy_from_bricks {
Subroutine _copy_from_bricks: called CC=5 SCAR=16.2
178 1 my ( $self, $website_dir, @brick_names ) = @_;
179 1 my %vars = ( website_dir => $website_dir );
180 1 foreach my $brick_name ( @brick_names ) {
181 7 my $brick = OpenInteract2::Brick->new( $brick_name );
182 7 my $status = $brick->copy_all_resources_to( $website_dir, \%vars );
183 7 foreach my $file_copied ( @{ $status->{copied} } ) {
184 74 $self->_add_status({
185 is_ok => 'yes',
186 action => "copy '$brick_name'",
187 message => 'Copied file from class resource',
188 filename => $file_copied,
189 });
190 }
191 7 foreach my $file_skipped ( @{ $status->{skipped} } ) {
192 0 $self->_add_status({
193 is_ok => 'yes',
194 action => "copy '$brick_name'",
195 message => 'Skipped copying file from class resource - marked as readonly',
196 filename => $file_skipped,
197 });
198 }
199 7 foreach my $file_same ( @{ $status->{same} } ) {
200 0 $self->_add_status({
201 is_ok => 'yes',
202 action => "copy '$brick_name'",
203 message => 'Skippe copying file from class resource - resources same',
204 filename => $file_same,
205 });
206 }
207 }
208 }
209  
210 # Create nowrite flags for HTML and widget dirs
211  
212 0 ▶sub _set_nowrite_files {
Subroutine _set_nowrite_files: not called CC=3 SCAR=24.8
213 0 my ( $self, $website_dir ) = @_;
214 0 my $action = 'set nowrite file';
215 0 my $html_dir = catdir( $website_dir, 'html' );
216 0 my $html_message =
217 'If a file is listed here it will not be updated when the ' .
218 'package to which it belongs is updated.';
219 0 eval {
220 0 OpenInteract2::Config::Readonly
221 ->new( $html_dir )
222 ->write_readonly_files( [ 'index.html', 'main.css' ], $html_message );
223 };
224 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
225 0 warn
226 $self->_add_status({
227 is_ok => 'no',
228 action => $action,
229 filename => $html_dir,
230 message => "$@",
231 });
232 }
233 else {
234 0 $self->_add_status({
235 is_ok => 'yes',
236 action => $action,
237 filename => $html_dir,
238 message => 'Read-only file created ok',
239 });
240 }
241  
242 0 my $tmpl_dir = catdir( $website_dir, 'template' );
243 0 my $tmpl_status = { action => $action,
244 filename => $tmpl_dir };
245 0 my $tmpl_message =
246 'These are templates that will not be overwritten when you ' .
247 'refresh the widgets or upgrade your site (one template per line)';
248 0 eval {
249 0 OpenInteract2::Config::Readonly
250 ->new( $tmpl_dir )
251 ->write_readonly_files( [ 'base_main', 'base_simple' ], $tmpl_message );
252 };
253 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
254 0 $self->_add_status({
255 is_ok => 'no',
256 action => $action,
257 filename => $tmpl_dir,
258 message => "$@",
259 });
260 }
261 else {
262 0 $self->_add_status({
263 is_ok => 'yes',
264 action => $action,
265 filename => $tmpl_dir,
266 message => 'Read-only file created ok',
267 });
268 }
269 }
270  
271 87 OpenInteract2::Manage->register_factory_type( get_name() => __PACKAGE__ );
272  
273 87 1;
274  
275 __END__
« OpenInteract2/Config/PackageChanges.pm ↑ Summary OpenInteract2/Controller/ManageTemplates.pm »