|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the EasyDeploy project. |
| 5 | + * |
| 6 | + * (c) Javier Eguiluz <javier.eguiluz@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace EasyCorp\Bundle\EasyDeployBundle\Command; |
| 13 | + |
| 14 | +use EasyCorp\Bundle\EasyDeployBundle\Context; |
| 15 | +use EasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 22 | +use Symfony\Component\Filesystem\Filesystem; |
| 23 | +use Symfony\Component\HttpKernel\Config\FileLocator; |
| 24 | +use Symfony\Component\HttpKernel\Kernel; |
| 25 | + |
| 26 | +class DeployCommand extends Command |
| 27 | +{ |
| 28 | + private $fileLocator; |
| 29 | + private $projectDir; |
| 30 | + private $logDir; |
| 31 | + private $configFilePath; |
| 32 | + |
| 33 | + public function __construct(FileLocator $fileLocator, string $projectDir, string $logDir) |
| 34 | + { |
| 35 | + $this->fileLocator = $fileLocator; |
| 36 | + $this->projectDir = realpath($projectDir); |
| 37 | + $this->logDir = $logDir; |
| 38 | + |
| 39 | + parent::__construct(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + $this |
| 45 | + ->setName('deploy') |
| 46 | + ->setDescription('Deploys a Symfony application to one or more remote servers.') |
| 47 | + ->setHelp('...') |
| 48 | + ->addArgument('stage', InputArgument::OPTIONAL, 'The stage to deploy to ("production", "staging", etc.)', 'prod') |
| 49 | + ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Load configuration from the given file path') |
| 50 | + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Shows the commands to perform the deployment without actually executing them') |
| 51 | + ; |
| 52 | + } |
| 53 | + |
| 54 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 55 | + { |
| 56 | + $customConfigPath = $input->getOption('configuration'); |
| 57 | + if (null !== $customConfigPath && !is_readable($customConfigPath)) { |
| 58 | + throw new \RuntimeException(sprintf("The given configuration file ('%s') does not exist or it's not readable.", $customConfigPath)); |
| 59 | + } |
| 60 | + |
| 61 | + if (null !== $customConfigPath && is_readable($customConfigPath)) { |
| 62 | + return $this->configFilePath = $customConfigPath; |
| 63 | + } |
| 64 | + |
| 65 | + $defaultConfigPath = $this->getDefaultConfigPath($input->getArgument('stage')); |
| 66 | + if (is_readable($defaultConfigPath)) { |
| 67 | + return $this->configFilePath = $defaultConfigPath; |
| 68 | + } |
| 69 | + |
| 70 | + $this->createDefaultConfigFile($input, $output, $defaultConfigPath, $input->getArgument('stage')); |
| 71 | + } |
| 72 | + |
| 73 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 74 | + { |
| 75 | + $logFilePath = sprintf('%s/deploy_%s.log', $this->logDir, $input->getArgument('stage')); |
| 76 | + $context = new Context($input, $output, $this->projectDir, $logFilePath, true === $input->getOption('dry-run'), $output->isVerbose()); |
| 77 | + |
| 78 | + $deployer = include $this->configFilePath; |
| 79 | + $deployer->initialize($context); |
| 80 | + $deployer->doDeploy(); |
| 81 | + } |
| 82 | + |
| 83 | + private function getDefaultConfigPath(string $stageName) : string |
| 84 | + { |
| 85 | + $symfonyVersion = Kernel::MAJOR_VERSION; |
| 86 | + $defaultConfigPaths = [ |
| 87 | + 2 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), |
| 88 | + 3 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), |
| 89 | + 4 => sprintf('%s/etc/%s/deploy.php', $this->projectDir, $stageName), |
| 90 | + ]; |
| 91 | + |
| 92 | + if (!isset($defaultConfigPaths[$symfonyVersion])) { |
| 93 | + throw new SymfonyVersionException($symfonyVersion); |
| 94 | + } |
| 95 | + |
| 96 | + return $defaultConfigPaths[$symfonyVersion]; |
| 97 | + } |
| 98 | + |
| 99 | + private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName) : void |
| 100 | + { |
| 101 | + $helper = $this->getHelper('question'); |
| 102 | + $question = new ConfirmationQuestion(sprintf("\n<bg=yellow> WARNING </> There is no config file to deploy '%s' stage.\nDo you want to create a minimal config file for it? [Y/n] ", $stageName), true); |
| 103 | + |
| 104 | + if (!$helper->ask($input, $output, $question)) { |
| 105 | + $output->writeln(sprintf('<fg=green>OK</>, but before running this command again, create this config file: %s', $defaultConfigPath)); |
| 106 | + } else { |
| 107 | + (new Filesystem())->copy($this->fileLocator->locate('@EasyDeployBundle/Resources/skeleton/deploy.php.dist'), $defaultConfigPath); |
| 108 | + $output->writeln(sprintf('<fg=green>OK</>, now edit the "%s" config file and run this command again.', $defaultConfigPath)); |
| 109 | + } |
| 110 | + |
| 111 | + exit(0); |
| 112 | + } |
| 113 | +} |
0 commit comments