From b04114f95cc1f25c1d541236bb77edd51fe07ba0 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 27 Nov 2012 12:48:14 -0800 Subject: [PATCH] Allow Drydock to allocate (very basic) working copy resources Summary: This is missing a lot of features, but technically allows working copy allocation. Test Plan: Ran `drydock lease --type working-copy --attributes repositoryID=12`, got a working copy of Phabricator allocated on disk. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2015 Differential Revision: https://secure.phabricator.com/D3999 --- src/__phutil_library_map__.php | 2 + .../drydock/blueprint/DrydockBlueprint.php | 6 +- .../blueprint/DrydockLocalHostBlueprint.php | 10 +++ .../blueprint/DrydockWorkingCopyBlueprint.php | 69 +++++++++++++++++++ .../drydock/storage/DrydockLease.php | 8 ++- 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/applications/drydock/blueprint/DrydockWorkingCopyBlueprint.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index d5b523ca92..a4a1457121 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -444,6 +444,7 @@ phutil_register_library_map(array( 'DrydockResourceViewController' => 'applications/drydock/controller/DrydockResourceViewController.php', 'DrydockSSHCommandInterface' => 'applications/drydock/interface/command/DrydockSSHCommandInterface.php', 'DrydockWebrootInterface' => 'applications/drydock/interface/webroot/DrydockWebrootInterface.php', + 'DrydockWorkingCopyBlueprint' => 'applications/drydock/blueprint/DrydockWorkingCopyBlueprint.php', 'FeedPublisherWorker' => 'applications/feed/worker/FeedPublisherWorker.php', 'HarbormasterDAO' => 'applications/harbormaster/storage/HarbormasterDAO.php', 'HarbormasterObject' => 'applications/harbormaster/storage/HarbormasterObject.php', @@ -1701,6 +1702,7 @@ phutil_register_library_map(array( 'DrydockResourceViewController' => 'DrydockController', 'DrydockSSHCommandInterface' => 'DrydockCommandInterface', 'DrydockWebrootInterface' => 'DrydockInterface', + 'DrydockWorkingCopyBlueprint' => 'DrydockBlueprint', 'FeedPublisherWorker' => 'PhabricatorWorker', 'HarbormasterDAO' => 'PhabricatorLiskDAO', 'HarbormasterObject' => 'HarbormasterDAO', diff --git a/src/applications/drydock/blueprint/DrydockBlueprint.php b/src/applications/drydock/blueprint/DrydockBlueprint.php index c8a52a7743..2963ccfd4d 100644 --- a/src/applications/drydock/blueprint/DrydockBlueprint.php +++ b/src/applications/drydock/blueprint/DrydockBlueprint.php @@ -36,6 +36,10 @@ abstract class DrydockBlueprint { $this->log('Acquiring Lease'); try { + $lease->setStatus(DrydockLeaseStatus::STATUS_ACTIVE); + $lease->setResourceID($resource->getID()); + $lease->attachResource($resource); + $this->executeAcquireLease($resource, $lease); } catch (Exception $ex) { $this->logException($ex); @@ -45,8 +49,6 @@ abstract class DrydockBlueprint { throw $ex; } - $lease->setResourceID($resource->getID()); - $lease->setStatus(DrydockLeaseStatus::STATUS_ACTIVE); $lease->save(); $this->activeResource = null; diff --git a/src/applications/drydock/blueprint/DrydockLocalHostBlueprint.php b/src/applications/drydock/blueprint/DrydockLocalHostBlueprint.php index af012589a5..0ea16fc3e7 100644 --- a/src/applications/drydock/blueprint/DrydockLocalHostBlueprint.php +++ b/src/applications/drydock/blueprint/DrydockLocalHostBlueprint.php @@ -30,6 +30,7 @@ final class DrydockLocalHostBlueprint extends DrydockBlueprint { $resource = $this->newResourceTemplate('localhost'); $resource->setStatus(DrydockResourceStatus::STATUS_OPEN); + $resource->setAttribute('path', $path); $resource->save(); return $resource; @@ -38,6 +39,15 @@ final class DrydockLocalHostBlueprint extends DrydockBlueprint { protected function executeAcquireLease( DrydockResource $resource, DrydockLease $lease) { + + $lease_id = $lease->getID(); + + $cmd = $lease->getInterface('command'); + $cmd->execx('mkdir %s', $lease_id); + + $lease->setAttribute('path', $resource->getAttribute('path').'/'.$lease_id); + $lease->save(); + return; } diff --git a/src/applications/drydock/blueprint/DrydockWorkingCopyBlueprint.php b/src/applications/drydock/blueprint/DrydockWorkingCopyBlueprint.php new file mode 100644 index 0000000000..81c0d9e5d3 --- /dev/null +++ b/src/applications/drydock/blueprint/DrydockWorkingCopyBlueprint.php @@ -0,0 +1,69 @@ +getAttribute('repositoryID'); + if (!$repository_id) { + throw new Exception( + "Lease is missing required 'repositoryID' attribute."); + } + + $repository = id(new PhabricatorRepository())->load($repository_id); + + if (!$repository) { + throw new Exception( + "Repository '{$repository_id}' does not exist!"); + } + + switch ($repository->getVersionControlSystem()) { + case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: + break; + default: + throw new Exception("Unsupported VCS!"); + } + + $host_lease = id(new DrydockLease()) + ->setResourceType('host') + ->waitUntilActive(); + + $path = $host_lease->getAttribute('path').'/'.$repository->getCallsign(); + + $cmd = $host_lease->getInterface('command'); + $cmd->execx( + 'git clone --origin origin %s %s', + $repository->getRemoteURI(), + $path); + + $resource = $this->newResourceTemplate($repository->getCallsign()); + $resource->setStatus(DrydockResourceStatus::STATUS_OPEN); + $resource->setAttribute('lease.host', $host_lease->getID()); + $resource->setAttribute('path', $path); + $resource->save(); + + return $resource; + } + + protected function executeAcquireLease( + DrydockResource $resource, + DrydockLease $lease) { + return; + } + + public function getType() { + return 'working-copy'; + } + + public function getInterface( + DrydockResource $resource, + DrydockLease $lease, + $type) { + + throw new Exception("No interface of type '{$type}'."); + } + +} diff --git a/src/applications/drydock/storage/DrydockLease.php b/src/applications/drydock/storage/DrydockLease.php index 68a93b67d7..de0ebf9bf6 100644 --- a/src/applications/drydock/storage/DrydockLease.php +++ b/src/applications/drydock/storage/DrydockLease.php @@ -26,12 +26,12 @@ final class DrydockLease extends DrydockDAO { } public function setAttribute($key, $value) { - $this->attributes[$key] = $value; + $this->attributes[strtolower($key)] = $value; return $this; } public function getAttribute($key, $default = null) { - return idx($this->attributes, $key, $default); + return idx($this->attributes, strtolower($key), $default); } public function generatePHID() { @@ -150,6 +150,10 @@ final class DrydockLease extends DrydockDAO { } public function waitUntilActive() { + if (!$this->getID()) { + $this->queueForActivation(); + } + self::waitForLeases(array($this)); return $this; }