| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- namespace backend\modules\common\services;
- use common\enums\RabbitMqEnum;
- use Yii;
- /**
- * BaseService
- * @package backend\modules\common\services
- */
- class MallService extends BaseService
- {
- /**
- * @var string
- */
- const FILE_NAME = 'add_subdomain.sh';
- /**
- * 设置域名
- *
- * @return void
- */
- public function setDomain(
- int $mall_id,
- string $admin,
- string $api,
- string $h5,
- string $static,
- string $path
- )
- {
- static::setContentSsl(compact(
- 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
- ));
- // Yii::$app->services->rabbitMq->push(
- // RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, compact(
- // 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
- // ), self::class, 'setContentSsl'
- // );
- Yii::info('站点子域名设置:' . json_encode(compact(
- 'mall_id', 'admin', 'api', 'h5', 'static', 'path'
- )));
- }
- /**
- * @param integer $mall_id
- * @param string $admin
- * @param string $api
- * @param string $h5
- * @param string $static
- * @return void
- */
- /**
- * @param array $params
- * @return void
- */
- public static function setContentSsl(
- array $params
- )
- {
- $mall_id = $params['mall_id'];
- $admin = $params['admin'];
- $api = $params['api'];
- $h5 = $params['h5'];
- $static = $params['static'];
- $path = $params['path'];
- $pwd = Yii::$app->params['oskad'];
- $is_docker_deploy = Yii::$app->params['is_docker_deploy'];
- $host = Yii::$app->params['host'];
- $admin_url = substr(Yii::$app->services->setting->platform->get('system.backend_url'), 8);
- $api_url = substr(Yii::$app->services->setting->platform->get('system.api_url'), 8);
- $h5_url = substr(Yii::$app->services->setting->platform->get('system.h5_url'), 8);
- $static_url = substr(Yii::$app->services->setting->platform->get('system.static_url'), 8);
- $replaces = [
- '{ORIGINAL_ADMIN}' => $admin_url,
- '{ORIGINAL_H5}' => $h5_url,
- '{ORIGINAL_API}' => $api_url,
- '{ORIGINAL_STATIC}' => $static_url,
- '{NOW_ADMIN}' => $admin,
- '{NOW_H5}' => $api,
- '{NOW_API}' => $h5,
- '{NOW_STATIC}' => $static,
- '{SUBDOMAIN_DIR}' => $mall_id,
- '{SSL_PATH}' => $path,
- '{PWD}' => $pwd,
- '{FILE_NAME}' => static::FILE_NAME,
- ];
- //是否为docker部署,是则获取docker模板
- $file_path = '/data/wwwroot/qimall/';
- if ($is_docker_deploy) {
- $file_path = '/www/wwwroot/qimall/';
- $file_content = static::shDockerTemplate();
-
- } else { //不是则获取普通模板
- $file_content = static::shTemplate();
- }
- $content = str_replace(array_keys($replaces), array_values($replaces), $file_content);
- file_put_contents($file_path . static::FILE_NAME, $content);
- if ($is_docker_deploy) {
- // exec('sshpass -p "'. $pwd.'" ssh root@'. $host .' "sh '. $file_path . static::FILE_NAME.'"');
- exec("sshpass -p '".$pwd."' ssh -o StrictHostKeyChecking=no root@". $host." 'sh ". $file_path . static::FILE_NAME . "'");
- } else {
- exec('sh '. $file_path . static::FILE_NAME);
- }
- // Yii::$app->services->rabbitMq->push(
- // RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [], self::class, 'execSh'
- // );
- }
- /**
- * @return void
- */
- public static function execSh(array $params = [])
- {
- exec('sh /data/wwwroot/qimall/' . static::FILE_NAME);
- }
- /**
- * sh模板
- *
- * @return string
- */
- protected static function shTemplate()
- {
- return <<<'EOC'
- #!/bin/bash
- # 检查是否有sudo权限
- if [ "$(id -u)" != "0" ]; then
- # 设置root密码,这里是示例密码,请替换为您的实际密码
- root_password='{PWD}'
- # 使用echo和管道将密码传递给su命令
- echo $root_password | su -c "sh /data/wwwroot/qimall/{FILE_NAME}"
- fi
- # 检查是否有sudo权限
- if [ "$(id -u)" == "0" ]; then
- # 本域名
- original_admin='{ORIGINAL_ADMIN}'
- original_h5='{ORIGINAL_H5}'
- original_api='{ORIGINAL_API}'
- original_static='{ORIGINAL_STATIC}'
- # 子域名
- now_admin='{NOW_ADMIN}'
- now_h5='{NOW_H5}'
- now_api='{NOW_API}'
- now_static='{NOW_STATIC}'
- # 子配置目录
- subdomain_dir='{SUBDOMAIN_DIR}'
- # 证书路径
- ssl_path='{SSL_PATH}'
- # 不管有没有配置,都删除配置
- rm -rf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}
- # 如果文件夹不存在,新建子域名配置文件夹
- cd /application/nginx/conf/vhosts
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /application/nginx/conf/vhosts/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- # 如果文件夹不存在,新建子域名证书文件夹
- cd /application/nginx/conf/ssl
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /application/nginx/conf/ssl/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- # 如果文件夹不存在,新建子域名日志文件夹
- cd /application/nginx/logs
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /application/nginx/logs/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- # 复制Nginx配置文件
- # 复制文件 admin
- cp /application/nginx/conf/vhosts/${original_admin}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_admin}.conf
- # 复制文件 h5
- cp /application/nginx/conf/vhosts/${original_h5}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_h5}.conf
- # 复制文件 api
- cp /application/nginx/conf/vhosts/${original_api}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_api}.conf
- # 复制文件 static
- cp /application/nginx/conf/vhosts/${original_static}.conf /application/nginx/conf/vhosts/subdomain/${subdomain_dir}/${now_static}.conf
- # 切换到配置文件夹,修改Nginx配置
- cd /application/nginx/conf/vhosts/subdomain/${subdomain_dir}
- # 替换域名 admin
- # 证书目录
- old=\/application\/nginx\/conf\/ssl\/${original_admin}
- new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_admin}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_admin}.conf
- # 日志目录替换
- old_log=logs\/${original_admin}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_admin}
- sed -i "s|${old_log}|${new_log}|g" ${now_admin}.conf
- # 域名替换
- sed -i "s|${original_admin}|${now_admin}|g" ${now_admin}.conf
- # 替换域名 h5
- # 证书目录
- old=\/application\/nginx\/conf\/ssl\/${original_h5}
- new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_h5}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_h5}.conf
- # 日志目录替换
- old_log=logs\/${original_h5}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_h5}
- sed -i "s|${old_log}|${new_log}|g" ${now_h5}.conf
- # 域名替换
- sed -i "s|${original_h5}|${now_h5}|g" ${now_h5}.conf
- # 替换域名 api
- # 证书目录
- old=\/application\/nginx\/conf\/ssl\/${original_api}
- new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_api}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_api}.conf
- # 日志目录替换
- old_log=logs\/${original_api}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_api}
- sed -i "s|${old_log}|${new_log}|g" ${now_api}.conf
- # 域名替换
- sed -i "s|${original_api}|${now_api}|g" ${now_api}.conf
- # 替换域名 static
- # 证书目录
- old=\/application\/nginx\/conf\/ssl\/${original_static}
- new=\/application\/nginx\/conf\/ssl\/subdomain\/${subdomain_dir}\/${now_static}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_static}.conf
- # 日志目录替换
- old_log=logs\/${original_static}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_static}
- sed -i "s|${old_log}|${new_log}|g" ${now_static}.conf
- # 域名替换
- sed -i "s|${original_static}|${now_static}|g" ${now_static}.conf
- # 证书
- # 移动证书到目录
- dir=/application/nginx/conf/ssl/subdomain/${subdomain_dir}
- # 证书目录
- ssl=/data/wwwroot/qimall/${ssl_path}
- # 复制
- cp ${ssl} ${dir}/ssl.zip
- cd ${dir}
- # 解压
- unzip -o ssl.zip
- # 重启nginx
- # systemctl restart nginx
- /application/nginx/sbin/nginx -s reload
- # 重启消费者cd
- # systemctl restart swoole_consume_run
- fi
- EOC;
- }
- protected static function shDockerTemplate()
- {
- return <<<'EOC'
- #!/bin/bash
- # 检查是否有sudo权限
- if [ "$(id -u)" != "0" ]; then
- # 设置root密码,这里是示例密码,请替换为您的实际密码
- root_password='{PWD}'
- # 使用echo和管道将密码传递给su命令
- echo $root_password | su -c "sh /www/wwwroot/qimall/{FILE_NAME}"
- fi
- # 检查是否有sudo权限
- if [ "$(id -u)" == "0" ]; then
- # 本域名
- original_admin='{ORIGINAL_ADMIN}'
- original_h5='{ORIGINAL_H5}'
- original_api='{ORIGINAL_API}'
- original_static='{ORIGINAL_STATIC}'
- # 子域名
- now_admin='{NOW_ADMIN}'
- now_h5='{NOW_H5}'
- now_api='{NOW_API}'
- now_static='{NOW_STATIC}'
- # 子配置目录
- subdomain_dir='{SUBDOMAIN_DIR}'
- # 证书路径
- ssl_path='{SSL_PATH}'
- # 不管有没有配置,都删除配置
- rm -rf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}
- # 如果文件夹不存在,新建子域名配置文件夹
- cd /data/confs/nginx/conf.d
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /data/confs/nginx/conf.d/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- # 如果文件夹不存在,新建子域名证书文件夹
- cd /data/confs/nginx/ssl
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /data/confs/nginx/ssl/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- cd /data/confs/nginx
- if [ ! -d 'logs' ]; then
- mkdir logs
- fi
- # 如果文件夹不存在,新建子域名日志文件夹
- cd /data/confs/nginx/logs
- if [ ! -d 'subdomain' ]; then
- mkdir subdomain
- fi
- cd /data/confs/nginx/logs/subdomain
- if [ ! -d "$subdomain_dir" ]; then
- mkdir $subdomain_dir
- fi
- # 复制Nginx配置文件
- # 复制文件 admin
- cp /data/confs/nginx/conf.d/${original_admin}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_admin}.conf
- # 复制文件 h5
- cp /data/confs/nginx/conf.d/${original_h5}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_h5}.conf
- # 复制文件 api
- cp /data/confs/nginx/conf.d/${original_api}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_api}.conf
- # 复制文件 static
- cp /data/confs/nginx/conf.d/${original_static}.conf /data/confs/nginx/conf.d/subdomain/${subdomain_dir}/${now_static}.conf
- # 切换到配置文件夹,修改Nginx配置
- cd /data/confs/nginx/conf.d/subdomain/${subdomain_dir}
- # 替换域名 admin
- # 证书目录
- old=\/etc\/nginx\/ssl\/${original_admin}
- new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_admin}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_admin}.conf
- # 日志目录替换
- old_log=logs\/${original_admin}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_admin}
- sed -i "s|${old_log}|${new_log}|g" ${now_admin}.conf
- # 域名替换
- sed -i "s|${original_admin}|${now_admin}|g" ${now_admin}.conf
- # 替换域名 h5
- # 证书目录
- old=\/etc\/nginx\/ssl\/${original_h5}
- new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_h5}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_h5}.conf
- # 日志目录替换
- old_log=logs\/${original_h5}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_h5}
- sed -i "s|${old_log}|${new_log}|g" ${now_h5}.conf
- # 域名替换
- sed -i "s|${original_h5}|${now_h5}|g" ${now_h5}.conf
- # 替换域名 api
- # 证书目录
- old=\/etc\/nginx\/ssl\/${original_api}
- new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_api}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_api}.conf
- # 日志目录替换
- old_log=logs\/${original_api}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_api}
- sed -i "s|${old_log}|${new_log}|g" ${now_api}.conf
- # 域名替换
- sed -i "s|${original_api}|${now_api}|g" ${now_api}.conf
- # 替换域名 static
- # 证书目录
- old=\/etc\/nginx\/ssl\/${original_static}
- new=\/etc\/nginx\/ssl\/subdomain\/${subdomain_dir}\/${now_static}
- # 替换证书目录
- sed -i "s|${old}|${new}|g" ${now_static}.conf
- # 日志目录替换
- old_log=logs\/${original_static}
- new_log=logs\/subdomain\/${subdomain_dir}\/${now_static}
- sed -i "s|${old_log}|${new_log}|g" ${now_static}.conf
- # 域名替换
- sed -i "s|${original_static}|${now_static}|g" ${now_static}.conf
- # 证书
- # 移动证书到目录
- dir=/data/confs/nginx/ssl/subdomain/${subdomain_dir}
- # 证书目录
- ssl=/www/wwwroot/qimall/${ssl_path}
- # 复制
- cp ${ssl} ${dir}/ssl.zip
- cd ${dir}
- # 解压
- unzip -o ssl.zip
- # 重启nginx
- #systemctl restart nginx
- #/usr/sbin/nginx -s reload
- 重启docker
- sudo docker restart nginx
- # 重启消费者cd
- # systemctl restart swoole_consume_run
- fi
- EOC;
- }
- }
|