Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / drydock / management / DrydockManagementReleaseResourceWorkflow.php
blobafd826cbc066081165758c26858b89843f7584d4
1 <?php
3 final class DrydockManagementReleaseResourceWorkflow
4 extends DrydockManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('release-resource')
9 ->setSynopsis(pht('Release a resource.'))
10 ->setArguments(
11 array(
12 array(
13 'name' => 'id',
14 'param' => 'id',
15 'repeat' => true,
16 'help' => pht('Resource ID to release.'),
18 array(
19 'name' => 'all',
20 'help' => pht('Release all resources. Dangerous!'),
22 ));
25 public function execute(PhutilArgumentParser $args) {
26 $is_all = $args->getArg('all');
27 $ids = $args->getArg('id');
28 if (!$ids && !$is_all) {
29 throw new PhutilArgumentUsageException(
30 pht(
31 'Specify which resources you want to release. See "--help" for '.
32 'guidance.'));
35 $viewer = $this->getViewer();
36 $statuses = $this->getReleaseableResourceStatuses();
38 $query = id(new DrydockResourceQuery())
39 ->setViewer($viewer)
40 ->withStatuses(mpull($statuses, 'getKey'));
42 if ($ids) {
43 $query->withIDs($ids);
46 $resources = $query->execute();
48 if ($ids) {
49 $id_map = mpull($resources, null, 'getID');
51 foreach ($ids as $id) {
52 $resource = idx($resources, $id);
54 if (!$resource) {
55 throw new PhutilArgumentUsageException(
56 pht('Resource "%s" does not exist.', $id));
60 $resources = array_select_keys($id_map, $ids);
63 if (!$resources) {
64 echo tsprintf(
65 "%s\n",
66 pht('No resources selected for release.'));
68 return 0;
71 $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
73 PhabricatorWorker::setRunAllTasksInProcess(true);
75 foreach ($resources as $resource) {
76 if (!$resource->canRelease()) {
77 echo tsprintf(
78 "%s\n",
79 pht(
80 'Resource "%s" is not releasable.',
81 $resource->getDisplayName()));
82 continue;
85 $command = DrydockCommand::initializeNewCommand($viewer)
86 ->setTargetPHID($resource->getPHID())
87 ->setAuthorPHID($drydock_phid)
88 ->setCommand(DrydockCommand::COMMAND_RELEASE)
89 ->save();
91 $resource->scheduleUpdate();
93 echo tsprintf(
94 "%s\n",
95 pht(
96 'Scheduled release of resource "%s".',
97 $resource->getDisplayName()));
100 return 0;
103 private function getReleaseableResourceStatuses() {
104 $statuses = DrydockResourceStatus::getAllStatuses();
105 foreach ($statuses as $key => $status) {
106 $statuses[$key] = DrydockResourceStatus::newStatusObject($status);
109 foreach ($statuses as $key => $status) {
110 if (!$status->canRelease()) {
111 unset($statuses[$key]);
115 return $statuses;