Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
action.php
1<?php
2namespace Bitrix\Scale;
3
5use \Bitrix\Main\Localization\Loc;
6Loc::loadMessages(__FILE__);
7
12class Action
13{
14 protected $id = "";
15 protected $userParams = array();
16 protected $freeParams = array();
17 protected $actionParams = array();
18 protected $serverHostname = "";
19
20 protected $shellAdapter = null;
21 protected $result = array();
23
34 public function __construct($actionId, $actionParams, $serverHostname="", $userParams = array(), $freeParams = array())
35 {
36 if($actionId == '')
37 throw new \Bitrix\Main\ArgumentNullException("actionId");
38
39 if(!is_array($actionParams) || empty($actionParams))
40 throw new \Exception("Params of action ".$actionId." are not defined correctly!");
41
42 if(!isset($actionParams["START_COMMAND_TEMPLATE"]) || $actionParams["START_COMMAND_TEMPLATE"] == '')
43 throw new \Exception("Required param START_COMMAND_TEMPLATE of action ".$actionId." are not defined!");
44
45 if(!is_array($userParams))
46 throw new \Bitrix\Main\ArgumentTypeException("userParams", "array");
47
48 if(!is_array($freeParams))
49 throw new \Bitrix\Main\ArgumentTypeException("freeParams", "array");
50
51 $this->id = $actionId;
52 $this->userParams = $userParams;
53 $this->freeParams = $freeParams;
54 $this->actionParams = $actionParams;
55 $this->serverHostname = $serverHostname;
56 $this->shellAdapter = new ShellAdapter;
57
58 if(isset($actionParams["LOG_LEVEL"]))
59 $this->logLevel = $actionParams["LOG_LEVEL"];
60 }
61
62 protected function getServerParams()
63 {
64 return ServersData::getServer($this->serverHostname);
65 }
66
73 protected function makeStartCommand($inputParams = array())
74 {
75 if(!is_array($inputParams))
76 throw new \Bitrix\Main\ArgumentTypeException("inputParams", "array");
77
78 $retStr = $this->actionParams["START_COMMAND_TEMPLATE"];
79
80 foreach ($this->userParams as $key => $paramValue)
81 {
82 if($this->actionParams['USER_PARAMS'][$key]['THROUGH_FILE'] == 'Y')
83 {
84 if($paramValue <> '')
85 {
86 $tmpDir = Helper::getTmpDir();
87 $tmpFile = $tmpDir.'/.'.randString();
88 $res = File::putFileContents($tmpFile, $paramValue);
89
90 if($res === false)
91 return '';
92
93 $paramValue = $tmpFile;
94 }
95 }
96
97 $retStr = str_replace('##USER_PARAMS:'.$key.'##', $paramValue, $retStr);
98 }
99
100 if($this->serverHostname <> '' && $this->serverHostname != "global")
101 {
102 $serverParams = $this->getServerParams();
103 $serverParams["hostname"] = $this->serverHostname;
104
105 if(is_array($serverParams))
106 {
107 foreach ($serverParams as $key => $paramValue)
108 {
109 if(is_string($paramValue))
110 {
111 $retStr = str_replace('##SERVER_PARAMS:' . $key . '##', $paramValue, $retStr);
112 }
113 }
114 }
115 }
116
117 if(!empty($inputParams))
118 foreach ($inputParams as $key => $paramValue)
119 $retStr = str_replace('##INPUT_PARAMS:'.$key.'##', $paramValue, $retStr);
120
121 if(isset($this->actionParams["CODE_PARAMS"]) && is_array($this->actionParams["CODE_PARAMS"]))
122 {
123 foreach($this->actionParams["CODE_PARAMS"] as $paramId => $paramCode)
124 {
125 $res = eval($paramCode);
126 $retStr = str_replace('##CODE_PARAMS:'.$paramId.'##', $res, $retStr);
127 }
128 }
129
130 foreach ($this->freeParams as $key => $paramValue)
131 $retStr = str_replace('##'.$key.'##', $paramValue, $retStr);
132
133 return $retStr;
134 }
135
143 public function start(array $inputParams = array())
144 {
145 if(!is_array($inputParams))
146 throw new \Bitrix\Main\ArgumentTypeException("inputParams", "array");
147
148 if(isset($this->actionParams["MODIFYERS"]) && is_array($this->actionParams["MODIFYERS"]))
149 {
150 $needMoreUserInfo = false;
151
152 foreach($this->actionParams["MODIFYERS"] as $modifyerFunction)
153 {
154 if(is_callable($modifyerFunction))
155 {
156 try
157 {
158 $this->actionParams = call_user_func($modifyerFunction, $this->id, $this->actionParams, $this->serverHostname, $this->userParams);
159 }
161 {
162 $this->actionParams = $e->getActionParams();
163 $needMoreUserInfo = true;
164 }
165 }
166 }
167
168 if($needMoreUserInfo)
169 throw new NeedMoreUserInfoException("Need more user's info", $this->actionParams);
170 }
171
172 $result = null;
173 $output = '';
174 $arOutput = array();
175 $command = $this->makeStartCommand($inputParams);
176
177 if($command <> '')
178 {
179 $result = $this->shellAdapter->syncExec($command);
180 $output = $this->shellAdapter->getLastOutput();
181 $arOutput = array();
182
183 if($output <> '')
184 {
185 $arOut = json_decode($output, true);
186
187 if(is_array($arOut) && !empty($arOut))
188 $arOutput = $arOut;
189 }
190
191 //error returned by shell
192 $error = $this->shellAdapter->getLastError();
193
194 //error returned by bitrix-env
195 if(isset($arOutput["error"]) && intval($arOutput["error"]) > 0 && isset($arOutput["message"]) && $arOutput["message"] <> '')
196 $error .= " ".$arOutput["message"];
197
198 $this->makeLogRecords($command, $result, $output, $error);
199 }
200 else //$command == ''
201 {
202 $result = false;
203 $error = 'Cant\'t create command for action execution';
204 }
205
206 $this->result = array(
207 $this->id => array(
208 "NAME" => isset($this->actionParams["NAME"]) ? $this->actionParams["NAME"] : "[".$this->id."]",
209 "RESULT" => $result ? "OK" : "ERROR",
210 "OUTPUT" => array(
211 "TEXT" => $output,
212 "DATA" => $arOutput
213 ),
214 "ERROR" => $error
215 )
216 );
217
218 return $result;
219 }
220
224 public function getResult()
225 {
226 return $this->result;
227 }
228
229 protected function makeLogRecords($command = "", $result = null, $output = "", $error = "")
230 {
231 if($command <> '')
232 {
233 //cut password data from log records
234 $preg = "/(-p.*\s+|--mysql_password=.*\s+|--cluster_password=.*\s+|--replica_password=.*\s+|--password=.*\s+)/is";
235 $command = preg_replace($preg, ' PASS_PARAMS ', $command);
236
237 $this->log(
239 "SCALE_ACTION_STARTED",
240 $this->actionParams["NAME"],
241 $command
242 );
243 }
244
245 if($result !== null)
246 {
247 $this->log(
249 "SCALE_ACTION_RESULT",
250 $this->actionParams["NAME"],
251 $result ? Loc::getMessage("SCALE_ACTION_RESULT_SUCCESS") : Loc::getMessage("SCALE_ACTION_RESULT_ERROR")
252 );
253 }
254
255 if($output <> '')
256 {
257 $this->log(
259 "SCALE_ACTION_OUTPUT",
260 $this->actionParams["NAME"],
261 $output
262 );
263 }
264
265 if($error <> '')
266 {
267 $this->log(
269 "SCALE_ACTION_ERROR",
270 $this->actionParams["NAME"],
271 $error
272 );
273 }
274 }
275
276 protected function log($level, $auditType, $actionId, $description)
277 {
278 if($this->logLevel < $level)
279 return false;
280
281 return Logger::addRecord($level, $auditType, $actionId, $description);
282 }
283}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
log($level, $auditType, $actionId, $description)
Definition action.php:276
makeLogRecords($command="", $result=null, $output="", $error="")
Definition action.php:229
start(array $inputParams=array())
Definition action.php:143
__construct($actionId, $actionParams, $serverHostname="", $userParams=array(), $freeParams=array())
Definition action.php:34
makeStartCommand($inputParams=array())
Definition action.php:73
static getTmpDir()
Definition helper.php:216
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
static getServer($hostname)