public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $resource_type = $args->getArg('type');
     if (!$resource_type) {
         throw new PhutilArgumentUsageException(pht('Specify a resource type with `%s`.', '--type'));
     }
     $until = $args->getArg('until');
     if (strlen($until)) {
         $until = strtotime($until);
         if ($until <= 0) {
             throw new PhutilArgumentUsageException(pht('Unable to parse argument to "%s".', '--until'));
         }
     }
     $attributes = $args->getArg('attributes');
     if ($attributes) {
         $options = new PhutilSimpleOptions();
         $options->setCaseSensitive(true);
         $attributes = $options->parse($attributes);
     }
     $lease = id(new DrydockLease())->setResourceType($resource_type);
     if ($attributes) {
         $lease->setAttributes($attributes);
     }
     if ($until) {
         $lease->setUntil($until);
     }
     $lease->queueForActivation();
     echo tsprintf("%s\n", pht('Waiting for daemons to activate lease...'));
     $lease->waitUntilActive();
     echo tsprintf("%s\n", pht('Activated lease "%s".', $lease->getID()));
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $resource_name = $args->getArg('name');
     if (!$resource_name) {
         throw new PhutilArgumentUsageException('Specify a resource name with `--name`.');
     }
     $blueprint_id = $args->getArg('blueprint');
     if (!$blueprint_id) {
         throw new PhutilArgumentUsageException('Specify a blueprint ID with `--blueprint`.');
     }
     $attributes = $args->getArg('attributes');
     if ($attributes) {
         $options = new PhutilSimpleOptions();
         $options->setCaseSensitive(true);
         $attributes = $options->parse($attributes);
     }
     $viewer = $this->getViewer();
     $blueprint = id(new DrydockBlueprintQuery())->setViewer($viewer)->withIDs(array($blueprint_id))->executeOne();
     if (!$blueprint) {
         throw new PhutilArgumentUsageException('Specified blueprint does not exist.');
     }
     $resource = id(new DrydockResource())->setBlueprintPHID($blueprint->getPHID())->setType($blueprint->getImplementation()->getType())->setName($resource_name)->setStatus(DrydockResourceStatus::STATUS_OPEN);
     if ($attributes) {
         $resource->setAttributes($attributes);
     }
     $resource->save();
     $console->writeOut("Created Resource %s\n", $resource->getID());
     return 0;
 }
 public function testSimpleOptionsCaseParse()
 {
     $map = array('legs=4, LEGS=8, LeGs' => array('legs' => '4', 'LEGS' => '8', 'LeGs' => true));
     foreach ($map as $string => $expect) {
         $parser = new PhutilSimpleOptions();
         $parser->setCaseSensitive(true);
         $this->assertEqual($expect, $parser->parse($string), "Correct case-sensitive parse of '{$string}'");
     }
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $resource_type = $args->getArg('type');
     if (!$resource_type) {
         throw new PhutilArgumentUsageException(pht('Specify a resource type with `%s`.', '--type'));
     }
     $until = $args->getArg('until');
     if (strlen($until)) {
         $until = strtotime($until);
         if ($until <= 0) {
             throw new PhutilArgumentUsageException(pht('Unable to parse argument to "%s".', '--until'));
         }
     }
     $attributes = $args->getArg('attributes');
     if ($attributes) {
         $options = new PhutilSimpleOptions();
         $options->setCaseSensitive(true);
         $attributes = $options->parse($attributes);
     }
     $lease = id(new DrydockLease())->setResourceType($resource_type);
     $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
     $lease->setAuthorizingPHID($drydock_phid);
     // 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 ($attributes) {
         $lease->setAttributes($attributes);
     }
     if ($until) {
         $lease->setUntil($until);
     }
     $lease->queueForActivation();
     echo tsprintf("%s\n", pht('Waiting for daemons to activate lease...'));
     $lease->waitUntilActive();
     echo tsprintf("%s\n", pht('Activated lease "%s".', $lease->getID()));
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $resource_type = $args->getArg('type');
     if (!$resource_type) {
         throw new PhutilArgumentUsageException(pht('Specify a resource type with `%s`.', '--type'));
     }
     $attributes = $args->getArg('attributes');
     if ($attributes) {
         $options = new PhutilSimpleOptions();
         $options->setCaseSensitive(true);
         $attributes = $options->parse($attributes);
     }
     $lease = id(new DrydockLease())->setResourceType($resource_type);
     if ($attributes) {
         $lease->setAttributes($attributes);
     }
     $lease->queueForActivation()->waitUntilActive();
     $console->writeOut("%s\n", pht('Acquired Lease %s', $lease->getID()));
     return 0;
 }