-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathProcessRunner.php
More file actions
79 lines (66 loc) · 2.29 KB
/
Copy pathProcessRunner.php
File metadata and controls
79 lines (66 loc) · 2.29 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
<?php
declare(strict_types=1);
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\ProcessRunner;
use Deployer\Exception\RunException;
use Deployer\Exception\TimeoutException;
use Deployer\Host\Host;
use Deployer\Logger\Logger;
use Deployer\Ssh\RunParams;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
use function Deployer\Support\deployer_root;
use function Deployer\Support\env_stringify;
use function Deployer\Support\replace_secrets;
class ProcessRunner
{
private Logger $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function run(Host $host, string $command, RunParams $params): string
{
$this->logger->command($host, 'run', $command);
if (!empty($params->env)) {
$env = env_stringify($params->env);
$command = "export $env; $command";
}
if (!empty($params->dotenv)) {
$command = "source $params->dotenv; $command";
}
$process = Process::fromShellCommandline($params->shell)
->setInput(replace_secrets($command, $params->secrets))
->setTimeout($params->timeout)
->setIdleTimeout($params->idleTimeout)
->setWorkingDirectory($params->cwd ?? deployer_root());
$callback = function ($type, $buffer) use ($params, $host) {
$this->logger->print($host, $buffer, $params->forceOutput);
};
try {
$process->mustRun($callback);
return $process->getOutput();
} catch (ProcessFailedException) {
if ($params->nothrow) {
return '';
}
throw new RunException(
$host,
$command,
$process->getExitCode(),
$process->getOutput(),
$process->getErrorOutput(),
);
} catch (ProcessTimedOutException $exception) {
throw new TimeoutException(
$command,
$exception->getExceededTimeout(),
);
}
}
}