1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class to deploy graviton |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\Deployment; |
7
|
|
|
|
8
|
|
|
use Graviton\Deployment\Steps\StepInterface; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/deploy-scripts/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class Deployment |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Deploy steps |
21
|
|
|
* |
22
|
|
|
* @var StepInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $steps = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Builder for process |
28
|
|
|
* |
29
|
|
|
* @var ProcessBuilder |
30
|
|
|
*/ |
31
|
|
|
private $processBuilder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ProcessBuilder $processBuilder factory to create processes |
35
|
|
|
*/ |
36
|
18 |
|
public function __construct(ProcessBuilder $processBuilder) |
37
|
|
|
{ |
38
|
18 |
|
$this->processBuilder = $processBuilder; |
39
|
18 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Adds a whole bunch of steps at once. |
43
|
|
|
* |
44
|
|
|
* @param array $steps List of steps to be added to the deployment. |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
9 |
|
public function registerSteps(array $steps) |
49
|
|
|
{ |
50
|
9 |
|
foreach ($steps as $step) { |
51
|
8 |
|
if (!$step instanceof StepInterface) { |
52
|
1 |
|
throw new \InvalidArgumentException( |
53
|
|
|
'Provided step is not an instance of \Graviton\Deployment\Steps\StepInterface.' |
54
|
1 |
|
); |
55
|
|
|
} |
56
|
7 |
|
$this->steps[] = $step; |
57
|
8 |
|
} |
58
|
|
|
|
59
|
8 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* deploys the steps |
64
|
|
|
* |
65
|
|
|
* @param bool $immediateOutput Forces the Process to dump every output immediately. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
7 |
|
public function deploy($immediateOutput = false) |
70
|
|
|
{ |
71
|
7 |
|
$callback = null; |
72
|
7 |
|
$output = ''; |
73
|
|
|
|
74
|
7 |
|
if (empty($this->steps)) { |
75
|
1 |
|
return 'No steps registered! Aborting.'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @link http://symfony.com/doc/current/components/process.html#getting-real-time-process-output |
80
|
|
|
*/ |
81
|
6 |
|
if (true === $immediateOutput) { |
82
|
3 |
|
$callback = function ($type, $buffer) { |
83
|
3 |
|
if (Process::ERR === $type) { |
84
|
|
|
echo 'ERR > '.$buffer; |
85
|
|
|
} else { |
86
|
3 |
|
echo 'OUT > '.$buffer; |
87
|
|
|
} |
88
|
3 |
|
}; |
89
|
3 |
|
} |
90
|
|
|
|
91
|
6 |
|
foreach ($this->steps as $step) { |
92
|
6 |
|
$command = $step->getCommand(); |
93
|
6 |
|
$process = $this->processBuilder |
94
|
6 |
|
->setArguments($command) |
95
|
6 |
|
->getProcess(); |
96
|
6 |
|
$process->mustRun($callback); |
97
|
|
|
|
98
|
|
|
// do not add the already printed output to the consolidated output to be printed later. |
99
|
6 |
|
if (false === $immediateOutput) { |
100
|
6 |
|
$output .= $process->getOutput(); |
101
|
6 |
|
} |
102
|
6 |
|
} |
103
|
|
|
|
104
|
6 |
|
return $output; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Rests this the current instance. |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
14 |
|
public function resetSteps() |
113
|
|
|
{ |
114
|
14 |
|
$this->steps = array(); |
115
|
|
|
|
116
|
14 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|