-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathTaskRunner.php
More file actions
92 lines (75 loc) · 3.17 KB
/
Copy pathTaskRunner.php
File metadata and controls
92 lines (75 loc) · 3.17 KB
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
<?php
/*
* This file is part of the EasyDeploy project.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EasyCorp\Bundle\EasyDeployBundle\Task;
use EasyCorp\Bundle\EasyDeployBundle\Helper\Str;
use EasyCorp\Bundle\EasyDeployBundle\Logger;
use EasyCorp\Bundle\EasyDeployBundle\Server\Property;
use EasyCorp\Bundle\EasyDeployBundle\Server\Server;
use Symfony\Component\Process\Process;
class TaskRunner
{
private $isDryRun;
private $logger;
public function __construct(bool $isDryRun, Logger $logger)
{
$this->isDryRun = $isDryRun;
$this->logger = $logger;
}
/**
* @return TaskCompleted[]
*/
public function run(Task $task): array
{
$results = [];
foreach ($task->getServers() as $server) {
$results[] = $this->doRun($server, $server->resolveProperties($task->getShellCommand()), $task->getEnvVars());
}
return $results;
}
private function doRun(Server $server, string $shellCommand, array $envVars): TaskCompleted
{
if ($server->has(Property::project_dir)) {
$shellCommand = sprintf('cd %s && %s', $server->get(Property::project_dir), $shellCommand);
}
// env vars aren't set with $process->setEnv() because it causes problems
// that can't be fully solved with inheritEnvironmentVariables()
if (!empty($envVars)) {
$envVarsAsString = http_build_query($envVars, '', ' ');
// the ';' after the env vars makes them available to all commands, not only the first one
// parenthesis create a sub-shell so the env vars don't affect to the parent shell
$shellCommand = sprintf('(export %s; %s)', $envVarsAsString, $shellCommand);
}
if ($server->has(Property::command_decorator)) {
$shellCommand = $this->applyCommandDecorator($server->get(Property::command_decorator), $server, $shellCommand, $envVars);
}
$this->logger->log(sprintf('[<server>%s</>] Executing command: <command>%s</>', $server, $shellCommand));
if ($this->isDryRun) {
return new TaskCompleted($server, '', 0);
}
if ($server->isLocalHost()) {
$process = new Process($shellCommand);
} else {
$process = new Process(sprintf('%s %s', $server->getSshConnectionString(), escapeshellarg($shellCommand)));
}
$process->setTimeout(null);
$process = $process->mustRun(function ($type, $buffer) {
if (Process::ERR === $type) {
$this->logger->log(Str::prefix(rtrim($buffer, PHP_EOL), '| <stream>err ::</> '));
} else {
$this->logger->log(Str::prefix(rtrim($buffer, PHP_EOL), '| <stream>out ::</> '));
}
});
return new TaskCompleted($server, $process->getOutput(), $process->getExitCode());
}
private function applyCommandDecorator(callable $decorator, Server $server, string $shellCommand, array $envVars): string
{
return $decorator($server, $shellCommand, $envVars);
}
}