1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 16:22:43 +01:00

Add a "close" action to Drydock resources

Summary: This does nothing fancy, just closes the resource and releases/breaks leases. They'll get cleaned up in some to-be-written GC process.

Test Plan: Closed resources from web UI and CLI.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2015

Differential Revision: https://secure.phabricator.com/D3998
This commit is contained in:
epriestley 2012-11-27 12:48:03 -08:00
parent 1bbdfa60c7
commit 5cbc31644b
7 changed files with 143 additions and 0 deletions

View file

@ -17,6 +17,7 @@ $args->parseStandardArguments();
$workflows = array(
new DrydockManagementWaitForLeaseWorkflow(),
new DrydockManagementLeaseWorkflow(),
new DrydockManagementCloseWorkflow(),
new PhutilHelpArgumentWorkflow(),
);

View file

@ -433,10 +433,12 @@ phutil_register_library_map(array(
'DrydockLog' => 'applications/drydock/storage/DrydockLog.php',
'DrydockLogController' => 'applications/drydock/controller/DrydockLogController.php',
'DrydockLogQuery' => 'applications/drydock/query/DrydockLogQuery.php',
'DrydockManagementCloseWorkflow' => 'applications/drydock/management/DrydockManagementCloseWorkflow.php',
'DrydockManagementLeaseWorkflow' => 'applications/drydock/management/DrydockManagementLeaseWorkflow.php',
'DrydockManagementWaitForLeaseWorkflow' => 'applications/drydock/management/DrydockManagementWaitForLeaseWorkflow.php',
'DrydockManagementWorkflow' => 'applications/drydock/management/DrydockManagementWorkflow.php',
'DrydockResource' => 'applications/drydock/storage/DrydockResource.php',
'DrydockResourceCloseController' => 'applications/drydock/controller/DrydockResourceCloseController.php',
'DrydockResourceListController' => 'applications/drydock/controller/DrydockResourceListController.php',
'DrydockResourceStatus' => 'applications/drydock/constants/DrydockResourceStatus.php',
'DrydockResourceViewController' => 'applications/drydock/controller/DrydockResourceViewController.php',
@ -1688,10 +1690,12 @@ phutil_register_library_map(array(
'DrydockLog' => 'DrydockDAO',
'DrydockLogController' => 'DrydockController',
'DrydockLogQuery' => 'PhabricatorOffsetPagedQuery',
'DrydockManagementCloseWorkflow' => 'DrydockManagementWorkflow',
'DrydockManagementLeaseWorkflow' => 'DrydockManagementWorkflow',
'DrydockManagementWaitForLeaseWorkflow' => 'DrydockManagementWorkflow',
'DrydockManagementWorkflow' => 'PhutilArgumentWorkflow',
'DrydockResource' => 'DrydockDAO',
'DrydockResourceCloseController' => 'DrydockController',
'DrydockResourceListController' => 'DrydockController',
'DrydockResourceStatus' => 'DrydockConstants',
'DrydockResourceViewController' => 'DrydockController',

View file

@ -33,6 +33,7 @@ final class PhabricatorApplicationDrydock extends PhabricatorApplication {
'resource/' => array(
'' => 'DrydockResourceListController',
'(?P<id>[1-9]\d*)/' => 'DrydockResourceViewController',
'(?P<id>[1-9]\d*)/close/' => 'DrydockResourceCloseController',
),
'lease/' => array(
'' => 'DrydockLeaseListController',

View file

@ -0,0 +1,53 @@
<?php
final class DrydockResourceCloseController extends DrydockController {
private $id;
public function willProcessRequest(array $data) {
$this->id = $data['id'];
}
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$resource = id(new DrydockResource())->load($this->id);
if (!$resource) {
return new Aphront404Response();
}
$resource_uri = '/resource/'.$resource->getID().'/';
$resource_uri = $this->getApplicationURI($resource_uri);
if ($resource->getStatus() != DrydockResourceStatus::STATUS_OPEN) {
$dialog = id(new AphrontDialogView())
->setUser($user)
->setTitle(pht('Resource Not Open'))
->appendChild(
'<p>'.pht('You can only close "open" resources.').'</p>')
->addCancelButton($resource_uri);
return id(new AphrontDialogResponse())->setDialog($dialog);
}
if (!$request->isDialogFormPost()) {
$dialog = id(new AphrontDialogView())
->setUser($user)
->setTitle(pht('Really close resource?'))
->appendChild(
'<p>'.pht(
'Closing a resource releases all leases and destroys the '.
'resource. It can not be undone. Continue?').'</p>')
->addSubmitButton(pht('Close Resource'))
->addCancelButton($resource_uri);
return id(new AphrontDialogResponse())->setDialog($dialog);
}
$resource->closeResource();
return id(new AphrontReloadResponse())->setURI($resource_uri);
}
}

View file

@ -63,6 +63,18 @@ final class DrydockResourceViewController extends DrydockController {
->setUser($this->getRequest()->getUser())
->setObject($resource);
$can_close = ($resource->getStatus() == DrydockResourceStatus::STATUS_OPEN);
$uri = '/resource/'.$resource->getID().'/close/';
$uri = $this->getApplicationURI($uri);
$view->addAction(
id(new PhabricatorActionView())
->setHref($uri)
->setName(pht('Close Resource'))
->setIcon('delete')
->setWorkflow(true)
->setDisabled(!$can_close));
return $view;
}

View file

@ -0,0 +1,42 @@
<?php
final class DrydockManagementCloseWorkflow
extends DrydockManagementWorkflow {
public function didConstruct() {
$this
->setName('close')
->setSynopsis('Close a resource.')
->setArguments(
array(
array(
'name' => 'ids',
'wildcard' => true,
),
));
}
public function execute(PhutilArgumentParser $args) {
$console = PhutilConsole::getConsole();
$ids = $args->getArg('ids');
if (!$ids) {
throw new PhutilArgumentUsageException(
"Specify one or more resource IDs to close.");
}
foreach ($ids as $id) {
$resource = id(new DrydockResource())->load($id);
if (!$resource) {
$console->writeErr("Resource %d does not exist!\n", $id);
} else if ($resource->getStatus() != DrydockResourceStatus::STATUS_OPEN) {
$console->writeErr("Resource %d is not 'open'!\n", $id);
} else {
$resource->closeResource();
$console->writeErr("Closed resource %d.\n", $id);
}
}
}
}

View file

@ -54,4 +54,34 @@ final class DrydockResource extends DrydockDAO {
return $this->blueprint;
}
public function closeResource() {
$this->openTransaction();
$leases = id(new DrydockLease())->loadAllWhere(
'resourceID = %d AND status IN (%Ld)',
$this->getID(),
array(
DrydockLeaseStatus::STATUS_PENDING,
DrydockLeaseStatus::STATUS_ACTIVE,
));
foreach ($leases as $lease) {
switch ($lease->getStatus()) {
case DrydockLeaseStatus::STATUS_PENDING:
$message = pht('Breaking pending lease (resource closing).');
$lease->setStatus(DrydockLeaseStatus::STATUS_BROKEN);
break;
case DrydockLeaseStatus::STATUS_ACTIVE:
$message = pht('Releasing active lease (resource closing).');
$lease->setStatus(DrydockLeaseStatus::STATUS_RELEASED);
break;
}
DrydockBlueprint::writeLog($this, $lease, $message);
$lease->save();
}
$this->setStatus(DrydockResourceStatus::STATUS_CLOSED);
$this->save();
$this->saveTransaction();
}
}