MallService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. namespace backend\modules\common\services;
  3. use common\enums\RabbitMqEnum;
  4. use Yii;
  5. /**
  6. * BaseService
  7. * @package backend\modules\common\services
  8. */
  9. class MallService extends BaseService
  10. {
  11. /**
  12. * @var string
  13. */
  14. const FILE_NAME = 'add_subdomain.sh';
  15. /**
  16. * 设置域名
  17. *
  18. * @return void
  19. */
  20. public function setDomain(
  21. int $mall_id,
  22. string $admin,
  23. string $api,
  24. string $h5,
  25. string $static,
  26. string $path
  27. )
  28. {
  29. static::setContentSsl(compact(
  30. 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
  31. ));
  32. // Yii::$app->services->rabbitMq->push(
  33. // RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, compact(
  34. // 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
  35. // ), self::class, 'setContentSsl'
  36. // );
  37. Yii::info('站点子域名设置:' . json_encode(compact(
  38. 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
  39. )));
  40. }
  41. /**
  42. * @param integer $mall_id
  43. * @param string $admin
  44. * @param string $api
  45. * @param string $h5
  46. * @param string $static
  47. * @return void
  48. */
  49. /**
  50. * @param array $params
  51. * @return void
  52. */
  53. public static function setContentSsl(
  54. array $params
  55. )
  56. {
  57. $mall_id = $params['mall_id'];
  58. $admin = $params['admin'];
  59. $api = $params['api'];
  60. $h5 = $params['h5'];
  61. $static = $params['static'];
  62. $path = $params['path'];
  63. $pwd = Yii::$app->params['oskad'];
  64. $is_docker_deploy = Yii::$app->params['is_docker_deploy'];
  65. $host = Yii::$app->params['host'];
  66. $admin_url = substr(Yii::$app->services->setting->platform->get('system.backend_url'), 8);
  67. $api_url = substr(Yii::$app->services->setting->platform->get('system.api_url'), 8);
  68. $h5_url = substr(Yii::$app->services->setting->platform->get('system.h5_url'), 8);
  69. $static_url = substr(Yii::$app->services->setting->platform->get('system.static_url'), 8);
  70. $replaces = [
  71. '{ORIGINAL_ADMIN}' => $admin_url,
  72. '{ORIGINAL_H5}' => $h5_url,
  73. '{ORIGINAL_API}' => $api_url,
  74. '{ORIGINAL_STATIC}' => $static_url,
  75. '{NOW_ADMIN}' => $admin,
  76. '{NOW_H5}' => $api,
  77. '{NOW_API}' => $h5,
  78. '{NOW_STATIC}' => $static,
  79. '{SUBDOMAIN_DIR}' => $mall_id,
  80. '{SSL_PATH}' => $path,
  81. '{PWD}' => $pwd,
  82. '{FILE_NAME}' => static::FILE_NAME,
  83. ];
  84. //是否为docker部署,是则获取docker模板
  85. $file_path = '/data/wwwroot/qimall/';
  86. if ($is_docker_deploy) {
  87. $file_path = '/www/wwwroot/qimall/';
  88. $file_content = static::shDockerTemplate();
  89. } else { //不是则获取普通模板
  90. $file_content = static::shTemplate();
  91. }
  92. $content = str_replace(array_keys($replaces), array_values($replaces), $file_content);
  93. file_put_contents($file_path . static::FILE_NAME, $content);
  94. if ($is_docker_deploy) {
  95. // exec('sshpass -p "'. $pwd.'" ssh root@'. $host .' "sh '. $file_path . static::FILE_NAME.'"');
  96. exec("sshpass -p '".$pwd."' ssh -o StrictHostKeyChecking=no root@". $host." 'sh ". $file_path . static::FILE_NAME . "'");
  97. } else {
  98. exec('sh '. $file_path . static::FILE_NAME);
  99. }
  100. // Yii::$app->services->rabbitMq->push(
  101. // RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [], self::class, 'execSh'
  102. // );
  103. }
  104. /**
  105. * @return void
  106. */
  107. public static function execSh(array $params = [])
  108. {
  109. exec('sh /data/wwwroot/qimall/' . static::FILE_NAME);
  110. }
  111. /**
  112. * sh模板
  113. *
  114. * @return string
  115. */
  116. protected static function shTemplate()
  117. {
  118. return <<<'EOC'
  119. #!/bin/bash
  120. # 检查是否有sudo权限
  121. if [ "$(id -u)" != "0" ]; then
  122. # 设置root密码,这里是示例密码,请替换为您的实际密码
  123. root_password='{PWD}'
  124. # 使用echo和管道将密码传递给su命令
  125. echo $root_password | su -c "sh /data/wwwroot/qimall/{FILE_NAME}"
  126. fi
  127. # 检查是否有sudo权限
  128. if [ "$(id -u)" == "0" ]; then
  129. # 本域名
  130. original_admin='{ORIGINAL_ADMIN}'
  131. original_h5='{ORIGINAL_H5}'
  132. original_api='{ORIGINAL_API}'
  133. original_static='{ORIGINAL_STATIC}'
  134. # 子域名
  135. now_admin='{NOW_ADMIN}'
  136. now_h5='{NOW_H5}'
  137. now_api='{NOW_API}'
  138. now_static='{NOW_STATIC}'
  139. # 子配置目录
  140. subdomain_dir='{SUBDOMAIN_DIR}'
  141. # 证书路径
  142. ssl_path='{SSL_PATH}'
  143. # 不管有没有配置,都删除配置
  144. rm -rf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}
  145. # 如果文件夹不存在,新建子域名配置文件夹
  146. cd /application/nginx/conf/vhosts
  147. if [ ! -d 'subdomain' ]; then
  148. mkdir subdomain
  149. fi
  150. cd /application/nginx/conf/vhosts/subdomain
  151. if [ ! -d "$subdomain_dir" ]; then
  152. mkdir $subdomain_dir
  153. fi
  154. # 如果文件夹不存在,新建子域名证书文件夹
  155. cd /application/nginx/conf/ssl
  156. if [ ! -d 'subdomain' ]; then
  157. mkdir subdomain
  158. fi
  159. cd /application/nginx/conf/ssl/subdomain
  160. if [ ! -d "$subdomain_dir" ]; then
  161. mkdir $subdomain_dir
  162. fi
  163. # 如果文件夹不存在,新建子域名日志文件夹
  164. cd /application/nginx/logs
  165. if [ ! -d 'subdomain' ]; then
  166. mkdir subdomain
  167. fi
  168. cd /application/nginx/logs/subdomain
  169. if [ ! -d "$subdomain_dir" ]; then
  170. mkdir $subdomain_dir
  171. fi
  172. # 复制Nginx配置文件
  173. # 复制文件 admin
  174. cp /application/nginx/conf/vhosts/${original_admin}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_admin}.conf
  175. # 复制文件 h5
  176. cp /application/nginx/conf/vhosts/${original_h5}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_h5}.conf
  177. # 复制文件 api
  178. cp /application/nginx/conf/vhosts/${original_api}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_api}.conf
  179. # 复制文件 static
  180. cp /application/nginx/conf/vhosts/${original_static}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_static}.conf
  181. # 切换到配置文件夹,修改Nginx配置
  182. cd /application/nginx/conf/vhosts/subdomain/${subdomain_dir}
  183. # 替换域名 admin
  184. # 证书目录
  185. old=\/application\/nginx\/conf\/ssl\/${original_admin}
  186. new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_admin}
  187. # 替换证书目录
  188. sed -i "s|${old}|${new}|g" ${now_admin}.conf
  189. # 日志目录替换
  190. old_log=logs\/${original_admin}
  191. new_log=logs\/subdomain\/${subdomain_dir}\/${now_admin}
  192. sed -i "s|${old_log}|${new_log}|g" ${now_admin}.conf
  193. # 域名替换
  194. sed -i "s|${original_admin}|${now_admin}|g" ${now_admin}.conf
  195. # 替换域名 h5
  196. # 证书目录
  197. old=\/application\/nginx\/conf\/ssl\/${original_h5}
  198. new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_h5}
  199. # 替换证书目录
  200. sed -i "s|${old}|${new}|g" ${now_h5}.conf
  201. # 日志目录替换
  202. old_log=logs\/${original_h5}
  203. new_log=logs\/subdomain\/${subdomain_dir}\/${now_h5}
  204. sed -i "s|${old_log}|${new_log}|g" ${now_h5}.conf
  205. # 域名替换
  206. sed -i "s|${original_h5}|${now_h5}|g" ${now_h5}.conf
  207. # 替换域名 api
  208. # 证书目录
  209. old=\/application\/nginx\/conf\/ssl\/${original_api}
  210. new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_api}
  211. # 替换证书目录
  212. sed -i "s|${old}|${new}|g" ${now_api}.conf
  213. # 日志目录替换
  214. old_log=logs\/${original_api}
  215. new_log=logs\/subdomain\/${subdomain_dir}\/${now_api}
  216. sed -i "s|${old_log}|${new_log}|g" ${now_api}.conf
  217. # 域名替换
  218. sed -i "s|${original_api}|${now_api}|g" ${now_api}.conf
  219. # 替换域名 static
  220. # 证书目录
  221. old=\/application\/nginx\/conf\/ssl\/${original_static}
  222. new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_static}
  223. # 替换证书目录
  224. sed -i "s|${old}|${new}|g" ${now_static}.conf
  225. # 日志目录替换
  226. old_log=logs\/${original_static}
  227. new_log=logs\/subdomain\/${subdomain_dir}\/${now_static}
  228. sed -i "s|${old_log}|${new_log}|g" ${now_static}.conf
  229. # 域名替换
  230. sed -i "s|${original_static}|${now_static}|g" ${now_static}.conf
  231. # 证书
  232. # 移动证书到目录
  233. dir=/application/nginx/conf/ssl/subdomain/${subdomain_dir}
  234. # 证书目录
  235. ssl=/data/wwwroot/qimall/${ssl_path}
  236. # 复制
  237. cp ${ssl} ${dir}/ssl.zip
  238. cd ${dir}
  239. # 解压
  240. unzip -o ssl.zip
  241. # 重启nginx
  242. # systemctl restart nginx
  243. /application/nginx/sbin/nginx -s reload
  244. # 重启消费者cd
  245. # systemctl restart swoole_consume_run
  246. fi
  247. EOC;
  248. }
  249. protected static function shDockerTemplate()
  250. {
  251. return <<<'EOC'
  252. #!/bin/bash
  253. # 检查是否有sudo权限
  254. if [ "$(id -u)" != "0" ]; then
  255. # 设置root密码,这里是示例密码,请替换为您的实际密码
  256. root_password='{PWD}'
  257. # 使用echo和管道将密码传递给su命令
  258. echo $root_password | su -c "sh /www/wwwroot/qimall/{FILE_NAME}"
  259. fi
  260. # 检查是否有sudo权限
  261. if [ "$(id -u)" == "0" ]; then
  262. # 本域名
  263. original_admin='{ORIGINAL_ADMIN}'
  264. original_h5='{ORIGINAL_H5}'
  265. original_api='{ORIGINAL_API}'
  266. original_static='{ORIGINAL_STATIC}'
  267. # 子域名
  268. now_admin='{NOW_ADMIN}'
  269. now_h5='{NOW_H5}'
  270. now_api='{NOW_API}'
  271. now_static='{NOW_STATIC}'
  272. # 子配置目录
  273. subdomain_dir='{SUBDOMAIN_DIR}'
  274. # 证书路径
  275. ssl_path='{SSL_PATH}'
  276. # 不管有没有配置,都删除配置
  277. rm -rf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}
  278. # 如果文件夹不存在,新建子域名配置文件夹
  279. cd /data/confs/nginx/conf.d
  280. if [ ! -d 'subdomain' ]; then
  281. mkdir subdomain
  282. fi
  283. cd /data/confs/nginx/conf.d/subdomain
  284. if [ ! -d "$subdomain_dir" ]; then
  285. mkdir $subdomain_dir
  286. fi
  287. # 如果文件夹不存在,新建子域名证书文件夹
  288. cd /data/confs/nginx/ssl
  289. if [ ! -d 'subdomain' ]; then
  290. mkdir subdomain
  291. fi
  292. cd /data/confs/nginx/ssl/subdomain
  293. if [ ! -d "$subdomain_dir" ]; then
  294. mkdir $subdomain_dir
  295. fi
  296. cd /data/confs/nginx
  297. if [ ! -d 'logs' ]; then
  298. mkdir logs
  299. fi
  300. # 如果文件夹不存在,新建子域名日志文件夹
  301. cd /data/confs/nginx/logs
  302. if [ ! -d 'subdomain' ]; then
  303. mkdir subdomain
  304. fi
  305. cd /data/confs/nginx/logs/subdomain
  306. if [ ! -d "$subdomain_dir" ]; then
  307. mkdir $subdomain_dir
  308. fi
  309. # 复制Nginx配置文件
  310. # 复制文件 admin
  311. cp /data/confs/nginx/conf.d/${original_admin}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_admin}.conf
  312. # 复制文件 h5
  313. cp /data/confs/nginx/conf.d/${original_h5}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_h5}.conf
  314. # 复制文件 api
  315. cp /data/confs/nginx/conf.d/${original_api}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_api}.conf
  316. # 复制文件 static
  317. cp /data/confs/nginx/conf.d/${original_static}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_static}.conf
  318. # 切换到配置文件夹,修改Nginx配置
  319. cd /data/confs/nginx/conf.d/subdomain/${subdomain_dir}
  320. # 替换域名 admin
  321. # 证书目录
  322. old=\/etc\/nginx\/ssl\/${original_admin}
  323. new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_admin}
  324. # 替换证书目录
  325. sed -i "s|${old}|${new}|g" ${now_admin}.conf
  326. # 日志目录替换
  327. old_log=logs\/${original_admin}
  328. new_log=logs\/subdomain\/${subdomain_dir}\/${now_admin}
  329. sed -i "s|${old_log}|${new_log}|g" ${now_admin}.conf
  330. # 域名替换
  331. sed -i "s|${original_admin}|${now_admin}|g" ${now_admin}.conf
  332. # 替换域名 h5
  333. # 证书目录
  334. old=\/etc\/nginx\/ssl\/${original_h5}
  335. new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_h5}
  336. # 替换证书目录
  337. sed -i "s|${old}|${new}|g" ${now_h5}.conf
  338. # 日志目录替换
  339. old_log=logs\/${original_h5}
  340. new_log=logs\/subdomain\/${subdomain_dir}\/${now_h5}
  341. sed -i "s|${old_log}|${new_log}|g" ${now_h5}.conf
  342. # 域名替换
  343. sed -i "s|${original_h5}|${now_h5}|g" ${now_h5}.conf
  344. # 替换域名 api
  345. # 证书目录
  346. old=\/etc\/nginx\/ssl\/${original_api}
  347. new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_api}
  348. # 替换证书目录
  349. sed -i "s|${old}|${new}|g" ${now_api}.conf
  350. # 日志目录替换
  351. old_log=logs\/${original_api}
  352. new_log=logs\/subdomain\/${subdomain_dir}\/${now_api}
  353. sed -i "s|${old_log}|${new_log}|g" ${now_api}.conf
  354. # 域名替换
  355. sed -i "s|${original_api}|${now_api}|g" ${now_api}.conf
  356. # 替换域名 static
  357. # 证书目录
  358. old=\/etc\/nginx\/ssl\/${original_static}
  359. new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_static}
  360. # 替换证书目录
  361. sed -i "s|${old}|${new}|g" ${now_static}.conf
  362. # 日志目录替换
  363. old_log=logs\/${original_static}
  364. new_log=logs\/subdomain\/${subdomain_dir}\/${now_static}
  365. sed -i "s|${old_log}|${new_log}|g" ${now_static}.conf
  366. # 域名替换
  367. sed -i "s|${original_static}|${now_static}|g" ${now_static}.conf
  368. # 证书
  369. # 移动证书到目录
  370. dir=/data/confs/nginx/ssl/subdomain/${subdomain_dir}
  371. # 证书目录
  372. ssl=/www/wwwroot/qimall/${ssl_path}
  373. # 复制
  374. cp ${ssl} ${dir}/ssl.zip
  375. cd ${dir}
  376. # 解压
  377. unzip -o ssl.zip
  378. # 重启nginx
  379. #systemctl restart nginx
  380. #/usr/sbin/nginx -s reload
  381. 重启docker
  382. sudo docker restart nginx
  383. # 重启消费者cd
  384. # systemctl restart swoole_consume_run
  385. fi
  386. EOC;
  387. }
  388. }