2012-11-27 21:48:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DrydockWorkingCopyBlueprint extends DrydockBlueprint {
|
|
|
|
|
|
|
|
public function isEnabled() {
|
2012-12-13 03:42:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function canAllocateLease(
|
|
|
|
DrydockResource $resource,
|
|
|
|
DrydockLease $lease) {
|
|
|
|
|
|
|
|
$resource_repo = $resource->getAttribute('repositoryID');
|
|
|
|
$lease_repo = $lease->getAttribute('repositoryID');
|
|
|
|
|
2012-12-17 22:53:32 +01:00
|
|
|
return ($resource_repo && $lease_repo && ($resource_repo == $lease_repo));
|
2012-12-13 03:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldAllocateLease(
|
|
|
|
DrydockResource $resource,
|
|
|
|
DrydockLease $lease,
|
|
|
|
array $other_leases) {
|
|
|
|
|
|
|
|
return !$other_leases;
|
2012-11-27 21:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function executeAllocateResource(DrydockLease $lease) {
|
|
|
|
$repository_id = $lease->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();
|
|
|
|
|
2012-12-13 03:42:12 +01:00
|
|
|
$path = $host_lease->getAttribute('path').$repository->getCallsign();
|
|
|
|
|
|
|
|
$this->log(
|
|
|
|
pht('Cloning %s into %s....', $repository->getCallsign(), $path));
|
2012-11-27 21:48:14 +01:00
|
|
|
|
|
|
|
$cmd = $host_lease->getInterface('command');
|
|
|
|
$cmd->execx(
|
|
|
|
'git clone --origin origin %s %s',
|
|
|
|
$repository->getRemoteURI(),
|
|
|
|
$path);
|
|
|
|
|
2012-12-13 03:42:12 +01:00
|
|
|
$this->log(pht('Complete.'));
|
|
|
|
|
2012-12-17 23:47:21 +01:00
|
|
|
$resource = $this->newResourceTemplate(
|
|
|
|
'Working Copy ('.$repository->getCallsign().')');
|
2012-11-27 21:48:14 +01:00
|
|
|
$resource->setStatus(DrydockResourceStatus::STATUS_OPEN);
|
|
|
|
$resource->setAttribute('lease.host', $host_lease->getID());
|
|
|
|
$resource->setAttribute('path', $path);
|
2012-12-17 22:53:32 +01:00
|
|
|
$resource->setAttribute('repositoryID', $repository->getID());
|
2012-11-27 21:48:14 +01:00
|
|
|
$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) {
|
|
|
|
|
2012-12-17 22:43:26 +01:00
|
|
|
switch ($type) {
|
|
|
|
case 'command':
|
|
|
|
return $this
|
|
|
|
->loadLease($resource->getAttribute('lease.host'))
|
|
|
|
->getInterface($type);
|
|
|
|
}
|
|
|
|
|
2012-11-27 21:48:14 +01:00
|
|
|
throw new Exception("No interface of type '{$type}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|