-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathScriptDockerBindingFilesTrait.php
More file actions
171 lines (152 loc) · 4.51 KB
/
Copy pathScriptDockerBindingFilesTrait.php
File metadata and controls
171 lines (152 loc) · 4.51 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
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace ProcessMaker\Models;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Exception\ScriptException;
use ProcessMaker\Exception\ScriptTimeoutException;
use ProcessMaker\Facades\Docker;
/**
* Execute a docker container binding files to interchange information.
*/
trait ScriptDockerBindingFilesTrait
{
private $temporalFiles = [];
private $outputFiles = [];
/**
* Run a command in a docker container.
*
* @param array $options
*
* @return array
* @throws \RuntimeException
*/
protected function executeBinding(array $options)
{
$bindings = '';
foreach ($options['inputs'] as $guestFile => $content) {
$bindings .= $this->bindInput($guestFile, $content);
}
foreach ($options['outputs'] as $name => $guestFile) {
$bindings .= $this->bindOutput($guestFile, $name);
}
$response = $this->runContainer(
$options['image'],
$options['command'],
$options['parameters'],
$bindings,
$options['timeout']
);
return $response;
}
/**
* Execute a docker container.
*
* @param string $image
* @param string $command
* @param string $parameters
* @param string $bindings
* @param int $timeout
*
* @return array
* @throws ScriptTimeoutException
*/
private function runContainer($image, $command, $parameters, $bindings, $timeout)
{
$cmd = Docker::command($timeout) . sprintf(
' run --rm %s %s %s %s 2>&1',
$parameters,
$bindings,
$image,
$command
);
Log::debug('Running Docker container', [
'timeout' => $timeout,
'cmd' => $cmd,
]);
$line = exec($cmd, $output, $returnCode);
if ($returnCode) {
if ($returnCode == 137 || $returnCode == 9) {
Log::error('Script timed out');
$this->removeTemporalFiles();
throw new ScriptTimeoutException(
__('Script took too long to complete. Consider increasing the timeout.')
. "\n"
. __('Timeout: :timeout seconds', ['timeout' => $timeout])
. "\n"
. implode("\n", $output)
);
}
Log::error('Script threw return code ' . $returnCode . ' Message: ' . implode("\n", $output));
$message = implode("\n", $output);
$message .= "\n\nProcessMaker Stack:\n";
$message .= (new \Exception)->getTraceAsString();
$this->removeTemporalFiles();
throw new ScriptException($message);
}
$outputs = $this->getOutputFilesContent();
$this->removeTemporalFiles();
return compact('line', 'output', 'returnCode', 'outputs');
}
/**
* Get the parameter to bind two files.
*
* @param string $hostFile
* @param string $guestFile
*
* @return string
*/
private function bindFile($hostFile, $guestFile)
{
return sprintf(' -v %s:%s', $hostFile, $guestFile);
}
/**
* Put a content into a gust file.
*
* @param string $guestFile
* @param string $content
*
* @return string
*/
private function bindInput($guestFile, $content)
{
$hostFile = tempnam(config('app.processmaker_scripts_home'), 'put');
$this->temporalFiles[] = $hostFile;
file_put_contents($hostFile, $content);
return $this->bindFile($hostFile, $guestFile);
}
/**
* Put a content into a gust file.
*
* @param string $guestFile
*
* @return string
*/
private function bindOutput($guestFile, $name)
{
$hostFile = tempnam(config('app.processmaker_scripts_home'), 'get');
$this->temporalFiles[] = $hostFile;
$this->outputFiles[$name] = $hostFile;
return $this->bindFile($hostFile, $guestFile);
}
/**
* Get the contents of the binded output files.
*
* @return array
*/
private function getOutputFilesContent()
{
$outputs = [];
foreach ($this->outputFiles as $name => $filename) {
$outputs[$name] = file_get_contents($filename);
}
return $outputs;
}
/**
* Remove the temporal files.
*/
private function removeTemporalFiles()
{
foreach ($this->temporalFiles as $filename) {
unlink($filename);
}
}
}