1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-18 19:40:55 +01:00

Provide a workflow to restart Harbormaster builds

Summary:
Ref T10867 for original use case.  This workflow provides a plausible way for administrators to stop the daemons when performing upgrades or maintenance, then bring those daemons back up without resulting in the failure of builds that were running at the time.

On our organization's phab install, builds are running 24/7. The majority of these builds last for at least several minutes, and contain build steps which fail if interrupted and then resumed, as happens when turning daemons on and off.

Instead of allowing these build steps to resume execution as normal, this workflow will instruct active builds to restart their entire build process instead of just resuming whichever step they were on.

Test Plan:
contrived a build plan which would fail if resumed partway through:

 - lease a working copy
 - command `touch restart_{build.id}`
 - command `test -e restart_{build.id} && rm restart_{build.id} && sleep 60`

followed old procedure:

 - run a few of these builds manually
 - `./bin/phd stop`
 - `./bin/phd start`
 - saw the builds fail

followed new procedure:

 - run a few of these builds manually
 - `./bin/phd stop`
 - `./bin/harbormaster restart --active`
 - `./bin/phd start`
 - saw the builds pass

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Maniphest Tasks: T10867

Differential Revision: https://secure.phabricator.com/D16485
This commit is contained in:
Mike Riley 2016-09-02 03:22:59 +00:00
parent 0eb5a80e7b
commit 403073c989
3 changed files with 107 additions and 0 deletions

View file

@ -1190,6 +1190,7 @@ phutil_register_library_map(array(
'HarbormasterLintPropertyView' => 'applications/harbormaster/view/HarbormasterLintPropertyView.php',
'HarbormasterManagementArchiveLogsWorkflow' => 'applications/harbormaster/management/HarbormasterManagementArchiveLogsWorkflow.php',
'HarbormasterManagementBuildWorkflow' => 'applications/harbormaster/management/HarbormasterManagementBuildWorkflow.php',
'HarbormasterManagementRestartWorkflow' => 'applications/harbormaster/management/HarbormasterManagementRestartWorkflow.php',
'HarbormasterManagementUpdateWorkflow' => 'applications/harbormaster/management/HarbormasterManagementUpdateWorkflow.php',
'HarbormasterManagementWorkflow' => 'applications/harbormaster/management/HarbormasterManagementWorkflow.php',
'HarbormasterMessageType' => 'applications/harbormaster/engine/HarbormasterMessageType.php',
@ -5785,6 +5786,7 @@ phutil_register_library_map(array(
'HarbormasterLintPropertyView' => 'AphrontView',
'HarbormasterManagementArchiveLogsWorkflow' => 'HarbormasterManagementWorkflow',
'HarbormasterManagementBuildWorkflow' => 'HarbormasterManagementWorkflow',
'HarbormasterManagementRestartWorkflow' => 'HarbormasterManagementWorkflow',
'HarbormasterManagementUpdateWorkflow' => 'HarbormasterManagementWorkflow',
'HarbormasterManagementWorkflow' => 'PhabricatorManagementWorkflow',
'HarbormasterMessageType' => 'Phobject',

View file

@ -0,0 +1,100 @@
<?php
final class HarbormasterManagementRestartWorkflow
extends HarbormasterManagementWorkflow {
protected function didConstruct() {
$this
->setName('restart')
->setExamples(
"**restart** --active\n".
'**restart** --id id')
->setSynopsis(pht('Restart Harbormaster builds.'))
->setArguments(
array(
array(
'name' => 'id',
'param' => 'id',
'repeat' => true,
'help' => pht('Select one or more builds by ID.'),
),
array(
'name' => 'active',
'help' => pht('Select all active builds.'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$viewer = $this->getViewer();
$ids = $args->getArg('id');
$active = $args->getArg('active');
if (!$ids && !$active) {
throw new PhutilArgumentUsageException(
pht('Use --id or --active to select builds.'));
} if ($ids && $active) {
throw new PhutilArgumentUsageException(
pht('Use one of --id or --active to select builds, but not both.'));
}
$query = id(new HarbormasterBuildQuery())
->setViewer($viewer);
if ($ids) {
$query->withIDs($ids);
} else {
$query->withBuildStatuses(
HarbormasterBuildStatus::getActiveStatusConstants());
}
$builds = $query->execute();
$console = PhutilConsole::getConsole();
$count = count($builds);
if (!$count) {
$console->writeOut("%s\n", pht('No builds to restart.'));
return 0;
}
$prompt = pht('Restart %s build(s)?', new PhutilNumber($count));
if (!phutil_console_confirm($prompt)) {
$console->writeOut("%s\n", pht('Cancelled.'));
return 1;
}
$app_phid = id(new PhabricatorHarbormasterApplication())->getPHID();
$editor = id(new HarbormasterBuildTransactionEditor())
->setActor($viewer)
->setActingAsPHID($app_phid)
->setContentSource($this->newContentSource());
foreach ($builds as $build) {
$console->writeOut(
"<bg:blue> %s </bg> %s\n",
pht('RESTARTING'),
pht('Build %d: %s', $build->getID(), $build->getName()));
if (!$build->canRestartBuild()) {
$console->writeOut(
"<bg:yellow> %s </bg> %s\n",
pht('INVALID'),
pht('Cannot be restarted.'));
continue;
}
$xactions = array();
$xactions[] = id(new HarbormasterBuildTransaction())
->setTransactionType(HarbormasterBuildTransaction::TYPE_COMMAND)
->setNewValue(HarbormasterBuildCommand::COMMAND_RESTART);
try {
$editor->applyTransactions($build, $xactions);
} catch (Exception $e) {
$message = phutil_console_wrap($e->getMessage(), 2);
$console->writeOut(
"<bg:red> %s </bg>\n%s\n",
pht('FAILED'),
$message);
continue;
}
$console->writeOut("<bg:green> %s </bg>\n", pht('SUCCESS'));
}
return 0;
}
}

View file

@ -1573,6 +1573,11 @@ final class PhabricatorUSEnglishTranslation
'%s updated the invite list for %s, invited %s: %s; uninvinted %s: %s.' =>
'%s updated the invite list for %s, invited: %4$s; uninvited: %6$s.',
'Restart %s build(s)?' => array(
'Restart %s build?',
'Restart %s builds?',
),
);
}