Repository navigation
Expand file tree
/
Copy pathScriptDockerBindingFilesTrait.php
More file actions
120 lines (110 loc) · 3.13 KB
/
Copy pathScriptDockerBindingFilesTrait.php
File metadata and controls
120 lines (110 loc) · 3.13 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
<?php
namespace ProcessMaker\Models;
use RuntimeException;
/**
* 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);
return $response;
}
/**
* Execute a docker container.
*
* @param string $image
* @param string $command
* @param string $parameters
* @param string $bindings
*
* @return array
* @throws RuntimeException
*/
private function runContainer($image, $command, $parameters, $bindings)
{
$cmd = config('app.bpm_scripts_docker') . sprintf(' run %s %s %s %s 2>&1',
$parameters, $bindings, $image, $command);
$line = exec($cmd, $output, $returnCode);
if ($returnCode) {
throw new RuntimeException('Unable to create a docker container: ' . implode("\n",
$output));
}
$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.bpm_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.bpm_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;
}
}