|
| 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 | +namespaceEasyCorp\Bundle\EasyDeployBundle\Command; |
| 13 | + |
| 14 | +useEasyCorp\Bundle\EasyDeployBundle\Context; |
| 15 | +useEasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException; |
| 16 | +useSymfony\Component\Console\Command\Command; |
| 17 | +useSymfony\Component\Console\Input\InputArgument; |
| 18 | +useSymfony\Component\Console\Input\InputInterface; |
| 19 | +useSymfony\Component\Console\Input\InputOption; |
| 20 | +useSymfony\Component\Console\Output\OutputInterface; |
| 21 | +useSymfony\Component\Console\Question\ConfirmationQuestion; |
| 22 | +useSymfony\Component\Filesystem\Filesystem; |
| 23 | +useSymfony\Component\HttpKernel\Config\FileLocator; |
| 24 | +useSymfony\Component\HttpKernel\Kernel; |
| 25 | + |
| 26 | +class DeployCommandextends Command |
| 27 | +{ |
| 28 | +private$fileLocator; |
| 29 | +private$projectDir; |
| 30 | +private$logDir; |
| 31 | +private$configFilePath; |
| 32 | + |
| 33 | +publicfunction__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 | +protectedfunctionconfigure() |
| 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 | +protectedfunctioninitialize(InputInterface$input,OutputInterface$output) |
| 55 | + { |
| 56 | +$customConfigPath =$input->getOption('configuration'); |
| 57 | +if (null !==$customConfigPath && !is_readable($customConfigPath)) { |
| 58 | +thrownew \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 | +protectedfunctionexecute(InputInterface$input,OutputInterface$output) |
| 74 | + { |
| 75 | +$logFilePath =sprintf('%s/deploy_%s.log',$this->logDir,$input->getArgument('stage')); |
| 76 | +$context =newContext($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 | +privatefunctiongetDefaultConfigPath(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 | +thrownewSymfonyVersionException($symfonyVersion); |
| 94 | + } |
| 95 | + |
| 96 | +return$defaultConfigPaths[$symfonyVersion]; |
| 97 | + } |
| 98 | + |
| 99 | +privatefunctioncreateDefaultConfigFile(InputInterface$input,OutputInterface$output,string$defaultConfigPath,string$stageName) :void |
| 100 | + { |
| 101 | +$helper =$this->getHelper('question'); |
| 102 | +$question =newConfirmationQuestion(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 | + (newFilesystem())->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 | +} |