This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
/
ConnectionsHandler.php
452 lines (385 loc) · 11.7 KB
/
ConnectionsHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/*
* This file is part of Rocketeer
*
* (c) Maxime Fabre <ehtnam6@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Rocketeer\Services\Connections;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Rocketeer\Exceptions\ConnectionException;
use Rocketeer\Traits\HasLocator;
/**
* Handles, get and return, the various connections/stages
* and their credentials.
*
* @author Maxime Fabre <ehtnam6@gmail.com>
*/
class ConnectionsHandler
{
use HasLocator;
/**
* The current handle.
*
* @type string
*/
protected $handle;
/**
* The current stage.
*
* @type string
*/
protected $stage;
/**
* The current server.
*
* @type int
*/
protected $currentServer = 0;
/**
* The connections to use.
*
* @type array|null
*/
protected $connections;
/**
* The current connection.
*
* @type string|null
*/
protected $connection;
/**
* Build the current connection's handle.
*
* @param string|null $connection
* @param int|null $server
* @param string|null $stage
*
* @return string
*/
public function getHandle($connection = null, $server = null, $stage = null)
{
// Get identifiers
$connection = $connection ?: $this->getConnection();
$server = $server ?: $this->getServer();
$stage = $stage ?: $this->getStage();
// Filter values
$handle = [$connection, $server, $stage];
if ($this->isMultiserver($connection)) {
$handle = array_filter($handle, function ($value) {
return !is_null($value);
});
} else {
$handle = array_filter($handle);
}
// Concatenate
$handle = implode('/', $handle);
$this->handle = $handle;
return $handle;
}
//////////////////////////////////////////////////////////////////////
////////////////////////////// SERVERS ///////////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* @return int
*/
public function getServer()
{
return $this->currentServer;
}
/**
* Check if a connection is multiserver or not.
*
* @param string $connection
*
* @return bool
*/
public function isMultiserver($connection)
{
return (bool) count($this->getConnectionCredentials($connection));
}
////////////////////////////////////////////////////////////////////
//////////////////////////////// STAGES ////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Get the current stage.
*
* @return string
*/
public function getStage()
{
return $this->stage;
}
/**
* Set the stage Tasks will execute on.
*
* @param string|null $stage
*/
public function setStage($stage)
{
if ($stage === $this->stage) {
return;
}
$this->stage = $stage;
$this->handle = null;
// If we do have a stage, cleanup previous events
if ($stage) {
$this->tasks->registerConfiguredEvents();
}
}
/**
* Get the various stages provided by the User.
*
* @return array
*/
public function getStages()
{
return (array) $this->rocketeer->getOption('stages.stages');
}
////////////////////////////////////////////////////////////////////
///////////////////////////// APPLICATION //////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Whether the repository used is using SSH or HTTPS.
*
* @return bool
*/
public function needsCredentials()
{
return Str::contains($this->getRepositoryEndpoint(), 'https://');
}
/**
* Get the available connections.
*
* @return string[][]|string[]
*/
public function getAvailableConnections()
{
// Fetch stored credentials
$storage = (array) $this->localStorage->get('connections');
// Merge with defaults from config file
$configuration = (array) $this->config->get('rocketeer::connections');
// Fetch from remote file
$remote = (array) $this->config->get('remote.connections');
// Merge configurations
$connections = array_replace_recursive($remote, $configuration, $storage);
// Unify multiservers
foreach ($connections as $key => $servers) {
$servers = Arr::get($servers, 'servers', [$servers]);
$servers = array_values($servers);
foreach ($servers as $serverKey => $server) {
if ($server && array_key_exists('key', $server)) {
$servers[$serverKey]['key'] = str_replace('~/', $this->paths->getUserHomeFolder().'/', $server['key']);
}
}
$connections[$key] = ['servers' => $servers];
}
return $connections;
}
/**
* Check if a connection has credentials related to it.
*
* @param string $connection
*
* @return bool
*/
public function isValidConnection($connection)
{
$available = (array) $this->getAvailableConnections();
return (bool) Arr::get($available, $connection.'.servers');
}
/**
* Get the connection in use.
*
* @return string[]
*/
public function getConnections()
{
// Get cached resolved connections
if ($this->connections) {
return $this->connections;
}
// Get all and defaults
$connections = (array) $this->config->get('rocketeer::default');
$default = $this->config->get('remote.default');
// Remove invalid connections
$instance = $this;
$connections = array_filter($connections, function ($value) use ($instance) {
return $instance->isValidConnection($value);
});
// Return default if no active connection(s) set
if (empty($connections) && $default) {
return [$default];
}
// Set current connection as default
$this->connections = $connections;
return $connections;
}
/**
* Set the active connections.
*
* @param string|string[] $connections
*
* @throws ConnectionException
*/
public function setConnections($connections)
{
if (!is_array($connections)) {
$connections = explode(',', $connections);
}
// Sanitize and set connections
$filtered = array_filter($connections, [$this, 'isValidConnection']);
if (!$filtered) {
throw new ConnectionException('Invalid connection(s): '.implode(', ', $connections));
}
$this->connections = $filtered;
$this->handle = null;
}
/**
* Get the active connection.
*
* @return string
*/
public function getConnection()
{
// Get cached resolved connection
if ($this->connection) {
return $this->connection;
}
$connection = Arr::get($this->getConnections(), 0);
$this->connection = $connection;
return $this->connection;
}
/**
* Set the current connection.
*
* @param string $connection
* @param int $server
*/
public function setConnection($connection, $server = 0)
{
if (!$this->isValidConnection($connection) || (($this->connection === $connection) && ($this->currentServer === $server))) {
return;
}
// Set the connection
$this->handle = null;
$this->connection = $connection;
$this->localStorage = $server;
$this->currentServer = $server;
// Update events
$this->tasks->registerConfiguredEvents();
}
/**
* Get the credentials for a particular connection.
*
* @param string|null $connection
*
* @return string[][]
*/
public function getConnectionCredentials($connection = null)
{
$connection = $connection ?: $this->getConnection();
$available = $this->getAvailableConnections();
return Arr::get($available, $connection.'.servers');
}
/**
* Get thecredentials for as server.
*
* @param string|null $connection
* @param int|null $server
*
* @return mixed
*/
public function getServerCredentials($connection = null, $server = null)
{
$connection = $this->getConnectionCredentials($connection);
$server = !is_null($server) ? $server : $this->currentServer;
return Arr::get($connection, $server, []);
}
/**
* Sync Rocketeer's credentials with Laravel's.
*
* @param string|null $connection
* @param string[]|null $credentials
* @param int $server
*/
public function syncConnectionCredentials($connection = null, array $credentials = [], $server = 0)
{
// Store credentials if any
if ($credentials) {
$this->localStorage->set('connections.'.$connection.'.servers.'.$server, $credentials);
}
// Get connection
$connection = $connection ?: $this->getConnection();
$credentials = $this->getConnectionCredentials($connection);
$this->config->set('remote.connections.'.$connection, $credentials);
}
/**
* Flush active connection(s).
*/
public function disconnect()
{
$this->connection = null;
$this->connections = null;
}
////////////////////////////////////////////////////////////////////
/////////////////////////// GIT REPOSITORY /////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Get the credentials for the repository.
*
* @return array
*/
public function getRepositoryCredentials()
{
$config = (array) $this->rocketeer->getOption('scm');
$credentials = (array) $this->localStorage->get('credentials');
return array_merge($config, $credentials);
}
/**
* Get the URL to the Git repository.
*
* @return string
*/
public function getRepositoryEndpoint()
{
// Get credentials
$repository = $this->getRepositoryCredentials();
$username = Arr::get($repository, 'username');
$password = Arr::get($repository, 'password');
$repository = Arr::get($repository, 'repository');
// Add credentials if possible
if ($username || $password) {
$username = urlencode($username);
$password = urlencode($password);
// Build credentials chain
$credentials = $password ? $username.':'.$password : $username;
$credentials .= '@';
// Add them in chain
$repository = preg_replace('#https://(.+)@#', 'https://', $repository);
$repository = str_replace('https://', 'https://'.$credentials, $repository);
}
return $repository;
}
/**
* Get the repository branch to use.
*
* @return string
*/
public function getRepositoryBranch()
{
// If we passed a branch, use it
if ($branch = $this->getOption('branch')) {
return $branch;
}
// Compute the fallback branch
exec($this->scm->currentBranch(), $fallback);
$fallback = Arr::get($fallback, 0, 'master');
$fallback = trim($fallback);
$branch = $this->rocketeer->getOption('scm.branch') ?: $fallback;
return $branch;
}
}