2013-11-09 03:15:14 +01:00
|
|
|
<?php
|
|
|
|
|
2013-12-05 04:06:22 +01:00
|
|
|
final class CommandBuildStepImplementation
|
2013-11-09 03:15:14 +01:00
|
|
|
extends VariableBuildStepImplementation {
|
|
|
|
|
|
|
|
public function getName() {
|
2013-12-05 04:06:22 +01:00
|
|
|
return pht('Run Command');
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getGenericDescription() {
|
2013-12-05 04:06:22 +01:00
|
|
|
return pht('Run a command on Drydock host.');
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
$settings = $this->getSettings();
|
|
|
|
|
|
|
|
return pht(
|
|
|
|
'Run \'%s\' on \'%s\'.',
|
|
|
|
$settings['command'],
|
2013-12-05 04:06:22 +01:00
|
|
|
$settings['hostartifact']);
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(
|
|
|
|
HarbormasterBuild $build,
|
2013-12-05 02:01:12 +01:00
|
|
|
HarbormasterBuildTarget $build_target) {
|
2013-11-09 03:15:14 +01:00
|
|
|
|
|
|
|
$settings = $this->getSettings();
|
2013-12-05 02:01:12 +01:00
|
|
|
$variables = $build_target->getVariables();
|
2013-11-09 03:36:48 +01:00
|
|
|
|
2013-11-09 16:16:12 +01:00
|
|
|
$command = $this->mergeVariables(
|
|
|
|
'vcsprintf',
|
|
|
|
$settings['command'],
|
|
|
|
$variables);
|
2013-11-09 03:36:48 +01:00
|
|
|
|
2013-12-05 04:06:22 +01:00
|
|
|
$artifact = id(new HarbormasterBuildArtifactQuery())
|
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
|
|
->withArtifactKeys(
|
|
|
|
$build->getPHID(),
|
|
|
|
array($settings['hostartifact']))
|
|
|
|
->executeOne();
|
|
|
|
if ($artifact === null) {
|
|
|
|
throw new Exception("Associated Drydock host artifact not found!");
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 04:06:22 +01:00
|
|
|
$data = $artifact->getArtifactData();
|
|
|
|
|
|
|
|
// FIXME: Is there a better way of doing this?
|
|
|
|
$lease = id(new DrydockLease())->load(
|
|
|
|
$data['drydock-lease']);
|
|
|
|
if ($lease === null) {
|
|
|
|
throw new Exception("Associated Drydock lease not found!");
|
|
|
|
}
|
|
|
|
$resource = id(new DrydockResource())->load(
|
|
|
|
$lease->getResourceID());
|
|
|
|
if ($resource === null) {
|
|
|
|
throw new Exception("Associated Drydock resource not found!");
|
|
|
|
}
|
|
|
|
$lease->attachResource($resource);
|
|
|
|
|
|
|
|
$interface = $lease->getInterface('command');
|
|
|
|
|
|
|
|
$future = $interface->getExecFuture('%C', $command);
|
|
|
|
|
2013-12-05 02:01:12 +01:00
|
|
|
$log_stdout = $build->createLog($build_target, "remote", "stdout");
|
|
|
|
$log_stderr = $build->createLog($build_target, "remote", "stderr");
|
2013-11-09 03:15:14 +01:00
|
|
|
|
|
|
|
$start_stdout = $log_stdout->start();
|
|
|
|
$start_stderr = $log_stderr->start();
|
|
|
|
|
|
|
|
// Read the next amount of available output every second.
|
|
|
|
while (!$future->isReady()) {
|
|
|
|
list($stdout, $stderr) = $future->read();
|
|
|
|
$log_stdout->append($stdout);
|
|
|
|
$log_stderr->append($stderr);
|
|
|
|
$future->discardBuffers();
|
|
|
|
|
|
|
|
// Check to see if we have moved from a "Building" status. This
|
|
|
|
// can occur if the user cancels the build, in which case we want
|
|
|
|
// to terminate whatever we're doing and return as quickly as possible.
|
|
|
|
if ($build->checkForCancellation()) {
|
|
|
|
$log_stdout->finalize($start_stdout);
|
|
|
|
$log_stderr->finalize($start_stderr);
|
|
|
|
$future->resolveKill();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait one second before querying for more data.
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the return value so we can log that as well.
|
|
|
|
list($err) = $future->resolve();
|
|
|
|
|
2013-11-09 04:06:45 +01:00
|
|
|
// Retrieve the last few bits of information.
|
|
|
|
list($stdout, $stderr) = $future->read();
|
|
|
|
$log_stdout->append($stdout);
|
|
|
|
$log_stderr->append($stderr);
|
|
|
|
$future->discardBuffers();
|
|
|
|
|
2013-11-09 03:15:14 +01:00
|
|
|
$log_stdout->finalize($start_stdout);
|
|
|
|
$log_stderr->finalize($start_stderr);
|
|
|
|
|
|
|
|
if ($err) {
|
|
|
|
$build->setBuildStatus(HarbormasterBuild::STATUS_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateSettings() {
|
|
|
|
$settings = $this->getSettings();
|
|
|
|
|
|
|
|
if ($settings['command'] === null || !is_string($settings['command'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-05 04:06:22 +01:00
|
|
|
if ($settings['hostartifact'] === null ||
|
|
|
|
!is_string($settings['hostartifact'])) {
|
2013-11-09 03:15:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-05 04:06:22 +01:00
|
|
|
// TODO: Check if the host artifact is provided by previous build steps.
|
2013-11-09 03:15:14 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSettingDefinitions() {
|
|
|
|
return array(
|
|
|
|
'command' => array(
|
|
|
|
'name' => 'Command',
|
|
|
|
'description' => 'The command to execute on the remote machine.',
|
|
|
|
'type' => BuildStepImplementation::SETTING_TYPE_STRING),
|
2013-12-05 04:06:22 +01:00
|
|
|
'hostartifact' => array(
|
|
|
|
'name' => 'Host Artifact',
|
2013-11-09 03:15:14 +01:00
|
|
|
'description' =>
|
2013-12-05 04:06:22 +01:00
|
|
|
'The host artifact that determines what machine the command '.
|
|
|
|
'will run on.',
|
|
|
|
'type' => BuildStepImplementation::SETTING_TYPE_ARTIFACT,
|
|
|
|
'artifact_type' => HarbormasterBuildArtifact::TYPE_HOST));
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|