forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptExecutor.php
More file actions
219 lines (186 loc) · 6.05 KB
/
ScriptExecutor.php
File metadata and controls
219 lines (186 loc) · 6.05 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\Rule;
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
{
use HasVersioning;
use Exportable;
use HideSystemResources;
protected $fillable = [
'title', 'description', 'language', 'config', 'is_system',
];
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) {
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 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)
{
return [
'title' => 'required',
'language' => [
'required',
Rule::in(Script::scriptFormatValues()),
],
];
}
public static function list($language = null)
{
$list = [];
$executors =
self::where('is_system', false)->orderBy('language', 'asc')
->orderBy('created_at', 'asc');
if ($language) {
$executors->where('language', $language);
}
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;
}));
}
}