Skip to content

Commit b0131df

Browse files
committed
- fix error handling in update-ssh-keys
- make runone exit with the exit code of the executed program - log stdout/stderr of all programs to /var/log/php-cron-box.log - log failed executions with error code to /var/log/php-cron-box.fail - don't swallow errors from named
1 parent 0b588d8 commit b0131df

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

cron-box

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22
# $Id$
33
# Run the cron for a box
44

5+
FAIL_LOGFILE=/var/log/php-cron-box.fail
6+
7+
LOGFILE=/var/log/php-cron-box.log
8+
9+
exec >> $LOGFILE
10+
exec 2>&1
11+
exec </dev/null
12+
13+
now() {
14+
date +%Y.%m.%d-%H.%M.%S
15+
}
16+
17+
log_fail() {
18+
SVC=$1
19+
ERR=$2
20+
21+
echo "`now` $SVC $ERR" >> $FAIL_LOGFILE
22+
}
23+
524
# pull in details for this box
625
. /local/systems/boxen/`hostname`
726

@@ -58,7 +77,14 @@ for svc in $WHAT ; do
5877
fi;
5978

6079
if test -x /local/systems/$svc ; then
80+
echo
81+
echo "`now` running $svc"
6182
runone /tmp/.$svc.runone.pid /local/systems/$svc
83+
ERR=$?
84+
85+
if test "$ERR" != "0"; then
86+
log_fail $SVC $ERR
87+
fi
6288
else
6389
echo "Something is wrong. /local/systems/$svc on `hostname` is not executable." \
6490
| mail -s "Broken cronjob on `hostname`" [email protected] -- [email protected]

process-zone-file

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ if php /local/systems/maintain-master-dns.php > $file ; then
66
if /usr/sbin/named-checkzone php.net $file ; then
77
cp -f $file /local/mirrors/php-master-web/fetch/php.net.zone
88
mv -f $file /var/named/php.net
9-
/etc/init.d/named restart 2>&1 >/dev/null;
9+
/etc/init.d/named restart
1010
fi
1111
fi

runone

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (-e $lockfile) {
1414
# only warn when the lock is more than an hour old
1515
print "already running, pid $pid\n"
1616
if (stat _)[9] < time() - 3600;
17-
exit;
17+
exit(99);
1818
}
1919
close FILE;
2020
}
@@ -25,4 +25,7 @@ print FILE $$;
2525
close FILE;
2626

2727
system @ARGV;
28+
my $err = $? >> 8; # perl wtf
2829
unlink $lockfile;
30+
31+
exit($err);

update-ssh-keys

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
#!/usr/bin/env php
22
<?php
3+
4+
function die1($msg)
5+
{
6+
fwrite(STDERR, $msg . "\n");
7+
exit(1);
8+
}
9+
10+
function run_system($cmd, $errmsg = "")
11+
{
12+
system($cmd, $exit);
13+
14+
if ($exit !== 0) {
15+
die1("failed: "
16+
. $errmsg
17+
. ' ('
18+
. $cmd
19+
. ')');
20+
}
21+
}
22+
323
/* Fetch SSH Keys from the master db and create [email protected]
424
* files and push them to the gitolite administration repository
525
*/
626
define('SSH_KEY_REPO', '/local/gitolite-admin/keydir');
727

828
mysql_connect('localhost', 'nobody')
9-
or die ('unable to connect to server');
29+
or die1 ('unable to connect to server');
1030
mysql_select_db('phpmasterdb')
11-
or die ('unable to select database');
31+
or die1 ('unable to select database');
1232

1333
$query = 'SELECT DISTINCT username, ssh_keys FROM users';
14-
$res = mysql_query($query) or die ('cannot query ssh keys');
34+
$res = mysql_query($query) or die1 ('cannot query ssh keys');
1535

1636
chdir(SSH_KEY_REPO)
17-
or die ('cannot change working directory');
37+
or die1 ('cannot change working directory');
1838

19-
false !== system('git pull origin master')
20-
or die ('cannot update repository');
39+
run_system('git pull origin master', 'pull failed');
2140

2241
#TODO: remove all keys first in order to remove unused keys!
2342

@@ -32,8 +51,7 @@ while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
3251
}
3352
}
3453

35-
false !== system('git add -u :/')
36-
or die ('cannot update git information');
54+
run_system('git add -u :/', 'cannot update git information');
3755

3856
foreach($ssh_keys as $keyinfo) {
3957
$filename = sprintf('%s@%s.pub',
@@ -43,16 +61,13 @@ while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
4361
$fp = fopen($filename, 'w+');
4462
fwrite($fp, trim($keyinfo['key']) . "\n");
4563
fclose($fp);
46-
false !== system('git add ' . $filename)
47-
or die ('cannot execute git');
64+
run_system('git add ' . $filename, 'cannot execute git');
4865
}
4966
}
5067

5168
if (system('git status --porcelain -uno')) {
52-
false !== system('git commit -m"Automatic update"')
53-
or die ('cannot commit');
54-
false !== system('git push origin master')
55-
or die ('cannot push');
69+
run_system('git commit -m"Automatic update"', 'cannot commit');
70+
run_system('git push origin master', 'cannot push');
5671
}
5772

5873
function get_ssh_keys($string) {

0 commit comments

Comments
 (0)