1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Runs a command on a Cloud Foundry instance |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\Deployment\Command\CloudFoundry; |
7
|
|
|
|
8
|
|
|
use Graviton\Deployment\Command\AbstractCommand; |
9
|
|
|
use Graviton\Deployment\Configuration; |
10
|
|
|
use Graviton\Deployment\Deployment; |
11
|
|
|
use Graviton\Deployment\Services\CloudFoundry\DeploymentUtils; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
19
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
20
|
|
|
* @link http://swisscom.ch |
21
|
|
|
*/ |
22
|
|
|
final class RunCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
/** @var Deployment */ |
25
|
|
|
private $deployHandler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Deployment $deploymentHandler Managing the actual deployment |
29
|
|
|
* @param Configuration $configuration Set of configuration options to influence the current command. |
30
|
|
|
* @param string|null $name Name of the command |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Deployment $deploymentHandler, Configuration $configuration, $name = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($configuration, $name); |
35
|
|
|
$this->deployHandler = $deploymentHandler; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Configures the current command. |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setName('graviton:deployment:cf:run') |
47
|
|
|
->setDescription('Run a command as on a Cloud Foundry instance') |
48
|
|
|
->addArgument( |
49
|
|
|
'applicationName', |
50
|
|
|
InputArgument::REQUIRED, |
51
|
|
|
'Which application shall be deployed?' |
52
|
|
|
) |
53
|
|
|
->addArgument( |
54
|
|
|
'cmd', |
55
|
|
|
InputArgument::REQUIRED, |
56
|
|
|
'Shell command passed as a string e.g. "php app/console doctrine:mongodb:fixtures:load unstable"' |
57
|
|
|
) |
58
|
|
|
->addArgument( |
59
|
|
|
'versionName', |
60
|
|
|
InputArgument::OPTIONAL, |
61
|
|
|
'Which application shall be deployed?', |
62
|
|
|
'unstable' |
63
|
|
|
) |
64
|
|
|
->addOption( |
65
|
|
|
'no-logout', |
66
|
|
|
null, |
67
|
|
|
InputOption::VALUE_NONE, |
68
|
|
|
'Will keep the CF session open after deployment. ' . |
69
|
|
|
'<fg=yellow;options=bold>Keep in mind to close it by yourself!</fg=yellow;options=bold>' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Executes the current command. |
75
|
|
|
* |
76
|
|
|
* @param InputInterface $input User input on console |
77
|
|
|
* @param OutputInterface $output Output of the command |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
82
|
|
|
{ |
83
|
|
|
DeploymentUtils::login($this->deployHandler, $output, $this->configuration); |
84
|
|
|
$applicationName = $input->getArgument('applicationName') . '-' . $input->getArgument('versionName'); |
85
|
|
|
DeploymentUtils::runCommand( |
86
|
|
|
$this->deployHandler, |
87
|
|
|
$output, |
88
|
|
|
$this->configuration, |
89
|
|
|
$input->getArgument('cmd'), |
90
|
|
|
$applicationName |
91
|
|
|
); |
92
|
|
|
$noLogout = $input->getOption('no-logout'); |
93
|
|
View Code Duplication |
if (true == $noLogout) { |
|
|
|
|
94
|
|
|
$output->writeln( |
95
|
|
|
'<bg=yellow;fg=black;options=bold>' . |
96
|
|
|
' ' . PHP_EOL . |
97
|
|
|
' Cloud Foundry session will not be closed (»no-logout« option was set) ! ' . |
98
|
|
|
PHP_EOL . ' ' . |
99
|
|
|
'</bg=yellow;fg=black;options=bold>' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
DeploymentUtils::logout($this->deployHandler, $output, $this->configuration); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.