|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onigoetz\Deployer; |
|
4
|
|
|
|
|
5
|
|
|
use Net_SFTP; |
|
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
7
|
|
|
|
|
8
|
|
|
class RemoteActionRunner |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $output; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Net_SFTP |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $ssh; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(OutputInterface $output, Net_SFTP $ssh) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->output = $output; |
|
23
|
|
|
$this->ssh = $ssh; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function exec($command, $cwd = null) |
|
27
|
|
|
{ |
|
28
|
|
|
if (VERBOSE) { |
|
29
|
|
|
$this->output->writeln("<bg=blue;options=bold> -> $command </bg=blue;options=bold>"); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($cwd) { |
|
33
|
|
|
$command = "cd \"$cwd\" && $command"; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$result = $this->ssh->exec($command); |
|
37
|
|
|
|
|
38
|
|
|
if ($status = $this->ssh->getExitStatus()) { |
|
39
|
|
|
throw new RemoteException("Command '$command' Failed with status '$status': $result"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $result; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function symlink($target, $linkName) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->exec('ln -s ' . $target . ' ' . $linkName); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function rmfile($file) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->exec('rm -f "' . $file . '"'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function rmdir($file) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->exec('rm -rf "' . $file . '"'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getComposerCommand($dir) |
|
61
|
|
|
{ |
|
62
|
|
|
//is composer installed on the system ? |
|
63
|
|
|
$composerCommand = str_replace("\n", '', $this->ssh->exec('which composer')); |
|
64
|
|
|
if ($composerCommand != '') { |
|
65
|
|
|
return $composerCommand; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//is composer installed locally ? |
|
69
|
|
|
if (file_exists("$dir/composer.phar")) { |
|
70
|
|
|
return "$dir/composer.phar"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
//if not install it locally |
|
74
|
|
|
$response = $this->exec('curl -s https://getcomposer.org/installer | php -- --install-dir="' . $dir . '"'); |
|
75
|
|
|
|
|
76
|
|
|
$this->output->write('<fg=green>' . $response . '</fg=green>'); |
|
77
|
|
|
|
|
78
|
|
|
return "$dir/composer.phar"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function composer($dir) |
|
82
|
|
|
{ |
|
83
|
|
|
$composerCommand = $this->getComposerCommand($dir); |
|
84
|
|
|
|
|
85
|
|
|
$command = "$composerCommand install --prefer-dist --optimize-autoloader --no-dev --no-interaction -d $dir"; |
|
86
|
|
|
|
|
87
|
|
|
return $this->exec($command . ((VERBOSE) ? ' -v' : '')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Tests if a directory exists |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $dir |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function isDir($dir) |
|
97
|
|
|
{ |
|
98
|
|
|
$pwd = $this->ssh->pwd(); |
|
99
|
|
|
|
|
100
|
|
|
if (!$this->ssh->chdir($dir)) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->ssh->chdir($pwd); |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getSymlinkDestination($folder) |
|
110
|
|
|
{ |
|
111
|
|
|
//get previous deploy symlink |
|
112
|
|
|
$link = str_replace("\n", '', $this->ssh->exec("ls -la $folder")); |
|
113
|
|
|
|
|
114
|
|
|
//Store "previous" deploy |
|
115
|
|
|
return trim(substr($link, strpos($link, '->') + 3)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function setupServer($destinationDir) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!$this->isDir($destinationDir)) { |
|
121
|
|
|
$this->exec('mkdir -p "' . $destinationDir . '"'); |
|
122
|
|
|
|
|
123
|
|
|
if (!$this->isDir($destinationDir)) { |
|
124
|
|
|
throw new \Exception("Cannot create directory '$destinationDir'"); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|