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
187 lines (160 loc) · 5.47 KB
/
ScriptExecutor.php
File metadata and controls
187 lines (160 loc) · 5.47 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Model;
use ProcessMaker\Traits\HasVersioning;
use Illuminate\Validation\Rule;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use ProcessMaker\Exception\ScriptLanguageNotSupported;
class ScriptExecutor extends Model
{
use HasVersioning;
protected $fillable = [
'title', 'description', 'language', 'config'
];
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::orderBy('language', 'asc')
->orderBy('created_at', 'asc');
if ($language) {
$executors->where('language', $language);
}
foreach ($executors->get() as $executor) {
$list[$executor->id] = $executor->language . " - " . $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);
}
/**
* If we need to run a docker image in a test, chances are the Executor IDs wont
* match up with the docker image names. To prevent errors, lets just grab the first
* image available on the testing machine and explicitly set the executor image.
* When 'image' is set in the config, it will ignore the one provided by the factory executor.
*
* @param string $language
* @return void
*/
public static function setTestConfig($language)
{
ScriptExecutor::firstOrCreate(
['language' => $language],
['title' => 'Test Executor']
);
$images = self::listOfExecutorImages($language);
if (count($images) === 0) {
throw new \Exception("No matching docker image for $language");
}
config(["script-runners.${language}.image" => $images[0]]);
}
public static function listOfExecutorImages($filterByLanguage = null)
{
exec('docker 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;
}));
}
}