Terminal.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 think\facade\Cookie;
  16. use app\admin\library\Auth;
  17. use app\admin\library\module\Manage;
  18. use think\exception\HttpResponseException;
  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 执行状态
  43. */
  44. protected int $procStatus = 0;
  45. /**
  46. * @var string 命令在前台的uuid
  47. */
  48. protected string $uuid = '';
  49. /**
  50. * @var string 扩展信息
  51. */
  52. protected string $extend = '';
  53. /**
  54. * @var string 命令执行输出文件
  55. */
  56. protected string $outputFile = '';
  57. /**
  58. * @var string 命令执行实时输出内容
  59. */
  60. protected string $outputContent = '';
  61. /**
  62. * @var string 自动构建的前端文件的 outDir(相对于根目录)
  63. */
  64. protected static string $distDir = 'web' . DIRECTORY_SEPARATOR . 'dist';
  65. /**
  66. * @var array 状态标识
  67. */
  68. protected array $flag = [
  69. // 连接成功
  70. 'link-success' => 'command-link-success',
  71. // 执行成功
  72. 'exec-success' => 'command-exec-success',
  73. // 执行完成
  74. 'exec-completed' => 'command-exec-completed',
  75. // 执行出错
  76. 'exec-error' => 'command-exec-error',
  77. ];
  78. /**
  79. * 初始化
  80. */
  81. public static function instance(): Terminal
  82. {
  83. if (is_null(self::$instance)) {
  84. self::$instance = new static();
  85. }
  86. return self::$instance;
  87. }
  88. /**
  89. * 构造函数
  90. */
  91. public function __construct()
  92. {
  93. $this->uuid = request()->param('uuid', '');
  94. $this->extend = request()->param('extend', '');
  95. // 初始化日志文件
  96. $outputDir = root_path() . 'runtime' . DIRECTORY_SEPARATOR . 'terminal';
  97. $this->outputFile = $outputDir . DIRECTORY_SEPARATOR . 'exec.log';
  98. if (!is_dir($outputDir)) {
  99. mkdir($outputDir, 0755, true);
  100. }
  101. file_put_contents($this->outputFile, '');
  102. /**
  103. * 命令执行结果输出到文件而不是管道
  104. * 因为输出到管道时有延迟,而文件虽然需要频繁读取和对比内容,但是输出实时的
  105. */
  106. $this->descriptorsPec = [0 => ['pipe', 'r'], 1 => ['file', $this->outputFile, 'w'], 2 => ['file', $this->outputFile, 'w']];
  107. }
  108. /**
  109. * 获取命令
  110. * @param string $key 命令key
  111. * @return array|bool
  112. */
  113. public static function getCommand(string $key): bool|array
  114. {
  115. if (!$key) {
  116. return false;
  117. }
  118. $commands = Config::get('terminal.commands');
  119. if (stripos($key, '.')) {
  120. $key = explode('.', $key);
  121. if (!array_key_exists($key[0], $commands) || !is_array($commands[$key[0]]) || !array_key_exists($key[1], $commands[$key[0]])) {
  122. return false;
  123. }
  124. $command = $commands[$key[0]][$key[1]];
  125. } else {
  126. if (!array_key_exists($key, $commands)) {
  127. return false;
  128. }
  129. $command = $commands[$key];
  130. }
  131. if (!is_array($command)) {
  132. $command = [
  133. 'cwd' => root_path(),
  134. 'command' => $command,
  135. ];
  136. } else {
  137. $command = [
  138. 'cwd' => root_path() . $command['cwd'],
  139. 'command' => $command['command'],
  140. ];
  141. }
  142. $command['cwd'] = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $command['cwd']);
  143. return $command;
  144. }
  145. /**
  146. * 执行命令
  147. * @param bool $authentication 是否鉴权
  148. * @throws Throwable
  149. */
  150. public function exec(bool $authentication = true): void
  151. {
  152. header('X-Accel-Buffering: no');
  153. header('Content-Type: text/event-stream');
  154. header('Cache-Control: no-cache');
  155. while (ob_get_level()) {
  156. ob_end_clean();
  157. }
  158. if (!ob_get_level()) ob_start();
  159. $this->commandKey = request()->param('command');
  160. $command = self::getCommand($this->commandKey);
  161. if (!$command) {
  162. $this->execError('The command was not allowed to be executed', true);
  163. }
  164. if ($authentication) {
  165. $token = request()->server('HTTP_BATOKEN', request()->request('batoken', Cookie::get('batoken') ?: false));
  166. $auth = Auth::instance();
  167. $auth->init($token);
  168. if (!$auth->isLogin() || !$auth->isSuperAdmin()) {
  169. $this->execError("You are not super administrator or not logged in", true);
  170. }
  171. }
  172. $this->beforeExecution();
  173. $this->outputFlag('link-success');
  174. $this->output('> ' . $command['command'], false);
  175. $this->process = proc_open($command['command'], $this->descriptorsPec, $this->pipes, $command['cwd']);
  176. if (!is_resource($this->process)) {
  177. $this->execError('Failed to execute', true);
  178. }
  179. while ($this->getProcStatus()) {
  180. $contents = file_get_contents($this->outputFile);
  181. if (strlen($contents) && $this->outputContent != $contents) {
  182. $newOutput = str_replace($this->outputContent, '', $contents);
  183. if (preg_match('/\r\n|\r|\n/', $newOutput)) {
  184. $this->output($newOutput);
  185. $this->outputContent = $contents;
  186. }
  187. }
  188. usleep(500000);
  189. }
  190. foreach ($this->pipes as $pipe) {
  191. fclose($pipe);
  192. }
  193. proc_close($this->process);
  194. $this->outputFlag('exec-completed');
  195. }
  196. /**
  197. * 获取执行状态
  198. * @throws Throwable
  199. */
  200. public function getProcStatus(): bool
  201. {
  202. $status = proc_get_status($this->process);
  203. if ($status['running']) {
  204. $this->procStatus = 1;
  205. return true;
  206. } elseif ($this->procStatus === 1) {
  207. $this->procStatus = 0;
  208. $this->output('exitcode: ' . $status['exitcode']);
  209. if ($status['exitcode'] === 0) {
  210. if ($this->successCallback()) {
  211. $this->outputFlag('exec-success');
  212. } else {
  213. $this->output('Error: Command execution succeeded, but callback execution failed');
  214. $this->outputFlag('exec-error');
  215. }
  216. } else {
  217. $this->outputFlag('exec-error');
  218. }
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. }
  224. /**
  225. * 输出 EventSource 数据
  226. * @param string $data
  227. * @param bool $callback
  228. */
  229. public function output(string $data, bool $callback = true): void
  230. {
  231. $data = self::outputFilter($data);
  232. $data = [
  233. 'data' => $data,
  234. 'uuid' => $this->uuid,
  235. 'extend' => $this->extend,
  236. 'key' => $this->commandKey,
  237. ];
  238. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  239. if ($data) {
  240. echo 'data: ' . $data . "\n\n";
  241. if ($callback) $this->outputCallback($data);
  242. @ob_flush();// 刷新浏览器缓冲区
  243. }
  244. }
  245. /**
  246. * 输出状态标记
  247. * @param string $flag
  248. */
  249. public function outputFlag(string $flag): void
  250. {
  251. $this->output($this->flag[$flag], false);
  252. }
  253. /**
  254. * 输出后回调
  255. */
  256. public function outputCallback($data): void
  257. {
  258. }
  259. /**
  260. * 成功后回调
  261. * @return bool
  262. * @throws Throwable
  263. */
  264. public function successCallback(): bool
  265. {
  266. if (stripos($this->commandKey, '.')) {
  267. $commandKeyArr = explode('.', $this->commandKey);
  268. $commandPKey = $commandKeyArr[0] ?? '';
  269. } else {
  270. $commandPKey = $this->commandKey;
  271. }
  272. if ($commandPKey == 'web-build') {
  273. if (!self::mvDist()) {
  274. $this->output('Build succeeded, but move file failed. Please operate manually.');
  275. return false;
  276. }
  277. } elseif ($commandPKey == 'web-install' && $this->extend) {
  278. [$type, $value] = explode(':', $this->extend);
  279. if ($type == 'module-install' && $value) {
  280. Manage::instance($value)->dependentInstallComplete('npm');
  281. }
  282. } elseif ($commandPKey == 'composer' && $this->extend) {
  283. [$type, $value] = explode(':', $this->extend);
  284. if ($type == 'module-install' && $value) {
  285. Manage::instance($value)->dependentInstallComplete('composer');
  286. }
  287. } elseif ($commandPKey == 'nuxt-install' && $this->extend) {
  288. [$type, $value] = explode(':', $this->extend);
  289. if ($type == 'module-install' && $value) {
  290. Manage::instance($value)->dependentInstallComplete('nuxt_npm');
  291. }
  292. }
  293. return true;
  294. }
  295. /**
  296. * 执行前埋点
  297. */
  298. public function beforeExecution(): void
  299. {
  300. if ($this->commandKey == 'test.pnpm') {
  301. @unlink(root_path() . 'public' . DIRECTORY_SEPARATOR . 'npm-install-test' . DIRECTORY_SEPARATOR . 'pnpm-lock.yaml');
  302. } elseif ($this->commandKey == 'web-install.pnpm') {
  303. @unlink(root_path() . 'web' . DIRECTORY_SEPARATOR . 'pnpm-lock.yaml');
  304. }
  305. }
  306. /**
  307. * 输出过滤
  308. */
  309. public static function outputFilter($str): string
  310. {
  311. $str = trim($str);
  312. $preg = '/\[(.*?)m/i';
  313. $str = preg_replace($preg, '', $str);
  314. $str = str_replace(["\r\n", "\r", "\n"], "\n", $str);
  315. return mb_convert_encoding($str, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
  316. }
  317. /**
  318. * 执行错误
  319. */
  320. public function execError($error, $break = false): void
  321. {
  322. $this->output('Error:' . $error);
  323. $this->outputFlag('exec-error');
  324. if ($break) $this->break();
  325. }
  326. /**
  327. * 退出执行
  328. */
  329. public function break(): void
  330. {
  331. throw new HttpResponseException(Response::create()->contentType('text/event-stream'));
  332. }
  333. /**
  334. * 执行一个命令并以字符串的方式返回执行输出
  335. * 代替 exec 使用,这样就只需要解除 proc_open 的函数禁用了
  336. * @param $commandKey
  337. * @return string|bool
  338. */
  339. public static function getOutputFromProc($commandKey): bool|string
  340. {
  341. if (!function_exists('proc_open') || !function_exists('proc_close')) {
  342. return false;
  343. }
  344. $command = self::getCommand($commandKey);
  345. if (!$command) {
  346. return false;
  347. }
  348. $descriptorsPec = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
  349. $process = proc_open($command['command'], $descriptorsPec, $pipes, null, null);
  350. if (is_resource($process)) {
  351. $info = stream_get_contents($pipes[1]);
  352. $info .= stream_get_contents($pipes[2]);
  353. fclose($pipes[1]);
  354. fclose($pipes[2]);
  355. proc_close($process);
  356. return self::outputFilter($info);
  357. }
  358. return '';
  359. }
  360. public static function mvDist(): bool
  361. {
  362. $distPath = root_path() . self::$distDir . DIRECTORY_SEPARATOR;
  363. $indexHtmlPath = $distPath . 'index.html';
  364. $assetsPath = $distPath . 'assets';
  365. if (!file_exists($indexHtmlPath) || !file_exists($assetsPath)) {
  366. return false;
  367. }
  368. $toIndexHtmlPath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'index.html';
  369. $toAssetsPath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'assets';
  370. @unlink($toIndexHtmlPath);
  371. Filesystem::delDir($toAssetsPath);
  372. if (rename($indexHtmlPath, $toIndexHtmlPath) && rename($assetsPath, $toAssetsPath)) {
  373. Filesystem::delDir($distPath);
  374. return true;
  375. } else {
  376. return false;
  377. }
  378. }
  379. public static function changeTerminalConfig($config = []): bool
  380. {
  381. // 不保存在数据库中,因为切换包管理器时,数据库资料可能还未配置
  382. $oldPort = Config::get('terminal.install_service_port');
  383. $oldPackageManager = Config::get('terminal.npm_package_manager');
  384. $newPort = request()->post('port', $config['port'] ?? $oldPort);
  385. $newPackageManager = request()->post('manager', $config['manager'] ?? $oldPackageManager);
  386. if ($oldPort == $newPort && $oldPackageManager == $newPackageManager) {
  387. return true;
  388. }
  389. $buildConfigFile = config_path() . 'terminal.php';
  390. $buildConfigContent = @file_get_contents($buildConfigFile);
  391. $buildConfigContent = preg_replace("/'install_service_port'(\s+)=>(\s+)'$oldPort'/", "'install_service_port'\$1=>\$2'$newPort'", $buildConfigContent);
  392. $buildConfigContent = preg_replace("/'npm_package_manager'(\s+)=>(\s+)'$oldPackageManager'/", "'npm_package_manager'\$1=>\$2'$newPackageManager'", $buildConfigContent);
  393. $result = @file_put_contents($buildConfigFile, $buildConfigContent);
  394. return (bool)$result;
  395. }
  396. }