From 12b3ad83c9421cae247b1740fe438511361fdbee Mon Sep 17 00:00:00 2001 From: Matthew Brabham Date: Thu, 26 Sep 2024 15:17:20 -0700 Subject: [PATCH] Add cron job creation functionality Implemented a new method in `AcquiaCommand.php` to create cron jobs and added a corresponding command in `CronCommand.php`. The new command allows users to create scheduled cron jobs by interacting with the application and environment selection helpers. --- src/Commands/AcquiaCommand.php | 19 +++++++ src/Commands/CronCommand.php | 97 ++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/src/Commands/AcquiaCommand.php b/src/Commands/AcquiaCommand.php index f1ea93b..015064d 100644 --- a/src/Commands/AcquiaCommand.php +++ b/src/Commands/AcquiaCommand.php @@ -356,6 +356,25 @@ protected function getCrons(string $envUuid) { return $cronList; } + /** + * Create a cron job. + * + * @param string $envUuid + * The environment UUID. + * @param string $command + * The cron command to execute. + * @param string $frequency + * The frequency of the cron job. + * @param string $label + * The label for the cron job. + * + * @return \AcquiaCloudApi\Response\OperationResponse + */ + protected function createCron(string $envUuid, string $command, string $frequency, string $label) { + $cron = new Crons($this->client); + return $cron->create($envUuid, $command, $frequency, $label); + } + /** * Create a new Database. * diff --git a/src/Commands/CronCommand.php b/src/Commands/CronCommand.php index 2a129f5..d083213 100644 --- a/src/Commands/CronCommand.php +++ b/src/Commands/CronCommand.php @@ -2,12 +2,11 @@ namespace OsuWams\Commands; - use Consolidation\OutputFormatters\FormatterManager; use Consolidation\OutputFormatters\Options\FormatterOptions; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Symfony\Component\Console\Question\ChoiceQuestion; use Exception; +use Symfony\Component\Console\Question\ChoiceQuestion; class CronCommand extends AcquiaCommand { @@ -18,8 +17,8 @@ public function __construct() { /** * Get a list of Scheduled Jobs. * - * Optional arguments: app,env. If app and/or env are not provided, a helper will ask you to select - * from a generated list. + * Optional arguments: app,env. If app and/or env are not provided, a helper + * will ask you to select from a generated list. * * @command cron:list * @@ -45,7 +44,8 @@ public function listCrons(array $options = [ 'env' => NULL, 'format' => 'table', 'fields' => 'label,command', - ]) { + ] + ) { if (is_null($options['app'])) { $this->say('Getting Applications...'); $appHelper = new ChoiceQuestion('Select which Acquia Cloud Application you want to operate on', $this->getApplicationsId()); @@ -96,4 +96,91 @@ public function listCrons(array $options = [ $formatterManager->write($this->output, $opts->getFormat(), new RowsOfFields($cronList), $opts); } + /** + * Create a new scheduled cron job. + * + * Optional arguments: app, env, domain. + * If app, env, and/or domain are not provided, a helper will ask you to + * select from a generated list. + * + * @command cron:create + * + * @param array $options + * @option $app The Acquia Cloud Application name: prod:shortname + * @option $env The Environment short name: dev|prod|test + * @option $domain The Domain name associated with the application + * + * @return void + */ + public function cronCreate(array $options = [ + 'app' => NULL, + 'env' => NULL, + 'domain' => NULL, + ] + ) { + if (is_null($options['app'])) { + $this->say('Getting Applications...'); + $appHelper = new ChoiceQuestion('Select which Acquia Cloud Application you want to operate on', $this->getApplicationsId()); + $appName = $this->doAsk($appHelper); + } + else { + $appName = $options['app']; + } + // Attempt to get the UUID of this application. + try { + $appUuId = $this->getUuidFromName($appName); + } + catch (Exception $e) { + $this->say('Incorrect Application ID.'); + exit($e->getCode()); + } + if (is_null($options['env'])) { + // Get a list of environments for this App UUID. + $this->writeln('Getting Environment ID\'s...'); + $envList = $this->getEnvironments($appUuId); + // Get the Env for the scheduled jobs. + $envHelper = new ChoiceQuestion('Which Environment do you want to create the cron for...', $envList); + $environment = $this->doAsk($envHelper); + } + else { + $environment = $options['env']; + } + try { + $envUuId = $this->getEnvUuIdFromApp($appUuId, $environment); + } + catch (Exception $e) { + $this->say('Incorrect Environment and Application id.'); + exit($e->getCode()); + } + if (is_null($options['domain'])) { + $this->writeln("Getting list of Domains..."); + $domainList = $this->getDomains($envUuId); + $domainHelper = new ChoiceQuestion("Which domain do you want to make a Cron job for?", $domainList); + $domain = $this->doAsk($domainHelper); + } + else { + $domain = $options['domain']; + } + $site = explode(":", $appName)[1]; + $randomHour = random_int(0, 23); + $randomMinute = random_int(0, 59); + $defaultLabel = "Cron - $domain"; + $defaultCommand = "/usr/local/bin/cron-wrapper.sh $site.$environment https://$domain"; + $cronLabel = $this->askDefault("Enter the cron label:", $defaultLabel); + $cronCommand = $this->askDefault("Enter the cron command.", $defaultCommand); + $cronMinute = $this->askDefault("Enter the cron minute: (0-59)", $randomMinute); + $cronHour = $this->askDefault("Enter the cron hour: (0-23)", $randomHour); + $cronDayMonth = $this->askDefault('Enter the cron day of month: (1-31)', '*'); + $cronMonth = $this->askDefault('Enter the cron month: (1-12)', '*'); + $cronDayWeek = $this->askDefault('Enter the cron day of week: (0-6)', '*'); + $frequency = "$cronMinute $cronHour $cronDayMonth $cronMonth $cronDayWeek"; + $makeItSo = $this->confirm("Create the cron job with the label '$cronLabel'.\nCommand of '$cronCommand'.\nWith the frequency of '$frequency'?"); + if ($makeItSo) { + $this->createCron($envUuId, $cronCommand, $frequency, $cronLabel); + } + else { + $this->say("Aborting."); + } + } + }