Differential D19761 Diff 47214 src/applications/drydock/blueprint/DrydockAlmanacServiceHostBlueprintImplementation.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/blueprint/DrydockAlmanacServiceHostBlueprintImplementation.php
| Show First 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | $device_name = $resource->getAttribute( | ||||
| pht('<Unknown>')); | pht('<Unknown>')); | ||||
| return pht('Host (%s)', $device_name); | return pht('Host (%s)', $device_name); | ||||
| } | } | ||||
| public function canAcquireLeaseOnResource( | public function canAcquireLeaseOnResource( | ||||
| DrydockBlueprint $blueprint, | DrydockBlueprint $blueprint, | ||||
| DrydockResource $resource, | DrydockResource $resource, | ||||
| DrydockLease $lease) { | DrydockLease $lease) { | ||||
| // Require the binding to a given host be active before we'll hand out more | |||||
| // leases on the corresponding resource. | |||||
| $binding = $this->loadBindingForResource($resource); | |||||
| if ($binding->getIsDisabled()) { | |||||
| return false; | |||||
| } | |||||
| return true; | return true; | ||||
| } | } | ||||
| public function acquireLease( | public function acquireLease( | ||||
| DrydockBlueprint $blueprint, | DrydockBlueprint $blueprint, | ||||
| DrydockResource $resource, | DrydockResource $resource, | ||||
| DrydockLease $lease) { | DrydockLease $lease) { | ||||
| Show All 25 Lines | final class DrydockAlmanacServiceHostBlueprintImplementation | ||||
| } | } | ||||
| public function getInterface( | public function getInterface( | ||||
| DrydockBlueprint $blueprint, | DrydockBlueprint $blueprint, | ||||
| DrydockResource $resource, | DrydockResource $resource, | ||||
| DrydockLease $lease, | DrydockLease $lease, | ||||
| $type) { | $type) { | ||||
| $viewer = PhabricatorUser::getOmnipotentUser(); | |||||
| switch ($type) { | switch ($type) { | ||||
| case DrydockCommandInterface::INTERFACE_TYPE: | case DrydockCommandInterface::INTERFACE_TYPE: | ||||
| $credential_phid = $blueprint->getFieldValue('credentialPHID'); | $credential_phid = $blueprint->getFieldValue('credentialPHID'); | ||||
| $binding_phid = $resource->getAttribute('almanacBindingPHID'); | $binding = $this->loadBindingForResource($resource); | ||||
| $binding = id(new AlmanacBindingQuery()) | |||||
| ->setViewer($viewer) | |||||
| ->withPHIDs(array($binding_phid)) | |||||
| ->executeOne(); | |||||
| if (!$binding) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Unable to load binding "%s" to create command interface.', | |||||
| $binding_phid)); | |||||
| } | |||||
| $interface = $binding->getInterface(); | $interface = $binding->getInterface(); | ||||
| return id(new DrydockSSHCommandInterface()) | return id(new DrydockSSHCommandInterface()) | ||||
| ->setConfig('credentialPHID', $credential_phid) | ->setConfig('credentialPHID', $credential_phid) | ||||
| ->setConfig('host', $interface->getAddress()) | ->setConfig('host', $interface->getAddress()) | ||||
| ->setConfig('port', $interface->getPort()); | ->setConfig('port', $interface->getPort()); | ||||
| } | } | ||||
| } | } | ||||
| Show All 25 Lines | if (!$this->services) { | ||||
| $service_phids = $blueprint->getFieldValue('almanacServicePHIDs'); | $service_phids = $blueprint->getFieldValue('almanacServicePHIDs'); | ||||
| if (!$service_phids) { | if (!$service_phids) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'This blueprint ("%s") does not define any Almanac Service PHIDs.', | 'This blueprint ("%s") does not define any Almanac Service PHIDs.', | ||||
| $blueprint->getBlueprintName())); | $blueprint->getBlueprintName())); | ||||
| } | } | ||||
| $viewer = PhabricatorUser::getOmnipotentUser(); | $viewer = $this->getViewer(); | ||||
| $services = id(new AlmanacServiceQuery()) | $services = id(new AlmanacServiceQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withPHIDs($service_phids) | ->withPHIDs($service_phids) | ||||
| ->withServiceTypes($this->getAlmanacServiceTypes()) | ->withServiceTypes($this->getAlmanacServiceTypes()) | ||||
| ->needBindings(true) | ->needBindings(true) | ||||
| ->execute(); | ->execute(); | ||||
| $services = mpull($services, null, 'getPHID'); | $services = mpull($services, null, 'getPHID'); | ||||
| Show All 16 Lines | final class DrydockAlmanacServiceHostBlueprintImplementation | ||||
| private function loadAllBindings(array $services) { | private function loadAllBindings(array $services) { | ||||
| assert_instances_of($services, 'AlmanacService'); | assert_instances_of($services, 'AlmanacService'); | ||||
| $bindings = array_mergev(mpull($services, 'getBindings')); | $bindings = array_mergev(mpull($services, 'getBindings')); | ||||
| return mpull($bindings, null, 'getPHID'); | return mpull($bindings, null, 'getPHID'); | ||||
| } | } | ||||
| private function loadFreeBindings(DrydockBlueprint $blueprint) { | private function loadFreeBindings(DrydockBlueprint $blueprint) { | ||||
| if ($this->freeBindings === null) { | if ($this->freeBindings === null) { | ||||
| $viewer = PhabricatorUser::getOmnipotentUser(); | $viewer = $this->getViewer(); | ||||
| $pool = id(new DrydockResourceQuery()) | $pool = id(new DrydockResourceQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withBlueprintPHIDs(array($blueprint->getPHID())) | ->withBlueprintPHIDs(array($blueprint->getPHID())) | ||||
| ->withStatuses( | ->withStatuses( | ||||
| array( | array( | ||||
| DrydockResourceStatus::STATUS_PENDING, | DrydockResourceStatus::STATUS_PENDING, | ||||
| DrydockResourceStatus::STATUS_ACTIVE, | DrydockResourceStatus::STATUS_ACTIVE, | ||||
| Show All 30 Lines | final class DrydockAlmanacServiceHostBlueprintImplementation | ||||
| } | } | ||||
| private function getAlmanacServiceTypes() { | private function getAlmanacServiceTypes() { | ||||
| return array( | return array( | ||||
| AlmanacDrydockPoolServiceType::SERVICETYPE, | AlmanacDrydockPoolServiceType::SERVICETYPE, | ||||
| ); | ); | ||||
| } | } | ||||
| private function loadBindingForResource(DrydockResource $resource) { | |||||
| $binding_phid = $resource->getAttribute('almanacBindingPHID'); | |||||
| if (!$binding_phid) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Drydock resource ("%s") has no Almanac binding PHID, so its '. | |||||
| 'binding can not be loaded.', | |||||
| $resource->getPHID())); | |||||
| } | |||||
| $viewer = $this->getViewer(); | |||||
| $binding = id(new AlmanacBindingQuery()) | |||||
| ->setViewer($viewer) | |||||
| ->withPHIDs(array($binding_phid)) | |||||
| ->executeOne(); | |||||
| if (!$binding) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Unable to load Almanac binding ("%s") for resource ("%s").', | |||||
amckinley: This message doesn't make sense now that it's been decoupled from the `DrydockCommandInterface… | |||||
| $binding_phid, | |||||
| $resource->getPHID())); | |||||
| } | |||||
| return $binding; | |||||
| } | |||||
| } | } | ||||
This message doesn't make sense now that it's been decoupled from the DrydockCommandInterface::INTERFACE_TYPE case statement in getInterface().