File Coverage

blib/lib/Rex/JobControl/Mojolicious/Command/jobcontrol.pm
Criterion Covered Total %
statement 18 147 12.2
branch 0 48 0.0
condition 0 9 0.0
subroutine 6 21 28.5
pod n/a
total 24 225 10.6


line stmt bran cond sub pod time code
1             package Mojolicious::Command::jobcontrol;
2             $Mojolicious::Command::jobcontrol::VERSION = '0.18.0';
3 1     1   4 use Mojo::Base 'Mojolicious::Command';
  1         1  
  1         5  
4              
5 1     1   112 use Data::Dumper;
  1         2  
  1         41  
6 1     1   432 use Digest::Bcrypt;
  1         4130  
  1         27  
7 1     1   5 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  1         3  
  1         7  
8 1     1   180 use File::Path;
  1         2  
  1         42  
9 1     1   5 use File::Basename;
  1         3  
  1         1801  
10              
11             has description => 'JobControl Commands';
12             has usage => sub { shift->extract_usage };
13              
14             sub run {
15 0     0     my ( $self, $command, @args ) = @_;
16              
17 0 0 0       if ( !$command || $command eq "help" ) {
18 0           print "Usage:\n";
19 0           print "$0 jobcontrol []\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 0         if ( $command eq "version" ) {
39 0           print "This is Rex::JobControl (" . $Rex::JobControl::VERSION . ")\n";
40 0           exit 0;
41             }
42              
43 0 0         if ( $command eq "systemd" ) {
44 0           my ( $create_unit, $start, $stop, $restart ) = @_;
45              
46             GetOptionsFromArray \@args,
47 0     0     'c|create-unit' => sub { $create_unit = 1 },
48 0     0     's|start' => sub { $start = 1 },
49 0     0     'r|restart' => sub { $restart = 1 },
50 0     0     'k|stop' => sub { $stop = 1 };
  0            
51              
52 0 0         if ($create_unit) {
53 0           $self->create_systemd_unit();
54             }
55             }
56              
57 0 0         if ( $command eq "upstart" ) {
58 0           my ( $create_unit, $start, $stop, $restart ) = @_;
59              
60             GetOptionsFromArray \@args,
61 0     0     'c|create-unit' => sub { $create_unit = 1 },
62 0     0     's|start' => sub { $start = 1 },
63 0     0     'r|restart' => sub { $restart = 1 },
64 0     0     'k|stop' => sub { $stop = 1 };
  0            
65              
66 0 0         if ($create_unit) {
67 0           $self->create_upstart_unit();
68             }
69             }
70              
71 0 0         if ( $command eq "setup" ) {
72 0           my $changed = 0;
73              
74 0 0         if ( !-d $self->app->config->{project_path} ) {
75 0           $self->app->log->info(
76             "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 0         if ( !-d dirname( $self->app->config->{minion_db_file} ) ) {
82 0           $self->app->log->info( "Creating minion db path: "
83             . 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 0         if ( !-d $self->app->config->{upload_tmp_path} ) {
89 0           $self->app->log->info(
90             "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 0         if ( !-d dirname( $self->app->config->{auth}->{passwd} ) ) {
96 0           $self->app->log->info( "Creating passwd path: "
97             . 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 0 0       if ( exists $self->app->config->{log}->{audit_log}
103             && !-d dirname( $self->app->config->{log}->{audit_log} ) )
104             {
105 0           $self->app->log->info( "Creating audit.log path: "
106             . dirname( $self->app->config->{log}->{audit_log} ) );
107 0           File::Path::make_path(
108             dirname( $self->app->config->{log}->{audit_log} ) );
109 0           $changed = 1;
110             }
111              
112 0 0 0       if ( exists $self->app->config->{log}->{access_log}
113             && !-d dirname( $self->app->config->{log}->{access_log} ) )
114             {
115 0           $self->app->log->info( "Creating access.log path: "
116             . dirname( $self->app->config->{log}->{access_log} ) );
117 0           File::Path::make_path(
118             dirname( $self->app->config->{log}->{access_log} ) );
119 0           $changed = 1;
120             }
121              
122 0 0         if ( !-f $self->app->config->{auth}->{passwd} ) {
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 0         if ( $changed == 0 ) {
131 0           $self->app->log->info("Everything seems ok.");
132             }
133              
134             }
135              
136 0 0         if ( $command eq "adduser" ) {
137 0           my ( $user, $password );
138              
139             GetOptionsFromArray \@args,
140 0     0     'u|user=s' => sub { $user = $_[1] },
141 0     0     'p|password=s' => sub { $password = $_[1] };
  0            
142              
143 0           $self->add_user( $user, $password );
144             }
145              
146 0 0         if ( $command eq "deluser" ) {
147              
148 0           my $user;
149              
150 0     0     GetOptionsFromArray \@args, 'u|user=s' => sub { $user = $_[1] };
  0            
151              
152 0           my @lines =
153             grep { !m/^$user:/ }
154 0           eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
  0            
  0            
155              
156 0 0         open( my $fh, ">", $self->app->config->{auth}->{passwd} ) or die($!);
157 0           print $fh join( "\n", @lines );
158 0           close($fh);
159             }
160              
161 0 0         if ( $command eq "listuser" ) {
162             my @lines =
163 0           eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
  0            
  0            
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             sub add_user {
175 0     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 0         open( my $fh, ">>", $self->app->config->{auth}->{passwd} ) or die($!);
190 0           print $fh "$user:$pw\n";
191 0           close($fh);
192              
193             }
194              
195             sub create_upstart_unit {
196 0     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 0         open( my $fh, ">", "/etc/init/rex-jobcontrol.conf" ) or die($!);
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 0         open( $fh, ">", "/etc/init/rex-jobcontrol-minion.conf" ) or die($!);
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             sub create_systemd_unit {
231 0     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 0         open( my $fh, ">", "/lib/systemd/system/rex-jobcontrol.service" ) or die($!);
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 0         open( my $fh_m, ">", "/lib/systemd/system/rex-jobcontrol-minion.service" )
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;