| 1 | package Test::Smoke::App::ConfigSmoke::Scheduler; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.002'; | ||
| 6 | |||
| 7 | use Exporter 'import'; | ||
| 8 | our @EXPORT = qw/ | ||
| 9 | config_scheduler | ||
| 10 | schedule_entry_ms_schtasks | ||
| 11 | schedule_entry_ms_at | ||
| 12 | schedule_entry_crontab | ||
| 13 | query_entry_ms_schtasks | ||
| 14 | /; | ||
| 15 | |||
| 16 | use Test::Smoke::App::Options; | ||
| 17 | use Test::Smoke::Util::FindHelpers 'whereis'; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Test::Smoke::App::ConfigSmoke::Scheduler - Mixin for L<Test::Smoke::App::ConfigSmoke> | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | These methods will be added to the L<Test::Smoke::App::ConfigSmoke> class. | ||
| 26 | |||
| 27 | =head2 config_scheduler | ||
| 28 | |||
| 29 | Configure options C<crontime>, C<add2cron> (and temporarily C<cronbin> and C<docron>). | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub config_scheduler { | ||
| 34 | my $self = shift; | ||
| 35 | return if $^O eq 'VMS'; | ||
| 36 | |||
| 37 | print "\n-- Scheduler section --\n"; | ||
| 38 | |||
| 39 | my ($cronbin, $has_crond) = get_avail_scheduler(); | ||
| 40 | |||
| 41 | if (not $cronbin) { | ||
| 42 | print "!!!!!\nCannot find a scheduler.\n!!!!!\n"; | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->current_values->{cronbin} = $cronbin; | ||
| 47 | my $has_crontime = $self->default_for_option(crontime_option()); | ||
| 48 | my $docron = $self->handle_option(docron_option($cronbin, $has_crontime)); | ||
| 49 | return unless $docron; | ||
| 50 | |||
| 51 | my $crontime = $self->handle_option(crontime_option()); | ||
| 52 | |||
| 53 | my @current_cron; | ||
| 54 | if ($^O eq 'MSWin32') { | ||
| 55 | $self->{_smoke_script} = $self->prefix . '.cmd'; | ||
| 56 | $self->{_smoke_script_abs} = Cwd::abs_path($self->smoke_script); | ||
| 57 | if ($cronbin =~ m{schtasks}i) { | ||
| 58 | my $task_name = sprintf("P5Smoke-%s", $self->prefix); | ||
| 59 | my @old_schedule = qx{"$cronbin" /nh /tn "$task_name" 2>&1}; | ||
| 60 | my ($curr_shedule) = grep { m{\Q$task_name\E} } @old_schedule; | ||
| 61 | |||
| 62 | my $new_entry = $self->schedule_entry_ms_schtasks($cronbin, $crontime); | ||
| 63 | if ($curr_shedule) { | ||
| 64 | printf "\n!!! I see you already have:\n\t%s\n", $curr_shedule; | ||
| 65 | $new_entry .= " /F"; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $add2cron = $self->handle_option(add2cron_option($new_entry)); | ||
| 69 | |||
| 70 | system $new_entry if $add2cron; | ||
| 71 | } | ||
| 72 | elsif (open(my $crontab, "$cronbin |")) { | ||
| 73 | @current_cron = <$crontab>; | ||
| 74 | close($crontab) or warn "Error reading schedule: $!\n"; | ||
| 75 | |||
| 76 | @current_cron = grep { m{^\s+\d+\s+.+\d+:\d+\s/} } @current_cron; | ||
| 77 | |||
| 78 | my $new_entry = $self->schedule_entry_ms_at($cronbin, $crontime); | ||
| 79 | my $add2cron = $self->handle_option(add2cron_option($new_entry)); | ||
| 80 | |||
| 81 | system $new_entry if $add2cron; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | print "!!!!!\nError reading current schedual(fork): $!\n!!!!!\n"; | ||
| 85 | print "Please, fix this yourself.\n"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | my $jcl = $self->{_smoke_script} = $self->prefix . '.sh'; | ||
| 90 | $self->{_smoke_script_abs} = Cwd::abs_path($self->smoke_script); | ||
| 91 | if (open(my $crontab, "$cronbin -l |")) { | ||
| 92 | @current_cron = <$crontab>; | ||
| 93 | close($crontab) or warn "Error reading schedule: $!\n"; | ||
| 94 | |||
| 95 | if ( "@current_cron" =~ m{^# DO NOT EDIT THIS FILE} ) { | ||
| 96 | splice @current_cron, 0, 3; | ||
| 97 | } | ||
| 98 | |||
| 99 | my $new_entry = $self->schedule_entry_crontab($cronbin, $crontime); | ||
| 100 | @current_cron = grep { $_ !~ m{^$new_entry$} } @current_cron; | ||
| 101 | s{^ (?<!\#) \s* (.+?(?:$jcl)) }{# $1}x for @current_cron; | ||
| 102 | |||
| 103 | my $cronout_file = $self->prefix . '.crontab'; | ||
| 104 | if (open(my $cronout, '>', $cronout_file)) { | ||
| 105 | print {$cronout} $_ for @current_cron; | ||
| 106 | print {$cronout} "\n# Test::Smoke\n$new_entry\n"; | ||
| 107 | close($cronout); | ||
| 108 | print "\n >> Created '$cronout_file'.\n"; | ||
| 109 | |||
| 110 | my $add2cron = $self->handle_option(add2cron_option($new_entry)); | ||
| 111 | system($cronbin, $cronout_file) if $add2cron; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | print "!!!!!\nError creating($cronout_file): $!\n!!!!!\n"; | ||
| 115 | print "Please, fix this yourself.\n"; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | print "!!!!!\nError reading current schedual(fork): $!\n!!!!!\n"; | ||
| 120 | print "Please, fix this yourself.\n"; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 get_avail_scheduler | ||
| 126 | |||
| 127 | Looks for F<at.exe> or F<schtasks.exe> on C<MSWin32> or F<cron(tab)> on other systems. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub get_avail_scheduler { | ||
| 132 | my( $scheduler, $has_crond ); | ||
| 133 | |||
| 134 | if ( $^O eq 'MSWin32' ) { # We're looking for 'SchTasks.exe' or 'at.exe' | ||
| 135 | $scheduler = whereis( 'schtasks') || whereis( 'at' ); | ||
| 136 | } | ||
| 137 | else { # We're looking for 'crontab' or 'cron' | ||
| 138 | $scheduler = whereis( 'crontab' ) || whereis( 'cron' ); | ||
| 139 | ( $has_crond ) = grep /\bcrond?\b/ => `ps -e`; | ||
| 140 | } | ||
| 141 | return ( $scheduler, $has_crond ); | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 schedule_entry_ms_schtasks | ||
| 145 | |||
| 146 | Return the command to create a new entry for a MS-C<SchTasks> task. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub schedule_entry_ms_schtasks { | ||
| 151 | my $self = shift; | ||
| 152 | my ($cron, $crontime) = @_; | ||
| 153 | my $script = $self->smoke_script_abs; | ||
| 154 | |||
| 155 | return '' unless $crontime; | ||
| 156 | |||
| 157 | my $tn = "P5Smoke-" . $self->prefix; | ||
| 158 | return sprintf( | ||
| 159 | qq[%s /Create /SC DAILY /ST %s /TN %s /TR "%s"], | ||
| 160 | $cron, $crontime, $tn, $script | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 query_entry_ms_schtasks { | ||
| 165 | |||
| 166 | Return the command to query the scheduler. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub query_entry_ms_schtasks { | ||
| 171 | my $self = shift; | ||
| 172 | my ($cronbin) = @_; | ||
| 173 | my $tn = "P5Smoke-" . $self->prefix; | ||
| 174 | |||
| 175 | return sprintf(qq<"%s" /Query /TN %s /V /FO list>, $cronbin, $tn); | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 schedule_entry_ms_at | ||
| 179 | |||
| 180 | Return the command to create a new entry for MS-C<AT>. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub schedule_entry_ms_at { | ||
| 185 | my $self = shift; | ||
| 186 | my ($cron, $crontime) = @_; | ||
| 187 | my $script = $self->smoke_script_abs; | ||
| 188 | |||
| 189 | return '' unless $crontime; | ||
| 190 | my ($hour, $min) = $crontime =~ /(\d+):(\d+)/; | ||
| 191 | |||
| 192 | |||
| 193 | return sprintf( | ||
| 194 | qq[$cron %02d:%02d /EVERY:M,T,W,Th,F,S,Su "%s"], | ||
| 195 | $hour, $min, $script | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 schedule_entry_crontab | ||
| 200 | |||
| 201 | Return an entry for C<crontab(5)> (3 stars) | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub schedule_entry_crontab { | ||
| 206 | my $self = shift; | ||
| 207 | my ($cron, $crontime) = @_; | ||
| 208 | |||
| 209 | return '' unless $crontime; | ||
| 210 | my ($hour, $min) = $crontime =~ /(\d+):(\d+)/; | ||
| 211 | |||
| 212 | return sprintf(qq[%02d %02d * * * '%s'], $min, $hour, $self->smoke_script_abs); | ||
| 213 | } | ||
| 214 | |||
| 215 | =head2 docron_option | ||
| 216 | |||
| 217 | This option C<docron> is not in the config-file, but is only needed to continue | ||
| 218 | on the scheduler path. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub docron_option { | ||
| 223 | my ($scheduler, $schedule_time) = @_; | ||
| 224 | return Test::Smoke::App::AppOption->new( | ||
| 225 | name => 'docron', | ||
| 226 | allow => undef, | ||
| 227 | default => 0, | ||
| 228 | helptext => "I see you have '$scheduler'\n", | ||
| 229 | configtext => 'Should the smoke be scheduled?', | ||
| 230 | configtype => 'prompt_yn', | ||
| 231 | configalt => sub { [qw/ N y /] }, | ||
| 232 | configdft => sub { defined($schedule_time) ? 'Y' : 'N' }, | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | |||
| 236 | =head2 crontime_option | ||
| 237 | |||
| 238 | This option C<crontime> will be in the config-file, but only as a reminder. | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub crontime_option { | ||
| 243 | return Test::Smoke::App::AppOption->new( | ||
| 244 | name => 'crontime', | ||
| 245 | helptext => 'At what time should the smoke be scheduled?', | ||
| 246 | configdft => sub { '22:25' }, | ||
| 247 | chk => '(?:random|(?:[012]?\d:[0-5]?\d))', | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | |||
| 251 | =head2 add2cron_option | ||
| 252 | |||
| 253 | This option C<add2cron> will not be in the config-file. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub add2cron_option { | ||
| 258 | my ($new_entry) = @_; | ||
| 259 | my $df = $new_entry =~ m{schtasks}i | ||
| 260 | ? $new_entry =~ m{/F$} | ||
| 261 | ? 'N' | ||
| 262 | : 'Y' | ||
| 263 | : 'Y'; | ||
| 264 | my %ndf = ( N => 'y', Y => 'n' ); | ||
| 265 | return Test::Smoke::App::AppOption->new( | ||
| 266 | name => 'add2cron', | ||
| 267 | configtext => "Add this line to your schedule?\n\t$new_entry\n", | ||
| 268 | configtype => 'prompt_yn', | ||
| 269 | configalt => sub { [ $df, $ndf{$df}] }, | ||
| 270 | configdft => sub { $df }, | ||
| 271 | ); | ||
| 272 | } | ||
| 273 | |||
| 274 | 1; | ||
| 275 | |||
| 276 | =head1 COPYRIGHT | ||
| 277 | |||
| 278 | (c) 2020, All rights reserved. | ||
| 279 | |||
| 280 | * Abe Timmerman <abeltje@cpan.org> | ||
| 281 | |||
| 282 | This library is free software; you can redistribute it and/or modify | ||
| 283 | it under the same terms as Perl itself. | ||
| 284 | |||
| 285 | See: | ||
| 286 | |||
| 287 | =over 4 | ||
| 288 | |||
| 289 | =item * L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 290 | |||
| 291 | =item * L<http://www.gnu.org/copyleft/gpl.html> | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | This program is distributed in the hope that it will be useful, | ||
| 296 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 297 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 298 | |||
| 299 | =cut | ||
| 300 |