|
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; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Collection\Collection; |
|
11
|
|
|
use Deployer\Console\Application; |
|
12
|
|
|
use Deployer\Console\CommandEvent; |
|
13
|
|
|
use Deployer\Console\InitCommand; |
|
14
|
|
|
use Deployer\Console\TaskCommand; |
|
15
|
|
|
use Deployer\Console\WorkerCommand; |
|
16
|
|
|
use Deployer\Server; |
|
17
|
|
|
use Deployer\Stage\StageStrategy; |
|
18
|
|
|
use Deployer\Task; |
|
19
|
|
|
use Deployer\Type\Config; |
|
20
|
|
|
use Deployer\Util\Reporter; |
|
21
|
|
|
use Monolog\Handler\NullHandler; |
|
22
|
|
|
use Monolog\Handler\StreamHandler; |
|
23
|
|
|
use Monolog\Logger; |
|
24
|
|
|
use Pimple\Container; |
|
25
|
|
|
use Psr\Log\LoggerInterface; |
|
26
|
|
|
use Symfony\Component\Console; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @property Task\TaskCollection|Task\Task[] tasks |
|
30
|
|
|
* @property Server\ServerCollection|Server\ServerInterface[] servers |
|
31
|
|
|
* @property Server\EnvironmentCollection|Server\Environment[] environments |
|
32
|
|
|
* @property Collection config |
|
33
|
|
|
*/ |
|
34
|
|
|
class Deployer extends Container |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Global instance of deployer. It's can be accessed only after constructor call. |
|
38
|
|
|
* @var Deployer |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $instance; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Application $console |
|
44
|
|
|
* @param Console\Input\InputInterface $input |
|
45
|
|
|
* @param Console\Output\OutputInterface $output |
|
46
|
|
|
*/ |
|
47
|
36 |
|
public function __construct(Application $console, Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
36 |
|
parent::__construct(); |
|
50
|
|
|
|
|
51
|
|
|
/****************************** |
|
52
|
|
|
* Console * |
|
53
|
|
|
******************************/ |
|
54
|
|
|
|
|
55
|
|
|
$this['console'] = function () use ($console) { |
|
56
|
11 |
|
return $console; |
|
57
|
|
|
}; |
|
58
|
|
|
$this['input'] = function () use ($input) { |
|
59
|
|
|
return $input; |
|
60
|
|
|
}; |
|
61
|
|
|
$this['output'] = function () use ($output) { |
|
62
|
|
|
return $output; |
|
63
|
|
|
}; |
|
64
|
|
|
|
|
65
|
|
|
/****************************** |
|
66
|
|
|
* Config * |
|
67
|
|
|
******************************/ |
|
68
|
|
|
|
|
69
|
|
|
$this['config'] = function () { |
|
70
|
36 |
|
return new Collection(); |
|
71
|
|
|
}; |
|
72
|
36 |
|
$this->config['ssh_type'] = 'phpseclib'; |
|
73
|
36 |
|
$this->config['default_stage'] = null; |
|
74
|
|
|
|
|
75
|
|
|
/****************************** |
|
76
|
|
|
* Core * |
|
77
|
|
|
******************************/ |
|
78
|
|
|
|
|
79
|
|
|
$this['tasks'] = function () { |
|
80
|
17 |
|
return new Task\TaskCollection(); |
|
81
|
|
|
}; |
|
82
|
|
|
$this['servers'] = function () { |
|
83
|
19 |
|
return new Server\ServerCollection(); |
|
84
|
|
|
}; |
|
85
|
|
|
$this['environments'] = function () { |
|
86
|
19 |
|
return new Server\EnvironmentCollection(); |
|
87
|
|
|
}; |
|
88
|
|
|
$this['scriptManager'] = function ($c) { |
|
89
|
15 |
|
return new Task\ScriptManager($c['tasks']); |
|
90
|
|
|
}; |
|
91
|
|
|
$this['stageStrategy'] = function ($c) { |
|
92
|
12 |
|
return new StageStrategy($c['servers'], $c['environments'], $c['config']['default_stage']); |
|
93
|
|
|
}; |
|
94
|
|
|
$this['onFailure'] = function () { |
|
95
|
11 |
|
return new Collection(); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
/****************************** |
|
99
|
|
|
* Logger * |
|
100
|
|
|
******************************/ |
|
101
|
|
|
|
|
102
|
36 |
|
$this['log_level'] = Logger::DEBUG; |
|
103
|
|
|
$this['log_handler'] = function () { |
|
104
|
13 |
|
return isset($this->config['log_file']) |
|
105
|
13 |
|
? new StreamHandler($this->config['log_file'], $this['log_level']) |
|
106
|
13 |
|
: new NullHandler($this['log_level']); |
|
107
|
|
|
}; |
|
108
|
|
|
$this['log'] = function () { |
|
109
|
13 |
|
$name = isset($this->config['log_name']) ? $this->config['log_name'] : 'Deployer'; |
|
110
|
13 |
|
return new Logger($name, [ |
|
111
|
13 |
|
$this['log_handler'] |
|
112
|
13 |
|
]); |
|
113
|
|
|
}; |
|
114
|
|
|
|
|
115
|
|
|
/****************************** |
|
116
|
|
|
* Init command * |
|
117
|
|
|
******************************/ |
|
118
|
|
|
|
|
119
|
|
|
$this['init_command'] = function () { |
|
120
|
|
|
return new InitCommand(); |
|
121
|
|
|
}; |
|
122
|
|
|
|
|
123
|
36 |
|
self::$instance = $this; |
|
124
|
36 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return Deployer |
|
128
|
|
|
*/ |
|
129
|
32 |
|
public static function get() |
|
130
|
|
|
{ |
|
131
|
32 |
|
return self::$instance; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @param mixed $value |
|
137
|
|
|
*/ |
|
138
|
14 |
|
public static function setDefault($name, $value) |
|
139
|
|
|
{ |
|
140
|
14 |
|
Deployer::get()->config[$name] = $value; |
|
141
|
14 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* @param mixed $default |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
18 |
|
public static function getDefault($name, $default = null) |
|
149
|
|
|
{ |
|
150
|
18 |
|
return self::hasDefault($name) ? Deployer::get()->config[$name] : $default; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $name |
|
155
|
|
|
* @return boolean |
|
156
|
|
|
*/ |
|
157
|
18 |
|
public static function hasDefault($name) |
|
158
|
|
|
{ |
|
159
|
18 |
|
return isset(Deployer::get()->config[$name]); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $name |
|
164
|
|
|
* @param array $array |
|
165
|
|
|
*/ |
|
166
|
2 |
|
public static function addDefault($name, $array) |
|
167
|
|
|
{ |
|
168
|
2 |
|
if (self::hasDefault($name)) { |
|
169
|
2 |
|
$config = self::getDefault($name); |
|
170
|
2 |
|
if (!is_array($config)) { |
|
171
|
1 |
|
throw new \RuntimeException("Configuration parameter `$name` isn't array."); |
|
172
|
|
|
} |
|
173
|
1 |
|
self::setDefault($name, Config::merge($config, $array)); |
|
174
|
1 |
|
} else { |
|
175
|
|
|
self::setDefault($name, $array); |
|
176
|
|
|
} |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Run console application. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function run() |
|
183
|
|
|
{ |
|
184
|
|
|
$this->addConsoleCommands(); |
|
185
|
|
|
|
|
186
|
|
|
$this->getConsole()->add(new WorkerCommand($this)); |
|
187
|
|
|
$this->getConsole()->add($this['init_command']); |
|
188
|
|
|
$this->getConsole()->addCallback([$this, 'collectAnonymousStats']); |
|
189
|
|
|
|
|
190
|
|
|
$this->getConsole()->run($this['input'], $this['output']); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Transform tasks to console commands. |
|
195
|
|
|
*/ |
|
196
|
11 |
|
public function addConsoleCommands() |
|
197
|
|
|
{ |
|
198
|
11 |
|
$this->getConsole()->addUserArgumentsAndOptions(); |
|
199
|
|
|
|
|
200
|
11 |
|
foreach ($this->tasks as $name => $task) { |
|
201
|
11 |
|
if ($task->isPrivate()) { |
|
202
|
11 |
|
continue; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
11 |
|
$this->getConsole()->add(new TaskCommand($name, $task->getDescription(), $this)); |
|
206
|
11 |
|
} |
|
207
|
11 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $name |
|
211
|
|
|
* @return mixed |
|
212
|
|
|
* @throws \InvalidArgumentException |
|
213
|
|
|
*/ |
|
214
|
39 |
|
public function __get($name) |
|
215
|
|
|
{ |
|
216
|
39 |
|
if (isset($this[$name])) { |
|
217
|
39 |
|
return $this[$name]; |
|
218
|
|
|
} else { |
|
219
|
1 |
|
throw new \InvalidArgumentException("Property \"$name\" does not exist."); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return Application |
|
225
|
|
|
*/ |
|
226
|
11 |
|
public function getConsole() |
|
227
|
|
|
{ |
|
228
|
11 |
|
return $this['console']; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @return Console\Input\InputInterface |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getInput() |
|
235
|
|
|
{ |
|
236
|
|
|
return $this['input']; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return Console\Output\OutputInterface |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getOutput() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this['output']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $name |
|
249
|
|
|
* @return Console\Helper\HelperInterface |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getHelper($name) |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->getConsole()->getHelperSet()->get($name); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return StageStrategy |
|
258
|
|
|
*/ |
|
259
|
12 |
|
public function getStageStrategy() |
|
260
|
|
|
{ |
|
261
|
12 |
|
return $this['stageStrategy']; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return Task\ScriptManager |
|
266
|
|
|
*/ |
|
267
|
15 |
|
public function getScriptManager() |
|
268
|
|
|
{ |
|
269
|
15 |
|
return $this['scriptManager']; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @return LoggerInterface |
|
274
|
|
|
*/ |
|
275
|
13 |
|
public function getLogger() |
|
276
|
|
|
{ |
|
277
|
13 |
|
return $this['log']; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Collect anonymous stats about Deployer usage for improving developer experience. |
|
282
|
|
|
* If you are not comfortable with this, you will always be able to disable this |
|
283
|
|
|
* by setting `allow_anonymous_stats` to false in your deploy.php file. |
|
284
|
|
|
* |
|
285
|
|
|
* @param CommandEvent $commandEvent |
|
286
|
|
|
*/ |
|
287
|
|
|
public function collectAnonymousStats(CommandEvent $commandEvent) |
|
288
|
|
|
{ |
|
289
|
|
|
if ($this->config->has('allow_anonymous_stats') && $this->config['allow_anonymous_stats'] === false) { |
|
290
|
|
|
return; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$stats = [ |
|
294
|
|
|
'status' => 'success', |
|
295
|
|
|
'command_name' => $commandEvent->getCommand()->getName(), |
|
296
|
|
|
'project_hash' => empty($this->config['repository']) ? null : sha1($this->config['repository']), |
|
297
|
|
|
'servers_count' => $this->servers->count(), |
|
298
|
|
|
'deployer_version' => $this->getConsole()->getVersion(), |
|
299
|
|
|
'deployer_phar' => $this->getConsole()->isPharArchive(), |
|
300
|
|
|
'php_version' => phpversion(), |
|
301
|
|
|
'extension_pcntl' => extension_loaded('pcntl'), |
|
302
|
|
|
'extension_curl' => extension_loaded('curl'), |
|
303
|
|
|
'os' => defined('PHP_OS_FAMILY') ? PHP_OS_FAMILY : (stristr(PHP_OS, 'DAR') ? 'OSX' : (stristr(PHP_OS, 'WIN') ? 'WIN' : (stristr(PHP_OS, 'LINUX') ? 'LINUX' : PHP_OS))), |
|
304
|
|
|
'exception' => null, |
|
305
|
|
|
]; |
|
306
|
|
|
|
|
307
|
|
|
if ($commandEvent->getException() !== null) { |
|
308
|
|
|
$stats['status'] = 'error'; |
|
309
|
|
|
$stats['exception'] = get_class($commandEvent->getException()); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
Reporter::report($stats); |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|