|
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\Server; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Builder\BuilderInterface; |
|
11
|
|
|
use Deployer\Server\Password\AskPasswordGetter; |
|
12
|
|
|
use Deployer\Server\Password\PasswordGetterInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Build server configuration |
|
16
|
|
|
*/ |
|
17
|
|
|
class Builder implements BuilderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Configuration |
|
21
|
|
|
*/ |
|
22
|
|
|
private $config; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Environment |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $env; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Construct |
|
31
|
|
|
* |
|
32
|
|
|
* @param Configuration $config |
|
33
|
|
|
* @param Environment $env |
|
34
|
|
|
*/ |
|
35
|
33 |
|
public function __construct(Configuration $config, Environment $env) |
|
36
|
|
|
{ |
|
37
|
33 |
|
$this->config = $config; |
|
38
|
33 |
|
$this->env = $env; |
|
39
|
|
|
|
|
40
|
33 |
|
$env->setAsProtected('server', [ |
|
41
|
33 |
|
'name' => $config->getName(), |
|
42
|
33 |
|
'host' => $config->getHost(), |
|
43
|
33 |
|
'port' => $config->getPort(), |
|
44
|
33 |
|
]); |
|
45
|
33 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
16 |
|
public function user($name) |
|
51
|
16 |
|
{ |
|
52
|
2 |
|
$this->config->setUser($name); |
|
53
|
|
|
|
|
54
|
2 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function password($password = null) |
|
61
|
|
|
{ |
|
62
|
6 |
|
$password = $this->checkPassword($password); |
|
63
|
|
|
|
|
64
|
4 |
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_PASSWORD); |
|
65
|
4 |
|
$this->config->setPassword($password); |
|
66
|
|
|
|
|
67
|
4 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Define server host |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $host |
|
74
|
|
|
* |
|
75
|
|
|
* @return BuilderInterface |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function host($host) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->config->setHost($host); |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Define server port |
|
86
|
|
|
* |
|
87
|
|
|
* @param int $port |
|
88
|
|
|
* |
|
89
|
|
|
* @return BuilderInterface |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function port($port) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$this->config->setPort($port); |
|
94
|
|
|
|
|
95
|
1 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function configFile($file = '~/.ssh/config') |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_CONFIG); |
|
104
|
1 |
|
$this->config->setConfigFile($file); |
|
105
|
|
|
|
|
106
|
1 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function identityFile($publicKeyFile = '~/.ssh/id_rsa.pub', $privateKeyFile = '~/.ssh/id_rsa', $passPhrase = '') |
|
113
|
|
|
{ |
|
114
|
3 |
|
$passPhrase = $this->checkPassword($passPhrase); |
|
115
|
|
|
|
|
116
|
3 |
|
if (is_null($publicKeyFile)) { |
|
117
|
|
|
// Use default value |
|
118
|
2 |
|
$publicKeyFile = '~/.ssh/id_rsa.pub'; |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
if (is_null($privateKeyFile)) { |
|
122
|
|
|
// Use default value |
|
123
|
2 |
|
$privateKeyFile = '~/.ssh/id_rsa'; |
|
124
|
2 |
|
} |
|
125
|
|
|
|
|
126
|
3 |
|
if (is_null($passPhrase)) { |
|
127
|
|
|
// Ask pass phrase before connection |
|
128
|
|
|
$passPhrase = AskPasswordGetter::createLazyGetter(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
3 |
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_IDENTITY_FILE); |
|
132
|
3 |
|
$this->config->setPublicKey($publicKeyFile); |
|
133
|
3 |
|
$this->config->setPrivateKey($privateKeyFile); |
|
134
|
3 |
|
$this->config->setPassPhrase($passPhrase); |
|
135
|
|
|
|
|
136
|
3 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Authenticate with public key + password (2-factor) |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $publicKeyFile |
|
143
|
|
|
* @param string $privateKeyFile |
|
144
|
|
|
* @param string $passPhrase |
|
145
|
|
|
* @param string $password |
|
146
|
|
|
* |
|
147
|
|
|
* @return BuilderInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
public function identityFileAndPassword($publicKeyFile = '~/.ssh/id_rsa.pub', $privateKeyFile = '~/.ssh/id_rsa', $passPhrase = '', $password = null) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->identityFile($publicKeyFile, $privateKeyFile, $passPhrase); |
|
152
|
|
|
$this->password($password); |
|
153
|
|
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_IDENTITY_FILE_AND_PASSWORD); |
|
154
|
|
|
|
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function pemFile($pemFile) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_PEM_FILE); |
|
164
|
1 |
|
$this->config->setPemFile($pemFile); |
|
165
|
|
|
|
|
166
|
1 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function forwardAgent() |
|
173
|
|
|
{ |
|
174
|
2 |
|
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_AGENT); |
|
175
|
|
|
|
|
176
|
2 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
17 |
|
public function set($name, $value) |
|
183
|
|
|
{ |
|
184
|
17 |
|
$this->env->set($name, $value); |
|
185
|
|
|
|
|
186
|
17 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
3 |
|
public function stage($stages) |
|
193
|
|
|
{ |
|
194
|
3 |
|
$this->env->set('stages', (array) $stages); |
|
195
|
|
|
|
|
196
|
3 |
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Check password valid |
|
201
|
|
|
* |
|
202
|
|
|
* @param mixed $password |
|
203
|
|
|
* |
|
204
|
|
|
* @return mixed |
|
205
|
|
|
*/ |
|
206
|
8 |
|
private function checkPassword($password) |
|
207
|
|
|
{ |
|
208
|
8 |
|
if (is_null($password)) { |
|
209
|
2 |
|
return AskPasswordGetter::createLazyGetter(); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
6 |
|
if (is_scalar($password)) { |
|
213
|
3 |
|
return $password; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
3 |
|
if (is_object($password) && $password instanceof PasswordGetterInterface) { |
|
217
|
1 |
|
return $password; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
// Invalid password |
|
221
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
|
222
|
2 |
|
'The password should be a string or PasswordGetterInterface instances, but "%s" given.', |
|
223
|
2 |
|
is_object($password) ? get_class($password) : gettype($password) |
|
224
|
2 |
|
)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Use pty connection |
|
229
|
|
|
* |
|
230
|
|
|
* @param $pty |
|
231
|
|
|
* @return BuilderInterface |
|
232
|
|
|
*/ |
|
233
|
|
|
public function pty($pty) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->config->setPty($pty); |
|
236
|
|
|
|
|
237
|
|
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Use pty in ssh2 connection |
|
242
|
|
|
* |
|
243
|
|
|
* @param $ssh2Pty |
|
244
|
|
|
* @deprecated |
|
245
|
|
|
* @return BuilderInterface |
|
246
|
|
|
*/ |
|
247
|
|
|
public function ssh2Pty($ssh2Pty) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->config->setSsh2Pty($ssh2Pty); |
|
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
return $this; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
This method has been deprecated.