File Coverage

blib/lib/XAS/Lib/Modules/Environment.pm
Criterion Covered Total %
statement 12 132 9.0
branch 0 100 0.0
condition n/a
subroutine 4 18 22.2
pod 7 10 70.0
total 23 260 8.8


line stmt bran cond sub pod time code
1             package XAS::Lib::Modules::Environment;
2              
3             our $VERSION = '0.02';
4              
5 1     1   1208 use File::Basename;
  1         2  
  1         49  
6 1     1   3 use Config::IniFiles;
  1         2  
  1         22  
7 1     1   414 use Net::Domain qw(hostdomain);
  1         3256  
  1         69  
8              
9             use XAS::Class
10 1         6 debug => 0,
11             version => $VERSION,
12             base => 'XAS::Singleton',
13             utils => ':validation dir_walk',
14             constants => ':logging :alerts STOMP_LEVELS',
15             filesystem => 'File Dir Path Cwd',
16             accessors => 'path host domain username commandline',
17             mutators => 'mqserver mqport mxserver mxport mxtimeout msgs alerts xdebug',
18 1     1   5 ;
  1         2  
19              
20             # ------------------------------------------------------------------------
21             # Public Methods
22             # ------------------------------------------------------------------------
23              
24             sub mxmailer {
25 0     0 1   my $self = shift;
26 0           my ($mailer) = validate_params(\@_, [
27             { optional => 1, default => undef, regex => qr/sendmail|smtp/ }
28             ]);
29              
30 0 0         $self->{'mxmailer'} = $mailer if (defined($mailer));
31              
32 0           return $self->{'mxmailer'};
33              
34             }
35              
36             sub mqlevel {
37 0     0 1   my $self = shift;
38 0           my ($level) = validate_params(\@_, [
39             { optional => 1, default => undef, regex => STOMP_LEVELS },
40             ]);
41              
42 0 0         $self->{'mqlevel'} = $level if (defined($level));
43              
44 0           return $self->{'mqlevel'};
45              
46             }
47              
48             sub log_type {
49 0     0 1   my $self = shift;
50 0           my ($type) = validate_params(\@_, [
51             { optional => 1, default => undef, regex => LOG_TYPES }
52             ]);
53              
54 0 0         $self->{'log_type'} = $type if (defined($type));
55              
56 0           return $self->{'log_type'};
57              
58             }
59              
60             sub log_facility {
61 0     0 1   my $self = shift;
62 0           my ($type) = validate_params(\@_, [
63             { optional => 1, default => undef, regex => LOG_FACILITY }
64             ]);
65              
66 0 0         $self->{'log_facility'} = $type if (defined($type));
67              
68 0           return $self->{'log_facility'};
69              
70             }
71              
72             sub throws {
73 0     0 1   my $self = shift;
74 0           my ($throws) = validate_params(\@_, [
75             { optional => 1, default => undef }
76             ]);
77              
78 0 0         $self->{'throws'} = $throws if (defined($throws));
79              
80 0           return $self->{'throws'};
81              
82             }
83              
84             sub priority {
85 0     0 0   my $self = shift;
86 0           my ($level) = validate_params(\@_, [
87             { optional => 1, default => undef, regex => ALERT_PRIORITY }
88             ]);
89              
90 0 0         $self->{'priority'} = $level if (defined($level));
91              
92 0           return $self->{'priority'};
93              
94             }
95              
96             sub facility {
97 0     0 0   my $self = shift;
98 0           my ($level) = validate_params(\@_, [
99             { optional => 1, default => undef, regex => ALERT_FACILITY }
100             ]);
101              
102 0 0         $self->{'facility'} = $level if (defined($level));
103              
104 0           return $self->{'facility'};
105              
106             }
107              
108             sub script {
109 0     0 1   my $self = shift;
110 0           my ($script) = validate_params(\@_, [
111             { optional => 1, default => undef }
112             ]);
113            
114 0 0         $self->{'script'} = $script if (defined($script));
115              
116 0           return $self->{'script'};
117              
118             }
119              
120             sub get_msgs {
121 0     0 0   my $self = shift;
122              
123 0           return $self->class->var('MESSAGES');
124              
125             }
126              
127             # ------------------------------------------------------------------------
128             # Private Methods
129             # ------------------------------------------------------------------------
130              
131             sub _load_msgs {
132 0     0     my $self = shift;
133              
134 0           my $messages = $self->class->any_var('MESSAGES');
135              
136 0           foreach my $path (@INC) {
137              
138 0           my $dir = Dir($path, 'XAS');
139              
140 0 0         if ($dir->exists) {
141              
142             dir_walk(
143             -directory => $dir,
144             -filter => $self->msgs,
145             -callback => sub {
146 0     0     my $file = shift;
147              
148 0           my $cfg = Config::IniFiles->new(-file => $file->path);
149 0 0         if (my @names = $cfg->Parameters('messages')) {
150              
151 0           foreach my $name (@names) {
152              
153 0           $messages->{$name} = $cfg->val('messages', $name);
154              
155             }
156              
157             }
158              
159             }
160 0           );
161              
162             }
163              
164             }
165              
166 0           $self->class->var('MESSAGES', $messages);
167              
168             }
169              
170             sub init {
171 0     0 1   my $self = shift;
172              
173 0           my $temp;
174             my $name;
175 0           my $path;
176 0           my $suffix;
177 0           my $commandline = $0;
178 0           my ($script) = ( $commandline =~ m#([^\\/]+)$# );
179              
180 0           foreach (@ARGV) {
181 0 0         $commandline .= /\s/
182             ? " \'" . $_ . "\'"
183             : " " . $_;
184             }
185              
186             # set some defaults
187              
188 0           $self->{'alerts'} = 1;
189 0           $self->{'xdebug'} = 0;
190 0           $self->{'mxtimeout'} = 60;
191 0           $self->{'script'} = $script;
192 0           $self->{'path'} = $ENV{'PATH'};
193 0           $self->{'commandline'} = $commandline;
194            
195             # Initialize variables - these are defaults
196              
197             $self->{'mqserver'} = defined($ENV{'XAS_MQSERVER'})
198 0 0         ? $ENV{'XAS_MQSERVER'}
199             : 'localhost';
200              
201             $self->{'mqport'} = defined($ENV{'XAS_MQPORT'})
202 0 0         ? $ENV{'XAS_MQPORT'}
203             : '61613';
204              
205             $self->{'mqlevel'} = defined ($ENV{'XAS_MQLEVEL'})
206 0 0         ? $ENV{'XAS_MQLEVEL'}
207             : '1.0';
208              
209             $self->{'mxserver'} = defined($ENV{'XAS_MXSERVER'})
210 0 0         ? $ENV{'XAS_MXSERVER'}
211             : 'localhost';
212              
213             $self->{'mxport'} = defined($ENV{'XAS_MXPORT'})
214 0 0         ? $ENV{'XAS_MXPORT'}
215             : '25';
216              
217             $self->{'domain'} = defined($ENV{'XAS_DOMAIN'})
218 0 0         ? $ENV{'XAS_DOMAIN'}
219             : hostdomain();
220              
221 0 0         $self->{'msgs'} = defined($ENV{'XAS_MSGS'})
222             ? qr/$ENV{'XAS_MSGS'}/i
223             : qr/.*\.msg$/i;
224              
225             $self->{'throws'} = defined($ENV{'XAS_ERR_THROWS'})
226 0 0         ? $ENV{'XAS_ERR_THROWS'}
227             : 'xas';
228              
229             $self->{'priority'} = defined($ENV{'XAS_ERR_PRIORITY'})
230 0 0         ? $ENV{'XAS_ERR_PRIORITY'}
231             : 'low';
232              
233             $self->{'facility'} = defined($ENV{'XAS_ERR_FACILITY'})
234 0 0         ? $ENV{'XAS_ERR_FACILITY'}
235             : 'systems';
236              
237             # platform specific
238              
239 0           my $OS = $^O;
240              
241 0 0         if ($OS eq "MSWin32") {
242              
243 0           require Win32;
244              
245             $self->{'host'} = defined($ENV{'XAS_HOSTNAME'})
246 0 0         ? $ENV{'XAS_HOSTNAME'}
247             : Win32::NodeName();
248              
249             $self->{'root'} = Dir(defined($ENV{'XAS_ROOT'})
250 0 0         ? $ENV{'XAS_ROOT'}
251             : ['C:', 'XAS']);
252              
253             $self->{'etc'} = Dir(defined($ENV{'XAS_ETC'})
254             ? $ENV{'XAS_ETC'}
255 0 0         : [$self->{root}, 'etc']);
256              
257             $self->{'tmp'} = Dir(defined($ENV{'XAS_TMP'})
258             ? $ENV{'XAS_TMP'}
259 0 0         : [$self->{root}, 'tmp']);
260              
261             $self->{'var'} = Dir(defined($ENV{'XAS_VAR'})
262             ? $ENV{'XAS_VAR'}
263 0 0         : [$self->{root}, 'var']);
264              
265             $self->{'lib'} = Dir(defined($ENV{'XAS_LIB'})
266             ? $ENV{'XAS_LIB'}
267 0 0         : [$self->{root}, 'var', 'lib']);
268              
269             $self->{'log'} = Dir(defined($ENV{'XAS_LOG'})
270             ? $ENV{'XAS_LOG'}
271 0 0         : [$self->{root}, 'var', 'log']);
272              
273             $self->{'locks'} = Dir(defined($ENV{'XAS_LOCKS'})
274             ? $ENV{'XAS_LOCKS'}
275 0 0         : [$self->{root}, 'var', 'lock']);
276              
277             $self->{'run'} = Dir(defined($ENV{'XAS_RUN'})
278             ? $ENV{'XAS_RUN'}
279 0 0         : [$self->{root}, 'var', 'run']);
280              
281             $self->{'spool'} = Dir(defined($ENV{'XAS_SPOOL'})
282             ? $ENV{'XAS_SPOOL'}
283 0 0         : [$self->{root}, 'var', 'spool']);
284              
285             $self->{'mxmailer'} = defined($ENV{'XAS_MXMAILER'})
286 0 0         ? $ENV{'XAS_MXMAILER'}
287             : 'smtp';
288              
289 0           $self->{'username'} = Win32::LoginName();
290              
291             } else {
292              
293             # this assumes a unix like working environment
294              
295             $self->{'host'} = defined($ENV{'XAS_HOSTNAME'})
296 0 0         ? $ENV{'XAS_HOSTNAME'}
297             : `hostname -s`;
298              
299 0           chomp($self->{'host'});
300              
301             $self->{'root'} = Dir(defined($ENV{'XAS_ROOT'})
302 0 0         ? $ENV{'XAS_ROOT'}
303             : ['/']);
304              
305             $self->{'etc'} = Dir(defined($ENV{'XAS_ETC'})
306             ? $ENV{'XAS_ETC'}
307 0 0         : [$self->{root}, 'etc', 'xas']);
308              
309             $self->{'tmp'} = Dir(defined($ENV{'XAS_TMP'})
310 0 0         ? $ENV{'XAS_TMP'}
311             : ['/', 'tmp']);
312              
313             $self->{'var'} = Dir(defined($ENV{'XAS_VAR'})
314             ? $ENV{'XAS_VAR'}
315 0 0         : [$self->{root}, 'var']);
316              
317             $self->{'lib'} = Dir(defined($ENV{'XAS_LIB'})
318             ? $ENV{'XAS_LIB'}
319 0 0         : [$self->{root}, 'var', 'lib', 'xas']);
320              
321             $self->{'log'} = Dir(defined($ENV{'XAS_LOG'})
322             ? $ENV{'XAS_LOG'}
323 0 0         : [$self->{root}, 'var', 'log', 'xas']);
324              
325             $self->{'locks'} = Dir(defined($ENV{'XAS_LOCKS'})
326             ? $ENV{'XAS_LOCKS'}
327 0 0         : [$self->{root}, 'var', 'lock', 'xas']);
328              
329             $self->{'run'} = Dir(defined($ENV{'XAS_RUN'})
330             ? $ENV{'XAS_RUN'}
331 0 0         : [$self->{root}, 'var', 'run', 'xas']);
332              
333             $self->{'spool'} = Dir(defined($ENV{'XAS_SPOOL'})
334             ? $ENV{'XAS_SPOOL'}
335 0 0         : [$self->{root}, 'var', 'spool', 'xas']);
336              
337             $self->{'mxmailer'} = defined($ENV{'XAS_MXMAILER'})
338 0 0         ? $ENV{'XAS_MXMAILER'}
339             : 'sendmail';
340              
341 0           $self->{'username'} = getpwuid($<);
342              
343             }
344              
345             # build some common paths
346              
347             $self->{'sbin'} = Dir(defined($ENV{'XAS_SBIN'})
348             ? $ENV{'XAS_SBIN'}
349 0 0         : [$self->{'root'}, 'sbin']);
350              
351             $self->{'bin'} = Dir(defined($ENV{'XAS_BIN'})
352             ? $ENV{'XAS_BIN'}
353 0 0         : [$self->{'root'}, 'bin']);
354              
355             # define some logging options
356              
357             $self->{'log_type'} = defined($ENV{'XAS_LOG_TYPE'})
358 0 0         ? $ENV{'XAS_LOG_TYPE'}
359             : 'console';
360              
361             $self->{'log_facility'} = defined($ENV{'XAS_LOG_FACILITY'})
362 0 0         ? $ENV{'XAS_LOG_FACILITY'}
363             : 'local6';
364              
365             # create some common file names
366              
367 0           ($name, $path, $suffix) = fileparse($0, qr{\..*});
368              
369 0           $self->{'log_file'} = File($self->{'log'}, $name . '.log');
370 0           $self->{'pid_file'} = File($self->{'run'}, $name . '.pid');
371 0           $self->{'cfg_file'} = File($self->{'etc'}, $name . '.ini');
372              
373             # build some methods, saves typing
374              
375 0           for my $datum (qw( log_file pid_file cfg_file )) {
376              
377             $self->class->methods($datum => sub {
378 0     0     my $self = shift;
379 0           my ($p) = validate_params(\@_, [
380             {optional => 1, default => undef, isa => 'Badger::Filesystem::File' }
381             ],
382             "xas.lib.modules.environment.$datum"
383             );
384              
385 0 0         $self->{$datum} = $p if (defined($p));
386              
387 0           return $self->{$datum};
388              
389 0           });
390              
391             }
392              
393 0           for my $datum (qw( root etc sbin tmp var bin lib log locks run spool )) {
394              
395             $self->class->methods($datum => sub {
396 0     0     my $self = shift;
397 0           my ($p) = validate_params(\@_, [
398             {optional => 1, default => undef, isa => 'Badger::Filesystem::Directory'}
399             ],
400             "xas.lib.modules.environment.$datum"
401             );
402              
403 0 0         $self->{$datum} = $p if (defined($p));
404              
405 0           return $self->{$datum};
406              
407 0           });
408              
409             }
410              
411 0           $self->_load_msgs();
412              
413 0           return $self;
414              
415             }
416              
417             1;
418              
419             __END__
420              
421             =head1 NAME
422              
423             XAS::Lib::Modules::Environment - The base environment for the XAS environment
424              
425             =head1 SYNOPSIS
426              
427             Your program can use this module in the following fashion:
428              
429             use XAS::Class
430             version => '0.01',
431             base => 'XAS::Base',
432             ;
433              
434             $pidfile = $self->env->pid_file;
435             $logfile = $self->env->log_file;
436              
437             printf("The XAS root is %s\n", $self->env->root);
438              
439             =head1 DESCRIPTION
440              
441             This module describes the base environment for XAS. This module is implemented
442             as a singleton and will be auto-loaded when invoked.
443              
444             =head1 METHODS
445              
446             =head2 new
447              
448             This method will initialize the base module. It parses the current environment
449             using the following variables:
450              
451             =over 4
452              
453             =item B<XAS_ROOT>
454              
455             The root of the directory structure. On Unix like boxes this will be
456             / and Windows this will be C:\XAS.
457              
458             =item B<XAS_LOG>
459              
460             The path for log files. On Unix like boxes this will be /var/log/xas and on
461             Windows this will be %XAS_ROOT%\var\log.
462              
463             =item B<XAS_LOCKS>
464              
465             The path for lock files. On Unix like boxes this will be /var/lock/xas and on
466             Windows this will be %XAS_ROOT%\var\lock.
467              
468             =item B<XAS_RUN>
469              
470             The path for pid files. On Unix like boxes this will be /var/run/xas and
471             on Windows this will be %XAS_ROOT%\var\run.
472              
473             =item B<XAS_SPOOL>
474              
475             The base path for spool files. On Unix like boxes this will be /var/spool/xas
476             and on Windows this will be %XAS_ROOT%\var\spool.
477              
478             =item B<XAS_LIB>
479              
480             The path to the lib directory. On Unix like boxes this will be /var/lib/xas
481             and on Windows this will be %XAS_ROOT%\var\lib.
482              
483             =item B<XAS_ETC>
484              
485             The path to the etc directory. On Unix like boxes this will be /usr/local/etc
486             and on Windows this will be %XAS_ROOT%\etc
487              
488             =item B<XAS_BIN>
489              
490             The path to the bin directory. On Unix like boxes this will be /usr/local/bin
491             and on Windows this will be %XAS_ROOT%\bin.
492              
493             =item B<XAS_SBIN>
494              
495             The path to the sbin directory. On Unix like boxes this will be /usr/local/sbin
496             and on Windows this will be %XAS_ROOT%\sbin.
497              
498             =item B<XAS_HOSTNAME>
499              
500             The host name of the system. If not provided, on Unix the "hostname -s" command
501             will be used and on Windows Win32::NodeName() will be called.
502              
503             =item B<XAS_DOMAIN>
504              
505             The domain of the system: If not provided, then Net::Domain::hostdomain() will
506             be used.
507              
508             =item B<XAS_MQSERVER>
509              
510             The server where a STOMP enabled message queue server is located. Default
511             is "localhost".
512              
513             =item B<XAS_MQPORT>
514              
515             The port that server is listening on. Default is "61613".
516              
517             =item B<XAS_MQLEVL>
518              
519             This sets the STOMP protocol level. The default is v1.0.
520              
521             =item B<XAS_MXSERVER>
522              
523             The server where a SMTP based mail server resides. Default is "localhost".
524              
525             =item B<XAS_MXPORT>
526              
527             The port it is listening on. Default is "25".
528              
529             =item B<XAS_MXMAILER>
530              
531             The mailer to use for sending email. On Unix like boxes this will be "sendmail"
532             on Windows this will be "smtp".
533              
534             =item B<XAS_MSGS>
535              
536             The regex to use when searching for message files. Defaults to /.*\.msg/i.
537              
538             =item B<XAS_LOG_FACILITY>
539              
540             The syslog facility class to use. Defaults to 'local6'. It uses the syslog
541             conventions.
542              
543             =item B<XAS_LOG_TYPE>
544              
545             The log type. This can be "console", "file", "json" or "syslog". Defaults
546             to "console"
547              
548             =item B<XAS_ERR_THROWS>
549              
550             The default error message type. Defaults to 'xas'.
551              
552             =item B<XAS_ERR_PRIORITY>
553              
554             The error message priority type. Defaults to "low".
555              
556             =item B<XAS_ERR_FACILITY>
557              
558             The error message facility type. Defaults to "systems".
559              
560             =back
561              
562             =head2 alerts
563              
564             This method sets or returns wither to send alerts.
565              
566             =head2 xdebug
567              
568             This method sets or returns the status of debug.
569              
570             =head2 script
571              
572             This method returns the name of the script.
573              
574             =head2 commandline
575              
576             This method returns the complete commandline.
577              
578             =head2 log_type
579              
580             This method will return the currently defined log type. By default this is
581             "console". i.e. all logging will go to the terminal screen. Valid options
582             are "console", "file", "json" and "syslog'.
583              
584             =head2 log_facility
585              
586             This method will return the log facility class to use when writting to
587             syslog or json.
588              
589             Example
590              
591             $facility = $xas->log_facility;
592             $xas->log_facility('local6');
593              
594             =head2 log_file
595              
596             This method will return a pre-generated name for a log file. The name will be
597             based on the programs name with a ".log" extension, along with the path to
598             the XAS log file directory. Or you can store your own self generated log
599             file name.
600              
601             Example
602              
603             $logfile = $xas->log_file;
604             $xas->log_file("/some/path/mylogfile.log");
605              
606             =head2 pid_file
607              
608             This method will return a pre-generated name for a pid file. The name will be
609             based on the programs name with a ".pid" extension, along with the path to
610             the XAS pid file directory. Or you can store your own self generated pid
611             file name.
612              
613             Example
614              
615             $pidfile = $xas->pid_file;
616             $xas->pid_file("/some/path/myfile.pid");
617              
618             =head2 cfg_file
619              
620             This method will return a pre-generated name for a configuration file. The
621             name will be based on the programs name with a ".ini" extension, along with
622             the path to the XAS configuration file directory. Or you can store your own
623             self generated configuration file name.
624              
625             Example
626              
627             $inifile = $xas->cfg_file;
628             $xas->cfg_file("/some/path/myfile.cfg");
629              
630             =head2 mqserver
631              
632             This method will return the name of the message queue server. Or you can
633             store a different name for the server.
634              
635             Example
636              
637             $mqserver = $xas->mqserver;
638             $xas->mqserver('mq.example.com');
639              
640             =head2 mqport
641              
642             This method will return the port for the message queue server, or you store
643             a different port number for that server.
644              
645             =head2 mqlevel
646              
647             This method will returns the STOMP protocol level. or you store
648             a different level. It can use 1.0, 1.1 or 1.2.
649              
650             Example
651              
652             $mqlevel = $xas->mqlevel;
653             $xas->mqlevel('1.0');
654              
655             =head2 mxserver
656              
657             This method will return the name of the mail server. Or you can
658             store a different name for the server.
659              
660             Example
661              
662             $mxserver = $xas->mxserver;
663             $xas->mxserver('mail.example.com');
664              
665             =head2 mxport
666              
667             This method will return the port for the mail server, or you store
668             a different port number for that server.
669              
670             Example
671              
672             $mxport = $xas->mxport;
673             $xas->mxport('25');
674              
675             =head2 mxmailer
676              
677             This method will return the mailer to use for sending email, or you can
678             change the mailer used.
679              
680             Example
681              
682             $mxmailer = $xas->mxmailer;
683             $xas->mxmailer('smtp');
684              
685             =head1 ACCESSORS
686              
687             =head2 path
688              
689             This accessor returns the currently defined path for this program.
690              
691             =head2 root
692              
693             This accessor returns the root directory of the XAS environment.
694              
695             =head2 bin
696              
697             This accessor returns the bin directory of the XAS environment. The bin
698             directory is used to place executable commands.
699              
700             =head2 sbin
701              
702             This accessor returns the sbin directory of the XAS environment. The sbin
703             directory is used to place system level commands.
704              
705             =head2 log
706              
707             This accessor returns the log directory of the XAS environment.
708              
709             =head2 run
710              
711             This accessor returns the run directory of the XAS environment. The run
712             directory is used to place pid files and other such files.
713              
714             =head2 etc
715              
716             This accessor returns the etc directory of the XAS environment.
717             Application configuration files should go into this directory.
718              
719             =head2 lib
720              
721             This accessor returns the lib directory of the XAS environment. This
722             directory is used to store supporting file for the environment.
723              
724             =head2 spool
725              
726             This accessor returns the spool directory of the XAS environment. This
727             directory is used to store spool files generated within the environment.
728              
729             =head2 tmp
730              
731             This accessor returns the tmp directory of the XAS environment. This
732             directory is used to store temporary files.
733              
734             =head2 var
735              
736             This accessor returns the var directory of the XAS environment.
737              
738             =head2 host
739              
740             This accessor returns the local hostname.
741              
742             =head2 domain
743              
744             This access returns the domain name of the local host.
745              
746             =head2 username
747              
748             This accessor returns the effective username of the current process.
749              
750             =head2 msgs
751              
752             The accessor to return the regex for messages files.
753              
754             =head1 SEE ALSO
755              
756             =over 4
757              
758             =item L<XAS|XAS>
759              
760             =back
761              
762             =head1 AUTHOR
763              
764             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
765              
766             =head1 COPYRIGHT AND LICENSE
767              
768             Copyright (C) 2014 Kevin L. Esteb
769              
770             This is free software; you can redistribute it and/or modify it under
771             the terms of the Artistic License 2.0. For details, see the full text
772             of the license at http://www.perlfoundation.org/artistic_license_2_0.
773              
774             =cut