| 1 | # | ||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | package Rex::Cron::Base; | ||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 2 | ▶ | use v5.14.4; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 2 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 2 | ▶ | use Rex::Logger; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 2 | ▶ | use Rex::Commands; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 2 | ▶ | use Rex::Commands::File; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 2 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 2 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 2 | ▶ | use Rex::Helper::Path; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 2 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||
| 21 | 2 | my $that = shift; | |||||||||||||||||||||
| 22 | 2 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||
| 23 | 2 | my $self = {@_}; | |||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 2 | bless( $self, $proto ); | |||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 2 | return $self; | |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 6 | ▶ | sub list { | ||||||||||||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 31 | 6 | my ($self) = @_; | |||||||||||||||||||||
| 32 | 6 | return @{ $self->{cron} }; | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | 4 | ▶ | sub list_jobs { | ||||||||||||||||||||
|
Subroutine
list_jobs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 36 | 4 | my ($self) = @_; | |||||||||||||||||||||
| 37 | 4 | my @jobs = @{ $self->{cron} }; | |||||||||||||||||||||
| 38 | my @ret = | ||||||||||||||||||||||
| 39 | 25 | map { { line => $_->{line}, %{ $_->{cron} } } } | |||||||||||||||||||||
| 40 | 4 | grep { $_->{type} eq "job" } @jobs; | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 6 | ▶ | sub list_envs { | ||||||||||||||||||||
|
Subroutine
list_envs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 44 | 6 | my ($self) = @_; | |||||||||||||||||||||
| 45 | 6 | my @jobs = @{ $self->{cron} }; | |||||||||||||||||||||
| 46 | 6 | my @ret = grep { $_->{type} eq "env" } @jobs; | |||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 4 | ▶ | sub add { | ||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||
| 50 | 4 | my ( $self, %config ) = @_; | |||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 4 | %config = $self->_create_defaults(%config); | |||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | my $new_cron = sprintf( | ||||||||||||||||||||||
| 55 | "%s %s %s %s %s %s", | ||||||||||||||||||||||
| 56 | $config{"minute"}, $config{"hour"}, $config{"day_of_month"}, | ||||||||||||||||||||||
| 57 | 4 | $config{"month"}, $config{"day_of_week"}, $config{"command"}, | |||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 4 | my $dupe = grep { $_->{line} eq $new_cron } @{ $self->{cron} }; | |||||||||||||||||||||
| 61 | 4 | ▶ | if ($dupe) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | 0 | Rex::Logger::debug("Job \"$new_cron\" already installed, skipping."); | |||||||||||||||||||||
| 63 | 0 | return 0; | |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | push( | ||||||||||||||||||||||
| 67 | 4 | @{ $self->{cron} }, | |||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||
| 69 | type => "job", | ||||||||||||||||||||||
| 70 | line => $new_cron, | ||||||||||||||||||||||
| 71 | cron => \%config, | ||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | 4 | return 1; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 2 | ▶ | sub add_env { | ||||||||||||||||||||
|
Subroutine
add_env: called CC=5 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||
| 79 | 2 | my ( $self, $name, $value ) = @_; | |||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 2 | my $env_index = 0; | |||||||||||||||||||||
| 82 | 2 | my $exists = 0; | |||||||||||||||||||||
| 83 | 2 | for my $env ( $self->list_envs ) { | |||||||||||||||||||||
| 84 | 7 | ▶ | if ( $env->{name} eq "$name" ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $env->{value} ne "\"$value\"" ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 86 | 0 | Rex::Logger::debug("Environment variable changed : $name"); | |||||||||||||||||||||
| 87 | 0 | $self->delete_env($env_index); | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||
| 90 | 0 | Rex::Logger::debug( | |||||||||||||||||||||
| 91 | "Environment variable already exists with same value: $name=$value"); | ||||||||||||||||||||||
| 92 | 0 | $exists = 1; | |||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 7 | $env_index++; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 2 | ▶ | if ( $exists == 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | unshift( | ||||||||||||||||||||||
| 101 | 2 | @{ $self->{cron} }, | |||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||
| 103 | type => "env", | ||||||||||||||||||||||
| 104 | line => "$name=\"$value\"", | ||||||||||||||||||||||
| 105 | name => $name, | ||||||||||||||||||||||
| 106 | value => $value, | ||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 2 | ▶ | sub delete_job { | ||||||||||||||||||||
|
Subroutine
delete_job: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||
| 113 | 2 | my ( $self, $num ) = @_; | |||||||||||||||||||||
| 114 | 2 | my @jobs = $self->list_jobs; | |||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | 2 | my $i = 0; | |||||||||||||||||||||
| 117 | 2 | my $to_delete; | |||||||||||||||||||||
| 118 | 2 | for my $j ( @{ $self->{cron} } ) { | |||||||||||||||||||||
| 119 | 23 | ▶ | if ( $j->{line} eq $jobs[$num]->{line} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 120 | 2 | $to_delete = $i; | |||||||||||||||||||||
| 121 | 2 | last; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 21 | $i++; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 2 | ▶ | unless ( defined $to_delete ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 128 | 0 | die("Cron Entry $num not found."); | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 2 | $self->delete($to_delete); | |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | 2 | ▶ | sub delete_env { | ||||||||||||||||||||
|
Subroutine
delete_env: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||
| 135 | 2 | my ( $self, $num ) = @_; | |||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 2 | my @jobs = $self->list_envs; | |||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | 2 | my $i = 0; | |||||||||||||||||||||
| 140 | 2 | my $to_delete; | |||||||||||||||||||||
| 141 | 2 | for my $j ( @{ $self->{cron} } ) { | |||||||||||||||||||||
| 142 | 3 | ▶ | if ( $j->{line} eq $jobs[$num]->{line} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 143 | 2 | $to_delete = $i; | |||||||||||||||||||||
| 144 | 2 | last; | |||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 1 | $i++; | |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||
| 149 | |||||||||||||||||||||||
| 150 | 2 | ▶ | unless ( defined $to_delete ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 151 | 0 | die("Cron Entry $num not found."); | |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 2 | $self->delete($to_delete); | |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 6 | ▶ | sub delete { | ||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 158 | 6 | my ( $self, $num ) = @_; | |||||||||||||||||||||
| 159 | 6 | splice( @{ $self->{cron} }, $num, 1 ); | |||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | # returns a filename where the new cron is written to | ||||||||||||||||||||||
| 163 | # after that the cronfile must be activated | ||||||||||||||||||||||
| 164 | 1 | ▶ | sub write_cron { | ||||||||||||||||||||
|
Subroutine
write_cron: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 165 | 1 | my ($self) = @_; | |||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | 1 | my $rnd_file = get_tmp_file; | |||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | 1 | my @lines = map { $_->{line} } @{ $self->{cron} }; | |||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | 1 | my $fh = file_write $rnd_file; | |||||||||||||||||||||
| 172 | 1 | $fh->write( join( "\n", @lines ) . "\n" ); | |||||||||||||||||||||
| 173 | 1 | $fh->close; | |||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | 1 | return $rnd_file; | |||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | 0 | ▶ | sub activate_user_cron { | ||||||||||||||||||||
|
Subroutine
activate_user_cron: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||
| 179 | 0 | my ( $self, $file, $user ) = @_; | |||||||||||||||||||||
| 180 | 0 | ▶ | $user = undef if $user eq &_whoami; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 0 | my $command = 'crontab'; | |||||||||||||||||||||
| 183 | 0 | ▶ | $command .= " -u $user" if defined $user; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 184 | |||||||||||||||||||||||
| 185 | 0 | i_run "$command $file"; | |||||||||||||||||||||
| 186 | 0 | unlink $file; | |||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | 0 | ▶ | sub read_user_cron { | ||||||||||||||||||||
|
Subroutine
read_user_cron: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||
| 190 | 0 | my ( $self, $user ) = @_; | |||||||||||||||||||||
| 191 | 0 | ▶ | $user = undef if $user eq &_whoami; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 192 | |||||||||||||||||||||||
| 193 | 0 | my $command = 'crontab -l'; | |||||||||||||||||||||
| 194 | 0 | ▶ | $command .= " -u $user" if defined $user; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 195 | 0 | $command .= ' 2> /dev/null'; | |||||||||||||||||||||
| 196 | |||||||||||||||||||||||
| 197 | 0 | my @lines = i_run $command, fail_ok => 1; | |||||||||||||||||||||
| 198 | 0 | $self->parse_cron(@lines); | |||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||
| 200 | |||||||||||||||||||||||
| 201 | 2 | ▶ | sub parse_cron { | ||||||||||||||||||||
|
Subroutine
parse_cron: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||
| 202 | 2 | my ( $self, @lines ) = @_; | |||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | 2 | chomp @lines; | |||||||||||||||||||||
| 205 | |||||||||||||||||||||||
| 206 | 2 | my @cron; | |||||||||||||||||||||
| 207 | |||||||||||||||||||||||
| 208 | 2 | for my $line (@lines) { | |||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | # comment | ||||||||||||||||||||||
| 211 | 32 | ▶ | if ( $line =~ m/^#/ ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 212 | 12 | push( | |||||||||||||||||||||
| 213 | @cron, | ||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||
| 215 | type => "comment", | ||||||||||||||||||||||
| 216 | line => $line, | ||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | # empty line | ||||||||||||||||||||||
| 222 | elsif ( $line =~ m/^\s*$/ ) { | ||||||||||||||||||||||
| 223 | 0 | push( | |||||||||||||||||||||
| 224 | @cron, | ||||||||||||||||||||||
| 225 | { | ||||||||||||||||||||||
| 226 | type => "empty", | ||||||||||||||||||||||
| 227 | line => $line, | ||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||
| 231 | |||||||||||||||||||||||
| 232 | # job | ||||||||||||||||||||||
| 233 | elsif ( $line =~ m/^(@|\*|[0-9])/ ) { | ||||||||||||||||||||||
| 234 | 14 | my ( $min, $hour, $day, $month, $dow, $cmd ) = split( /\s+/, $line, 6 ); | |||||||||||||||||||||
| 235 | 14 | push( | |||||||||||||||||||||
| 236 | @cron, | ||||||||||||||||||||||
| 237 | { | ||||||||||||||||||||||
| 238 | type => "job", | ||||||||||||||||||||||
| 239 | line => $line, | ||||||||||||||||||||||
| 240 | cron => { | ||||||||||||||||||||||
| 241 | minute => $min, | ||||||||||||||||||||||
| 242 | hour => $hour, | ||||||||||||||||||||||
| 243 | day_of_month => $day, | ||||||||||||||||||||||
| 244 | month => $month, | ||||||||||||||||||||||
| 245 | day_of_week => $dow, | ||||||||||||||||||||||
| 246 | command => $cmd, | ||||||||||||||||||||||
| 247 | }, | ||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||
| 249 | ); | ||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||
| 251 | |||||||||||||||||||||||
| 252 | elsif ( $line =~ m/=/ ) { | ||||||||||||||||||||||
| 253 | 6 | my ( $name, $value ) = split( /=/, $line, 2 ); | |||||||||||||||||||||
| 254 | 6 | $name =~ s/^\s+//; | |||||||||||||||||||||
| 255 | 6 | $name =~ s/\s+$//; | |||||||||||||||||||||
| 256 | 6 | $value =~ s/^\s+//; | |||||||||||||||||||||
| 257 | 6 | $value =~ s/\s+$//; | |||||||||||||||||||||
| 258 | |||||||||||||||||||||||
| 259 | 6 | push( | |||||||||||||||||||||
| 260 | @cron, | ||||||||||||||||||||||
| 261 | { | ||||||||||||||||||||||
| 262 | type => "env", | ||||||||||||||||||||||
| 263 | line => $line, | ||||||||||||||||||||||
| 264 | name => $name, | ||||||||||||||||||||||
| 265 | value => $value, | ||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||
| 269 | |||||||||||||||||||||||
| 270 | else { | ||||||||||||||||||||||
| 271 | 0 | Rex::Logger::debug("Error parsing cron line: $line"); | |||||||||||||||||||||
| 272 | 0 | next; | |||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||
| 274 | |||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||
| 276 | |||||||||||||||||||||||
| 277 | 2 | $self->{cron} = \@cron; | |||||||||||||||||||||
| 278 | 2 | return @cron; | |||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||
| 280 | |||||||||||||||||||||||
| 281 | 4 | ▶ | sub _create_defaults { | ||||||||||||||||||||
|
Subroutine
_create_defaults: called CC=7 SCAR=19.5
| |||||||||||||||||||||||
| 282 | 4 | my ( $self, %config ) = @_; | |||||||||||||||||||||
| 283 | |||||||||||||||||||||||
| 284 | 4 | ▶ | $config{"minute"} = "*" unless defined $config{minute}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 285 | 4 | ▶ | $config{"hour"} = "*" unless defined $config{hour}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 286 | 4 | ▶ | $config{"day_of_month"} = "*" unless defined $config{day_of_month}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 287 | 4 | ▶ | $config{"month"} = "*" unless defined $config{month}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 288 | 4 | ▶ | $config{"day_of_week"} = "*" unless defined $config{day_of_week}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 289 | 4 | ▶ | $config{"command"} ||= "false"; | ||||||||||||||||||||
|
Condition100%2/2
$config{"command"} ||= "false"
MC/DC100%1/1
$config{"command"} ||= "false"
$config{"command"}
Truth table100%2/2
A
A= $config{"command"}
| |||||||||||||||||||||||
| 290 | |||||||||||||||||||||||
| 291 | 4 | return %config; | |||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||
| 293 | |||||||||||||||||||||||
| 294 | 0 | ▶ | sub _whoami { | ||||||||||||||||||||
|
Subroutine
_whoami: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 295 | 0 | return i_run q(perl -e 'print scalar getpwuid($<)'); | |||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||
| 297 | |||||||||||||||||||||||
| 298 | 2 | 1; | |||||||||||||||||||||