Repository navigation
Expand file tree
/
Copy pathBase.php
More file actions
151 lines (112 loc) · 3.73 KB
/
Copy pathBase.php
File metadata and controls
151 lines (112 loc) · 3.73 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
<?php
namespace ProcessMaker\ScriptRunners;
use Log;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\ScriptDockerBindingFilesTrait;
use ProcessMaker\Models\ScriptDockerCopyingFilesTrait;
use ProcessMaker\Models\User;
use RuntimeException;
use ProcessMaker\GenerateAccessToken;
abstract class Base
{
use ScriptDockerCopyingFilesTrait;
use ScriptDockerBindingFilesTrait;
/**
* Prepare the docker configuration.
*
* @param string $code
* @param array $dockerConfig
*
* @return array
*/
abstract public function config($code, array $dockerConfig);
/**
* Set the user to run this script as
*
* @var \ProcessMaker\Models\User $user
*/
private $user;
/**
* Run a script code.
*
* @param string $code
* @param array $data
* @param array $config
* @param integer $timeout
* @param \ProcessMaker\Models\User $user
*
* @return array
* @throws \RuntimeException
*/
public function run($code, array $data, array $config, $timeout, ?User $user)
{
// Prepare the docker parameters
$environmentVariables = $this->getEnvironmentVariables();
// Create tokens for the SDK if a user is set
$token = null;
if ($user) {
$token = new GenerateAccessToken($user);
$environmentVariables[] = 'API_TOKEN=' . $token->getToken();
$environmentVariables[] = 'API_HOST=' . config('app.url') . '/api/1.0';
$environmentVariables[] = 'API_SSL_VERIFY=' . config('app.api_ssl_verify') ? 'true' : 'false';
}
if ($environmentVariables) {
$parameters = '-e ' . implode(' -e ', $environmentVariables);
} else {
$parameters = '';
}
$dockerConfig = $this->config($code, [
'timeout' => $timeout,
'parameters' => $parameters,
'inputs' => [
'/opt/executor/data.json' => json_encode($data),
'/opt/executor/config.json' => json_encode($config),
],
'outputs' => [
'response' => '/opt/executor/output.json'
]
]);
// Execute docker
$executeMethod = config('app.processmaker_scripts_docker_mode') === 'binding'
? 'executeBinding' : 'executeCopying';
Log::debug('Executing docker', [
'executeMethod' => $executeMethod,
]);
$response = $this->$executeMethod($dockerConfig);
// Delete the token we created for this run
if ($token) {
$token->delete();
}
// Process the output
$returnCode = $response['returnCode'];
$stdOutput = $response['output'];
$output = $response['outputs']['response'];
if ($returnCode || $stdOutput) {
// Has an error code
throw new RuntimeException(implode("\n", $stdOutput));
} else {
// Success
$response = json_decode($output, true);
return [
'output' => $response
];
}
}
/**
* Get the environment variables.
*
* @return array
*/
private function getEnvironmentVariables()
{
$variablesParameter = [];
EnvironmentVariable::chunk(50, function ($variables) use (&$variablesParameter) {
foreach ($variables as $variable) {
$variablesParameter[] = escapeshellarg($variable['name']) . '=' . escapeshellarg($variable['value']);
}
});
// Add the url to the host
$variablesParameter[] = 'HOST_URL=' . escapeshellarg(config('app.docker_host_url'));
return $variablesParameter;
}
}