Differential D21801 Diff 51968 src/applications/drydock/management/DrydockManagementLeaseWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/management/DrydockManagementLeaseWorkflow.php
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 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(); | ||||
} | } | ||||
$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); | ||||
} | } | ||||
// TODO: This is not hugely scalable, although this is a debugging | if ($blueprint_phids === null) { | ||||
// workflow so maybe it's fine. Do we even need `bin/drydock lease` in | $blueprint_phids = $this->newAllowedBlueprintPHIDs($lease); | ||||
// the long run? | |||||
$all_blueprints = id(new DrydockBlueprintQuery()) | |||||
->setViewer($viewer) | |||||
->execute(); | |||||
$allowed_phids = mpull($all_blueprints, 'getPHID'); | |||||
if (!$allowed_phids) { | |||||
throw new Exception( | |||||
pht( | |||||
'No blueprints exist which can plausibly allocate resources to '. | |||||
'satisfy the requested lease.')); | |||||
} | } | ||||
$lease->setAllowedBlueprintPHIDs($allowed_phids); | |||||
$lease->setAllowedBlueprintPHIDs($blueprint_phids); | |||||
if ($until) { | if ($until) { | ||||
$lease->setUntil($until); | $lease->setUntil($until); | ||||
} | } | ||||
// If something fatals or the user interrupts the process (for example, | // If something fatals or the user interrupts the process (for example, | ||||
// with "^C"), release the lease. We'll cancel this below, if the lease | // with "^C"), release the lease. We'll cancel this below, if the lease | ||||
// actually activates. | // actually activates. | ||||
▲ Show 20 Lines • Show All 139 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) { | |||||
$viewer = $this->getViewer(); | |||||
$impls = DrydockBlueprintImplementation::getAllForAllocatingLease($lease); | |||||
if (!$impls) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | |||||
'No known blueprint class can ever allocate the specified '. | |||||
'lease. Check that the resource type is spelled correctly.')); | |||||
} | |||||
$classes = array_keys($impls); | |||||
$blueprints = id(new DrydockBlueprintQuery()) | |||||
->setViewer($viewer) | |||||
->withBlueprintClasses($classes) | |||||
->withDisabled(false) | |||||
->execute(); | |||||
if (!$blueprints) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | |||||
'No enabled blueprints exist with a blueprint class that can '. | |||||
'plausibly allocate resources to satisfy the requested lease.')); | |||||
} | |||||
$phids = mpull($blueprints, 'getPHID'); | |||||
return $phids; | |||||
} | |||||
} | } |