Terminal.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | BuildAdmin [ Quickly create commercial-grade management system using popular technology stack ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2022~2022 http://buildadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 妙码生花 <hi@buildadmin.com>
  10. // +----------------------------------------------------------------------
  11. namespace ba;
  12. use Throwable;
  13. use think\Response;
  14. use think\facade\Config;
  15. use app\admin\library\Auth;
  16. use app\admin\library\module\Manage;
  17. use think\exception\HttpResponseException;
  18. use app\common\library\token\TokenExpirationException;
  19. class Terminal
  20. {
  21. /**
  22. * @var ?Terminal 对象实例
  23. */
  24. protected static ?Terminal $instance = null;
  25. /**
  26. * @var string 当前执行的命令 $command 的 key
  27. */
  28. protected string $commandKey = '';
  29. /**
  30. * @var array proc_open 的参数
  31. */
  32. protected array $descriptorsPec = [];
  33. /**
  34. * @var resource|bool proc_open 返回的 resource
  35. */
  36. protected $process = false;
  37. /**
  38. * @var array proc_open 的管道
  39. */
  40. protected array $pipes = [];
  41. /**
  42. * @var int proc执行状态:0=未执行,1=执行中,2=执行完毕
  43. */
  44. protected int $procStatusMark = 0;
  45. /**
  46. * @var array proc执行状态数据
  47. */
  48. protected array $procStatusData = [];
  49. /**
  50. * @var string 命令在前台的uuid
  51. */
  52. protected string $uuid = '';
  53. /**
  54. * @var string 扩展信息
  55. */
  56. protected string $extend = '';
  57. /**
  58. * @var string 命令执行输出文件
  59. */
  60. protected string $outputFile = '';
  61. /**
  62. * @var string 命令执行实时输出内容
  63. */
  64. protected string $outputContent = '';
  65. /**
  66. * @var string 自动构建的前端文件的 outDir(相对于根目录)
  67. */
  68. protected static string $distDir = 'web' . DIRECTORY_SEPARATOR . 'dist';
  69. /**
  70. * @var array 状态标识
  71. */
  72. protected array $flag = [
  73. // 连接成功
  74. 'link-success' => 'command-link-success',
  75. // 执行成功
  76. 'exec-success' => 'command-exec-success',
  77. // 执行完成
  78. 'exec-completed' => 'command-exec-completed',
  79. // 执行出错
  80. 'exec-error' => 'command-exec-error',
  81. ];
  82. /**
  83. * 初始化
  84. */
  85. public static function instance(): Terminal
  86. {
  87. if (is_null(self::$instance)) {
  88. self::$instance = new static();
  89. }
  90. return self::$instance;
  91. }
  92. /**
  93. * 构造函数
  94. */
  95. public function __construct()
  96. {
  97. $this->uuid = request()->param('uuid', '');
  98. $this->extend = request()->param('extend', '');
  99. // 初始化日志文件
  100. $outputDir = root_path() . 'runtime' . DIRECTORY_SEPARATOR . 'terminal';
  101. $this->outputFile = $outputDir . DIRECTORY_SEPARATOR . 'exec.log';
  102. if (!is_dir($outputDir)) {
  103. mkdir($outputDir, 0755, true);
  104. }
  105. file_put_contents($this->outputFile, '');
  106. /**
  107. * 命令执行结果输出到文件而不是管道
  108. * 因为输出到管道时有延迟,而文件虽然需要频繁读取和对比内容,但是输出实时的
  109. */
  110. $this->descriptorsPec = [0 => ['pipe', 'r'], 1 => ['file', $this->outputFile, 'w'], 2 => ['file', $this->outputFile, 'w']];
  111. }
  112. /**
  113. * 获取命令
  114. * @param string $key 命令key
  115. * @return array|bool
  116. */
  117. public static function getCommand(string $key): bool|array
  118. {
  119. if (!$key) {
  120. return false;
  121. }
  122. $commands = Config::get('terminal.commands');
  123. if (stripos($key, '.')) {
  124. $key = explode('.', $key);
  125. if (!array_key_exists($key[0], $commands) || !is_array($commands[$key[0]]) || !array_key_exists($key[1], $commands[$key[0]])) {
  126. return false;
  127. }
  128. $command = $commands[$key[0]][$key[1]];
  129. } else {
  130. if (!array_key_exists($key, $commands)) {
  131. return false;
  132. }
  133. $command = $commands[$key];
  134. }
  135. if (!is_array($command)) {
  136. $command = [
  137. 'cwd' => root_path(),
  138. 'command' => $command,
  139. ];
  140. } else {
  141. $command = [
  142. 'cwd' => root_path() . $command['cwd'],
  143. 'command' => $command['command'],
  144. ];
  145. }
  146. $command['cwd'] = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $command['cwd']);
  147. return $command;
  148. }
  149. /**
  150. * 执行命令
  151. * @param bool $authentication 是否鉴权
  152. * @throws Throwable
  153. */
  154. public function exec(bool $authentication = true): void
  155. {
  156. $this->sendHeader();
  157. while (ob_get_level()) {
  158. ob_end_clean();
  159. }
  160. if (!ob_get_level()) ob_start();
  161. $this->commandKey = request()->param('command');
  162. $command = self::getCommand($this->commandKey);
  163. if (!$command) {
  164. $this->execError('The command was not allowed to be executed', true);
  165. }
  166. if ($authentication) {
  167. try {
  168. $token = get_auth_token();
  169. $auth = Auth::instance();
  170. $auth->init($token);
  171. if (!$auth->isLogin() || !$auth->isSuperAdmin()) {
  172. $this->execError("You are not super administrator or not logged in", true);
  173. }
  174. } catch (TokenExpirationException) {
  175. $this->execError(__('Token expiration'));
  176. }
  177. }
  178. $this->beforeExecution();
  179. $this->outputFlag('link-success');
  180. $this->output('> ' . $command['command'], false);
  181. $this->process = proc_open($command['command'], $this->descriptorsPec, $this->pipes, $command['cwd']);
  182. if (!is_resource($this->process)) {
  183. $this->execError('Failed to execute', true);
  184. }
  185. while ($this->getProcStatus()) {
  186. $contents = file_get_contents($this->outputFile);
  187. if (strlen($contents) && $this->outputContent != $contents) {
  188. $newOutput = str_replace($this->outputContent, '', $contents);
  189. if (preg_match('/\r\n|\r|\n/', $newOutput)) {
  190. $this->output($newOutput);
  191. $this->outputContent = $contents;
  192. $this->checkOutput();
  193. }
  194. }
  195. // 输出执行状态信息
  196. if ($this->procStatusMark === 2) {
  197. $this->output('exitCode: ' . $this->procStatusData['exitcode']);
  198. if ($this->procStatusData['exitcode'] === 0) {
  199. if ($this->successCallback()) {
  200. $this->outputFlag('exec-success');
  201. } else {
  202. $this->output('Error: Command execution succeeded, but callback execution failed');
  203. $this->outputFlag('exec-error');
  204. }
  205. } else {
  206. $this->outputFlag('exec-error');
  207. }
  208. }
  209. usleep(500000);
  210. }
  211. foreach ($this->pipes as $pipe) {
  212. fclose($pipe);
  213. }
  214. proc_close($this->process);
  215. $this->outputFlag('exec-completed');
  216. }
  217. /**
  218. * 获取执行状态
  219. * @throws Throwable
  220. */
  221. public function getProcStatus(): bool
  222. {
  223. $this->procStatusData = proc_get_status($this->process);
  224. if ($this->procStatusData['running']) {
  225. $this->procStatusMark = 1;
  226. return true;
  227. } elseif ($this->procStatusMark === 1) {
  228. $this->procStatusMark = 2;
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. /**
  235. * 输出 EventSource 数据
  236. * @param string $data
  237. * @param bool $callback
  238. */
  239. public function output(string $data, bool $callback = true): void
  240. {
  241. $data = self::outputFilter($data);
  242. $data = [
  243. 'data' => $data,
  244. 'uuid' => $this->uuid,
  245. 'extend' => $this->extend,
  246. 'key' => $this->commandKey,
  247. ];
  248. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  249. if ($data) {
  250. $this->finalOutput($data);
  251. if ($callback) $this->outputCallback($data);
  252. @ob_flush();// 刷新浏览器缓冲区
  253. }
  254. }
  255. /**
  256. * 检查输出
  257. */
  258. public function checkOutput(): void
  259. {
  260. if (str_contains($this->outputContent, '(Y/n)')) {
  261. $this->execError('An interactive command has been detected, and you can manually execute the command to confirm the situation.', true);
  262. }
  263. }
  264. /**
  265. * 输出状态标记
  266. * @param string $flag
  267. */
  268. public function outputFlag(string $flag): void
  269. {
  270. $this->output($this->flag[$flag], false);
  271. }
  272. /**
  273. * 输出后回调
  274. */
  275. public function outputCallback($data): void
  276. {
  277. }
  278. /**
  279. * 成功后回调
  280. * @return bool
  281. * @throws Throwable
  282. */
  283. public function successCallback(): bool
  284. {
  285. if (stripos($this->commandKey, '.')) {
  286. $commandKeyArr = explode('.', $this->commandKey);
  287. $commandPKey = $commandKeyArr[0] ?? '';
  288. } else {
  289. $commandPKey = $this->commandKey;
  290. }
  291. if ($commandPKey == 'web-build') {
  292. if (!self::mvDist()) {
  293. $this->output('Build succeeded, but move file failed. Please operate manually.');
  294. return false;
  295. }
  296. } elseif ($commandPKey == 'web-install' && $this->extend) {
  297. [$type, $value] = explode(':', $this->extend);
  298. if ($type == 'module-install' && $value) {
  299. Manage::instance($value)->dependentInstallComplete('npm');
  300. }
  301. } elseif ($commandPKey == 'composer' && $this->extend) {
  302. [$type, $value] = explode(':', $this->extend);
  303. if ($type == 'module-install' && $value) {
  304. Manage::instance($value)->dependentInstallComplete('composer');
  305. }
  306. } elseif ($commandPKey == 'nuxt-install' && $this->extend) {
  307. [$type, $value] = explode(':', $this->extend);
  308. if ($type == 'module-install' && $value) {
  309. Manage::instance($value)->dependentInstallComplete('nuxt_npm');
  310. }
  311. }
  312. return true;
  313. }
  314. /**
  315. * 执行前埋点
  316. */
  317. public function beforeExecution(): void
  318. {
  319. if ($this->commandKey == 'test.pnpm') {
  320. @unlink(root_path() . 'public' . DIRECTORY_SEPARATOR . 'npm-install-test' . DIRECTORY_SEPARATOR . 'pnpm-lock.yaml');
  321. } elseif ($this->commandKey == 'web-install.pnpm') {
  322. @unlink(root_path() . 'web' . DIRECTORY_SEPARATOR . 'pnpm-lock.yaml');
  323. }
  324. }
  325. /**
  326. * 输出过滤
  327. */
  328. public static function outputFilter($str): string
  329. {
  330. $str = trim($str);
  331. $preg = '/\[(.*?)m/i';
  332. $str = preg_replace($preg, '', $str);
  333. $str = str_replace(["\r\n", "\r", "\n"], "\n", $str);
  334. return mb_convert_encoding($str, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
  335. }
  336. /**
  337. * 执行错误
  338. */
  339. public function execError($error, $break = false): void
  340. {
  341. $this->output('Error:' . $error);
  342. $this->outputFlag('exec-error');
  343. if ($break) $this->break();
  344. }
  345. /**
  346. * 退出执行
  347. */
  348. public function break(): void
  349. {
  350. throw new HttpResponseException(Response::create()->contentType('text/event-stream'));
  351. }
  352. /**
  353. * 执行一个命令并以字符串的方式返回执行输出
  354. * 代替 exec 使用,这样就只需要解除 proc_open 的函数禁用了
  355. * @param $commandKey
  356. * @return string|bool
  357. */
  358. public static function getOutputFromProc($commandKey): bool|string
  359. {
  360. if (!function_exists('proc_open') || !function_exists('proc_close')) {
  361. return false;
  362. }
  363. $command = self::getCommand($commandKey);
  364. if (!$command) {
  365. return false;
  366. }
  367. $descriptorsPec = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
  368. $process = proc_open($command['command'], $descriptorsPec, $pipes, null, null);
  369. if (is_resource($process)) {
  370. $info = stream_get_contents($pipes[1]);
  371. $info .= stream_get_contents($pipes[2]);
  372. fclose($pipes[1]);
  373. fclose($pipes[2]);
  374. proc_close($process);
  375. return self::outputFilter($info);
  376. }
  377. return '';
  378. }
  379. public static function mvDist(): bool
  380. {
  381. $distPath = root_path() . self::$distDir . DIRECTORY_SEPARATOR;
  382. $indexHtmlPath = $distPath . 'index.html';
  383. $assetsPath = $distPath . 'assets';
  384. if (!file_exists($indexHtmlPath) || !file_exists($assetsPath)) {
  385. return false;
  386. }
  387. $toIndexHtmlPath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'index.html';
  388. $toAssetsPath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'assets';
  389. @unlink($toIndexHtmlPath);
  390. Filesystem::delDir($toAssetsPath);
  391. if (rename($indexHtmlPath, $toIndexHtmlPath) && rename($assetsPath, $toAssetsPath)) {
  392. Filesystem::delDir($distPath);
  393. return true;
  394. } else {
  395. return false;
  396. }
  397. }
  398. public static function changeTerminalConfig($config = []): bool
  399. {
  400. // 不保存在数据库中,因为切换包管理器时,数据库资料可能还未配置
  401. $oldPort = Config::get('terminal.install_service_port');
  402. $oldPackageManager = Config::get('terminal.npm_package_manager');
  403. $newPort = request()->post('port', $config['port'] ?? $oldPort);
  404. $newPackageManager = request()->post('manager', $config['manager'] ?? $oldPackageManager);
  405. if ($oldPort == $newPort && $oldPackageManager == $newPackageManager) {
  406. return true;
  407. }
  408. $buildConfigFile = config_path() . 'terminal.php';
  409. $buildConfigContent = @file_get_contents($buildConfigFile);
  410. $buildConfigContent = preg_replace("/'install_service_port'(\s+)=>(\s+)'$oldPort'/", "'install_service_port'\$1=>\$2'$newPort'", $buildConfigContent);
  411. $buildConfigContent = preg_replace("/'npm_package_manager'(\s+)=>(\s+)'$oldPackageManager'/", "'npm_package_manager'\$1=>\$2'$newPackageManager'", $buildConfigContent);
  412. $result = @file_put_contents($buildConfigFile, $buildConfigContent);
  413. return (bool)$result;
  414. }
  415. /**
  416. * 最终输出
  417. */
  418. public function finalOutput(string $data): void
  419. {
  420. $app = app();
  421. if (!empty($app->worker) && !empty($app->connection)) {
  422. $app->connection->send(new \Workerman\Protocols\Http\ServerSentEvents(['event' => 'message', 'data' => $data]));
  423. } else {
  424. echo 'data: ' . $data . "\n\n";
  425. }
  426. }
  427. /**
  428. * 发送响应头
  429. */
  430. public function sendHeader(): void
  431. {
  432. $headers = array_merge(request()->allowCrossDomainHeaders ?? [], [
  433. 'X-Accel-Buffering' => 'no',
  434. 'Content-Type' => 'text/event-stream',
  435. 'Cache-Control' => 'no-cache',
  436. ]);
  437. $app = app();
  438. if (!empty($app->worker) && !empty($app->connection)) {
  439. $app->connection->send(new \Workerman\Protocols\Http\Response(200, $headers, "\r\n"));
  440. } else {
  441. foreach ($headers as $name => $val) {
  442. header($name . (!is_null($val) ? ':' . $val : ''));
  443. }
  444. }
  445. }
  446. }