Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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