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-06 04:11:05 +01:00
|
|
|
$artifact = $build->loadArtifact($settings['hostartifact']);
|
2013-11-09 03:15:14 +01:00
|
|
|
|
2013-12-06 04:11:05 +01:00
|
|
|
$lease = $artifact->loadDrydockLease();
|
2013-12-05 04:06:22 +01:00
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
// 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) {
|
2014-01-13 21:21:49 +01:00
|
|
|
throw new Exception(pht('Command failed with error %d.', $err));
|
2013-11-09 03:15:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|