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