ofborg/builder.php

118 lines
3.5 KiB
PHP
Raw Normal View History

2017-10-29 21:10:26 +00:00
<?php
require __DIR__ . '/config.php';
use PhpAmqpLib\Message\AMQPMessage;
# define('AMQP_DEBUG', true);
$connection = rabbitmq_conn();
$channel = $connection->channel();
$channel->basic_qos(null, 1, true);
/*
TODO: move this to a Leader-side job
2017-10-29 21:10:26 +00:00
list($queueName, , ) = $channel->queue_declare('build-results',
false, true, false, false);
2017-10-29 21:14:06 +00:00
list($queueName, , ) = $channel->queue_declare('build-inputs-' . NIX_SYSTEM,
2017-10-29 21:10:26 +00:00
false, true, false, false);
$channel->queue_bind($queueName, 'build-jobs');
*/
$queueName = 'build-inputs-' . NIX_SYSTEM;
2017-10-29 21:10:26 +00:00
function runner($msg) {
echo "got a job!\n";
$body = json_decode($msg->body);
2017-11-06 17:37:59 +00:00
$in = $body->original_payload;
2017-10-29 21:10:26 +00:00
2017-10-29 21:14:06 +00:00
$co = new GHE\Checkout(WORKING_DIR, "builder");
2017-10-29 21:10:26 +00:00
$pname = $co->checkOutRef($in->repository->full_name,
$in->repository->clone_url,
$in->issue->number,
"origin/master"
);
$patch_url = $in->issue->pull_request->patch_url;
echo "Building $patch_url\n";
$co->applyPatches($pname, $patch_url);
if ($body->build_default) {
echo "building via nix-build .\n";
$cmd = 'NIX_PATH=nixpkgs=%s nix-build --argstr system %s --option restrict-eval true --keep-going .';
$args = [$pname, NIX_SYSTEM];
2017-10-29 21:10:26 +00:00
} else {
echo "building via nix-build . -A\n";
$attrs = array_intersperse(array_values((array)$body->attrs), '-A');
var_dump($attrs);
$fillers = implode(" ", array_fill(0, count($attrs), '%s'));
2017-11-04 15:56:57 +00:00
$cmd = 'NIX_PATH=nixpkgs=%s nix-build --no-out-link --argstr system %s --option restrict-eval true --keep-going . ' . $fillers;
2017-10-29 21:10:26 +00:00
$args = $attrs;
array_unshift($args, NIX_SYSTEM);
2017-10-29 21:10:26 +00:00
array_unshift($args, $pname);
}
try {
$output = GHE\Exec::exec($cmd, $args);
$pass = true;
} catch (GHE\ExecException $e) {
$output = $e->getOutput();
$pass = false;
}
$lastlines = array_reverse(
array_slice(
array_reverse($output),
0, 10
)
);
$forward = [
2017-10-29 21:48:48 +00:00
'system' => NIX_SYSTEM,
2017-10-29 21:10:26 +00:00
'payload' => $in,
'output' => $lastlines,
'success' => $pass,
];
$message = new AMQPMessage(json_encode($forward),
2017-10-31 08:06:14 +00:00
array(
'content_type' => 'application/json',
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
));
$msg->delivery_info['channel']->basic_publish($message, 'build-results');
2017-10-29 21:10:26 +00:00
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
echo "finished\n";
}
function array_intersperse($array, $val) {
return array_reduce($array,
function($c, $elem) use ($val) {
$c[] = $val;
$c[] = $elem;
return $c;
},
array());
}
function outrunner($msg) {
try {
return runner($msg);
2017-10-29 21:42:23 +00:00
} catch (\GHE\ExecException $e) {
2017-10-29 21:10:26 +00:00
var_dump($e->getMessage());
var_dump($e->getCode());
var_dump($e->getOutput());
2017-10-29 21:45:44 +00:00
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
2017-10-29 21:10:26 +00:00
}
}
$consumerTag = 'consumer' . getmypid();
$channel->basic_consume($queueName, $consumerTag, false, false, false, false, 'outrunner');
while(count($channel->callbacks)) {
$channel->wait();
}