summaryrefslogtreecommitdiff
blob: 5d5fb6bc1cd83524782b97a433041dbf4b7de811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/php
<?php
require_once(dirname(__FILE__).'/../shared/include/includes.php'); // USE __DIR__ once 5.3.0 is out (and 2 lines down)
require_once(BACKEND.'/include/signals.php');
declare(ticks=1);
$pidfile='/var/run/ingenue.pid'; // Doesn't work when not run as root
$opts=getopt('fk');
if (isset($opts['f'])) {
	$f=pcntl_fork();
	switch($f) {
	case -1:
		die("Failed to fork");
	case 0:
		$conf['debug']=false;
		break;
	default:
		die();
	}
}
if (isset($opts['k'])) {
	if (is_file($pidfile)) {
		$pid=trim(file_get_contents($pidfile));
		if (posix_kill($pid, 0)) {
			debug("Sending SIGTERM to $pid");
			if (!posix_kill($pid, SIGTERM)) {
				debug("Failed to send SIGTERM to $pid");
				die(1);
			}
		} else {
			debug("Couldn't send signal 0 to $pid");
			die(1);
		}
	} else {
		debug('No PID file found');
	}
	die();
}
if (is_file($pidfile)) {
	$pid=trim(file_get_contents($pidfile));
	if (posix_kill($pid, 0))
		die("Found already running backend PID=$pid.\n");
}
if (posix_geteuid() !== 0)
	debug(STDERR, "Not running as root... this is not going to accomplish much.");
if (file_put_contents($pidfile, posix_getpid()))
	$unlinkpidfile=true;
require_once(SHARED.'/include/dbinit.php');
while (true) {
	// TODO check first for builds that need to be resumed (and figure out how to resume things)
	$r=$S['pdo']->query('SELECT * FROM `builds` WHERE `status`="build/ready" ORDER BY `ctime` ASC LIMIT 1');
	if ($r->rowCount()) {
		$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
		$build->start=time();
		$build->status='build/running';
		$build->write();
		log_msg('Starting build id='.$build->id);
		$image=null;
		try {
			$image=build($build);
		} catch (Exception $e) {
			log_msg('Caught exception: '.$e->getMessage());
			$build->status='finished/failed: '.$e->getMessage();
		}
		$build->finish=time();
		log_msg('Finished with build id='.$build->id);
		if (isset($image)) {
			log_msg("Completed image at $image");
			$build->status='finished/success';
		}
		$build->write();
		unset($build);
	}
	log_msg('Sleeping...', false);
	sleep(5);
	log_msg("done");
}
?>