Differential D21802 Diff 51969 src/applications/drydock/management/DrydockManagementLeaseWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/management/DrydockManagementLeaseWorkflow.php
Show All 26 Lines | $this | ||||
'from stdin.'), | 'from stdin.'), | ||||
), | ), | ||||
array( | array( | ||||
'name' => 'count', | 'name' => 'count', | ||||
'param' => 'N', | 'param' => 'N', | ||||
'default' => 1, | 'default' => 1, | ||||
'help' => pht('Lease a given number of identical resources.'), | 'help' => pht('Lease a given number of identical resources.'), | ||||
), | ), | ||||
array( | |||||
'name' => 'blueprint', | |||||
'param' => 'identifier', | |||||
'repeat' => true, | |||||
'help' => pht('Lease resources from a specific blueprint.'), | |||||
), | |||||
)); | )); | ||||
} | } | ||||
public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$resource_type = $args->getArg('type'); | $resource_type = $args->getArg('type'); | ||||
if (!phutil_nonempty_string($resource_type)) { | if (!phutil_nonempty_string($resource_type)) { | ||||
Show All 31 Lines | if (phutil_nonempty_string($attributes_file)) { | ||||
$data = Filesystem::readFile($attributes_file); | $data = Filesystem::readFile($attributes_file); | ||||
} | } | ||||
$attributes = phutil_json_decode($data); | $attributes = phutil_json_decode($data); | ||||
} else { | } else { | ||||
$attributes = array(); | $attributes = array(); | ||||
} | } | ||||
$filter_identifiers = $args->getArg('blueprint'); | |||||
if ($filter_identifiers) { | |||||
$filter_blueprints = $this->getBlueprintFilterMap($filter_identifiers); | |||||
} else { | |||||
$filter_blueprints = array(); | |||||
} | |||||
$blueprint_phids = null; | $blueprint_phids = null; | ||||
$leases = array(); | $leases = array(); | ||||
for ($idx = 0; $idx < $count; $idx++) { | for ($idx = 0; $idx < $count; $idx++) { | ||||
$lease = id(new DrydockLease()) | $lease = id(new DrydockLease()) | ||||
->setResourceType($resource_type); | ->setResourceType($resource_type); | ||||
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID(); | $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID(); | ||||
$lease->setAuthorizingPHID($drydock_phid); | $lease->setAuthorizingPHID($drydock_phid); | ||||
if ($attributes) { | if ($attributes) { | ||||
$lease->setAttributes($attributes); | $lease->setAttributes($attributes); | ||||
} | } | ||||
if ($blueprint_phids === null) { | if ($blueprint_phids === null) { | ||||
$blueprint_phids = $this->newAllowedBlueprintPHIDs($lease); | $blueprint_phids = $this->newAllowedBlueprintPHIDs( | ||||
$lease, | |||||
$filter_blueprints); | |||||
} | } | ||||
$lease->setAllowedBlueprintPHIDs($blueprint_phids); | $lease->setAllowedBlueprintPHIDs($blueprint_phids); | ||||
if ($until) { | if ($until) { | ||||
$lease->setUntil($until); | $lease->setUntil($until); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | while (!$is_active) { | ||||
if ($is_active) { | if ($is_active) { | ||||
break; | break; | ||||
} else { | } else { | ||||
sleep(1); | sleep(1); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private function newAllowedBlueprintPHIDs(DrydockLease $lease) { | private function getBlueprintFilterMap(array $identifiers) { | ||||
$viewer = $this->getViewer(); | |||||
$query = id(new DrydockBlueprintQuery()) | |||||
->setViewer($viewer) | |||||
->withIdentifiers($identifiers); | |||||
$blueprints = $query->execute(); | |||||
$blueprints = mpull($blueprints, null, 'getPHID'); | |||||
$map = $query->getIdentifierMap(); | |||||
$seen = array(); | |||||
foreach ($identifiers as $identifier) { | |||||
if (!isset($map[$identifier])) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | |||||
'Blueprint "%s" could not be loaded. Try a blueprint ID or '. | |||||
'PHID.', | |||||
$identifier)); | |||||
} | |||||
$blueprint = $map[$identifier]; | |||||
$blueprint_phid = $blueprint->getPHID(); | |||||
if (isset($seen[$blueprint_phid])) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | |||||
'Blueprint "%s" is specified more than once (as "%s" and "%s").', | |||||
$blueprint->getBlueprintName(), | |||||
$seen[$blueprint_phid], | |||||
$identifier)); | |||||
} | |||||
$seen[$blueprint_phid] = true; | |||||
} | |||||
return mpull($map, null, 'getPHID'); | |||||
} | |||||
private function newAllowedBlueprintPHIDs( | |||||
DrydockLease $lease, | |||||
array $filter_blueprints) { | |||||
assert_instances_of($filter_blueprints, 'DrydockBlueprint'); | |||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$impls = DrydockBlueprintImplementation::getAllForAllocatingLease($lease); | $impls = DrydockBlueprintImplementation::getAllForAllocatingLease($lease); | ||||
if (!$impls) { | if (!$impls) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht( | pht( | ||||
'No known blueprint class can ever allocate the specified '. | 'No known blueprint class can ever allocate the specified '. | ||||
Show All 12 Lines | if (!$blueprints) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht( | pht( | ||||
'No enabled blueprints exist with a blueprint class that can '. | 'No enabled blueprints exist with a blueprint class that can '. | ||||
'plausibly allocate resources to satisfy the requested lease.')); | 'plausibly allocate resources to satisfy the requested lease.')); | ||||
} | } | ||||
$phids = mpull($blueprints, 'getPHID'); | $phids = mpull($blueprints, 'getPHID'); | ||||
if ($filter_blueprints) { | |||||
$allowed_map = array_fuse($phids); | |||||
$filter_map = mpull($filter_blueprints, null, 'getPHID'); | |||||
foreach ($filter_map as $filter_phid => $blueprint) { | |||||
if (!isset($allowed_map[$filter_phid])) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | |||||
'Specified blueprint "%s" is not capable of satisfying the '. | |||||
'configured lease.', | |||||
$blueprint->getBlueprintName())); | |||||
} | |||||
} | |||||
$phids = mpull($filter_blueprints, 'getPHID'); | |||||
} | |||||
return $phids; | return $phids; | ||||
} | } | ||||
} | } |