File Coverage

blib/lib/Rex/JobControl/Mojolicious/Command/jobcontrol.pm
Criterion Covered Total %
statement 18 130 13.8
branch 0 40 0.0
condition 0 3 0.0
subroutine 6 21 28.5
pod n/a
total 24 194 12.3


line stmt bran cond sub pod time code
1             package Mojolicious::Command::jobcontrol;
2             $Mojolicious::Command::jobcontrol::VERSION = '0.6.0';
3 1     1   7 use Mojo::Base 'Mojolicious::Command';
  1         3  
  1         9  
4              
5 1     1   201 use Data::Dumper;
  1         2  
  1         45  
6 1     1   10737 use Digest::Bcrypt;
  1         16020  
  1         45  
7 1     1   13 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  1         3  
  1         12  
8 1     1   207 use File::Path;
  1         2  
  1         56  
9 1     1   7 use File::Basename;
  1         3  
  1         2779  
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 "setup create all required folders\n";
24 0           print "systemd -c create systemd unit files for JobControl and Minion.\n";
25 0           print "upstart -c create upstart files for JobControl and Minion.\n";
26 0           print "\n";
27 0           print "User Management:\n";
28 0           print "\n";
29 0           print "adduser -u username -p password will add a new user\n";
30 0           print "deluser -u username will delete a user\n";
31 0           print "listuser list all users\n";
32 0           exit 0;
33             }
34              
35 0 0         if($command eq "systemd") {
36 0           my ($create_unit, $start, $stop, $restart) = @_;
37              
38             GetOptionsFromArray \@args,
39 0     0     'c|create-unit' => sub { $create_unit = 1 },
40 0     0     's|start' => sub { $start = 1 },
41 0     0     'r|restart' => sub { $restart = 1 },
42 0     0     'k|stop' => sub { $stop = 1 };
  0            
43              
44 0 0         if($create_unit) {
45 0           $self->create_systemd_unit();
46             }
47             }
48              
49 0 0         if($command eq "upstart") {
50 0           my ($create_unit, $start, $stop, $restart) = @_;
51              
52             GetOptionsFromArray \@args,
53 0     0     'c|create-unit' => sub { $create_unit = 1 },
54 0     0     's|start' => sub { $start = 1 },
55 0     0     'r|restart' => sub { $restart = 1 },
56 0     0     'k|stop' => sub { $stop = 1 };
  0            
57              
58 0 0         if($create_unit) {
59 0           $self->create_upstart_unit();
60             }
61             }
62              
63 0 0         if ( $command eq "setup" ) {
64 0           my $changed = 0;
65              
66 0 0         if(! -d $self->app->config->{project_path} ) {
67 0           $self->app->log->info("Creating project path: " . $self->app->config->{project_path});
68 0           File::Path::make_path($self->app->config->{project_path});
69 0           $changed = 1;
70             }
71              
72 0 0         if(! -d dirname($self->app->config->{minion_db_file}) ) {
73 0           $self->app->log->info("Creating minion db path: " . dirname($self->app->config->{minion_db_file}));
74 0           File::Path::make_path(dirname($self->app->config->{minion_db_file}));
75 0           $changed = 1;
76             }
77              
78 0 0         if(! -d dirname($self->app->config->{auth}->{passwd}) ) {
79 0           $self->app->log->info("Creating passwd path: " . dirname($self->app->config->{auth}->{passwd}));
80 0           File::Path::make_path(dirname($self->app->config->{auth}->{passwd}));
81 0           $changed = 1;
82             }
83              
84 0 0         if(! -f $self->app->config->{auth}->{passwd}) {
85 0           $self->app->log->info("No passwd file found. Creating one with user 'admin' and password 'admin'.");
86 0           $self->add_user("admin", "admin");
87 0           $changed = 1;
88             }
89              
90 0 0         if($changed == 0) {
91 0           $self->app->log->info("Everything seems ok.");
92             }
93              
94             }
95              
96 0 0         if ( $command eq "adduser" ) {
97 0           my ( $user, $password );
98              
99             GetOptionsFromArray \@args,
100 0     0     'u|user=s' => sub { $user = $_[1] },
101 0     0     'p|password=s' => sub { $password = $_[1] };
  0            
102              
103 0           $self->add_user($user, $password);
104             }
105              
106 0 0         if ( $command eq "deluser" ) {
107              
108 0           my $user;
109              
110 0     0     GetOptionsFromArray \@args, 'u|user=s' => sub { $user = $_[1] };
  0            
111              
112 0           my @lines =
113             grep { !m/^$user:/ }
114 0           eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
  0            
  0            
115              
116 0 0         open( my $fh, ">", $self->app->config->{auth}->{passwd} ) or die($!);
117 0           print $fh join( "\n", @lines );
118 0           close($fh);
119             }
120              
121 0 0         if ( $command eq "listuser" ) {
122             my @lines =
123 0           eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
  0            
  0            
124 0           chomp @lines;
125              
126 0           for my $l (@lines) {
127 0           my ( $user, $pass ) = split( /:/, $l );
128 0           print "> $user\n";
129             }
130             }
131              
132             }
133              
134             sub add_user {
135 0     0     my ($self, $user, $password) = @_;
136              
137 0           $self->app->log->debug("Creating new user $user with password $password");
138              
139 0           my $salt = $self->app->config->{auth}->{salt};
140 0           my $cost = $self->app->config->{auth}->{cost};
141              
142 0           my $bcrypt = Digest::Bcrypt->new;
143 0           $bcrypt->salt($salt);
144 0           $bcrypt->cost($cost);
145 0           $bcrypt->add($password);
146              
147 0           my $pw = $bcrypt->hexdigest;
148              
149 0 0         open( my $fh, ">>", $self->app->config->{auth}->{passwd} ) or die($!);
150 0           print $fh "$user:$pw\n";
151 0           close($fh);
152              
153             }
154              
155             sub create_upstart_unit {
156 0     0     my ($self) = @_;
157              
158 0           my $pid_file = $self->app->config->{hypnotoad}->{pid_file};
159              
160 0           my $whereis_hypnotoad = qx{which hypnotoad};
161 0           chomp $whereis_hypnotoad;
162              
163 0 0         open(my $fh, ">", "/etc/init/rex-jobcontrol.conf") or die ($!);
164 0           print $fh qq~# Rex::JobControl
165              
166             description "Rex::JobControl upstart job"
167              
168             start on runlevel [2345]
169             stop on runlevel [!2345]
170              
171             pre-start exec $whereis_hypnotoad $0
172             post-stop exec $whereis_hypnotoad $0 -s
173             ~;
174 0           close($fh);
175              
176 0 0         open(my $fh, ">", "/etc/init/rex-jobcontrol-minion.conf") or die ($!);
177 0           print $fh qq~# Rex::JobControl Minion
178              
179             description "Rex::JobControl Minion upstart job"
180              
181             start on runlevel [2345]
182             stop on runlevel [!2345]
183              
184             exec /usr/local/bin/rex_job_control minion worker
185             ~;
186 0           close($fh);
187              
188             }
189              
190             sub create_systemd_unit {
191 0     0     my ($self) = @_;
192              
193 0           my $pid_file = $self->app->config->{hypnotoad}->{pid_file};
194              
195 0           my $whereis_hypnotoad = qx{which hypnotoad};
196 0           chomp $whereis_hypnotoad;
197              
198 0 0         open(my $fh, ">", "/lib/systemd/system/rex-jobcontrol.service") or die ($!);
199 0           print $fh qq~[Unit]
200             Description=Rex JobControl Server
201             After=network.target
202              
203             [Service]
204             Type=simple
205             SyslogIdentifier=rex-jobcontrol
206             PIDFile=$pid_file
207             ExecStart=$whereis_hypnotoad -f $0
208             ExecStop=$whereis_hypnotoad -s $0
209             ExecReload=$whereis_hypnotoad $0
210             ~;
211 0           close($fh);
212              
213 0 0         open(my $fh_m, ">", "/lib/systemd/system/rex-jobcontrol-minion.service") or die ($!);
214 0           print $fh_m qq~[Unit]
215             Description=Rex JobControl Minion
216             After=network.target
217              
218             [Service]
219             Type=simple
220             SyslogIdentifier=rex-jobcontrol-minion
221             ExecStart=$0 minion worker
222             ~;
223 0           close($fh_m);
224              
225             }
226              
227             1;