Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
actionsdata.php
1<?php
2namespace Bitrix\Scale;
3
6use \Bitrix\Main\Localization\Loc;
7Loc::loadMessages(__FILE__);
8
14{
16
22 public static function getAction($actionId)
23 {
24 if($actionId == '')
25 throw new \Bitrix\Main\ArgumentNullException("actionId");
26
27 $actionsDefinitions = static::getList();
28
29 $result = [];
30
31 if(isset($actionsDefinitions[$actionId]))
32 $result = $actionsDefinitions[$actionId];
33
34 return $result;
35 }
36
48 public static function getActionObject(
49 $actionId,
50 $serverHostname = "",
51 array $userParams = [],
52 array $freeParams = [],
53 array $actionParams = []
54 )
55 {
56 if($actionId == '')
57 throw new \Bitrix\Main\ArgumentNullException("actionId");
58
59 if(!is_array($userParams))
60 throw new \Bitrix\Main\ArgumentTypeException("userParams", "array");
61
62 if(!is_array($userParams))
63 throw new \Bitrix\Main\ArgumentTypeException("freeParams", "array");
64
65 if(!is_array($actionParams))
66 throw new \Bitrix\Main\ArgumentTypeException("actionParams", "array");
67
68 $action = false;
69
70 if(!isset($actionParams["TYPE"]) || $actionParams["TYPE"] !== "MODIFYED")
71 $actionParams = static::getAction($actionId);
72
73 if(empty($actionParams))
74 throw new \Exception("Can't find params of action ".$actionId);
75
76 if(isset($actionParams["TYPE"]) && $actionParams["TYPE"] === "CHAIN")
77 $action = new ActionsChain($actionId, $actionParams, $serverHostname, $userParams, $freeParams);
78 else if(!empty($actionParams))
79 $action = new Action($actionId, $actionParams, $serverHostname, $userParams, $freeParams);
80
81 return $action;
82 }
83
89 public static function getActionState($bid)
90 {
91 $result = [];
92 $shellAdapter = new ShellAdapter();
93 $execRes = $shellAdapter->syncExec("sudo -u root /opt/webdir/bin/bx-process -a status -t ".$bid." -o json");
94 $data = $shellAdapter->getLastOutput();
95
96 if($execRes)
97 {
98 $arData = json_decode($data, true);
99
100 if(isset($arData["params"][$bid]))
101 {
102 $result = $arData["params"][$bid];
103 }
104
105 if($result["status"] === "finished")
106 {
109 "SCALE_ACTION_CHECK_STATE",
110 $bid,
111 Loc::getMessage("SCALE_ACTIONSDATA_ACTION_FINISHED")
112 );
113 }
114 elseif($result["status"] === "error")
115 {
118 "SCALE_ACTION_CHECK_STATE",
119 $bid,
120 Loc::getMessage("SCALE_ACTIONSDATA_ACTION_ERROR")
121 );
122 }
123
124 if(self::$logLevel >= Logger::LOG_LEVEL_DEBUG)
125 {
126 Logger::addRecord(Logger::LOG_LEVEL_DEBUG, "SCALE_ACTION_CHECK_STATE", $bid, $data);
127 }
128 }
129
130 return $result;
131 }
132
139 public static function getList($checkConditions = false)
140 {
141 static $def = null;
142
143 if($def == null)
144 {
145 $filename = \Bitrix\Main\Application::getDocumentRoot()."/bitrix/modules/scale/include/actionsdefinitions.php";
146 $file = new \Bitrix\Main\IO\File($filename);
147 $actionsDefinitions = [];
148
149 if($file->isExists())
150 require_once($filename);
151 else
152 throw new \Bitrix\Main\IO\FileNotFoundException($filename);
153
154 if(isset($actionsDefinitions))
155 {
156 $def = $actionsDefinitions;
157
158 if(is_array($def) && $checkConditions)
159 {
160 foreach($def as $actionId => $action)
161 {
162 if(isset($action["CONDITION"]) && !self::isConditionSatisfied($action["CONDITION"]))
163 {
164 unset($def[$actionId]);
165 }
166 }
167 }
168
169 if(getenv('BITRIX_ENV_TYPE') === 'crm')
170 {
171 unset(
172 $def['MONITORING_ENABLE'],
173 $def['SITE_CREATE'],
174 $def['SITE_CREATE_LINK'],
175 $def['SITE_CREATE_KERNEL'],
176 $def['SITE_DEL'],
177 $def['MEMCACHED_ADD_ROLE'],
178 $def['MEMCACHED_DEL_ROLE'],
179 $def['SPHINX_ADD_ROLE'],
180 $def['PUSH_DEL_ROLE']
181 );
182 }
183 }
184 else
185 {
186 $def = [];
187 }
188 }
189
190 return $def;
191 }
192
197 protected static function isConditionSatisfied($condition): bool
198 {
199 $result = true;
200
201 if(!isset($condition["COMMAND"], $condition["PARAMS"]) || !is_array($condition["PARAMS"]))
202 {
203 return true;
204 }
205
206 if(!isset($condition["PARAMS"][0], $condition["PARAMS"][1], $condition["PARAMS"][2]))
207 {
208 return true;
209 }
210
211 $actRes = static::getConditionActionResult($condition["COMMAND"]);
212
213 if(isset($actRes["condition"]["OUTPUT"]["DATA"]["params"]))
214 {
215 $conditionValue = static::extractConditionValue(
216 $condition["PARAMS"][0],
217 $actRes["condition"]["OUTPUT"]["DATA"]["params"]
218 );
219
220 if($conditionValue)
221 {
222 $result = static::checkCondition(
223 $conditionValue,
224 $condition["PARAMS"][1],
225 $condition["PARAMS"][2]
226 );
227 }
228 }
229
230 return $result;
231 }
232
238 protected static function extractConditionValue(string $paramName, array $paramsValues): ?string
239 {
240 $result = null;
241 $params = explode(":", $paramName);
242
243 if(!is_array($params) || count($params) !== 2)
244 {
245 throw new ArgumentException('paramName must be like paramSection:paramName');
246 }
247
248 if(isset($paramsValues[$params[0]][$params[1]]))
249 {
250 $result = (string)$paramsValues[$params[0]][$params[1]];
251 }
252
253 return $result;
254 }
255
260 protected static function getConditionActionResult(string $command): array
261 {
262 $result = [];
263
264 try
265 {
266 $action = new Action("condition", [
267 "START_COMMAND_TEMPLATE" => $command,
268 "LOG_LEVEL" => Logger::LOG_LEVEL_DISABLE
269 ], "", []
270 );
271
272 if($action->start())
273 {
274 $result = $action->getResult();
275 }
276 }
277 catch(\Exception $excpt)
278 {}
279
280 return $result;
281 }
282
291 protected static function checkCondition(string $operand1, string $operator, string $operand2): bool
292 {
293 $allowedOperators = ['==='];
294
295 if(!in_array($operator, $allowedOperators))
296 {
297 throw new ArgumentOutOfRangeException('This "operator" is not allowed');
298 };
299
300 $allowedOperandRegex = '/^[0-9a-zA-Z_:\-\'\"]+$/i';
301
302 if(!preg_match($allowedOperandRegex, $operand1))
303 {
304 return false;
305 }
306
307 if(!preg_match($allowedOperandRegex, $operand2))
308 {
309 throw new ArgumentOutOfRangeException('This "operand2" is wrong');
310 }
311
312 return eval("return ('{$operand1}' {$operator} '{$operand2}');");
313 }
314
318 public static function setLogLevel($logLevel)
319 {
320 self::$logLevel = $logLevel;
321 }
322
329 public static function checkRunningAction()
330 {
331 $result = [];
332 $shellAdapter = new ShellAdapter();
333 $execRes = $shellAdapter->syncExec("sudo -u root /opt/webdir/bin/bx-process -a list -o json");
334 $data = $shellAdapter->getLastOutput();
335
336 if($execRes)
337 {
338 $arData = json_decode($data, true);
339 $result = [];
340
341 if(isset($arData["params"]) && is_array($arData["params"]))
342 {
343 foreach($arData["params"] as $bid => $actionParams)
344 {
345 if(mb_strpos($bid, 'common_') === 0) // || strpos($bid, 'monitor_') === 0)
346 continue;
347
348 if($actionParams["status"] === "running")
349 {
350 $result = [$bid => $actionParams];
351 break;
352 }
353 }
354 }
355 }
356
357 return $result;
358 }
359}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getActionState($bid)
static checkCondition(string $operand1, string $operator, string $operand2)
static getActionObject( $actionId, $serverHostname="", array $userParams=[], array $freeParams=[], array $actionParams=[])
static extractConditionValue(string $paramName, array $paramsValues)
static setLogLevel($logLevel)
static isConditionSatisfied($condition)
static getConditionActionResult(string $command)
static getAction($actionId)
static getList($checkConditions=false)
const LOG_LEVEL_DISABLE
Definition logger.php:14
static addRecord($level, $auditType, $itemId, $description)
Definition logger.php:26
const LOG_LEVEL_DEBUG
Definition logger.php:17
const LOG_LEVEL_ERROR
Definition logger.php:15