lib/CallBackery/Config.pm

filter: Statementstmt 54.0% 152 / 281 Branchbran 31.4% 17 / 54 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 67.6% 23 / 34 Podpod 66.6% 8 / 12 totaltotal 50.2% 202 / 402 CC 52 SCAR 29.8
CC
52
Cov
49%
restoreConfigBlob
90.0
unConfigure
55.6
getConfigBlob
30.0
CRAP 19.8
« lib/CallBackery/GuiPlugin/AbstractForm.pm ↑ Summary lib/CallBackery/Plugin/Doc.pm »
1 # $Id: Config.pm 539 2013-12-09 22:28:11Z oetiker $
2 package CallBackery::Config;
3  
4 =head1 NAME
5  
6 CallBackery::Config - get parse configuration file for CallBackery
7  
8 =head1 SYNOPSIS
9  
10 use Nq::Config;
11 my $cfg = CallBackery::Config->new(file=>$file);
12 my $hash_ref = $cfg->cfgHash();
13 my $pod = $cfg->pod();
14  
15 =head1 DESCRIPTION
16  
17 CallBackery gets much of its configuration from this config file.
18  
19 =cut
20  
21 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use CallBackery::Exception qw(mkerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use CallBackery::Translate qw(trm);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Config::Grammar::Dynamic;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Locale::PO;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head2 file
31  
32 the name of the config file
33  
34 =cut
35  
36 0 ▶has file => sub { croak "the file parameter is mandatory" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37  
38 1 ▶has secretFile => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 shift->file.'.secret';
40 1 };
41  
42 0 ▶has app => sub { croak "the app parameter is mandatory" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 0 ▶has log => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 shift->app->log;
46 1 };
47  
48 =head2 cfgHash
49  
50 a hash containing the data from the config file
51  
52 =cut
53  
54 1 ▶has cfgHash => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
55 1 my $self = shift;
56 1 my $cfg_file = shift;
57 1 my $parser = $self->makeParser();
58 1 ▶ my $cfg = $parser->parse($self->file) or croak($parser->{err});
Branch: 1 branch
BranchTrueFalseTotal
my $cfg = $parser->parse($self->file) or croak($parser->{"err"}) 1 0 1
59 1 $self->postProcessCfg($cfg);
60 1 return $cfg;
61 1 };
62  
63 =head2 pod
64  
65 returns a pod documenting the config file
66  
67 =cut
68  
69 0 ▶has pod => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 my $self = shift;
71 0 my $parser = $self->makeParser();
72 0 my $E = '=';
73 0 my $footer = <<"FOOTER";
74  
75 ${E}head1 COPYRIGHT
76  
77 Copyright (c) 2014 by OETIKER+PARTNER AG. All rights reserved.
78  
79 ${E}head1 AUTHOR
80  
81 S<Tobias Oetiker E<lt>tobi\@oetiker.chE<gt>>
82 S<Fritz Zaucker E<lt>fritz.zaucker\@oetiker.chE<gt>>
83  
84 ${E}head1 HISTORY
85  
86 2014-01-11 to 1.0 first version
87 2014-04-29 fz 1.1 implement plugin path
88  
89 FOOTER
90 0 my $header = <<"HEADER";
91 ${E}head1 NAME
92  
93 callbackery.cfg - The Apliance FRONTEND Builder config file
94  
95 ${E}head1 SYNOPSIS
96  
97 *** BACKEND ***
98 log_file = /tmp/nw-tobi.log
99  
100 *** FRONTEND ***
101 logo = logo.png
102 spinner = myspinner.gif
103 logo_small = logo-small.png
104 title = Appliance Configurator
105  
106 ${E}head1 DESCRIPTION
107  
108 The afb.cfg provides all the info for afb and its gui modules to interact with your appliance.
109  
110 ${E}head1 CONFIGURATION
111  
112 HEADER
113 0 return $header.$parser->makepod().$footer;
114 1 };
115  
116 =head2 pluginPath
117  
118 array of name spaces to look for gui plugins
119  
120 =cut
121  
122 1 ▶has pluginPath => sub { ['CallBackery::GuiPlugin']; };
Subroutine __ANON__: called CC=1 SCAR=0.0
123  
124 =head2 B<loadAndNewPlugin>('PluginModule')
125  
126 Find the given module in the F<pluginPath>, load it and create a first instance.
127  
128 =cut
129  
130  
131 4 ▶sub loadAndNewPlugin {
Subroutine loadAndNewPlugin: called CC=5 SCAR=16.1
Pod documented
132 4 my $self = shift;
133 4 my $plugin = shift;
134  
135 4 my $pluginPath = $self->pluginPath;
136 4 for my $path (@INC){
137 8 for my $pPath (@$pluginPath) {
138 8 my @pDirs = split /::/, $pPath;
139 8 my $fPath = File::Spec->catdir($path, @pDirs, '*.pm');
140 8 for my $file (glob($fPath)) {
141 21 my ($volume, $modulePath, $moduleName) = File::Spec->splitpath($file);
142 21 $moduleName =~ s{\.pm$}{};
143 21 ▶ if ($plugin eq $moduleName) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin eq $moduleName) 4 17 21
144 4 require $file;
145 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
146 4 return "${pPath}::${plugin}"->new();
147 }
148 }
149 }
150 }
151 0 die mkerror(123, "Plugin Module $plugin not found");
152 };
153  
154 1 ▶has grammar => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
155 1 my $self = shift;
156 1 my $pluginList = {};
157 1 my $pluginPath = $self->pluginPath;
158 1 for my $path (@INC){
159 10 for my $pPath (@$pluginPath) {
160 10 my @pDirs = split /::/, $pPath;
161 10 my $fPath = File::Spec->catdir($path, @pDirs, '*.pm');
162 10 for my $file (glob($fPath)) {
163 18 my ($volume, $modulePath, $moduleName) = File::Spec->splitpath($file);
164 18 $moduleName =~ s{\.pm$}{};
165 18 $pluginList->{$moduleName} = 'Plugin Module';
166 }
167 }
168 }
169 return {
170 _sections => [ qw(BACKEND FRONTEND FRONTEND-COLORS /PLUGIN:\s*\S+/)],
171 _mandatory => [qw(BACKEND FRONTEND)],
172 BACKEND => {
173 _doc => 'BACKEND Settings',
174 _vars => [ qw(log_file cfg_db sesame_user sesame_pass) ],
175 _mandatory => [ qw(cfg_db sesame_user sesame_user) ],
176 log_file => { _doc => 'write a log file to this location (unless in development mode)'},
177 cfg_db => { _doc => 'file to store the config database'},
178 sesame_user => { _doc => <<'DOC'},
179 In Open Sesame mode, one has to use this username to get access to the system.
180 The password you enter does not matter.
181 DOC
182 sesame_pass => { _doc => <<'DOC'},
183 Using sesame_user and sesame_pass, the system can always be accessed.
184 In default configuration sesame_pass is NOT set.
185 DOC
186 },
187 FRONTEND => {
188 _doc => 'Settings for the Web FRONTEND',
189 _vars => [ qw(logo logo_small spinner title initial_plugin company_name company_url company_support
190 hide_password hide_release hide_company max_width
191 )
192 ],
193 logo => {
194 _doc => 'url for the logo brand the login sceen',
195 },
196 company_name => {
197 _doc => 'who created the app',
198 },
199 company_url => {
200 _doc => 'link to the company homepage'
201 },
202 max_width => {
203 _doc => 'maximum content width'
204 },
205 company_support => {
206 _doc => 'company support eMail'
207 },
208 logo_small => {
209 _doc => 'url for the small logo brand the UI',
210 },
211 spinner => {
212 _doc => 'url for the busy animation spinner gif',
213 },
214 title => {
215 _doc => 'title string for the application'
216 },
217 initial_plugin => {
218 _doc => 'which tab should be active upon login ?'
219 },
220 hide_password => {
221 _doc => 'hide password field on login screen',
222 _re => '(yes|no)',
223 _re_error => 'pick yes or no',
224 },
225 hide_release => {
226 _doc => 'hide release string on login screen',
227 _re => '(yes|no)',
228 _re_error => 'pick yes or no',
229 },
230 hide_company => {
231 _doc => 'hide company string on login screen',
232 _re => '(yes|no)',
233 _re_error => 'pick yes or no',
234 },
235 },
236 'FRONTEND-COLORS' => {
237 _vars => [ '/[a-zA-Z]\S+/' ],
238 '/[a-zA-Z]\S+/' => {
239 _doc => <<COLORKEYS_END,
240 Use this section to override any color key used in the qooxdoo simple theme as well as the following:
241 C<tabview-page-background>,
242 C<tabview-page-border>,
243 C<tabview-button-background>,
244 C<tabview-button-checked-background>,
245 C<tabview-button-text>,
246 C<tabview-button-checked-text>,
247 C<tabview-button-border>,
248 C<tabview-button-checked-border>.
249 C<textfield-readonly>.
250  
251 The keys can be set to standard web colors C<rrggbb> or to other key names.
252 COLORKEYS_END
253 _example => <<EXAMPLE_END,
254 ff0000
255 EXAMPLE_END
256 0 ▶ _sub => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
257 0 ▶ if ($_[0] =~ /^\s*([0-9a-f]{3,6})\s*$/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*([0-9a-f]{3,6})\s*$/iu) 0 0 0
258 0 $_[0] = '#'.lc($1);
259 }
260 0 return undef;
261 }
262 }
263 },
264 '/PLUGIN:\s*\S+/' => {
265 _order => 1,
266 _doc => 'Plugins providing appliance specific funtionality',
267 _vars => [qw(module)],
268 _mandatory => [qw(module)],
269 module => {
270 2 ▶ _sub => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
271 2 eval {
272 2 $_[0] = $self->loadAndNewPlugin($_[0]);
273 };
274 2 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
275 0 return "Failed to load Plugin $_[0]: $@";
276 }
277 2 return undef;
278 },
279 2 ▶ _dyn => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
280 2 my $var = shift;
281 2 my $module = shift;
282 2 ▶ $module = $self->loadAndNewPlugin($module) if not ref $module;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $module 0 2 2
283 2 my $tree = shift;
284 2 my $grammar = $module->grammar();
285 2 push @{$grammar->{_vars}}, 'module';
286 2 for my $key (keys %$grammar){
287 10 $tree->{$key} = $grammar->{$key};
288 }
289 },
290 1 _dyndoc => $pluginList,
291 },
292 }
293 };
294 1 };
295  
296 1 ▶sub makeParser {
Subroutine makeParser: called CC=1 SCAR=0.0
Pod undocumented
297 1 my $self = shift;
298 1 my $parser = Config::Grammar::Dynamic->new($self->grammar);
299 1 return $parser;
300 }
301  
302 =head2 getTranslations
303  
304 Load translations from po files
305  
306 =cut
307  
308 1 ▶sub getTranslations {
Subroutine getTranslations: called CC=4 SCAR=22.1
Pod documented
309 1 my $self = shift;
310 1 my $cfg = shift;
311 1 my %lx;
312 1 my $path = $self->app->home->rel_file("share");
313 1 my $po = new Locale::PO();
314 1 for my $file (glob(File::Spec->catdir($path, '*.po'))) {
315 0 my ($volume, $localePath, $localeName) = File::Spec->splitpath($file);
316 0 my $locale = $localeName;
317 0 $locale =~ s/\.po$//;
318 0 my $lang = $locale;
319 0 $lang =~ s/_.+//;
320 0 local $_; # since load_file_ashash modifies $_ and does not localize it
321 0 my $href = Locale::PO->load_file_ashash($file, 'utf8');
322 0 for my $key (keys %$href) {
323 0 my $o = $href->{$key};
324 0 my $id = $po->dequote($o->msgid);
325 0 my $str = $po->dequote($o->msgstr);
326 0 ▶ next unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
327 0 $lx{$locale}{$id} = $str;
328 }
329 }
330 # use Data::Dumper;
331 # warn Dumper "lx=", \%lx;
332 1 return \%lx;
333 }
334  
335 =head2 postProcessCfg
336  
337 Post process the configuration data into a format that is easily used
338 by the application.
339  
340 =cut
341  
342 1 ▶sub postProcessCfg {
Subroutine postProcessCfg: called CC=8 SCAR=21.0
Pod documented
343 1 my $self = shift;
344 1 my $cfg = shift;
345 1 my %plugin;
346 my @pluginOrder;
347 1 for my $section (keys %$cfg){
348 6 my $sec = $cfg->{$section};
349 6 ▶ next unless ref $sec eq 'HASH'; # skip non hash stuff
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sec eq "HASH" 1 5 6
350 5 for my $key (keys %$sec){
351 16 ▶ next unless ref $sec->{$key} eq 'HASH' and $sec->{$key}{_text};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sec->{$key} eq "HASH" and $sec->{$key}{"_text"} 16 0 16
Condition33%1/3
ref $sec->{$key} eq "HASH" and $sec->{$key}{"_text"}
!ll&&!rl&&r
1600
MC/DC0%0/2
ref $sec->{$key} eq "HASH" and $sec->{$key}{"_text"}
ref $sec->{$key} eq "HASH" $sec->{$key}{"_text"}
Truth table33%1/3
A and B
A= ref $sec->{$key} eq "HASH"B= $sec->{$key}{"_text"}
ABresult
0X0
100
111
352 0 $sec->{$key} = $sec->{$key}{_text};
353 }
354 5 ▶ if ($section =~ /^PLUGIN:\s*(.+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /^PLUGIN:\s*(.+)/u) 2 3 5
355 2 my $name = $1;
356 2 $pluginOrder[$sec->{_order}] = $name;
357 2 delete $sec->{_order};
358  
359 2 my $obj = $cfg->{PLUGIN}{prototype}{$name} = $sec->{module};
360 2 delete $sec->{module};
361 2 $obj->config($sec);
362 2 $obj->name($name);
363 2 $obj->app($self->app);
364 2 $obj->massageConfig($cfg);
365 # cleanup the config
366 2 delete $cfg->{$section};
367 }
368 5 $cfg->{PLUGIN}{list} = \@pluginOrder;
369 }
370 # rename section
371 # delete returns the value of the deleted hash element
372 1 ▶ if (exists $cfg->{'FRONTEND-COLORS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{"FRONTEND-COLORS"}) 1 0 1
373 1 $cfg->{FRONTEND}{COLORS} = $cfg->{'FRONTEND-COLORS'};
374 1 delete $cfg->{'FRONTEND-COLORS'};
375 }
376 1 $cfg->{FRONTEND}{TRANSLATIONS} = $self->getTranslations();
377 1 return $cfg;
378 }
379  
380 =head2 instantiatePlugin(pluginName,userObj)
381  
382 create a new instance of this plugin prototype
383  
384 =cut
385  
386 2 ▶sub instantiatePlugin {
Subroutine instantiatePlugin: called CC=4 SCAR=13.9
Pod documented
387 2 my $self = shift;
388 2 my $name = shift;
389  
390 2 my $user = shift;
391 2 my $args = shift;
392  
393 2 my $prototype = $self->cfgHash->{PLUGIN}{prototype}{$name};
394  
395 # clean the name
396 2 $name =~ s/[^-_0-9a-z]/_/gi;
397  
398 2 ▶ die mkerror(39943,"No prototype for $name")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prototype 0 2 2
399 if not defined $prototype;
400  
401 2 ▶ my $obj = $prototype->new(
Condition50%1/2
$args // {}
l!l
02
MC/DC0%0/1
$args // {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
402 user => $user,
403 name => $prototype->name,
404 config => $prototype->config,
405 args => $args // {},
406 app => $self->app,
407 );
408 2 ▶ die mkerror(39944,"No permission to access $name")
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->checkAccess 1 1 2
409 if not $obj->checkAccess;
410 1 return $obj;
411 }
412  
413 =head2 $configBlob = $cfg->getConfigBlob()
414  
415 return the configuration state of the system as a blob
416  
417 =cut
418  
419 1 ▶has configPlugins => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
420 1 my $self = shift;
421 1 my $user = $self->app->userObject->new(app=>$self->app,userId=>'__CONFIG');
422 1 my $cfg = $self->cfgHash;
423 1 my @plugins;
424 1 for my $name (@{$cfg->{PLUGIN}{list}}){
425 2 ▶ my $obj = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { $self->instantiatePlugin($name, $user) } } or next 1 1 2
426 2 $self->instantiatePlugin($name,$user);
427 } or next;
428 1 push @plugins, $obj;
429 }
430 1 return \@plugins;
431 1 };
432  
433 0 ▶sub getCrypt {
Subroutine getCrypt: not called CC=2 SCAR=17.9
Pod undocumented
434 0 require Crypt::Rijndael;
435 0 my $self = shift;
436 0 ▶ my $password = substr((shift || '').('x' x 32),0,32);
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
437 0 return Crypt::Rijndael->new( $password,Crypt::Rijndael::MODE_CBC() );
438 }
439  
440 0 ▶sub pack16 {
Subroutine pack16: not called CC=1 SCAR=6.9
Pod undocumented
441 0 my $self = shift;
442 0 my $string = shift;
443 0 my $len = length($string);
444 0 my $mod = 16 - ($len % 16);
445 0 return sprintf("%016x%s",$len,$string.('x' x $mod));
446 }
447  
448 0 ▶sub unpack16 {
Subroutine unpack16: not called CC=3 SCAR=24.8
Pod undocumented
449 0 my $self = shift;
450 0 my $string = shift;
451 0 my $len = substr($string,0,16);
452 0 ▶ if ( $len !~ /^[0-9a-f]{16}$/ or hex($len) > length($string)-16 ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $len =~ /^[0-9a-f]{16}$/u or hex $len > length($string) - 16) 0 0 0
Condition0%0/3
not $len =~ /^[0-9a-f]{16}$/u or hex $len > length($string) - 16
l!l&&r!l&&!r
000
MC/DC0%0/2
not $len =~ /^[0-9a-f]{16}$/u or hex $len > length($string) - 16
not $len =~ /^[0-9a-f]{16}$/u hex $len > length($string) - 16
Truth table0%0/3
not(A) or B
A= not $len =~ /^[0-9a-f]{16}$/uB= hex $len > length($string) - 16
ABresult
1X1
011
000
453 0 die mkerror(3844,trm("Wrong password!"));
454 }
455 0 return substr($string,16,hex($len));
456 }
457  
458 0 ▶sub getConfigBlob {
Subroutine getConfigBlob: not called CC=5 SCAR=34.0
Pod documented
459 0 my $self = shift;
460 0 my $password = shift;
461 0 require Archive::Zip;
462  
463 0 my $zip = Archive::Zip->new();
464 0 my $cfg = $self->cfgHash;
465 # flush all the changes in the database to the db file
466 0 my $dumpfile = '/tmp/cbdump'.$$;
467 0 ▶ unlink $dumpfile if -f $dumpfile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $dumpfile 0 0 0
468 0 open my $dump, '|-','/usr/bin/sqlite3',$cfg->{BACKEND}{cfg_db};
469 0 print $dump ".output $dumpfile\n";
470 0 print $dump ".dump\n";
471 0 close $dump;
472 0 $zip->addFile({
473 filename => $dumpfile,
474 zipName => '{DATABASEDUMP}',
475 });
476 0 for my $obj (@{$self->configPlugins}){
477 0 my $name = $obj->name;
478 0 for my $file (@{$obj->stateFiles}) {
479 0 ▶ if (-r $file){
Branch: 1 branch
BranchTrueFalseTotal
if (-r $file) 0 0 0
480 0 $zip->addFile({
481 filename => $file,
482 zipName => '{PLUGINSTATE.'.$name.'}'.$file
483 })
484 }
485 }
486 }
487 0 my $zipData;
488 0 open(my $fh, ">", \$zipData);
489 0 $zip->writeToFileHandle($fh,0);
490  
491 0 my $crypt = $self->getCrypt($password);
492 0 return $crypt->encrypt($self->pack16($zipData));
493 }
494  
495 =head2 $cfg->restoreConfigBlob(configBlob)
496  
497 retore the confguration state
498  
499 =cut
500  
501 0 ▶sub restoreConfigBlob {
Subroutine restoreConfigBlob: not called CC=9 SCAR=45.0
Pod documented
502 0 my $self = shift;
503 0 my $config = shift;
504 0 my $password = shift;
505 0 require Archive::Zip;
506 0 my $crypt = $self->getCrypt($password);
507 0 $config = $self->unpack16($crypt->decrypt($config));
508  
509 0 my $cfg = $self->cfgHash;
510 0 my $user = $self->app->userObject->new(app=>$self->app,userId=>'__CONFIG');
511 0 open my $fh ,'<', \$config;
512 0 my $zip = Archive::Zip->new();
513 0 $zip->readFromFileHandle($fh);
514 0 my %stateFileCache;
515 0 for my $member ($zip->members){
516 0 for ($member->fileName){
517 0 ▶ /^\{DATABASE\}$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^\{DATABASE\}$/u and do { $self->log->warn("Restoring Database!"); &CallBackery::Config::unlink(glob($cfg->{"BACKEND"}{"cfg_db"} . '*')); $member->extractToFileNamed($cfg->{"BACKEND"}{"cfg_db"}); last } 0 0 0
518 0 $self->log->warn("Restoring Database!");
519 0 unlink glob $cfg->{BACKEND}{cfg_db}.'*';
520 0 $member->extractToFileNamed($cfg->{BACKEND}{cfg_db});
521 0 last;
522 };
523 0 ▶ /^\{DATABASEDUMP\}$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^\{DATABASEDUMP\}$/u and do { $self->log->warn("Restoring Database Dump!"); &CallBackery::Config::unlink(glob($cfg->{"BACKEND"}{"cfg_db"} . '*')); &CallBackery::Config::open(my $sqlite, "|-", "/usr/bin/sqlite3", $cfg->{"BACKEND"}{"cfg_db"}); my $sql = $member->contents; $sql =~ s/0$//u; print $sqlite $sql; &CallBackery::Config::close($sqlite); last } 0 0 0
524 0 $self->log->warn("Restoring Database Dump!");
525 0 unlink glob $cfg->{BACKEND}{cfg_db}.'*';
526 0 open my $sqlite, '|-', '/usr/bin/sqlite3',$cfg->{BACKEND}{cfg_db};
527 0 my $sql = $member->contents();
528 0 $sql =~ s/0$//; # for some reason the dump ends in 0
529 0 print $sqlite $sql;
530 0 close $sqlite;
531 0 last;
532 };
533 0 ▶ m/^\{PLUGINSTATE\.([^.]+)\}(.+)/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^\{PLUGINSTATE\.([^.]+)\}(.+)/u and do { my $plugin = $1; my $file = $2; unless ($stateFileCache{$plugin}) { my $obj = eval { do { $self->instantiatePlugin($plugin, $user) } }; unless ($obj) { $self->log->warn("Ignoring $file from plugin $plugin since the plugin is not available here."); next; } ; $stateFileCache{$plugin} = {map({$_, 1;} @{$obj->stateFiles;})}; } ; if ($stateFileCache{$plugin}{$file}) { $member->extractToFileNamed($file); } else { $self->log->warn("Ignoring $file from archive since it is not listed in $plugin stateFiles."); } } 0 0 0
534 0 my $plugin = $1;
535 0 my $file = $2;
536 0 ▶ if (not $stateFileCache{$plugin}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($stateFileCache{$plugin}) 0 0 0
537 0 my $obj = eval {
538 0 $self->instantiatePlugin($plugin,$user);
539 };
540 0 ▶ if (not $obj){
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 0 0
541 0 $self->log->warn("Ignoring $file from plugin $plugin since the plugin is not available here.");
542 0 next;
543 }
544 0 $stateFileCache{$plugin} = { map { $_ => 1 } @{$obj->stateFiles} };
545 };
546 0 ▶ if ($stateFileCache{$plugin}{$file}){
Branch: 1 branch
BranchTrueFalseTotal
if ($stateFileCache{$plugin}{$file}) { } 0 0 0
547 0 $member->extractToFileNamed($file);
548 }
549 else {
550 0 $self->log->warn("Ignoring $file from archive since it is not listed in $plugin stateFiles.");
551 }
552 }
553 }
554 }
555 0 $self->reConfigure;
556 }
557  
558 =head2 $cfg->reConfigure()
559  
560 Regenerate all the template based configuration files using input from the database.
561  
562 =cut
563  
564 1 ▶sub reConfigure {
Subroutine reConfigure: called CC=3 SCAR=11.0
Pod documented
565 1 my $self = shift;
566 1 my $secretFile = $self->secretFile;
567 1 ▶ if (not -f $secretFile){
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $secretFile) 1 0 1
568 1 open my $rand, '>', $secretFile;
569 1 chmod 0600,$secretFile;
570 1 print $rand sprintf('%x%x',int(rand()*1e14),int(rand()*1e14));
571 1 close $rand;
572 1 chmod 0400,$secretFile;
573 }
574 1 for my $obj (@{$self->configPlugins}){
575 1 $obj->reConfigure;
576 }
577 }
578  
579 =head2 $cfg->unConfigure()
580  
581 Restore the system to unconfigured state. By removing the
582 configuration database, unlinking all user supplied configuration
583 files and regenerating all template based configuration files with
584 empty input.
585  
586 =cut
587  
588 0 ▶sub unConfigure {
Subroutine unConfigure: not called CC=8 SCAR=40.2
Pod documented
589 1 ▶ no autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
590 0 my $self = shift;
591 0 my $cfg = $self->cfgHash;
592 0 $self->log->debug("unlinking config database ".$cfg->{BACKEND}{cfg_db});
593 0 ▶ unlink $cfg->{BACKEND}{cfg_db} if -f $cfg->{BACKEND}{cfg_db};
Branch: 1 branch
BranchTrueFalseTotal
if -f $cfg->{"BACKEND"}{"cfg_db"} 0 0 0
594 0 open my $gen, '>', $cfg->{BACKEND}{cfg_db}.'.flush';
595 0 close $gen;
596 #get 'clean' config files
597 0 $self->reConfigure();
598 # and now remove all state
599 0 for my $obj (@{$self->configPlugins}){
600 0 for my $file (@{$obj->stateFiles},@{$obj->unConfigureFiles}) {
601 0 ▶ next if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
602 0 $self->log->debug('['.$obj->name."] unlinking $file");
603 0 unlink $file;
604 }
605 }
606 0 ▶ unlink $cfg->{BACKEND}{log_file} if defined $cfg->{BACKEND}{log_file} and -f $cfg->{BACKEND}{log_file} ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg->{"BACKEND"}{"log_file"} and -f $cfg->{"BACKEND"}{"log_file"} 0 0 0
Condition0%0/3
defined $cfg->{"BACKEND"}{"log_file"} and -f $cfg->{"BACKEND"}{"log_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cfg->{"BACKEND"}{"log_file"} and -f $cfg->{"BACKEND"}{"log_file"}
defined $cfg->{"BACKEND"}{"log_file"} -f $cfg->{"BACKEND"}{"log_file"}
Truth table0%0/3
A and B
A= defined $cfg->{"BACKEND"}{"log_file"}B= -f $cfg->{"BACKEND"}{"log_file"}
ABresult
0X0
100
111
607 0 ▶ unlink $self->secretFile if -f $self->secretFile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $self->secretFile 0 0 0
608 0 system "sync";
609 }
610  
611  
612 1 1;
613  
614 __END__
« lib/CallBackery/GuiPlugin/AbstractForm.pm ↑ Summary lib/CallBackery/Plugin/Doc.pm »