Ejemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('Hello Terra!');
     // Ask for an app and environment.
     $this->getApp($input, $output);
     $this->getEnvironment($input, $output);
     $environment_name = $this->environment->name;
     $app_name = $this->app->name;
     $environment_factory = new EnvironmentFactory($this->environment, $this->app);
     $environment_factory->writeConfig();
     $current_scale = $environment_factory->getScale();
     $output->writeln("Scaling Environment <comment>{$app_name} {$environment_name}</comment>...");
     $output->writeln("Current scale: <comment>{$current_scale}</comment>");
     // If no scale ask for scale.
     if (empty($scale)) {
         $question = new Question('How many app containers? ');
         $helper = $this->getHelper('question');
         $scale = $helper->ask($input, $output, $question);
     }
     $output->writeln("Target scale: <comment>{$scale}</comment>");
     $environment_factory->scale($scale);
     $output->writeln("Environment <comment>{$app_name} {$environment_name}</comment> scaled to <info>{$scale}</info>");
     // Output the new URL.
     $local_url = 'http://' . $environment_factory->getHost() . ':' . $environment_factory->getPort();
     $output->writeln('<info>Environment enabled!</info>  Available at http://' . $environment_factory->getUrl() . ' and ' . $local_url);
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('Hello Terra!');
     // Ask for an app and environment.
     $this->getApp($input, $output);
     $this->getEnvironment($input, $output);
     $environment_factory = new EnvironmentFactory($this->environment, $this->app);
     $environment_factory->getConfig();
     // Run the tests
     // @TODO: Move to factory.
     $output->writeln('<info>TERRA</info> | <comment>Test: Start...</comment>');
     $output->writeln('<info>TERRA</info> | ' . $environment_factory->config['hooks']['test']);
     // Set environment variables for behat tests
     $env = array();
     $env['HOME'] = $_SERVER['HOME'];
     $behat_vars = array('extensions' => array('Behat\\MinkExtension' => array('base_url' => 'http://' . $environment_factory->getUrl())));
     // @TODO: This is NOT WORKING.  We MUST figure out how to override the base_url.
     $env['BEHAT_PARAMS'] = json_encode($behat_vars);
     $process = new Process($environment_factory->config['hooks']['test'], $environment_factory->getSourcePath(), $env);
     $process->run(function ($type, $buffer) {
         if (Process::ERR === $type) {
             echo $buffer;
         } else {
             echo $buffer;
         }
     });
     if (!$process->isSuccessful()) {
         $output->writeln('<info>TERRA</info> | <fg=red>Test Failed</> ' . $hook);
     } else {
         $output->writeln('<info>TERRA</info> | <info>Test Passed: </info> ' . $hook);
     }
     $output->writeln('');
 }
Ejemplo n.º 3
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Ask for an app and environment.
     $this->getApp($input, $output);
     $this->getEnvironment($input, $output);
     $environment_name = $this->environment->name;
     $app_name = $this->app->name;
     // Attempt to enable the environment.
     $environment_factory = new EnvironmentFactory($this->environment, $this->app);
     if (!$environment_factory->enable()) {
         $output->writeln('<error>Something went wrong, environment not enabled.</error>');
         return;
     }
     // Get new port, set new URL to environment object.
     $port = $environment_factory->getPort();
     $host = $environment_factory->getHost();
     $this->environment->url = "http://{$host}:{$port}";
     // When passing to saveEnvironment, it must have app and name properties (for now).
     $this->environment->app = $app_name;
     $this->environment->name = $environment_name;
     // Save environment metadata.
     $this->getApplication()->getTerra()->getConfig()->saveEnvironment($this->environment);
     if ($this->getApplication()->getTerra()->getConfig()->save()) {
         $output->writeln('<info>Environment enabled!</info>  Available at http://' . $environment_factory->getUrl() . ' and ' . $this->environment->url);
     } else {
         $output->writeln('<error>Environment info not saved.</error>');
     }
     // Write drush alias.
     $drush_alias_file_path = "{$_SERVER['HOME']}/.drush/{$app_name}.aliases.drushrc.php";
     if ($environment_factory->writeDrushAlias()) {
         $output->writeln("<info>Drush alias file created at {$drush_alias_file_path}</info>");
         $output->writeln("Wrote drush alias file to <comment>{$drush_alias_file_path}</comment>");
         $output->writeln("Use <info>drush @{$app_name}.{$environment_name}</info> to access the site.");
     } else {
         $output->writeln('<error>Unable to save drush alias.</error>');
     }
     // Run the enable hooks
     $output->writeln('');
     $output->writeln('Running <comment>ENABLE</comment> app hook...');
     $environment_factory->getConfig();
     // @TODO: Figure out how to only run this hook the first time!
     if (!empty($environment_factory->config['hooks']['enable_first'])) {
         // Output what we are running
         $formatter = $this->getHelper('formatter');
         $errorMessages = array($environment_factory->config['hooks']['enable_first']);
         $formattedBlock = $formatter->formatBlock($errorMessages, 'question');
         $output->writeln($formattedBlock);
         chdir($environment_factory->getSourcePath());
         $process = new Process($environment_factory->config['hooks']['enable_first']);
         $process->setTimeout(null);
         $process->run(function ($type, $buffer) {
             if (Process::ERR === $type) {
                 echo $buffer;
             } else {
                 echo $buffer;
             }
         });
     }
 }
Ejemplo n.º 4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Check if "app" argument is an action.
     if ($input->getArgument('app_name') == 'add' || $input->getArgument('app_name') == 'remove') {
         // Move argument from app_name to action
         $input->setArgument('action', $input->getArgument('app_name'));
         $input->setArgument('app_name', '');
         // Move argument from env_name to domain
         $input->setArgument('domain', $input->getArgument('environment_name'));
         $input->setArgument('environment_name', '');
     }
     // Ask for an app.
     $helper = $this->getHelper('question');
     $this->getApp($input, $output);
     // Ask for an environment.
     $helper = $this->getHelper('question');
     $this->getEnvironment($input, $output);
     // If action argument is empty, show the list.
     if (empty($input->getArgument('action'))) {
         $environment = new EnvironmentFactory($this->environment, $this->app);
         $rows[] = array('http://' . $environment->getHost() . ':' . $environment->getPort());
         $rows[] = array('http://' . $environment->getUrl());
         // Get all domains
         foreach ($environment->environment->domains as $domain) {
             $rows[] = array('http://' . $domain);
         }
         $table = $this->getHelper('table');
         $table->setHeaders(array("Domains for {$this->app->name} {$this->environment->name}"))->setRows($rows);
         $table->render($output);
         return;
     } elseif ($input->getArgument('action') == 'add') {
         $this->executeAddDomain($input, $output);
     } elseif ($input->getArgument('action') == 'remove') {
         $this->executeRemoveDomain($input, $output);
     }
     // Offer to re-enable the environment
     $helper = $this->getHelper('question');
     $question = new ConfirmationQuestion("Re-enable the environment <info>{$this->app->name}:{$this->environment->name}</info> [y/N]? ", FALSE);
     if ($helper->ask($input, $output, $question)) {
         // Run environment:enable command.
         $command = $this->getApplication()->find('environment:enable');
         $arguments = array('app_name' => $this->app->name, 'environment_name' => $this->environment->name);
         $input = new ArrayInput($arguments);
         $command->run($input, $output);
     }
 }
Ejemplo n.º 5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // If there are no apps, return
     if (count($this->getApplication()->getTerra()->getConfig()->get('apps')) == 0) {
         $output->writeln("<comment>There are no apps to remove!</comment>");
         $output->writeln("Use the command <info>terra app:add</info> to add your first app.");
         return;
     }
     $helper = $this->getHelper('question');
     $app_name = $input->getArgument('app_name');
     $environment_name = $input->getArgument('environment_name');
     // If no name specified provide options
     if (empty($app_name)) {
         $question = new ChoiceQuestion('Which app? ', array_keys($this->getApplication()->getTerra()->getConfig()->get('apps')), NULL);
         $app_name = $helper->ask($input, $output, $question);
     }
     $app = $this->getApplication()->getTerra()->getConfig()->get('apps', $app_name);
     // If no environments:
     if (count($app['environments']) == 0) {
         $output->writeln("<comment>There are no environments for the app {$app_name}!</comment>");
         $output->writeln("Use the command <info>terra environment:add</info> to add your first environment.");
         return;
     }
     // If no environment name specified provide options
     if (empty($environment_name)) {
         $question = new ChoiceQuestion('Which environment? ', array_keys($app['environments']), NULL);
         $environment_name = $helper->ask($input, $output, $question);
     }
     $environment = $app['environments'][$environment_name];
     $environment_factory = new EnvironmentFactory($environment, $app);
     $output->writeln($environment_factory->enable());
     $port = $environment_factory->getPort();
     $port = array_pop(explode(':', $port));
     $app['environments'][$environment_name]['url'] = "http://localhost:{$port}";
     $this->getApplication()->getTerra()->getConfig()->add('apps', $app_name, $app);
     if ($this->getApplication()->getTerra()->getConfig()->save()) {
         $output->writeln('<info>Environment enabled!</info>  Available at http://' . $environment_factory->getUrl());
     } else {
         $output->writeln('<error>Environment info not saved.</error>');
     }
 }
Ejemplo n.º 6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln("Hello Terra!");
     // If there are no apps, return
     if (count($this->getApplication()->getTerra()->getConfig()->get('apps')) == 0) {
         $output->writeln("<comment>There are no apps!</comment>");
         $output->writeln("Use the command <info>terra app:add</info> to add your first app.");
         return;
     }
     $helper = $this->getHelper('question');
     $app_name = $input->getArgument('app_name');
     $environment_name = $input->getArgument('environment_name');
     // If no name specified provide options
     if (empty($app_name)) {
         $question = new ChoiceQuestion('Which app? ', array_keys($this->getApplication()->getTerra()->getConfig()->get('apps')), NULL);
         $app_name = $helper->ask($input, $output, $question);
     }
     $app = $this->getApplication()->getTerra()->getConfig()->get('apps', $app_name);
     // If no environments:
     if (count($app['environments']) == 0) {
         $output->writeln("<comment>There are no environments!</comment>");
         $output->writeln("Use the command <info>terra environment:add</info> to add your first environment.");
         return;
     }
     // If no environment name specified provide options
     if (empty($environment_name)) {
         $question = new ChoiceQuestion('Which environment? ', array_keys($app['environments']), NULL);
         $environment_name = $helper->ask($input, $output, $question);
     }
     $environment = $app['environments'][$environment_name];
     $environment_factory = new EnvironmentFactory($environment, $app);
     $environment['scale'] = $environment_factory->getScale();
     $environment['url'] .= PHP_EOL . 'http://' . $environment_factory->getUrl();
     $table = $this->getHelper('table');
     $table->setHeaders(array('Name', 'Code Path', 'URL', 'Version', 'Scale'));
     $rows = array($environment);
     $table->setRows($rows);
     $table->render($output);
     $output->writeln("Docker Compose Path: " . $environment_factory->getDockerComposePath());
 }
Ejemplo n.º 7
0
 protected function prepareBehat(InputInterface $input, OutputInterface $output, EnvironmentFactory $environment_factory)
 {
     $output->writeln("I noticed you don't have behat tests for your project.");
     $helper = $this->getHelper('question');
     $question = new ConfirmationQuestion('Would you like to add behat tests? [y\\N] ');
     if ($helper->ask($input, $output, $question)) {
         $question = new Question('Where would you like to add your tests? (tests) ', 'tests');
         $tests_path = $helper->ask($input, $output, $question);
         // Create tests path
         $tests_path = $environment_factory->getSourcePath() . '/' . $tests_path;
         $fs = new Filesystem();
         try {
             $fs->mkdir($environment_factory->getSourcePath() . '/' . $tests_path);
         } catch (IOException $e) {
             throw \Exception($e->getMessage());
         }
         $output->writeln("<info>SUCCESS</info> Created {$tests_path}.");
         // Create composer.json and behat.yml
         $composer_path = $environment_factory->getSourcePath() . '/' . $tests_path . '/composer.json';
         $behat_yml_path = $environment_factory->getSourcePath() . '/' . $tests_path . '/behat.yml';
         try {
             $fs->dumpFile($composer_path, $this->getBehatDrupalComposer());
             $fs->dumpFile($behat_yml_path, $this->getBehatYml($environment_factory->getUrl()));
             $output->writeln("<info>SUCCESS</info> Created composer.json and behat.yml.");
         } catch (IOException $e) {
             throw \Exception($e->getMessage());
         }
         // Run composer install
         $process = new Process('composer install', $tests_path);
         // Run behat --init
         // Set behat_path in .terra.yml
     }
 }
Ejemplo n.º 8
0
 /**
  * Outputs the status of an environment.
  *
  * @param \Symfony\Component\Console\Input\InputInterface   $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  * @param $app
  * @param $environment
  *
  */
 protected function environmentStatus(InputInterface $input, OutputInterface $output)
 {
     // If there are no apps, return
     if (count($this->getApplication()->getTerra()->getConfig()->get('apps')) == 0) {
         $output->writeln('<comment>There are no apps!</comment>');
         $output->writeln('Use the command <info>terra app:add</info> to add your first app.');
         return;
     }
     $app_name = $input->getArgument('app_name');
     $environment_name = $input->getArgument('environment_name');
     $app = $this->getApplication()->getTerra()->getConfig()->get('apps', $app_name);
     // If no environments:
     if (count($app['environments']) == 0) {
         $output->writeln('<comment>There are no environments!</comment>');
         $output->writeln('Use the command <info>terra environment:add</info> to add your first environment.');
         return;
     }
     // If no environment by that name...
     if (!isset($app['environments'][$environment_name])) {
         $output->writeln("<error>There is no environment named {$environment_name} in the app {$app_name}</error>");
         return;
     }
     $environment = $app['environments'][$environment_name];
     $environment_factory = new EnvironmentFactory($environment, $app);
     $environment['scale'] = $environment_factory->getScale();
     $environment['url'] = 'http://' . $environment_factory->getHost() . ':' . $environment_factory->getPort();
     $environment['url'] .= PHP_EOL . 'http://' . $environment_factory->getUrl();
     $table = $this->getHelper('table');
     $table->setHeaders(array('Name', 'Code Path', 'docroot', 'URLs', 'Version', 'Scale'));
     $rows = array($environment);
     $table->setRows($rows);
     $table->render($output);
     $output->writeln('Docker Compose Path: ' . $environment_factory->getDockerComposePath());
 }
Ejemplo n.º 9
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // If there are no apps, return
     if (count($this->getApplication()->getTerra()->getConfig()->get('apps')) == 0) {
         $output->writeln('<comment>There are no apps to remove!</comment>');
         $output->writeln('Use the command <info>terra app:add</info> to add your first app.');
         return;
     }
     $helper = $this->getHelper('question');
     $app_name = $input->getArgument('app_name');
     $environment_name = $input->getArgument('environment_name');
     // If no name specified provide options
     if (empty($app_name)) {
         $question = new ChoiceQuestion('Which app? ', array_keys($this->getApplication()->getTerra()->getConfig()->get('apps')), null);
         $app_name = $helper->ask($input, $output, $question);
     }
     $app = $this->getApplication()->getTerra()->getConfig()->get('apps', $app_name);
     // If no environments:
     if (count($app['environments']) == 0) {
         $output->writeln("<comment>There are no environments for the app {$app_name}!</comment>");
         $output->writeln('Use the command <info>terra environment:add</info> to add your first environment.');
         return;
     }
     // If no environment name specified provide options
     if (empty($environment_name)) {
         $question = new ChoiceQuestion('Which environment? ', array_keys($app['environments']), null);
         $environment_name = $helper->ask($input, $output, $question);
     }
     $environment = $app['environments'][$environment_name];
     // Attempt to enable the environment.
     $environment_factory = new EnvironmentFactory($environment, $app);
     if (!$environment_factory->enable()) {
         $output->writeln('<error>Something went wrong, environment not enabled.</error>');
         return;
     }
     // Get new port, set new URL to environment object.
     $port = $environment_factory->getPort();
     $host = $environment_factory->getHost();
     $app['environments'][$environment_name]['url'] = "http://{$host}:{$port}";
     // Save environment metadata.
     $this->getApplication()->getTerra()->getConfig()->add('apps', array($app_name, 'environments', $environment_name), $app['environments'][$environment_name]);
     // Save config to files.
     if ($this->getApplication()->getTerra()->getConfig()->save()) {
         $output->writeln('<info>Environment enabled!</info>  Available at http://' . $environment_factory->getUrl() . ' and ' . $app['environments'][$environment_name]['url']);
     } else {
         $output->writeln('<error>Environment info not saved.</error>');
     }
     // Write drush alias.
     $drush_alias_file_path = "{$_SERVER['HOME']}/.drush/{$app_name}.aliases.drushrc.php";
     if ($environment_factory->writeDrushAlias()) {
         $output->writeln("<info>Drush alias file created at {$drush_alias_file_path}</info>");
         $output->writeln("Wrote drush alias file to <comment>{$drush_alias_file_path}</comment>");
         $output->writeln("Use <info>drush @{$app_name}.{$environment_name}</info> to access the site.");
     } else {
         $output->writeln('<error>Unable to save drush alias.</error>');
     }
     // Run the enable hooks
     $output->writeln('');
     $output->writeln('Running <comment>ENABLE</comment> app hook...');
     $environment_factory->getConfig();
     if (!empty($environment_factory->config['hooks']['enable'])) {
         // Output what we are running
         $formatter = $this->getHelper('formatter');
         $errorMessages = array($environment_factory->config['hooks']['enable']);
         $formattedBlock = $formatter->formatBlock($errorMessages, 'question');
         $output->writeln($formattedBlock);
         chdir($environment_factory->getSourcePath());
         $process = new Process($environment_factory->config['hooks']['enable']);
         $process->run(function ($type, $buffer) {
             if (Process::ERR === $type) {
                 echo $buffer;
             } else {
                 echo $buffer;
             }
         });
     }
 }
Ejemplo n.º 10
0
 /**
  * Using the config item "behat_path", run composer update and bin/behat in the behat path.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function executeBehatTests(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('Running Behat Tests...');
     $environment_factory = new EnvironmentFactory($this->environment, $this->app);
     $environment_factory->getConfig();
     // 1. Look for behat.yml
     $behat_path = $this->environment->path . '/' . $environment_factory->config['behat_path'];
     $behat_yml_path = $behat_path . '/behat.yml';
     if (!file_exists($behat_path)) {
         throw new \Exception("Path {$behat_path} not found. Check your app's .terra.yml file.");
     } elseif (!file_exists($behat_yml_path)) {
         throw new \Exception("Behat.yml file not found at {$behat_yml_path}. Check your app's .terra.yml file.");
     }
     $output->writeln('Found behat.yml file at ' . $behat_yml_path);
     // 2. Load it, replace necessary items, and clone it to a temporary file.
     $behat_yml = Yaml::parse(file_get_contents($behat_yml_path));
     // Set Base URL
     $behat_yml['default']['extensions']['Behat\\MinkExtension']['base_url'] = 'http://' . $environment_factory->getUrl();
     $behat_yml['default']['extensions']['Drupal\\DrupalExtension']['drush']['alias'] = $environment_factory->getDrushAlias();
     // If driver is drupal, add root.
     if ($behat_yml['default']['extensions']['Drupal\\DrupalExtension']['api_driver'] == 'drupal') {
         $behat_yml['default']['extensions']['Drupal\\DrupalExtension']['drupal']['root'] = $environment_factory->getDocumentRoot();
     }
     $behat_yml_new = Yaml::dump($behat_yml, 5, 2);
     $behat_path_new = 'behat.terra.yml';
     $fs = new Filesystem();
     $fs->dumpFile($behat_path_new, $behat_yml_new);
     $output->writeln('Generated new behat.yml file at ' . $behat_path_new);
     // 3. Run `composer install` in behat_path.
     $output->writeln('');
     $output->writeln('<fg=cyan>TERRA</> | <comment>Running: composer install</comment>');
     $process = new Process('composer install', $behat_path);
     $process->setTimeout(NULL);
     $process->run(function ($type, $buffer) {
         if (Process::ERR === $type) {
             echo $buffer;
         } else {
             echo $buffer;
         }
     });
     $output->writeln('');
     $output->writeln('<fg=cyan>TERRA</> | <comment>Behat Tests: Start</comment>');
     // 4. Run `bin/behat --colors --config=$PATH` in behat_path.
     // "expand:true" expands scenario outlines, making them readable.
     $cmd = 'bin/behat --colors --format-settings=\'{"expand": true}\' --config=' . $behat_path_new;
     if ($input->getOption('name')) {
         $cmd .= ' --name=' . $input->getOption('name');
     }
     $output->writeln("Running: {$cmd}");
     $output->writeln("in: {$behat_path}");
     $output->writeln('');
     $process = new Process($cmd, $behat_path);
     $process->setTimeout(NULL);
     $process->run(function ($type, $buffer) {
         if (Process::ERR === $type) {
             echo $buffer;
         } else {
             echo $buffer;
         }
     });
     $output->writeln('');
     if (!$process->isSuccessful()) {
         $output->writeln('<fg=cyan>TERRA</> | <fg=red>Test Failed</> ');
         return 1;
     } else {
         $output->writeln('<fg=cyan>TERRA</> | <info>Test Passed!</info> ');
         return 0;
     }
 }