JobControl/Mojolicious/Command/jobcontrol.pm

filter: Statementstmt 14.4% 22 / 152 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 27.2% 6 / 22 Podpod n/a totaltotal 11.8% 28 / 237 CC 29 SCAR 56.7
CC
29
Cov
11%
run
600.0
create_upstart_unit
12.0
create_systemd_unit
12.0
CRAP 290.3
↑ Summary JobControl/Formular.pm »
1 package Mojolicious::Command::jobcontrol;
2 1 $Mojolicious::Command::jobcontrol::VERSION = '0.18.0';
3 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::Bcrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has description => 'JobControl Commands';
12 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13  
14 0 ▶sub run {
Subroutine run: not called CC=24 SCAR=64.0
15 0 my ( $self, $command, @args ) = @_;
16  
17 0 ▶ if ( !$command || $command eq "help" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $command or $command eq "help") 0 0 0
Condition0%0/3
not $command or $command eq "help"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $command or $command eq "help"
not $command $command eq "help"
Truth table0%0/3
not(A) or B
A= not $commandB= $command eq "help"
ABresult
1X1
011
000
18 0 print "Usage:\n";
19 0 print "$0 jobcontrol <command> [<options>]\n";
20 0 print "\n";
21 0 print "Commands:\n";
22 0 print "\n";
23 0 print "version print the version.\n";
24 0 print "\n";
25 0 print "setup create all required folders\n";
26 0 print
27 "systemd -c create systemd unit files for JobControl and Minion.\n";
28 0 print "upstart -c create upstart files for JobControl and Minion.\n";
29 0 print "\n";
30 0 print "User Management:\n";
31 0 print "\n";
32 0 print "adduser -u username -p password will add a new user\n";
33 0 print "deluser -u username will delete a user\n";
34 0 print "listuser list all users\n";
35 0 exit 0;
36 }
37  
38 0 ▶ if ( $command eq "version" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "version") 0 0 0
39 0 print "This is Rex::JobControl (" . $Rex::JobControl::VERSION . ")\n";
40 0 exit 0;
41 }
42  
43 0 ▶ if ( $command eq "systemd" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "systemd") 0 0 0
44 0 my ( $create_unit, $start, $stop, $restart ) = @_;
45  
46 GetOptionsFromArray \@args,
47 0 ▶ 'c|create-unit' => sub { $create_unit = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ 's|start' => sub { $start = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ 'r|restart' => sub { $restart = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 ▶ 'k|stop' => sub { $stop = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
51  
52 0 ▶ if ($create_unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create_unit) 0 0 0
53 0 $self->create_systemd_unit();
54 }
55 }
56  
57 0 ▶ if ( $command eq "upstart" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "upstart") 0 0 0
58 0 my ( $create_unit, $start, $stop, $restart ) = @_;
59  
60 GetOptionsFromArray \@args,
61 0 ▶ 'c|create-unit' => sub { $create_unit = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 ▶ 's|start' => sub { $start = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 ▶ 'r|restart' => sub { $restart = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ 'k|stop' => sub { $stop = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
65  
66 0 ▶ if ($create_unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create_unit) 0 0 0
67 0 $self->create_upstart_unit();
68 }
69 }
70  
71 0 ▶ if ( $command eq "setup" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "setup") 0 0 0
72 0 my $changed = 0;
73  
74 0 ▶ if ( !-d $self->app->config->{project_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->app->config->{"project_path"}) 0 0 0
75 $self->app->log->info(
76 0 "Creating project path: " . $self->app->config->{project_path} );
77 0 File::Path::make_path( $self->app->config->{project_path} );
78 0 $changed = 1;
79 }
80  
81 0 ▶ if ( !-d dirname( $self->app->config->{minion_db_file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d dirname($self->app->config->{"minion_db_file"})) 0 0 0
82 $self->app->log->info( "Creating minion db path: "
83 0 . dirname( $self->app->config->{minion_db_file} ) );
84 0 File::Path::make_path( dirname( $self->app->config->{minion_db_file} ) );
85 0 $changed = 1;
86 }
87  
88 0 ▶ if ( !-d $self->app->config->{upload_tmp_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->app->config->{"upload_tmp_path"}) 0 0 0
89 $self->app->log->info(
90 0 "Creating upload_tmp_path: " . $self->app->config->{upload_tmp_path} );
91 0 File::Path::make_path( $self->app->config->{upload_tmp_path} );
92 0 $changed = 1;
93 }
94  
95 0 ▶ if ( !-d dirname( $self->app->config->{auth}->{passwd} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d dirname($self->app->config->{"auth"}{"passwd"})) 0 0 0
96 $self->app->log->info( "Creating passwd path: "
97 0 . dirname( $self->app->config->{auth}->{passwd} ) );
98 0 File::Path::make_path( dirname( $self->app->config->{auth}->{passwd} ) );
99 0 $changed = 1;
100 }
101  
102 0 ▶ if ( exists $self->app->config->{log}->{audit_log}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->app->config->{"log"}{"audit_log"} and not -d dirname($self->app->config->{"log"}{"audit_log"})) 0 0 0
Condition0%0/3
exists $self->app->config->{"log"}{"audit_log"} and not -d dirname($self->app->config->{"log"}{"audit_log"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->app->config->{"log"}{"audit_log"} and not -d dirname($self->app->config->{"log"}{"audit_log"})
exists $self->app->config->{"log"}{"audit_log"} not -d dirname($self->app->config->{"log"}{"audit_log"})
Truth table0%0/3
A and not(B)
A= exists $self->app->config->{"log"}{"audit_log"}B= not -d dirname($self->app->config->{"log"}{"audit_log"})
ABresult
0X0
100
111
103 && !-d dirname( $self->app->config->{log}->{audit_log} ) )
104 {
105 $self->app->log->info( "Creating audit.log path: "
106 0 . dirname( $self->app->config->{log}->{audit_log} ) );
107 File::Path::make_path(
108 0 dirname( $self->app->config->{log}->{audit_log} ) );
109 0 $changed = 1;
110 }
111  
112 0 ▶ if ( exists $self->app->config->{log}->{access_log}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->app->config->{"log"}{"access_log"} and not -d dirname($self->app->config->{"log"}{"access_log"})) 0 0 0
Condition0%0/3
exists $self->app->config->{"log"}{"access_log"} and not -d dirname($self->app->config->{"log"}{"access_log"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->app->config->{"log"}{"access_log"} and not -d dirname($self->app->config->{"log"}{"access_log"})
exists $self->app->config->{"log"}{"access_log"} not -d dirname($self->app->config->{"log"}{"access_log"})
Truth table0%0/3
A and not(B)
A= exists $self->app->config->{"log"}{"access_log"}B= not -d dirname($self->app->config->{"log"}{"access_log"})
ABresult
0X0
100
111
113 && !-d dirname( $self->app->config->{log}->{access_log} ) )
114 {
115 $self->app->log->info( "Creating access.log path: "
116 0 . dirname( $self->app->config->{log}->{access_log} ) );
117 File::Path::make_path(
118 0 dirname( $self->app->config->{log}->{access_log} ) );
119 0 $changed = 1;
120 }
121  
122 0 ▶ if ( !-f $self->app->config->{auth}->{passwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->app->config->{"auth"}{"passwd"}) 0 0 0
123 0 $self->app->log->info(
124 "No passwd file found. Creating one with user 'admin' and password 'admin'."
125 );
126 0 $self->add_user( "admin", "admin" );
127 0 $changed = 1;
128 }
129  
130 0 ▶ if ( $changed == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed == 0) 0 0 0
131 0 $self->app->log->info("Everything seems ok.");
132 }
133  
134 }
135  
136 0 ▶ if ( $command eq "adduser" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "adduser") 0 0 0
137 0 my ( $user, $password );
138  
139 GetOptionsFromArray \@args,
140 0 ▶ 'u|user=s' => sub { $user = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 ▶ 'p|password=s' => sub { $password = $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
142  
143 0 $self->add_user( $user, $password );
144 }
145  
146 0 ▶ if ( $command eq "deluser" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "deluser") 0 0 0
147  
148 0 my $user;
149  
150 0 ▶ GetOptionsFromArray \@args, 'u|user=s' => sub { $user = $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
151  
152 my @lines =
153 0 grep { !m/^$user:/ }
154 0 eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
155  
156 0 ▶ open( my $fh, ">", $self->app->config->{auth}->{passwd} ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->app->config->{"auth"}{"passwd"} or die $! 0 0 0
157 0 print $fh join( "\n", @lines );
158 0 close($fh);
159 }
160  
161 0 ▶ if ( $command eq "listuser" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "listuser") 0 0 0
162 my @lines =
163 0 eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
164 0 chomp @lines;
165  
166 0 for my $l (@lines) {
167 0 my ( $user, $pass ) = split( /:/, $l );
168 0 print "> $user\n";
169 }
170 }
171  
172 }
173  
174 0 ▶sub add_user {
Subroutine add_user: not called CC=2 SCAR=17.9
175 0 my ( $self, $user, $password ) = @_;
176  
177 0 $self->app->log->debug("Creating new user $user with password $password");
178  
179 0 my $salt = $self->app->config->{auth}->{salt};
180 0 my $cost = $self->app->config->{auth}->{cost};
181  
182 0 my $bcrypt = Digest::Bcrypt->new;
183 0 $bcrypt->salt($salt);
184 0 $bcrypt->cost($cost);
185 0 $bcrypt->add($password);
186  
187 0 my $pw = $bcrypt->hexdigest;
188  
189 0 ▶ open( my $fh, ">>", $self->app->config->{auth}->{passwd} ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $self->app->config->{"auth"}{"passwd"} or die $! 0 0 0
190 0 print $fh "$user:$pw\n";
191 0 close($fh);
192  
193 }
194  
195 0 ▶sub create_upstart_unit {
Subroutine create_upstart_unit: not called CC=3 SCAR=24.8
196 0 my ($self) = @_;
197  
198 0 my $pid_file = $self->app->config->{hypnotoad}->{pid_file};
199  
200 0 my $whereis_hypnotoad = qx{which hypnotoad};
201 0 chomp $whereis_hypnotoad;
202  
203 0 ▶ open( my $fh, ">", "/etc/init/rex-jobcontrol.conf" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "/etc/init/rex-jobcontrol.conf" or die $! 0 0 0
204 0 print $fh qq~# Rex::JobControl
205  
206 description "Rex::JobControl upstart job"
207  
208 start on runlevel [2345]
209 stop on runlevel [!2345]
210  
211 pre-start exec $whereis_hypnotoad $0
212 post-stop exec $whereis_hypnotoad $0 -s
213 ~;
214 0 close($fh);
215  
216 0 ▶ open( $fh, ">", "/etc/init/rex-jobcontrol-minion.conf" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "/etc/init/rex-jobcontrol-minion.conf" or die $! 0 0 0
217 0 print $fh qq~# Rex::JobControl Minion
218  
219 description "Rex::JobControl Minion upstart job"
220  
221 start on runlevel [2345]
222 stop on runlevel [!2345]
223  
224 exec $0 minion worker
225 ~;
226 0 close($fh);
227  
228 }
229  
230 0 ▶sub create_systemd_unit {
Subroutine create_systemd_unit: not called CC=3 SCAR=24.8
231 0 my ($self) = @_;
232  
233 0 my $pid_file = $self->app->config->{hypnotoad}->{pid_file};
234  
235 0 my $whereis_hypnotoad = qx{which hypnotoad};
236 0 chomp $whereis_hypnotoad;
237  
238 0 ▶ open( my $fh, ">", "/lib/systemd/system/rex-jobcontrol.service" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "/lib/systemd/system/rex-jobcontrol.service" or die $! 0 0 0
239 0 print $fh qq~[Unit]
240 Description=Rex JobControl Server
241 After=network.target
242  
243 [Service]
244 Type=simple
245 SyslogIdentifier=rex-jobcontrol
246 PIDFile=$pid_file
247 ExecStart=$whereis_hypnotoad -f $0
248 ExecStop=$whereis_hypnotoad -s $0
249 ExecReload=$whereis_hypnotoad $0
250 ~;
251 0 close($fh);
252  
253 0 ▶ open( my $fh_m, ">", "/lib/systemd/system/rex-jobcontrol-minion.service" )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_m, ">", "/lib/systemd/system/rex-jobcontrol-minion.service" or die $! 0 0 0
254 or die($!);
255 0 print $fh_m qq~[Unit]
256 Description=Rex JobControl Minion
257 After=network.target
258  
259 [Service]
260 Type=simple
261 SyslogIdentifier=rex-jobcontrol-minion
262 ExecStart=$0 minion worker
263 ~;
264 0 close($fh_m);
265  
266 }
267  
268 1 1;
↑ Summary JobControl/Formular.pm »