1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 14:52:41 +01:00

Allow "bin/drydock lease" to acquire many identical leases with "--count N"

Summary: Ref T13676. This makes it easier to create resource pressure without juggling a big pile of terminals.

Test Plan: Used `bin/drydock lease --count 5 ...` to acquire 5 leases.

Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13676

Differential Revision: https://secure.phabricator.com/D21800
This commit is contained in:
epriestley 2022-05-03 14:05:01 -07:00
parent 9ec6677c02
commit 30c3d1e929

View file

@ -26,6 +26,12 @@ final class DrydockManagementLeaseWorkflow
'JSON file with lease attributes. Use "-" to read attributes '. 'JSON file with lease attributes. Use "-" to read attributes '.
'from stdin.'), 'from stdin.'),
), ),
array(
'name' => 'count',
'param' => 'N',
'default' => 1,
'help' => pht('Lease a given number of identical resources.'),
),
)); ));
} }
@ -33,11 +39,10 @@ final class DrydockManagementLeaseWorkflow
$viewer = $this->getViewer(); $viewer = $this->getViewer();
$resource_type = $args->getArg('type'); $resource_type = $args->getArg('type');
if (!$resource_type) { if (!phutil_nonempty_string($resource_type)) {
throw new PhutilArgumentUsageException( throw new PhutilArgumentUsageException(
pht( pht(
'Specify a resource type with `%s`.', 'Specify a resource type with "--type".'));
'--type'));
} }
$until = $args->getArg('until'); $until = $args->getArg('until');
@ -46,17 +51,24 @@ final class DrydockManagementLeaseWorkflow
if ($until <= 0) { if ($until <= 0) {
throw new PhutilArgumentUsageException( throw new PhutilArgumentUsageException(
pht( pht(
'Unable to parse argument to "%s".', 'Unable to parse argument to "--until".'));
'--until'));
} }
} }
$count = $args->getArgAsInteger('count');
if ($count < 1) {
throw new PhutilArgumentUsageException(
pht(
'Value provided to "--count" must be a nonzero, positive '.
'number.'));
}
$attributes_file = $args->getArg('attributes'); $attributes_file = $args->getArg('attributes');
if (phutil_nonempty_string($attributes_file)) { if (phutil_nonempty_string($attributes_file)) {
if ($attributes_file == '-') { if ($attributes_file == '-') {
echo tsprintf( echo tsprintf(
"%s\n", "%s\n",
'Reading JSON attributes from stdin...'); pht('Reading JSON attributes from stdin...'));
$data = file_get_contents('php://stdin'); $data = file_get_contents('php://stdin');
} else { } else {
$data = Filesystem::readFile($attributes_file); $data = Filesystem::readFile($attributes_file);
@ -67,39 +79,45 @@ final class DrydockManagementLeaseWorkflow
$attributes = array(); $attributes = array();
} }
$lease = id(new DrydockLease()) $leases = array();
->setResourceType($resource_type); for ($idx = 0; $idx < $count; $idx++) {
$lease = id(new DrydockLease())
->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
// workflow so maybe it's fine. Do we even need `bin/drydock lease` in
// 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);
if ($until) {
$lease->setUntil($until);
}
// If something fatals or the user interrupts the process (for example,
// with "^C"), release the lease. We'll cancel this below, if the lease
// actually activates.
$lease->setReleaseOnDestruction(true);
$leases[] = $lease;
} }
// TODO: This is not hugely scalable, although this is a debugging workflow
// so maybe it's fine. Do we even need `bin/drydock lease` in 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);
if ($until) {
$lease->setUntil($until);
}
// If something fatals or the user interrupts the process (for example,
// with "^C"), release the lease. We'll cancel this below, if the lease
// actually activates.
$lease->setReleaseOnDestruction(true);
// TODO: This would probably be better handled with PhutilSignalRouter, // TODO: This would probably be better handled with PhutilSignalRouter,
// but it currently doesn't route SIGINT. We're initializing it to setup // but it currently doesn't route SIGINT. We're initializing it to setup
// SIGTERM handling and make eventual migration easier. // SIGTERM handling and make eventual migration easier.
@ -107,29 +125,52 @@ final class DrydockManagementLeaseWorkflow
pcntl_signal(SIGINT, array($this, 'didReceiveInterrupt')); pcntl_signal(SIGINT, array($this, 'didReceiveInterrupt'));
$t_start = microtime(true); $t_start = microtime(true);
$lease->queueForActivation();
echo tsprintf( echo tsprintf(
"%s\n\n __%s__\n\n%s\n", "%s\n\n",
pht('Queued lease for activation:'), pht('Leases queued for activation:'));
PhabricatorEnv::getProductionURI($lease->getURI()),
pht('Waiting for daemons to activate lease...'));
$this->waitUntilActive($lease); foreach ($leases as $lease) {
$lease->queueForActivation();
echo tsprintf(
" __%s__\n",
PhabricatorEnv::getProductionURI($lease->getURI()));
}
echo tsprintf(
"\n%s\n\n",
pht('Waiting for daemons to activate leases...'));
foreach ($leases as $lease) {
$this->waitUntilActive($lease);
}
// Now that we've survived activation and the lease is good, make it // Now that we've survived activation and the lease is good, make it
// durable. // durable.
$lease->setReleaseOnDestruction(false); foreach ($leases as $lease) {
$lease->setReleaseOnDestruction(false);
}
$t_end = microtime(true); $t_end = microtime(true);
echo tsprintf( echo tsprintf(
"%s\n\n %s\n\n%s\n", "\n%s\n\n",
pht( pht(
'Activation complete. This lease is permanent until manually '. 'Activation complete. Leases are permanent until manually '.
'released with:'), 'released with:'));
pht('$ ./bin/drydock release-lease --id %d', $lease->getID()),
foreach ($leases as $lease) {
echo tsprintf(
" %s\n",
pht('$ ./bin/drydock release-lease --id %d', $lease->getID()));
}
echo tsprintf(
"\n%s\n",
pht( pht(
'Lease activated in %sms.', 'Leases activated in %sms.',
new PhutilNumber((int)(($t_end - $t_start) * 1000)))); new PhutilNumber((int)(($t_end - $t_start) * 1000))));
return 0; return 0;
@ -183,7 +224,8 @@ final class DrydockManagementLeaseWorkflow
} }
echo tsprintf( echo tsprintf(
"<%s> %B\n", "(Lease #%d) <%s> %B\n",
$lease->getID(),
$type, $type,
$data); $data);
} }