-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathTaskRunner.php
More file actions
73 lines (60 loc) · 2.36 KB
/
Copy pathTaskRunner.php
File metadata and controls
73 lines (60 loc) · 2.36 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
<?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
{
// Start all processes asynchronously
$processes = [];
foreach ($task->getServers() as $server) {
$processes[] = $this->startProcess($server, $server->resolveProperties($task->getShellCommand()), $task->getEnvVars());
}
// Collect all their results
$results = [];
foreach ($processes as $process) {
$results[] = $process->getCompletionResult();
}
return $results;
}
private function startProcess(Server $server, string $shellCommand, array $envVars): PendingTask
{
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);
}
$this->logger->log(sprintf('[<server>%s</>] Executing command: <command>%s</>', $server, $shellCommand));
$pendingTask = new PendingTask($server, $shellCommand, $this->logger, $this->isDryRun);
$pendingTask->start();
return $pendingTask;
}
}