Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 206 lines (161 sloc) 4.562 kb
ab7a69c @perusionmike * Add new cron-style facility for determining HouseKeeping jobs.
perusionmike authored
1 #!/usr/local/bin/perl
2 ##!~_~perlpath~_~
3 #
4 # Interchange cron editor
5 #
07f71a7 @pajamian New Free Software Foundation Address in headers of various files
pajamian authored
6 # $Id: crontab.PL,v 2.2 2007-03-30 11:39:56 pajamian Exp $
ab7a69c @perusionmike * Add new cron-style facility for determining HouseKeeping jobs.
perusionmike authored
7 #
8 # Copyright (C) 2005 Interchange Development Group
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public
21 # License along with this program; if not, write to the Free
07f71a7 @pajamian New Free Software Foundation Address in headers of various files
pajamian authored
22 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 # MA 02110-1301 USA.
ab7a69c @perusionmike * Add new cron-style facility for determining HouseKeeping jobs.
perusionmike authored
24
25 use lib '/usr/local/interchange/lib';
26 #use lib '~_~INSTALLPRIVLIB~_~';
27 use lib '/usr/local/interchange';
28 #use lib '~_~INSTALLARCHLIB~_~';
29
30 BEGIN {
31
32 ($Global::VendRoot = $ENV{MINIVEND_ROOT})
33 if defined $ENV{MINIVEND_ROOT};
34 $Global::VendRoot = $Global::VendRoot || '/usr/local/interchange';
35 # $Global::VendRoot = $Global::VendRoot || '~_~INSTALLARCHLIB~_~';
36
37 if(-f "$Global::VendRoot/interchange.cfg") {
38 $Global::ExeName = 'interchange';
39 $Global::ConfigFile = 'interchange.cfg';
40 }
41 else {
42 $Global::ExeName = 'interchange';
43 $Global::ConfigFile = 'interchange.cfg';
44 }
45
46 }
47
48 ### END CONFIGURATION VARIABLES
49
50 use POSIX qw/tmpnam/;
51 use Vend::Cron;
52
53 my $prospect = tmpnam();
54
55 use File::Copy;
56 use Term::ReadLine;
57 use FindBin;
58
59 my $term = new Term::ReadLine 'Simple';
60
61 use Safe;
62 my $safe = new Safe;
63
64 my $configstring = `$FindBin::Bin/interchange -globalconfig`;
65
66 chdir $Global::VendRoot
67 or die "Unable to chdir to $Global::VendRoot: $!\n";
68
69 my $Global = $safe->reval($configstring)
70 or die "Unable to read configuration via $FindBin::Bin/interchange.\n";
71
72
73 my $hupit = 1;
74
75 if(! $Global->{HouseKeepingCron}) {
76 warn "No HouseKeepingCron is defined, this will do nothing.\n";
77 warn "Add:\n\n\tHouseKeepingCron <crontab\n\nto interchange.cfg to activate.\n";
78 undef $hupit;
79 }
80
81 $Vend::Quiet = 1;
82
83 my $file = "$Global->{ConfigDir}/crontab";
84
85 #warn "Cron file is $file\n";
86
87 if(-f $file) {
88 File::Copy::copy($file, $prospect);
89 }
90
91 my $ed = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
92
93 my @needed = qw/
94 :restart
95 :reconfig
96 :jobs
97 /;
98
99 EDCRON: {
100 system "$ed $prospect";
101
102 my $status = `diff $prospect $file`;
103
104 unless($?) {
105 warn "Crontab unchanged.\n";
106 exit;
107 }
108
109 open NEWCRON, "< $prospect"
110 or die "Cannot open prospective cron file: $!\n";
111
112 local($/);
113
114 my $lines = <NEWCRON>;
115 close NEWCRON;
116
117 #warn "Read lines (" . length($lines) . " bytes)\n";
118
119 my $obj;
120 eval {
121 $obj = Vend::Cron::read_cron($lines);
122 };
123
124 if(! $obj or $@) {
125 print "Cron file problem: $@\n";
126 my $prompt = "Retry? [y]";
127 my $ans = $term->readline($prompt);
128 if($ans =~ /^\s*n/) {
129 last EDCRON;
130 }
131 else {
132 redo EDCRON;
133 }
134 }
135
136 my %wanted = qw/ :reconfig 1 :jobs 1 /;
137 for(@$obj) {
138 my $things = $_->{things};
139 for(@$things) {
140 next unless $wanted{$_};
141 delete $wanted{$_};
142 }
143 }
144
145 my @errmsg;
146 for(keys %wanted) {
147 push @errmsg, sprintf("WARNING: suggested cron entry '%s' not present.", $_);
148 }
149
150 if(@errmsg) {
151 print join "\n", @errmsg, '';
152 my $prompt = "Retry? [y]";
153 my $ans = $term->readline($prompt);
154 unless ($ans =~ /^\s*n/) {
155 redo EDCRON;
156 }
157 }
158
159
160 #warn "read_cron returned $obj.\n";
161
162 File::Copy::copy($prospect, $file);
163 print "Wrote crontab file $file.\n";
164 #warn "copied $prospect to $file\n";
165
166 unlink $prospect;
167 if($hupit) {
168
169 open CRON, "< $file"
170 or die "Can't read cron file $file: $!\n";
171 my $lines = <CRON>;
172 close CRON;
173
174 my $rsfile = "$Global->{RunDir}/restart";
175 open RESTART, ">> $rsfile"
176 or die "Cannot write restart file: $!\n";
177 print RESTART "HouseKeepingCron <<EndOfCrontab\n";
178 print RESTART $lines;
179 print RESTART "\nEndOfCrontab\n";
180 close RESTART;
181 #warn "created restart file\n";
182
183 my $pidfile = $Global->{PIDfile};
184 unless (-f $pidfile) {
185 warn "Interchange not running, cannot tell to reread ($pidfile).\n";
186 }
187 open PID, "< $pidfile"
188 or die "Cannot read PID file $pidfile: $!\n";
189
190 my $pid = <PID>;
191 close PID;
192 $pid =~ s/\s+.*//s;
193 $pid =~ s/\D+//g;
194 #warn "Found pid=$pid\n";
195 chomp $pid;
196 if ($pid) {
197 kill 'HUP', $pid;
198 print "Sent HUP signal to Interchange server at $pid.\n";
199 }
200 else {
201 die "Unable to find pid at $pidfile.\n";
202 }
203 }
204 }
205
Something went wrong with that request. Please try again.