Repository navigation
Expand file tree
/
Copy pathScriptDockerCopyingFilesTrait.php
More file actions
148 lines (136 loc) · 4.42 KB
/
Copy pathScriptDockerCopyingFilesTrait.php
File metadata and controls
148 lines (136 loc) · 4.42 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
<?php
namespace ProcessMaker\Models;
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);
}
if (array_key_exists('folders', $options)) {
foreach ($options['folders'] as $source => $dest) {
list($returnCode, $output) = $this->execCopy($source, $container, $dest);
if ($returnCode) {
throw new RuntimeException('Unable to send data to container: ' . implode("\n", $output));
}
}
}
$response = $this->startContainer($container);
$outputs = [];
foreach ($options['outputs'] as $name => $path) {
$outputs[$name] = $this->getFromContainer($container, $path);
}
exec(config('app.bpm_scripts_docker') . ' 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.bpm_scripts_home'), 'cid');
unlink($cidfile);
$cmd = config('app.bpm_scripts_docker') . sprintf(' create %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.bpm_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 = config('app.bpm_scripts_docker')
. 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.bpm_scripts_home'), 'get');
$cmd = config('app.bpm_scripts_docker') . 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
*
* @return array
*/
private function startContainer($container)
{
$cmd = config('app.bpm_scripts_docker') . sprintf(' start %s -a 2>&1', $container);
$line = exec($cmd, $output, $returnCode);
if ($returnCode) {
throw new RuntimeException(implode("\n", $output));
}
return compact('line', 'output', 'returnCode');
}
}