File Coverage

blib/lib/App/Spoor/Installer.pm
Criterion Covered Total %
statement 58 58 100.0
branch 5 10 50.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 1 1 100.0
total 75 81 92.5


line stmt bran cond sub pod time code
1             package App::Spoor::Installer;
2              
3 1     1   769 use v5.10;
  1         3  
4 1     1   6 use strict;
  1         2  
  1         33  
5 1     1   5 use warnings;
  1         2  
  1         25  
6              
7 1     1   4 use YAML::Tiny;
  1         2  
  1         66  
8              
9 1     1   7 use App::Spoor::LoginUnitFile;
  1         1  
  1         19  
10 1     1   6 use App::Spoor::AccessUnitFile;
  1         1  
  1         26  
11 1     1   443 use App::Spoor::ErrorUnitFile;
  1         3  
  1         29  
12 1     1   402 use App::Spoor::TransmitterUnitFile;
  1         2  
  1         29  
13 1     1   395 use App::Spoor::CpanelHookFile;
  1         3  
  1         473  
14              
15             =head1 NAME
16              
17             App::Spoor::Installer
18              
19             =head1 VERSION
20              
21             Version 0.01
22              
23             =cut
24              
25             our $VERSION = '0.01';
26              
27             =head1 SYNOPSIS
28              
29             Creates directories, config file, systemd unit files as well as the package that will be added to forward-related hooks in CPanel.
30              
31             =head1 SUBROUTINES
32              
33             =head2 install
34              
35             my %installation_config =(
36             login_log_path => '/var/log/login',
37             access_log_path => '/var/log/access',
38             error_log_path => '/var/log/error',
39             api_identifier => 'ABC123',
40             api_secret => 'secret456'
41             );
42              
43             App::Spoor::Installer::install(\%installation_config);
44              
45             # Optionally, you can pass in an alternative to the root path '/' which is used when building the path
46             # to the config file. In the code snippet below, the method will create any directories and files relative to the
47             # provided root path. For example, in the snippet below, the method will create the spoor config file in
48             # /tmp/etc/spoor rather than /etc/spoor. This is primarily used to support testing.
49              
50             App::Spoor::Installer::install(\%installation_config, '/tmp');
51             =cut
52              
53             sub install {
54 8     8 1 46636 my $install_parameters = shift;
55 8   50     29 my $root_path = shift @_ // '/';
56 8         327 mkdir("$root_path/etc/spoor", 0700);
57              
58             my $config = YAML::Tiny->new({
59             followers => {
60             login => {
61             name => $install_parameters->{'login_log_path'},
62             maxinterval => 10,
63             debug => 1,
64             transformer => 'bin/login_log_transformer.pl',
65             },
66             access => {
67             name => $install_parameters->{'access_log_path'},
68             maxinterval => 10,
69             debug => 1,
70             transformer => 'bin/login_log_transformer.pl',
71             },
72             error => {
73             name => $install_parameters->{'error_log_path'},
74             maxinterval => 10,
75             debug => 1,
76             transformer => 'bin/login_log_transformer.pl',
77             },
78             },
79             transmission => {
80             credentials => {
81             api_identifier => $install_parameters->{'api_identifier'},
82 8         161 api_secret => $install_parameters->{'api_secret'},
83             },
84             host => 'https://spoor.capefox.co',
85             endpoints => {
86             report => '/api/reports',
87             }
88             }
89             });
90              
91 8         81 $config->write("$root_path/etc/spoor/spoor.yml");
92 8         12439 chmod(0600, "$root_path/etc/spoor/spoor.yml");
93              
94 8         341 mkdir("$root_path/var/lib/spoor", 0700);
95 8         291 mkdir("$root_path/var/lib/spoor/parsed", 0700);
96 8         292 mkdir("$root_path/var/lib/spoor/transmitted", 0700);
97 8         297 mkdir("$root_path/var/lib/spoor/transmission_failed", 0700);
98              
99 8 50       465 open(my $login_handle, '>:encoding(UTF-8)', "$root_path/etc/systemd/system/spoor-login-follower.service") or die("Could not open: $!");
100 8         587 print $login_handle App::Spoor::LoginUnitFile::contents();
101 8         279 close $login_handle;
102 8         148 chmod(0644, "$root_path/etc/systemd/system/spoor-login-follower.service");
103              
104 8 50       447 open(my $access_handle, '>:encoding(UTF-8)', "$root_path/etc/systemd/system/spoor-access-follower.service") or die("Could not open: $!");
105 8         579 print $access_handle App::Spoor::AccessUnitFile::contents();
106 8         268 close $access_handle;
107 8         146 chmod(0644, "$root_path/etc/systemd/system/spoor-access-follower.service");
108              
109 8 50       456 open(my $error_handle, '>:encoding(UTF-8)', "$root_path/etc/systemd/system/spoor-error-follower.service") or die("Could not open: $!");
110 8         569 print $error_handle App::Spoor::ErrorUnitFile::contents();
111 8         303 close $error_handle;
112 8         147 chmod(0644, "$root_path/etc/systemd/system/spoor-error-follower.service");
113              
114 8 50       415 open(my $transmitter_handle, '>:encoding(UTF-8)', "$root_path/etc/systemd/system/spoor-transmitter.service") or die("Could not open: $!");
115 8         577 print $transmitter_handle App::Spoor::TransmitterUnitFile::contents();
116 8         274 close $transmitter_handle;
117 8         156 chmod(0644, "$root_path/etc/systemd/system/spoor-transmitter.service");
118              
119 8         341 mkdir("$root_path/var/cpanel/perl5", 0755);
120 8         285 mkdir("$root_path/var/cpanel/perl5/lib", 0755);
121 8 50       433 open(my $hook_file_handle, '>:encoding(UTF-8)', "$root_path/var/cpanel/perl5/lib/SpoorForwardHook.pm") or die("Could not open; $!");
122 8         542 print $hook_file_handle App::Spoor::CpanelHookFile::contents();
123 8         185 close $hook_file_handle;
124 8         236 chmod(0644, "$root_path/var/cpanel/perl5/lib/SpoorForwardHook.pm");
125             }
126              
127             =head1 AUTHOR
128              
129             Rory McKinley, C<< <rorymckinley at capefox.co> >>
130              
131             =head1 BUGS
132              
133             Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
134             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
135             automatically be notified of progress on your bug as I make changes.
136              
137             =head1 SUPPORT
138              
139             You can find documentation for this module with the perldoc command.
140              
141             perldoc App::Spoor::Installer
142              
143              
144             You can also look for information at:
145              
146             =over 4
147              
148             =item * RT: CPAN's request tracker (report bugs here)
149              
150             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
151              
152             =item * AnnoCPAN: Annotated CPAN documentation
153              
154             L<http://annocpan.org/dist/.>
155              
156             =item * CPAN Ratings
157              
158             L<https://cpanratings.perl.org/d/.>
159              
160             =item * Search CPAN
161              
162             L<https://metacpan.org/release/.>
163              
164             =back
165              
166             =head1 LICENSE AND COPYRIGHT
167              
168             Copyright 2019 Rory McKinley.
169              
170             This program is free software; you can redistribute it and/or modify it
171             under the terms of the the Artistic License (2.0). You may obtain a
172             copy of the full license at:
173              
174             L<http://www.perlfoundation.org/artistic_license_2_0>
175              
176             Any use, modification, and distribution of the Standard or Modified
177             Versions is governed by this Artistic License. By using, modifying or
178             distributing the Package, you accept this license. Do not use, modify,
179             or distribute the Package, if you do not accept this license.
180              
181             If your Modified Version has been derived from a Modified Version made
182             by someone other than you, you are nevertheless required to ensure that
183             your Modified Version complies with the requirements of this license.
184              
185             This license does not grant you the right to use any trademark, service
186             mark, tradename, or logo of the Copyright Holder.
187              
188             This license includes the non-exclusive, worldwide, free-of-charge
189             patent license to make, have made, use, offer to sell, sell, import and
190             otherwise transfer the Package with respect to any patent claims
191             licensable by the Copyright Holder that are necessarily infringed by the
192             Package. If you institute patent litigation (including a cross-claim or
193             counterclaim) against any party alleging that the Package constitutes
194             direct or contributory patent infringement, then this Artistic License
195             to you shall terminate on the date that such litigation is filed.
196              
197             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
198             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
199             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
200             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
201             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
202             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
203             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
204             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205              
206             =cut
207              
208             1; # End of App::Spoor::Installer