1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Console; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Host\Host; |
12
|
|
|
use Deployer\Host\Localhost; |
13
|
|
|
use Deployer\Task\Context; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption as Option; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @codeCoverageIgnore |
23
|
|
|
*/ |
24
|
|
|
class SshCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Deployer |
28
|
|
|
*/ |
29
|
|
|
private $deployer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* SshCommand constructor. |
33
|
|
|
* @param Deployer $deployer |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Deployer $deployer) |
36
|
|
|
{ |
37
|
|
|
parent::__construct('ssh'); |
38
|
|
|
$this->setDescription('Connect to host through ssh'); |
39
|
|
|
$this->deployer = $deployer; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configures the command |
44
|
|
|
*/ |
45
|
|
|
protected function configure() |
46
|
|
|
{ |
47
|
|
|
$this->addArgument( |
48
|
|
|
'hostname', |
49
|
|
|
InputArgument::OPTIONAL, |
50
|
|
|
'Hostname' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
$hostname = $input->getArgument('hostname'); |
60
|
|
|
if (!empty($hostname)) { |
61
|
|
|
$host = $this->deployer->hosts->get($hostname); |
62
|
|
|
} else { |
63
|
|
|
$hostsAliases = []; |
64
|
|
|
foreach ($this->deployer->hosts as $host) { |
65
|
|
|
if ($host instanceof Localhost) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
$hostsAliases[] = $host->getAlias(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (count($hostsAliases) === 0) { |
72
|
|
|
$output->writeln('No remote hosts.'); |
73
|
|
|
return 2; // Because there are no hosts. |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (count($hostsAliases) === 1) { |
77
|
|
|
$host = current($this->deployer->hosts->all()); |
78
|
|
|
} else { |
79
|
|
|
$helper = $this->getHelper('question'); |
80
|
|
|
$question = new ChoiceQuestion( |
81
|
|
|
'<question>Select host:</question>', |
82
|
|
|
$hostsAliases |
83
|
|
|
); |
84
|
|
|
$question->setErrorMessage('There is no "%s" host.'); |
85
|
|
|
|
86
|
|
|
$hostname = $helper->ask($input, $output, $question); |
87
|
|
|
$host = $this->deployer->hosts->get($hostname); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$shell_path = 'exec $SHELL -l'; |
92
|
|
|
if ($host->has('shell_path')) { |
93
|
|
|
$shell_path = 'exec ' . $host->get('shell_path') . ' -l'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
Context::push(new Context($host, $input, $output)); |
97
|
|
|
$options = $host->getSshArguments(); |
98
|
|
|
$deployPath = $host->get('deploy_path', '~'); |
99
|
|
|
|
100
|
|
|
passthru("ssh -t $options {$host->getConnectionString()} 'cd '''$deployPath/current'''; $shell_path'"); |
101
|
|
|
return 0; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|