1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 05:50:55 +01:00

Add "--all" flags to "release-lease" and "release-resource" workflows in "bin/drydock"

Summary: Ref T13677. These flags increase the convenience of testing in a development environment.

Test Plan: Used new "--all" flags to release all resources and leases.

Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13677

Differential Revision: https://secure.phabricator.com/D21805
This commit is contained in:
epriestley 2022-05-05 15:58:51 -07:00
parent 6417e950f6
commit 1b6727ac3d
4 changed files with 136 additions and 34 deletions

View file

@ -15,40 +15,71 @@ final class DrydockManagementReleaseLeaseWorkflow
'repeat' => true,
'help' => pht('Lease ID to release.'),
),
array(
'name' => 'all',
'help' => pht('Release all leases. Dangerous!'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$is_all = $args->getArg('all');
$ids = $args->getArg('id');
if (!$ids) {
if (!$ids && !$is_all) {
throw new PhutilArgumentUsageException(
pht(
'Specify one or more lease IDs to release with "%s".',
'--id'));
'Select which leases you want to release. See "--help" for '.
'guidance.'));
}
$viewer = $this->getViewer();
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
$leases = id(new DrydockLeaseQuery())
$statuses = $this->getReleaseableLeaseStatuses();
$query = id(new DrydockLeaseQuery())
->setViewer($viewer)
->withIDs($ids)
->execute();
->withStatuses(mpull($statuses, 'getKey'));
PhabricatorWorker::setRunAllTasksInProcess(true);
foreach ($ids as $id) {
$lease = idx($leases, $id);
if (!$lease) {
echo tsprintf(
"%s\n",
pht('Lease "%s" does not exist.', $id));
continue;
if ($ids) {
$query->withIDs($ids);
}
$leases = $query->execute();
if ($ids) {
$id_map = mpull($leases, null, 'getID');
foreach ($ids as $id) {
$lease = idx($id_map, $id);
if (!$lease) {
throw new PhutilArgumentUsageException(
pht('Lease "%s" does not exist.', $id));
}
}
$leases = array_select_keys($id_map, $ids);
}
if (!$leases) {
echo tsprintf(
"%s\n",
pht('No leases selected for release.'));
return 0;
}
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
PhabricatorWorker::setRunAllTasksInProcess(true);
foreach ($leases as $lease) {
if (!$lease->canRelease()) {
echo tsprintf(
"%s\n",
pht('Lease "%s" is not releasable.', $id));
pht(
'Lease "%s" is not releasable.',
$lease->getDisplayName()));
continue;
}
@ -62,9 +93,26 @@ final class DrydockManagementReleaseLeaseWorkflow
echo tsprintf(
"%s\n",
pht('Scheduled release of lease "%s".', $id));
pht(
'Scheduled release of lease "%s".',
$lease->getDisplayName()));
}
}
private function getReleaseableLeaseStatuses() {
$statuses = DrydockLeaseStatus::getAllStatuses();
foreach ($statuses as $key => $status) {
$statuses[$key] = DrydockLeaseStatus::newStatusObject($status);
}
foreach ($statuses as $key => $status) {
if (!$status->canRelease()) {
unset($statuses[$key]);
}
}
return $statuses;
}
}

View file

@ -15,41 +15,70 @@ final class DrydockManagementReleaseResourceWorkflow
'repeat' => true,
'help' => pht('Resource ID to release.'),
),
array(
'name' => 'all',
'help' => pht('Release all resources. Dangerous!'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$is_all = $args->getArg('all');
$ids = $args->getArg('id');
if (!$ids) {
if (!$ids && !$is_all) {
throw new PhutilArgumentUsageException(
pht(
'Specify one or more resource IDs to release with "%s".',
'--id'));
'Specify which resources you want to release. See "--help" for '.
'guidance.'));
}
$viewer = $this->getViewer();
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
$statuses = $this->getReleaseableResourceStatuses();
$resources = id(new DrydockResourceQuery())
$query = id(new DrydockResourceQuery())
->setViewer($viewer)
->withIDs($ids)
->execute();
->withStatuses(mpull($statuses, 'getKey'));
PhabricatorWorker::setRunAllTasksInProcess(true);
foreach ($ids as $id) {
$resource = idx($resources, $id);
if ($ids) {
$query->withIDs($ids);
}
if (!$resource) {
echo tsprintf(
"%s\n",
pht('Resource "%s" does not exist.', $id));
continue;
$resources = $query->execute();
if ($ids) {
$id_map = mpull($resources, null, 'getID');
foreach ($ids as $id) {
$resource = idx($resources, $id);
if (!$resource) {
throw new PhutilArgumentUsageException(
pht('Resource "%s" does not exist.', $id));
}
}
$resources = array_select_keys($id_map, $ids);
}
if (!$resources) {
echo tsprintf(
"%s\n",
pht('No resources selected for release.'));
return 0;
}
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
PhabricatorWorker::setRunAllTasksInProcess(true);
foreach ($resources as $resource) {
if (!$resource->canRelease()) {
echo tsprintf(
"%s\n",
pht('Resource "%s" is not releasable.', $id));
pht(
'Resource "%s" is not releasable.',
$resource->getDisplayName()));
continue;
}
@ -63,9 +92,26 @@ final class DrydockManagementReleaseResourceWorkflow
echo tsprintf(
"%s\n",
pht('Scheduled release of resource "%s".', $id));
pht(
'Scheduled release of resource "%s".',
$resource->getDisplayName()));
}
return 0;
}
private function getReleaseableResourceStatuses() {
$statuses = DrydockResourceStatus::getAllStatuses();
foreach ($statuses as $key => $status) {
$statuses[$key] = DrydockResourceStatus::newStatusObject($status);
}
foreach ($statuses as $key => $status) {
if (!$status->canRelease()) {
unset($statuses[$key]);
}
}
return $statuses;
}
}

View file

@ -471,6 +471,10 @@ final class DrydockLease extends DrydockDAO
return "/drydock/lease/{$id}/";
}
public function getDisplayName() {
return pht('Drydock Lease %d', $this->getID());
}
/* -( Status )------------------------------------------------------------- */

View file

@ -286,6 +286,10 @@ final class DrydockResource extends DrydockDAO
return $log->save();
}
public function getDisplayName() {
return pht('Drydock Resource %d', $this->getID());
}
/* -( Status )------------------------------------------------------------- */