public function execute(PhutilArgumentParser $args)
 {
     $ids = $args->getArg('id');
     if (!$ids) {
         throw new PhutilArgumentUsageException(pht('Specify one or more lease IDs to release with "%s".', '--id'));
     }
     $viewer = $this->getViewer();
     $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
     $leases = id(new DrydockLeaseQuery())->setViewer($viewer)->withIDs($ids)->execute();
     PhabricatorWorker::setRunAllTasksInProcess(true);
     foreach ($ids as $id) {
         $lease = idx($leases, $id);
         if (!$lease) {
             echo tsprintf("%s\n", pht('Lease "%s" does not exist.', $id));
             continue;
         }
         if (!$lease->canRelease()) {
             echo tsprintf("%s\n", pht('Lease "%s" is not releasable.', $id));
             continue;
         }
         $command = DrydockCommand::initializeNewCommand($viewer)->setTargetPHID($lease->getPHID())->setAuthorPHID($drydock_phid)->setCommand(DrydockCommand::COMMAND_RELEASE)->save();
         $lease->scheduleUpdate();
         echo tsprintf("%s\n", pht('Scheduled release of lease "%s".', $id));
     }
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $names = $args->getArg('buildable');
     if (count($names) != 1) {
         throw new PhutilArgumentUsageException(pht('Specify exactly one buildable object, by object name.'));
     }
     $name = head($names);
     $buildable = id(new PhabricatorObjectQuery())->setViewer($viewer)->withNames($names)->executeOne();
     if (!$buildable) {
         throw new PhutilArgumentUsageException(pht('No such buildable "%s"!', $name));
     }
     if (!$buildable instanceof HarbormasterBuildableInterface) {
         throw new PhutilArgumentUsageException(pht('Object "%s" is not a buildable!', $name));
     }
     $plan_id = $args->getArg('plan');
     if (!$plan_id) {
         throw new PhutilArgumentUsageException(pht('Use --plan to specify a build plan to run.'));
     }
     $plan = id(new HarbormasterBuildPlanQuery())->setViewer($viewer)->withIDs(array($plan_id))->executeOne();
     if (!$plan) {
         throw new PhutilArgumentUsageException(pht('Build plan "%s" does not exist.', $plan_id));
     }
     $console = PhutilConsole::getConsole();
     $buildable = HarbormasterBuildable::initializeNewBuildable($viewer)->setIsManualBuildable(true)->setBuildablePHID($buildable->getHarbormasterBuildablePHID())->setContainerPHID($buildable->getHarbormasterContainerPHID())->save();
     $console->writeOut("%s\n", pht('Applying plan %s to new buildable %s...', $plan->getID(), 'B' . $buildable->getID()));
     $console->writeOut("\n    %s\n\n", PhabricatorEnv::getProductionURI('/B' . $buildable->getID()));
     PhabricatorWorker::setRunAllTasksInProcess(true);
     $buildable->applyPlan($plan);
     $console->writeOut("%s\n", pht('Done.'));
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $viewer = $this->getViewer();
     $subscription_phid = $args->getArg('subscription');
     if (!$subscription_phid) {
         throw new PhutilArgumentUsageException(pht('Specify which subscription to invoice with %s.', '--subscription'));
     }
     $subscription = id(new PhortuneSubscriptionQuery())->setViewer($viewer)->withPHIDs(array($subscription_phid))->needTriggers(true)->executeOne();
     if (!$subscription) {
         throw new PhutilArgumentUsageException(pht('Unable to load subscription with PHID "%s".', $subscription_phid));
     }
     $now = $args->getArg('now');
     $now = $this->parseTimeArgument($now);
     if (!$now) {
         $now = PhabricatorTime::getNow();
     }
     $time_guard = PhabricatorTime::pushTime($now, date_default_timezone_get());
     $console->writeOut("%s\n", pht('Set current time to %s.', phabricator_datetime(PhabricatorTime::getNow(), $viewer)));
     $auto_range = $args->getArg('auto-range');
     $last_arg = $args->getArg('last');
     $next_arg = $args->getARg('next');
     if (!$auto_range && !$last_arg && !$next_arg) {
         throw new PhutilArgumentUsageException(pht('Specify a billing range with %s and %s, or use %s.', '--last', '--next', '--auto-range'));
     } else {
         if (!$auto_range & (!$last_arg || !$next_arg)) {
             throw new PhutilArgumentUsageException(pht('When specifying %s or %s, you must specify both arguments ' . 'to define the beginning and end of the billing range.', '--last', '--next'));
         } else {
             if (!$auto_range && ($last_arg && $next_arg)) {
                 $last_time = $this->parseTimeArgument($args->getArg('last'));
                 $next_time = $this->parseTimeArgument($args->getArg('next'));
             } else {
                 if ($auto_range && ($last_arg || $next_arg)) {
                     throw new PhutilArgumentUsageException(pht('Use either %s or %s and %s to specify the ' . 'billing range, but not both.', '--auto-range', '--last', '--next'));
                 } else {
                     $trigger = $subscription->getTrigger();
                     $event = $trigger->getEvent();
                     if (!$event) {
                         throw new PhutilArgumentUsageException(pht('Unable to calculate %s, this subscription has not been ' . 'scheduled for billing yet. Wait for the trigger daemon to ' . 'schedule the subscription.', '--auto-range'));
                     }
                     $last_time = $event->getLastEventEpoch();
                     $next_time = $event->getNextEventEpoch();
                 }
             }
         }
     }
     $console->writeOut("%s\n", pht('Preparing to invoice subscription "%s" from %s to %s.', $subscription->getSubscriptionName(), $last_time ? phabricator_datetime($last_time, $viewer) : pht('subscription creation'), phabricator_datetime($next_time, $viewer)));
     PhabricatorWorker::setRunAllTasksInProcess(true);
     if (!$args->getArg('force')) {
         $console->writeOut("**<bg:yellow> %s </bg>**\n%s\n", pht('WARNING'), phutil_console_wrap(pht('Manually invoicing will double bill payment accounts if the ' . 'range overlaps an existing or future invoice. This script is ' . 'intended for testing and development, and should not be part ' . 'of routine billing operations. If you continue, you may ' . 'incorrectly overcharge customers.')));
         if (!phutil_console_confirm(pht('Really invoice this subscription?'))) {
             throw new Exception(pht('Declining to invoice.'));
         }
     }
     PhabricatorWorker::scheduleTask('PhortuneSubscriptionWorker', array('subscriptionPHID' => $subscription->getPHID(), 'trigger.last-epoch' => $last_time, 'trigger.this-epoch' => $next_time, 'manual' => true), array('objectPHID' => $subscription->getPHID()));
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $viewer = $this->getViewer();
     $tos = $args->getArg('to');
     $body = $args->getArg('body');
     PhabricatorWorker::setRunAllTasksInProcess(true);
     PhabricatorSMSImplementationAdapter::sendSMS($tos, $body);
     $console->writeErr("%s\n\n    phabricator/ \$ ./bin/sms list-outbound \n\n", pht('Send completed! You can view the list of SMS messages sent by ' . 'running this command:'));
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $is_all = $args->getArg('all');
     $is_type = $args->getArg('type');
     $is_force = $args->getArg('force');
     $obj_names = $args->getArg('objects');
     if ($obj_names && ($is_all || $is_type)) {
         throw new PhutilArgumentUsageException(pht("You can not name objects to index alongside the '%s' or '%s' flags.", '--all', '--type'));
     } else {
         if (!$obj_names && !($is_all || $is_type)) {
             throw new PhutilArgumentUsageException(pht("Provide one of '%s', '%s' or a list of object names.", '--all', '--type'));
         }
     }
     if ($obj_names) {
         $phids = $this->loadPHIDsByNames($obj_names);
     } else {
         $phids = $this->loadPHIDsByTypes($is_type);
     }
     if (!$phids) {
         throw new PhutilArgumentUsageException(pht('Nothing to index!'));
     }
     if ($args->getArg('background')) {
         $is_background = true;
     } else {
         PhabricatorWorker::setRunAllTasksInProcess(true);
         $is_background = false;
     }
     if (!$is_background) {
         $console->writeOut("%s\n", pht('Run this workflow with "%s" to queue tasks for the daemon workers.', '--background'));
     }
     $groups = phid_group_by_type($phids);
     foreach ($groups as $group_type => $group) {
         $console->writeOut("%s\n", pht('Indexing %d object(s) of type %s.', count($group), $group_type));
     }
     $bar = id(new PhutilConsoleProgressBar())->setTotal(count($phids));
     $parameters = array('force' => $is_force);
     $any_success = false;
     foreach ($phids as $phid) {
         try {
             PhabricatorSearchWorker::queueDocumentForIndexing($phid, $parameters);
             $any_success = true;
         } catch (Exception $ex) {
             phlog($ex);
         }
         $bar->update(1);
     }
     $bar->done();
     if (!$any_success) {
         throw new Exception(pht('Failed to rebuild search index for any documents.'));
     }
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $viewer = $this->getViewer();
     $triggers = $this->loadTriggers($args);
     $now = $args->getArg('now');
     $now = $this->parseTimeArgument($now);
     if (!$now) {
         $now = PhabricatorTime::getNow();
     }
     $time_guard = PhabricatorTime::pushTime($now, date_default_timezone_get());
     $console->writeOut("%s\n", pht('Set current time to %s.', phabricator_datetime(PhabricatorTime::getNow(), $viewer)));
     $last_time = $this->parseTimeArgument($args->getArg('last'));
     $next_time = $this->parseTimeArgument($args->getArg('next'));
     PhabricatorWorker::setRunAllTasksInProcess(true);
     foreach ($triggers as $trigger) {
         $console->writeOut("%s\n", pht('Executing trigger %s.', $this->describeTrigger($trigger)));
         $event = $trigger->getEvent();
         if ($event) {
             if (!$last_time) {
                 $last_time = $event->getLastEventEpoch();
             }
             if (!$next_time) {
                 $next_time = $event->getNextEventEpoch();
             }
         }
         if (!$next_time) {
             $console->writeOut("%s\n", pht('Trigger is not scheduled to execute. Use --next to simluate ' . 'a scheduled event.'));
             continue;
         } else {
             $console->writeOut("%s\n", pht('Executing event as though it was scheduled to execute at %s.', phabricator_datetime($next_time, $viewer)));
         }
         if (!$last_time) {
             $console->writeOut("%s\n", pht('Executing event as though it never previously executed.'));
         } else {
             $console->writeOut("%s\n", pht('Executing event as though it previously executed at %s.', phabricator_datetime($last_time, $viewer)));
         }
         $trigger->executeTrigger($last_time, $next_time);
         $reschedule_time = $trigger->getNextEventEpoch($next_time, $is_reschedule = true);
         if (!$reschedule_time) {
             $console->writeOut("%s\n", pht('After executing under these conditions, this event would never ' . 'execute again.'));
         } else {
             $console->writeOut("%s\n", pht('After executing under these conditions, this event would ' . 'next execute at %s.', phabricator_datetime($reschedule_time, $viewer)));
         }
     }
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $ids = $args->getArg('id');
     if (!$ids) {
         throw new PhutilArgumentUsageException(pht('Specify one or more lease IDs to update with "%s".', '--id'));
     }
     $leases = id(new DrydockLeaseQuery())->setViewer($viewer)->withIDs($ids)->execute();
     PhabricatorWorker::setRunAllTasksInProcess(true);
     foreach ($ids as $id) {
         $lease = idx($leases, $id);
         if (!$lease) {
             echo tsprintf("%s\n", pht('Lease "%s" does not exist.', $id));
             continue;
         }
         echo tsprintf("%s\n", pht('Updating lease "%s".', $id));
         $lease->scheduleUpdate();
     }
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $is_all = $args->getArg('all');
     $is_type = $args->getArg('type');
     $obj_names = $args->getArg('objects');
     if ($obj_names && ($is_all || $is_type)) {
         throw new PhutilArgumentUsageException("You can not name objects to index alongside the '--all' or '--type' " . "flags.");
     } else {
         if (!$obj_names && !($is_all || $is_type)) {
             throw new PhutilArgumentUsageException("Provide one of '--all', '--type' or a list of object names.");
         }
     }
     if ($obj_names) {
         $phids = $this->loadPHIDsByNames($obj_names);
     } else {
         $phids = $this->loadPHIDsByTypes($is_type);
     }
     if (!$phids) {
         throw new PhutilArgumentUsageException('Nothing to index!');
     }
     if ($args->getArg('background')) {
         $is_background = true;
     } else {
         PhabricatorWorker::setRunAllTasksInProcess(true);
         $is_background = false;
     }
     if (!$is_background) {
         $console->writeOut("%s\n", pht('Run this workflow with "--background" to queue tasks for the ' . 'daemon workers.'));
     }
     $groups = phid_group_by_type($phids);
     foreach ($groups as $group_type => $group) {
         $console->writeOut("%s\n", pht('Indexing %d object(s) of type %s.', count($group), $group_type));
     }
     $bar = id(new PhutilConsoleProgressBar())->setTotal(count($phids));
     $indexer = new PhabricatorSearchIndexer();
     foreach ($phids as $phid) {
         $indexer->queueDocumentForIndexing($phid);
         $bar->update(1);
     }
     $bar->done();
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $force_update = $args->getArg('force');
     $names = $args->getArg('buildable');
     if (count($names) != 1) {
         throw new PhutilArgumentUsageException(pht('Specify exactly one buildable, by object name.'));
     }
     $buildable = id(new PhabricatorObjectQuery())->setViewer($viewer)->withNames($names)->executeOne();
     if (!$buildable) {
         throw new PhutilArgumentUsageException(pht('No such buildable "%s"!', head($names)));
     }
     if (!$buildable instanceof HarbormasterBuildable) {
         throw new PhutilArgumentUsageException(pht('Object "%s" is not a Harbormaster Buildable!', head($names)));
     }
     // Reload the buildable directly to get builds.
     $buildable = id(new HarbormasterBuildableQuery())->setViewer($viewer)->withIDs(array($buildable->getID()))->needBuilds(true)->executeOne();
     $builds = $buildable->getBuilds();
     $builds = mpull($builds, null, 'getID');
     $build_id = $args->getArg('build');
     if ($build_id) {
         $builds = array_select_keys($builds, array($build_id));
         if (!$builds) {
             throw new PhutilArgumentUsageException(pht('The specified buildable does not have a build with ID "%s".', $build_id));
         }
     }
     $console = PhutilConsole::getConsole();
     if (!$args->getArg('background')) {
         PhabricatorWorker::setRunAllTasksInProcess(true);
     }
     foreach ($builds as $build) {
         $console->writeOut("%s\n", pht('Updating build %d of buildable %s...', $build->getID(), $buildable->getMonogram()));
         $engine = id(new HarbormasterBuildEngine())->setViewer($viewer)->setBuild($build);
         if ($force_update) {
             $engine->setForceBuildableUpdate(true);
         }
         $engine->continueBuild();
     }
     $console->writeOut("%s\n", pht('Done.'));
     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);
     }
     PhabricatorWorker::setRunAllTasksInProcess(true);
     $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;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
     PhabricatorWorker::setRunAllTasksInProcess(true);
     $resources = id(new DrydockResourceQuery())->setViewer($viewer)->withStatuses(array(DrydockResourceStatus::STATUS_ACTIVE))->execute();
     foreach ($resources as $resource) {
         $command = DrydockCommand::initializeNewCommand($viewer)->setTargetPHID($resource->getPHID())->setAuthorPHID($drydock_phid)->setCommand(DrydockCommand::COMMAND_RECLAIM)->save();
         $resource->scheduleUpdate();
         $resource = $resource->reload();
         $name = pht('Resource %d: %s', $resource->getID(), $resource->getResourceName());
         switch ($resource->getStatus()) {
             case DrydockResourceStatus::STATUS_RELEASED:
             case DrydockResourceStatus::STATUS_DESTROYED:
                 echo tsprintf("%s\n", pht('Resource "%s" was reclaimed.', $name));
                 break;
             default:
                 echo tsprintf("%s\n", pht('Resource "%s" could not be reclaimed.', $name));
                 break;
         }
     }
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $viewer = $this->getViewer();
     $key = $args->getArg('key');
     if (count($key) < 1) {
         throw new PhutilArgumentUsageException(pht('Specify a story key to republish.'));
     } else {
         if (count($key) > 1) {
             throw new PhutilArgumentUsageException(pht('Specify exactly one story key to republish.'));
         }
     }
     $key = head($key);
     $story = id(new PhabricatorFeedQuery())->setViewer($viewer)->withChronologicalKeys(array($key))->executeOne();
     if (!$story) {
         throw new PhutilArgumentUsageException(pht('No story exists with key "%s"!', $key));
     }
     $console->writeOut("%s\n", pht('Republishing story...'));
     PhabricatorWorker::setRunAllTasksInProcess(true);
     PhabricatorWorker::scheduleTask('FeedPublisherWorker', array('key' => $key));
     $console->writeOut("%s\n", pht('Done.'));
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $console = PhutilConsole::getConsole();
     $viewer = $this->getViewer();
     $from = $args->getArg('from');
     if ($from) {
         $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames(array($from))->executeOne();
         if (!$user) {
             throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $from));
         }
         $from = $user;
     }
     $tos = $args->getArg('to');
     $ccs = $args->getArg('cc');
     if (!$tos && !$ccs) {
         throw new PhutilArgumentUsageException(pht('Specify one or more users to send mail to with `%s` and `%s`.', '--to', '--cc'));
     }
     $names = array_merge($tos, $ccs);
     $users = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames($names)->execute();
     $users = mpull($users, null, 'getUsername');
     foreach ($tos as $key => $username) {
         if (empty($users[$username])) {
             throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $username));
         }
         $tos[$key] = $users[$username]->getPHID();
     }
     foreach ($ccs as $key => $username) {
         if (empty($users[$username])) {
             throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $username));
         }
         $ccs[$key] = $users[$username]->getPHID();
     }
     $subject = $args->getArg('subject');
     if ($subject === null) {
         $subject = pht('No Subject');
     }
     $tags = $args->getArg('tag');
     $attach = $args->getArg('attach');
     $is_bulk = $args->getArg('bulk');
     $console->writeErr("%s\n", pht('Reading message body from stdin...'));
     $body = file_get_contents('php://stdin');
     $mail = id(new PhabricatorMetaMTAMail())->addTos($tos)->addCCs($ccs)->setSubject($subject)->setBody($body)->setIsBulk($is_bulk)->setMailTags($tags);
     if ($args->getArg('html')) {
         $mail->setBody(pht('(This is a placeholder plaintext email body for a test message ' . 'sent with %s.)', '--html'));
         $mail->setHTMLBody($body);
     } else {
         $mail->setBody($body);
     }
     if ($from) {
         $mail->setFrom($from->getPHID());
     }
     foreach ($attach as $attachment) {
         $data = Filesystem::readFile($attachment);
         $name = basename($attachment);
         $mime = Filesystem::getMimeType($attachment);
         $file = new PhabricatorMetaMTAAttachment($data, $name, $mime);
         $mail->addAttachment($file);
     }
     PhabricatorWorker::setRunAllTasksInProcess(true);
     $mail->save();
     $console->writeErr("%s\n\n    phabricator/ \$ ./bin/mail show-outbound --id %d\n\n", pht('Mail sent! You can view details by running this command:'), $mail->getID());
 }
 /**
  * Get a list of @{class:HarbormasterBuildTarget} objects for a list of
  * autotarget keys.
  *
  * If some targets or builds do not exist, they are created.
  *
  * @param HarbormasterBuildable A buildable.
  * @param map<string, object> Map of keys to steps.
  * @return map<string, object> Map of keys to targets.
  */
 private function generateBuildTargetMap(HarbormasterBuildable $buildable, array $step_map)
 {
     $viewer = $this->getViewer();
     $plan_map = mgroup($step_map, 'getBuildPlanPHID');
     $builds = id(new HarbormasterBuildQuery())->setViewer($viewer)->withBuildablePHIDs(array($buildable->getPHID()))->withBuildPlanPHIDs(array_keys($plan_map))->needBuildTargets(true)->execute();
     $autobuilds = array();
     foreach ($builds as $build) {
         $plan_key = $build->getBuildPlan()->getPlanAutoKey();
         $autobuilds[$plan_key] = $build;
     }
     $new_builds = array();
     foreach ($plan_map as $plan_phid => $steps) {
         $plan = head($steps)->getBuildPlan();
         $plan_key = $plan->getPlanAutoKey();
         $build = idx($autobuilds, $plan_key);
         if ($build) {
             // We already have a build for this set of targets, so we don't need
             // to do any work. (It's possible the build is an older build that
             // doesn't have all of the right targets if new autotargets were
             // recently introduced, but we don't currently try to construct them.)
             continue;
         }
         // NOTE: Normally, `applyPlan()` does not actually generate targets.
         // We need to apply the plan in-process to perform target generation.
         // This is fine as long as autotargets are empty containers that don't
         // do any work, which they always should be.
         PhabricatorWorker::setRunAllTasksInProcess(true);
         try {
             // NOTE: We might race another process here to create the same build
             // with the same `planAutoKey`. The database will prevent this and
             // using autotargets only currently makes sense if you just created the
             // resource and "own" it, so we don't try to handle this, but may need
             // to be more careful here if use of autotargets expands.
             $build = $buildable->applyPlan($plan, array());
             PhabricatorWorker::setRunAllTasksInProcess(false);
         } catch (Exception $ex) {
             PhabricatorWorker::setRunAllTasksInProcess(false);
             throw $ex;
         }
         $new_builds[] = $build;
     }
     if ($new_builds) {
         $all_targets = id(new HarbormasterBuildTargetQuery())->setViewer($viewer)->withBuildPHIDs(mpull($new_builds, 'getPHID'))->execute();
     } else {
         $all_targets = array();
     }
     foreach ($builds as $build) {
         foreach ($build->getBuildTargets() as $target) {
             $all_targets[] = $target;
         }
     }
     $target_map = array();
     foreach ($all_targets as $target) {
         $target_key = $target->getImplementation()->getBuildStepAutotargetStepKey();
         if (!$target_key) {
             continue;
         }
         $target_map[$target_key] = $target;
     }
     $target_map = array_select_keys($target_map, array_keys($step_map));
     return $target_map;
 }