Repository navigation
Expand file tree
/
Copy pathScriptDockerCopyingFilesTrait.php
More file actions
159 lines (143 loc) · 4.68 KB
/
Copy pathScriptDockerCopyingFilesTrait.php
File metadata and controls
159 lines (143 loc) · 4.68 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace ProcessMaker\Models;
use Log;
use ProcessMaker\Exception\ScriptException;
use ProcessMaker\Exception\ScriptTimeoutException;
use ProcessMaker\Facades\Docker;
use RuntimeException;
/**
* Execute a docker container copying files to interchange information.
*/
trait ScriptDockerCopyingFilesTrait
{
/**
* Run a command in a docker container.
*
* @param array $options
*
* @return array
* @throws RuntimeException
*/
protected function executeCopying(array $options)
{
$container = $this->createContainer($options['image'], $options['command'], $options['parameters']);
foreach ($options['inputs'] as $path => $data) {
$this->putInContainer($container, $path, $data);
}
$response = $this->startContainer($container, $options['timeout']);
$outputs = [];
foreach ($options['outputs'] as $name => $path) {
$outputs[$name] = $this->getFromContainer($container, $path);
}
exec(Docker::command() . ' rm ' . $container);
$response['outputs'] = $outputs;
return $response;
}
/**
* Create a docker container.
*
* @param string $image
* @param string $command
* @param string $parameters
*
* @return string
* @throws RuntimeException
*/
private function createContainer($image, $command, $parameters = '')
{
$cidfile = tempnam(config('app.processmaker_scripts_home'), 'cid');
unlink($cidfile);
$cmd = Docker::command() . sprintf(' create --network=host %s --cidfile %s %s %s 2>&1', $parameters, $cidfile, $image, $command);
$line = exec($cmd, $output, $returnCode);
if ($returnCode) {
throw new RuntimeException('Unable to create a docker container: ' . implode("\n", $output));
}
if (!file_exists($cidfile)) {
throw new RuntimeException('Unable to create a docker container: ' . implode("\n", $output));
}
$cid = file_get_contents($cidfile);
unlink($cidfile);
return $cid;
}
/**
* Put a string content into a file in the container.
*
* @param string $container
* @param string $path
* @param string $content
*
* @throws RuntimeException
*/
private function putInContainer($container, $path, $content)
{
$source = tempnam(config('app.processmaker_scripts_home'), 'put');
file_put_contents($source, $content);
list($returnCode, $output) = $this->execCopy($source, $container, $path);
unlink($source);
if ($returnCode) {
throw new RuntimeException('Unable to send data to container: ' . implode("\n", $output));
}
}
/**
* Runs the docker copy command
*
* @param string $source
* @param string $container
* @param string $dest
*
* @throws RuntimeException
*/
private function execCopy($source, $container, $dest)
{
$cmd = Docker::command() . sprintf(' cp %s %s:%s 2>&1', $source, $container, $dest);
exec($cmd, $output, $returnCode);
return [$returnCode, $output];
}
/**
* Get the content from a file in the container.
*
* @param string $container
* @param string $path
*
* @return string
* @throws RuntimeException
*/
private function getFromContainer($container, $path)
{
$target = tempnam(config('app.processmaker_scripts_home'), 'get');
$cmd = Docker::command() . sprintf(' cp %s:%s %s 2>&1', $container, $path, $target);
exec($cmd, $output, $returnCode);
$content = file_get_contents($target);
unlink($target);
return $content;
}
/**
* Start the container.
*
* @param string $container
* @param int $timeout
*
* @return array
*/
private function startContainer($container, $timeout)
{
$cmd = Docker::command($timeout) . sprintf(' start %s -a 2>&1', $container);
Log::debug('Running Docker container', [
'timeout' => $timeout,
'cmd' => $cmd,
]);
$line = exec($cmd, $output, $returnCode);
if ($returnCode) {
if ($returnCode == 137) {
Log::error('Script timed out');
throw new ScriptTimeoutException(implode("\n", $output));
}
Log::error('Script threw return code ' . $returnCode);
$message = implode("\n", $output);
$message .= "\n\nProcessMaker Stack:\n";
$message .= (new \Exception)->getTraceAsString();
throw new ScriptException($message);
}
return compact('line', 'output', 'returnCode');
}
}