1 | <?php |
||
55 | class Deployer extends Container |
||
56 | { |
||
57 | /** |
||
58 | * Global instance of deployer. It's can be accessed only after constructor call. |
||
59 | * @var Deployer |
||
60 | */ |
||
61 | private static $instance; |
||
62 | |||
63 | /** |
||
64 | * @param Application $console |
||
65 | */ |
||
66 | 29 | public function __construct(Application $console) |
|
67 | { |
||
68 | 29 | parent::__construct(); |
|
69 | |||
70 | /****************************** |
||
71 | * Console * |
||
72 | ******************************/ |
||
73 | |||
74 | $this['console'] = function () use ($console) { |
||
75 | 14 | $console->catchIO(function ($input, $output) { |
|
76 | 14 | $this['input'] = $input; |
|
77 | 14 | $this['output'] = new OutputWatcher($output); |
|
78 | 14 | return [$this['input'], $this['output']]; |
|
79 | 14 | }); |
|
80 | 14 | return $console; |
|
81 | }; |
||
82 | |||
83 | /****************************** |
||
84 | * Config * |
||
85 | ******************************/ |
||
86 | |||
87 | 29 | $this['config'] = function () { |
|
88 | 29 | return new Collection(); |
|
89 | }; |
||
90 | 29 | $this->config['ssh_multiplexing'] = true; |
|
91 | 29 | $this->config['default_stage'] = null; |
|
92 | |||
93 | /****************************** |
||
94 | * Core * |
||
95 | ******************************/ |
||
96 | |||
97 | 10 | $this['pop'] = function ($c) { |
|
98 | 10 | return new ProcessOutputPrinter($c['output'], $c['logger']); |
|
99 | }; |
||
100 | 9 | $this['sshClient'] = function ($c) { |
|
101 | 9 | return new Ssh\Client($c['output'], $c['pop'], $c['config']['ssh_multiplexing']); |
|
102 | }; |
||
103 | $this['rsync'] = function ($c) { |
||
104 | return new Rsync($c['pop']); |
||
105 | }; |
||
106 | 10 | $this['processRunner'] = function ($c) { |
|
107 | 10 | return new ProcessRunner($c['pop']); |
|
108 | }; |
||
109 | 18 | $this['tasks'] = function () { |
|
110 | 18 | return new Task\TaskCollection(); |
|
111 | }; |
||
112 | 18 | $this['hosts'] = function () { |
|
113 | 18 | return new Host\HostCollection(); |
|
114 | }; |
||
115 | 16 | $this['scriptManager'] = function ($c) { |
|
116 | 16 | return new Task\ScriptManager($c['tasks']); |
|
117 | }; |
||
118 | 14 | $this['hostSelector'] = function ($c) { |
|
119 | 14 | $defaultStage = $c['config']['default_stage']; |
|
120 | 14 | if (is_object($defaultStage) && ($defaultStage instanceof \Closure)) { |
|
121 | $defaultStage = call_user_func($defaultStage); |
||
122 | } |
||
123 | 14 | return new Host\HostSelector($c['hosts'], $defaultStage); |
|
124 | }; |
||
125 | 9 | $this['fail'] = function () { |
|
126 | 9 | return new Collection(); |
|
127 | }; |
||
128 | 14 | $this['informer'] = function ($c) { |
|
129 | 14 | return new Informer($c['output']); |
|
130 | }; |
||
131 | 11 | $this['seriesExecutor'] = function ($c) { |
|
132 | 11 | return new SeriesExecutor($c['input'], $c['output'], $c['informer']); |
|
133 | }; |
||
134 | 3 | $this['parallelExecutor'] = function ($c) { |
|
135 | 3 | return new ParallelExecutor($c['input'], $c['output'], $c['informer'], $c['console']); |
|
136 | }; |
||
137 | |||
138 | /****************************** |
||
139 | * Logger * |
||
140 | ******************************/ |
||
141 | |||
142 | 10 | $this['log_handler'] = function () { |
|
143 | 10 | return !empty($this->config['log_file']) |
|
144 | ? new FileHandler($this->config['log_file']) |
||
145 | 10 | : new NullHandler(); |
|
146 | }; |
||
147 | 10 | $this['logger'] = function () { |
|
148 | 10 | return new Logger($this['log_handler']); |
|
149 | }; |
||
150 | |||
151 | /****************************** |
||
152 | * Init command * |
||
153 | ******************************/ |
||
154 | |||
155 | 14 | $this['init_command'] = function () { |
|
156 | 14 | return new InitCommand(); |
|
157 | }; |
||
158 | |||
159 | 29 | self::$instance = $this; |
|
160 | 29 | } |
|
161 | |||
162 | /** |
||
163 | * @return Deployer |
||
164 | */ |
||
165 | 31 | public static function get() |
|
169 | |||
170 | /** |
||
171 | * @param string $name |
||
172 | * @param mixed $value |
||
173 | */ |
||
174 | 13 | public static function setDefault($name, $value) |
|
178 | |||
179 | /** |
||
180 | * @param string $name |
||
181 | * @param mixed $default |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 15 | public static function getDefault($name, $default = null) |
|
188 | |||
189 | /** |
||
190 | * @param string $name |
||
191 | * @return boolean |
||
192 | */ |
||
193 | 15 | public static function hasDefault($name) |
|
197 | |||
198 | /** |
||
199 | * @param string $name |
||
200 | * @param array $array |
||
201 | */ |
||
202 | 2 | public static function addDefault($name, $array) |
|
214 | |||
215 | /** |
||
216 | * Init console application |
||
217 | */ |
||
218 | 14 | public function init() |
|
228 | |||
229 | /** |
||
230 | * Transform tasks to console commands. |
||
231 | */ |
||
232 | 14 | public function addConsoleCommands() |
|
244 | |||
245 | /** |
||
246 | * @param string $name |
||
247 | * @return mixed |
||
248 | * @throws \InvalidArgumentException |
||
249 | */ |
||
250 | 34 | public function __get($name) |
|
258 | |||
259 | /** |
||
260 | * @return Application |
||
261 | */ |
||
262 | 14 | public function getConsole() |
|
266 | |||
267 | /** |
||
268 | * @return Console\Input\InputInterface |
||
269 | */ |
||
270 | public function getInput() |
||
274 | |||
275 | /** |
||
276 | * @return Console\Output\OutputInterface |
||
277 | */ |
||
278 | public function getOutput() |
||
282 | |||
283 | /** |
||
284 | * @param string $name |
||
285 | * @return Console\Helper\HelperInterface |
||
286 | */ |
||
287 | public function getHelper($name) |
||
291 | |||
292 | /** |
||
293 | * Run Deployer |
||
294 | * |
||
295 | * @param string $version |
||
296 | * @param string $deployFile |
||
297 | */ |
||
298 | public static function run($version, $deployFile) |
||
299 | { |
||
300 | // Init Deployer |
||
301 | $console = new Application('Deployer', $version); |
||
302 | $input = new ArgvInput(); |
||
303 | $output = new ConsoleOutput(); |
||
304 | $deployer = new self($console); |
||
305 | |||
306 | // Pretty-print uncaught exceptions in symfony-console |
||
307 | set_exception_handler(function ($e) use ($input, $output) { |
||
308 | $io = new SymfonyStyle($input, $output); |
||
309 | $io->block($e->getMessage(), get_class($e), 'fg=white;bg=red', ' ', true); |
||
310 | $io->block($e->getTraceAsString()); |
||
311 | exit(1); |
||
312 | }); |
||
313 | |||
314 | // Require deploy.php file |
||
315 | if (is_readable($deployFile)) { |
||
316 | // Prevent variable leak into deploy.php file |
||
317 | call_user_func(function () use ($deployFile) { |
||
318 | require $deployFile; |
||
319 | }); |
||
320 | } |
||
321 | |||
322 | // Run Deployer |
||
323 | $deployer->init(); |
||
324 | $console->run($input, $output); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Collect anonymous stats about Deployer usage for improving developer experience. |
||
329 | * If you are not comfortable with this, you will always be able to disable this |
||
330 | * by setting `allow_anonymous_stats` to false in your deploy.php file. |
||
331 | * |
||
332 | * @param CommandEvent $commandEvent |
||
333 | * @codeCoverageIgnore |
||
334 | */ |
||
335 | public function collectAnonymousStats(CommandEvent $commandEvent) |
||
370 | } |
||
371 |