-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathScriptExecutor.php
More file actions
278 lines (236 loc) · 8.12 KB
/
ScriptExecutor.php
File metadata and controls
278 lines (236 loc) · 8.12 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\Exception\ScriptLanguageNotSupported;
use ProcessMaker\Facades\Docker;
use ProcessMaker\Traits\Exportable;
use ProcessMaker\Traits\HasVersioning;
use ProcessMaker\Traits\HideSystemResources;
/**
* Represents an Eloquent model of a Script Executor
*
*
* @property int id
* @property string title
* @property text description
* @property string language
* @property text config
* @property string value
* @property text initDockerFile
*
* @OA\Schema(
* schema="scriptExecutorsEditable",
* @OA\Property(property="title", type="string"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="language", type="string"),
* @OA\Property(property="config", type="string"),
* @OA\Property(property="is_system", type="boolean"),
* ),
* @OA\Schema(
* schema="scriptExecutors",
* allOf={
* @OA\Schema(ref="#/components/schemas/scriptExecutorsEditable"),
* @OA\Schema(
* @OA\Property(property="id", type="integer", format="id"),
* @OA\Property(property="created_at", type="string", format="date-time"),
* @OA\Property(property="updated_at", type="string", format="date-time"),
* )
* },
* ),
*
* @OA\Schema(
* schema="availableLanguages",
* @OA\Property(property="text", type="string"),
* @OA\Property(property="value", type="string"),
* @OA\Property(property="initDockerFile", type="string"),
* ),
*/
class ScriptExecutor extends ProcessMakerModel
{
const MICROSERVICE_LANGUAGES = [
'php' => 'PHP Executor',
'javascript' => 'JavaScript Executor',
'python' => 'Python Executor',
'csharp' => 'C# Executor',
'java' => 'Java Executor',
'javascript-ssr' => 'JavaScript SSR Executor',
];
use HasVersioning;
use Exportable;
use HideSystemResources;
protected $fillable = [
'title', 'description', 'language', 'config', 'is_system', 'type',
];
protected $casts = [
'type' => ScriptExecutorType::class,
];
// Lua and R are deprecated. This scope can be removed
// when they are removed permanently.
public function scopeActive($query)
{
return $query->whereNotIn('language', Script::deprecatedLanguages);
}
public static function install($params)
{
$language = $params['language'];
try {
$initialExecutor = self::initialExecutor($language);
} catch (ScriptLanguageNotSupported $e) {
$initialExecutor = null;
}
if ($initialExecutor) {
$initialExecutor->update($params);
} else {
$initialExecutor = self::create($params);
Script::where('language', $language)->update(['script_executor_id' => $initialExecutor->id]);
ScriptVersion::where('language', $language)->update(['script_executor_id' => $initialExecutor->id]);
}
return $initialExecutor;
}
public static function initialExecutor($language)
{
$initialExecutor = self::where('language', $language)
->orderBy('created_at', 'asc')
->first();
if (!$initialExecutor) {
if (app()->runningInConsole()) {
Log::error('Script Executor not found for language: ' . $language);
return null;
} else {
throw new ScriptLanguageNotSupported($language);
}
}
return $initialExecutor;
}
public function versions()
{
return $this->hasMany(ScriptExecutorVersion::class);
}
public static function initDockerfile($language)
{
// remove try/catch block after lang packages updated
try {
$dockerfile = file_get_contents(self::packagePath($language) . '/Dockerfile');
} catch (\ErrorException $e) {
$dockerfile = '';
}
$initDockerfile = self::config($language)['init_dockerfile'];
// remove check after lang packages updated
if (!is_array($initDockerfile)) {
$initDockerfile = explode("\n", $initDockerfile);
}
$dockerfile .= "\n" . implode("\n", $initDockerfile);
return $dockerfile;
}
public static function finalInstructions($language)
{
$finalInstruction = self::config($language)['final_instructions'] ?? '';
return is_array($finalInstruction) ? implode("\n", $finalInstruction) : $finalInstruction;
}
public static function packagePath($language)
{
return self::config($language)['package_path'];
}
public static function config($language)
{
$config = config('script-runners');
$language = strtolower($language);
if (!isset($config[$language])) {
throw new \ErrorException('Language not in config: ' . $language);
}
return $config[$language];
}
public static function rules($existing = null)
{
if ($existing) {
$allowedLanguages = Script::scriptFormatValues();
} else {
$allowedLanguages = array_filter(Script::scriptFormatValues(), function ($language) {
return !in_array($language, Script::deprecatedLanguages);
});
}
return [
'title' => 'required',
'language' => [
'required',
Rule::in($allowedLanguages),
],
'type' => [
'sometimes',
new Enum(ScriptExecutorType::class),
'nullable',
],
];
}
public static function list($language = null, $forEditMode = false)
{
$list = [];
$executors =
self::active()->where('is_system', false)->orderBy('language', 'asc')
->orderBy('created_at', 'asc');
if ($language) {
$executors->where('language', $language);
// If the list is for edition mode and "php" language is selected also include "php-nayra"
if ($language === 'php' && $forEditMode) {
$executors->orWhere('language', 'php-nayra');
}
// If the list is for edition mode and "php-nayra" language is selected also include "php"
if ($language === 'php-nayra' && $forEditMode) {
$executors->orWhere('language', 'php');
}
}
foreach ($executors->get() as $executor) {
$list[$executor->id] = [
'language' => $executor->language,
'title' => $executor->title,
];
}
return $list;
}
public function dockerImageName()
{
$lang = strtolower($this->language);
$id = $this->id;
$tag = $this->imageTag();
$instance = config('app.instance');
return "processmaker4/executor-{$instance}-{$lang}-{$id}:{$tag}";
}
public function imageTag()
{
$config = self::config($this->language);
$tag = 'v';
if (isset($config['package_version'])) {
$tag .= $config['package_version'];
}
return $tag;
}
public function scripts()
{
return $this->hasMany(Script::class);
}
public function getScriptsCountAttribute()
{
return $this->scripts()->count();
}
public function dockerImageExists()
{
$images = self::listOfExecutorImages();
return in_array($this->dockerImageName(), $images);
}
public static function listOfExecutorImages($filterByLanguage = null)
{
exec(Docker::command() . ' images | awk \'{r=$1":"$2; print r}\'', $result);
$instance = config('app.instance');
return array_values(array_filter($result, function ($image) use ($filterByLanguage, $instance) {
$filter = "processmaker4/executor-{$instance}-";
if ($filterByLanguage) {
$filter .= $filterByLanguage . '-';
}
return strpos($image, $filter) !== false;
}));
}
}