aboutsummaryrefslogtreecommitdiff
blob: 3665abcd98d7014b16a59605830d4b41d7e380c0 (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
<?php

	// Gentoaster build daemon worker
	// Licensed under GPL v3, see COPYING file

	$configurations_path = "/var/www/gentoaster";
	$gentoaster_path = "/usr/share/gentoaster";
	$tool_name = "create_image.sh";

	// DO NOT EDIT BELOW THIS LINE

	$progress_magic = 23;

	$worker = new GearmanWorker();
	$worker->addServer();
	$worker->addFunction("invoke_image_build", "image_build");
	while ($worker->work());

	function update_result($handle, $returncode, $result) {
		$result = trim($result);
		echo "A job finished with return code ".$returncode.": ".$result."\n";
	        $db = mysql_connect("localhost","gentoaster","");
        	if(!$db) die("Could not connect to database ".mysql_error());
        	mysql_select_db("gentoaster");
		mysql_query("UPDATE builds SET result = '".mysql_real_escape_string($result)."', returncode = '".$returncode."' WHERE handle = '".mysql_real_escape_string($handle)."'");
		return serialize(array($returncode, $result));
	}

	function image_build($job) {
		global $configurations_path, $gentoaster_path, $tool_name, $progress_magic;

		$handle = $job->handle();
		$handlehash = md5($handle);

		echo "Processing job handle hash ".$handlehash."\n";

		$configuration_string = $job->workload();
		$configuration_array = parse_ini_string($configuration_string);

		if($configuration_array !== FALSE && isset($configuration_array["BUILD_ID"])) {
			$build_id = $configuration_array["BUILD_ID"];
			$build_path = $configurations_path."/".$build_id;
			@mkdir($build_path, 0777, true);

			if(is_writable($build_path)) {
				chdir($build_path);
				file_put_contents("config.ini", $configuration_string);
				$tool_args = "--config config.ini";
				$process_handle = popen($gentoaster_path."/".$tool_name." ".$tool_args." 2>&1", "r");

				$nonstatus_output = "";

				while(!feof($process_handle)) {
					$progress_line = fgets($process_handle);
					preg_match("/Step (.+):/", $progress_line, $matches);
					if(sizeof($matches) > 0) {
						$job->sendStatus($matches[1], $progress_magic);
					} else {
						$nonstatus_output .= $progress_line;
					}
				}

				$returncode = pclose($process_handle);

				unlink("config.ini");

				return update_result($handle, $returncode, $nonstatus_output);
			} else {
				return update_result($handle, -2, "Configured build path is not writable");
			}
		} else {
			return update_result($handle, -3, "Configuration string is not valid");
		}
	}

?>