1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Magallanes package. |
5
|
|
|
* |
6
|
|
|
* (c) Andrés Montañez <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mage\Deploy\Strategy; |
13
|
|
|
|
14
|
|
|
use Mage\Runtime\Exception\RuntimeException; |
15
|
|
|
use Mage\Runtime\Runtime; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Strategy for Deployment with Rsync |
19
|
|
|
* |
20
|
|
|
* @author Andrés Montañez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RsyncStrategy implements StrategyInterface |
23
|
|
|
{ |
24
|
|
|
protected Runtime $runtime; |
25
|
|
|
|
26
|
22 |
|
public function getName(): string |
27
|
|
|
{ |
28
|
22 |
|
return 'Rsync'; |
29
|
|
|
} |
30
|
|
|
|
31
|
27 |
|
public function setRuntime(Runtime $runtime): void |
32
|
|
|
{ |
33
|
27 |
|
$this->runtime = $runtime; |
34
|
|
|
} |
35
|
|
|
|
36
|
23 |
|
public function getPreDeployTasks(): array |
37
|
|
|
{ |
38
|
23 |
|
$this->checkStage(Runtime::PRE_DEPLOY); |
39
|
22 |
|
$tasks = $this->runtime->getTasks(); |
40
|
|
|
|
41
|
|
|
if ( |
42
|
22 |
|
($this->runtime->getBranch() || $this->runtime->getTag()) && |
43
|
22 |
|
!$this->runtime->inRollback() && |
44
|
22 |
|
!in_array('git/change-branch', $tasks) |
45
|
|
|
) { |
46
|
15 |
|
array_unshift($tasks, 'git/change-branch'); |
47
|
|
|
} |
48
|
|
|
|
49
|
22 |
|
return $tasks; |
50
|
|
|
} |
51
|
|
|
|
52
|
18 |
|
public function getOnDeployTasks(): array |
53
|
|
|
{ |
54
|
18 |
|
$this->checkStage(Runtime::ON_DEPLOY); |
55
|
17 |
|
$tasks = $this->runtime->getTasks(); |
56
|
|
|
|
57
|
17 |
|
if (!$this->runtime->inRollback() && !in_array('deploy/rsync', $tasks)) { |
58
|
16 |
|
array_unshift($tasks, 'deploy/rsync'); |
59
|
|
|
} |
60
|
|
|
|
61
|
17 |
|
return $tasks; |
62
|
|
|
} |
63
|
|
|
|
64
|
15 |
|
public function getOnReleaseTasks(): array |
65
|
|
|
{ |
66
|
15 |
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
public function getPostReleaseTasks(): array |
70
|
|
|
{ |
71
|
15 |
|
return []; |
72
|
|
|
} |
73
|
|
|
|
74
|
15 |
|
public function getPostDeployTasks(): array |
75
|
|
|
{ |
76
|
15 |
|
$this->checkStage(Runtime::POST_DEPLOY); |
77
|
14 |
|
$tasks = $this->runtime->getTasks(); |
78
|
|
|
|
79
|
|
|
if ( |
80
|
14 |
|
($this->runtime->getBranch() || |
81
|
14 |
|
$this->runtime->getTag()) && |
82
|
14 |
|
!$this->runtime->inRollback() && |
83
|
14 |
|
!in_array('git/change-branch', $tasks) |
84
|
|
|
) { |
85
|
8 |
|
array_push($tasks, 'git/change-branch'); |
86
|
|
|
} |
87
|
|
|
|
88
|
14 |
|
return $tasks; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check the runtime stage is correct |
93
|
|
|
* |
94
|
|
|
* @throws RuntimeException |
95
|
|
|
*/ |
96
|
23 |
|
private function checkStage(string $stage): void |
97
|
|
|
{ |
98
|
23 |
|
if ($this->runtime->getStage() !== $stage) { |
99
|
1 |
|
throw new RuntimeException( |
100
|
1 |
|
sprintf('Invalid stage, got "%s" but expected "%s"', $this->runtime->getStage(), $stage) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|