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\Host; |
9
|
|
|
|
10
|
|
|
use Deployer\Configuration\Configuration; |
11
|
|
|
use Deployer\Configuration\ConfigurationAccessor; |
12
|
|
|
use Deployer\Ssh\Arguments; |
13
|
|
|
use Deployer\Ssh\Options; |
14
|
|
|
|
15
|
|
|
class Host |
16
|
|
|
{ |
17
|
|
|
use ConfigurationAccessor; |
18
|
|
|
|
19
|
|
|
private $hostname; |
20
|
|
|
private $user; |
21
|
|
|
private $port; |
22
|
|
|
private $configFile; |
23
|
|
|
private $identityFile; |
24
|
|
|
private $forwardAgent = true; |
25
|
|
|
private $multiplexing = null; |
26
|
|
|
private $sshArguments; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $hostname |
30
|
|
|
*/ |
31
|
19 |
|
public function __construct(string $hostname) |
32
|
|
|
{ |
33
|
19 |
|
$this->hostname = $hostname; |
34
|
19 |
|
$this->config = new Configuration(); |
35
|
19 |
|
$this->sshArguments = new Arguments; |
36
|
19 |
|
} |
37
|
|
|
|
38
|
3 |
|
private function initOptions() |
39
|
|
|
{ |
40
|
3 |
|
if ($this->port) { |
41
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-p', $this->port); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
if ($this->configFile) { |
45
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-F', $this->configFile); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
if ($this->identityFile) { |
49
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-i', $this->identityFile); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
if ($this->forwardAgent) { |
53
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-A'); |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns pair user/hostname |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
5 |
|
public function __toString() |
63
|
|
|
{ |
64
|
5 |
|
$user = empty($this->user) ? '' : "{$this->user}@"; |
65
|
5 |
|
$hostname = preg_replace('/\/.+$/', '', $this->hostname); |
66
|
5 |
|
return "$user$hostname"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
14 |
|
public function getHostname() |
73
|
|
|
{ |
74
|
14 |
|
return $this->hostname; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $hostname |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
4 |
|
public function hostname(string $hostname) |
82
|
|
|
{ |
83
|
4 |
|
$this->hostname = $hostname; |
84
|
4 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function getUser() |
91
|
|
|
{ |
92
|
2 |
|
return $this->user; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $user |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
4 |
|
public function user(string $user) |
100
|
|
|
{ |
101
|
4 |
|
$this->user = $user; |
102
|
4 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
4 |
|
public function getPort() |
109
|
|
|
{ |
110
|
4 |
|
return $this->port; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $port |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
4 |
|
public function port(int $port) |
118
|
|
|
{ |
119
|
4 |
|
$this->port = $port; |
120
|
4 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
2 |
|
public function getConfigFile() |
127
|
|
|
{ |
128
|
2 |
|
return $this->configFile; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $configFile |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
3 |
|
public function configFile(string $configFile) |
136
|
|
|
{ |
137
|
3 |
|
$this->configFile = $configFile; |
138
|
3 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
2 |
|
public function getIdentityFile() |
145
|
|
|
{ |
146
|
2 |
|
return $this->identityFile; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $identityFile |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
3 |
|
public function identityFile(string $identityFile) |
154
|
|
|
{ |
155
|
3 |
|
$this->identityFile = $identityFile; |
156
|
3 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
2 |
|
public function isForwardAgent() |
163
|
|
|
{ |
164
|
2 |
|
return $this->forwardAgent; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param bool $forwardAgent |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
3 |
|
public function forwardAgent(bool $forwardAgent = true) |
172
|
|
|
{ |
173
|
3 |
|
$this->forwardAgent = $forwardAgent; |
174
|
3 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
2 |
|
public function isMultiplexing() |
181
|
|
|
{ |
182
|
2 |
|
return $this->multiplexing; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param bool $multiplexing |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
3 |
|
public function multiplexing(bool $multiplexing = true) |
190
|
|
|
{ |
191
|
3 |
|
$this->multiplexing = $multiplexing; |
192
|
3 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
public function getSshArguments() : Arguments |
196
|
|
|
{ |
197
|
3 |
|
$this->initOptions(); |
198
|
3 |
|
return $this->sshArguments; |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
public function sshOptions(array $options) : Host |
202
|
|
|
{ |
203
|
3 |
|
$this->sshArguments = $this->sshArguments->withOptions($options); |
204
|
3 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
public function sshFlags(array $flags) : Host |
208
|
|
|
{ |
209
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlags($flags); |
210
|
2 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
public function addSshOption(string $option, $value) : Host |
214
|
|
|
{ |
215
|
1 |
|
$this->sshArguments = $this->sshArguments->withOption($option, $value); |
216
|
1 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function addSshFlag(string $flag, string $value = null) : Host |
220
|
|
|
{ |
221
|
|
|
$this->sshArguments = $this->sshArguments->withFlag($flag, $value); |
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set stage |
227
|
|
|
* |
228
|
|
|
* @param string $stage |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
2 |
|
public function stage(string $stage) |
232
|
|
|
{ |
233
|
2 |
|
$this->config->set('stage', $stage); |
234
|
2 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set roles |
239
|
|
|
* |
240
|
|
|
* @param array ...$roles |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
2 |
|
public function roles(...$roles) |
244
|
|
|
{ |
245
|
2 |
|
$this->config->set('roles', []); |
246
|
|
|
|
247
|
2 |
|
foreach ($roles as $role) { |
248
|
2 |
|
$this->config->add('roles', [$role]); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set become |
256
|
|
|
* |
257
|
|
|
* @param string $user |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
|
|
public function become(string $user) |
261
|
|
|
{ |
262
|
|
|
$this->config->set('become', $user); |
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|