作者 赵彬吉
<?php
namespace App\Console\Commands\Domain;
use App\Models\Project\Project;
use App\Models\Project\ProjectServerBackup;
use Illuminate\Console\Command;
class EmergencyRelieve extends Command
{
protected $signature = 'emergency_relieve';
protected $description = '危机解除,恢复项目服务器';
public function handle()
{
$backup_list = ProjectServerBackup::where('status', ProjectServerBackup::STATUS_NO)->get();
$project_model = new Project();
if ($backup_list->count() > 0) {
foreach ($backup_list as $item) {
$project_model->edit(['serve_id' => $item->serve_id], ['id' => $item->project_id]);
$item->status = ProjectServerBackup::STATUS_YES;
$item->save();
$this->output('项目ID:' . $item->project_id . ',恢复成功');
}
}
}
/**
* 输出处理日志
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}
... ...
<?php
namespace App\Console\Commands\Domain;
use App\Models\Com\Notify;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Project\Project;
use App\Models\Project\ProjectServerBackup;
use App\Models\Domain\DomainInfo;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class EmergencyRenewSite extends Command
{
protected $signature = 'emergency_renew_site';
protected $description = '紧急重建站点';
public function handle()
{
//目标服务器
$target_server_id = 1;
$target_server = ServersIp::select(['id', 'ip', 'domain'])->where('servers_id', $target_server_id)->first()->toArray();
//受灾服务器
$server_ids = [9, 13];
$server_ip_ids = ServersIp::whereIn('servers_id', $server_ids)->get()->pluck('id')->toArray();
//获取所有受灾项目
$project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();
$domain_model = new DomainInfo();
$create_model = new DomainCreateTask();
$notify_model = new Notify();
$backup_model = new ProjectServerBackup();
foreach ($project_list as $value) {
$domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
if (!$domain_info) {
//过滤未绑定正式域名的项目
continue;
}
//判断域名是否已经解析到目标服务器
if (!$this->check_cname($domain_info['domain'], $target_server)) {
$this->output($domain_info['domain'] . ' | 未解析到目标服务器');
}
//获取站点其他域名
$other_domain = [];
if (strpos($domain_info['domain'], 'www.') === 0) {
$other_domain[] = str_replace('www', '*', $domain_info['domain']);
$top_domain = str_replace('www.', '', $domain_info['domain']);
if ($this->check_cname($top_domain, $target_server)) {
$other_domain[] = $top_domain;
}
}
//创建目标服务器建站任务
$map_create = [
'type' => DomainCreateTask::TYPE_MAIN,
'server_id' => $target_server_id,
'project_id' => $value->id,
'domain_id' => $domain_info['id'],
'status' => DomainCreateTask::STATUS_UN,
];
$task_info = $create_model->read($map_create, ['id']);
if (!$task_info) {
$map_create['other_domain'] = json_encode($other_domain);
$create_model->add($map_create);
}
//创建目标服务器站点页面生成任务
$map_notify = [
'type' => Notify::TYPE_MASTER,
'server_id' => $target_server_id,
'project_id' => $value->id,
'status' => Notify::STATUS_INIT,
'route' => Notify::ROUTE_ALL,
];
$notify_info = $notify_model->read($map_notify);
if (!$notify_info) {
$map_notify['data'] = json_encode(['domain' => $domain_info['domain'], 'url' => [], 'language' => []]);
$map_notify['sort'] = 9;
$notify_model->add($map_notify);
}
//备份项目原始服务器
$backup_info = $backup_model->read(['project_id' => $value->id, 'status' => ProjectServerBackup::STATUS_NO], ['id']);
if ($backup_info) {
$backup_model->edit(['serve_id' => $value->serve_id], ['id' => $backup_info['id']]);
} else {
$backup_model->add(['project_id' => $value->id, 'serve_id' => $value->serve_id]);
}
//更改项目服务器
$value->serve_id = $target_server_id;
$value->save();
$this->output($domain_info['domain'] . ' | success');
}
}
/**
* 验证是否cname或者A记录解析到目标服务器
* @param $domain
* @param $server_info
* @return mixed
* @author zbj
* @date 2023/11/13
*/
public function check_cname($domain, $server_info)
{
$process = new Process(['nslookup', '-qt=a', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line) {
if ($line) {
$checkA = strpos($line, $server_info['ip']) !== false;
if ($checkA) {
return $domain;
}
}
}
//是否cname
$process = new Process(['nslookup', '-qt=cname', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line) {
if ($line) {
$checkCname = (strpos($line, $server_info['domain']) !== false);
if ($checkCname) {
return $domain;
}
}
}
return false;
}
/**
* 输出处理日志
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -13,6 +13,7 @@ use App\Helper\Arr;
use App\Helper\Translate;
use App\Models\Blog\Blog;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
... ... @@ -55,33 +56,35 @@ class UpdateRoute extends Command
*/
public function handle(){
$projectModel = new Project();
$list = $projectModel->list(['id'=>['in',[1659]]]);
$list = $projectModel->list(['id'=>['in',[2321]]]);
$data = [];
foreach ($list as $v){
echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
ProjectServer::useProject($v['id']);
$this->getProduct();
// $this->getProduct();
// $this->setProductKeyword();
// $this->getBlog();
$this->setCustomRoute($v['id']);
DB::disconnect('custom_mysql');
}
echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
}
public function setCustomRoute($project_id){
// $customModel = new CustomModuleContent();
// $list = $customModel->list();
// foreach ($list as $v){
// $route = RouteMap::setRoute($v['name'], RouteMap::SOURCE_MODULE, $v['id'], $project_id);
// $customModel->edit(['route'=>$route],['id'=>$v['id']]);
$customModel = new CustomModuleContent();
$list = $customModel->list();
foreach ($list as $v){
echo date('Y-m-d H:i:s') . '扩展模块id:'.$v['id'] . PHP_EOL;
$route = RouteMap::setRoute($v['name'], RouteMap::SOURCE_MODULE, $v['id'], $project_id);
$customModel->edit(['route'=>$route],['id'=>$v['id']]);
}
// $cateModel = new CustomModuleCategory();
// $lists = $cateModel->list();
// foreach ($lists as $v1){
// $route = RouteMap::setRoute($v1['name'], RouteMap::SOURCE_MODULE_CATE, $v1['id'], $project_id);
// $cateModel->edit(['route'=>$route],['id'=>$v1['id']]);
//
// }
$cateModel = new CustomModuleCategory();
$lists = $cateModel->list();
foreach ($lists as $v1){
$route = RouteMap::setRoute($v1['name'], RouteMap::SOURCE_MODULE_CATE, $v1['id'], $project_id);
$cateModel->edit(['route'=>$route],['id'=>$v1['id']]);
}
}
public function delProductKeyword(){
... ...
... ... @@ -189,6 +189,7 @@ class UserLoginLogic
if($info['is_customized'] == 1){
$info['is_visualization'] = json_decode($project['is_visualization']);
}
$info['is_comment'] = $project['deploy_build']['is_comment'] ?? 0;
$info['is_visualization_authority'] = $project['deploy_build']['is_visualization_authority'];
$info['is_inquiry_country'] = $project['is_inquiry_country'];
$info['is_subscribe'] = $project['is_subscribe'];
... ... @@ -294,6 +295,7 @@ class UserLoginLogic
$info['project_location'] = $project['project_location'];
$info['file_cdn'] = $project['deploy_build']['file_cdn'];
$info['service_duration'] = $project['deploy_build']['service_duration'] ?? 0;
$info['is_comment'] = $project['deploy_build']['is_comment'] ?? 0;
$info['remain_day'] = $project['remain_day'] ?? 0;
if($info['is_customized'] == 1){
$info['is_visualization'] = json_decode($project['is_visualization']);
... ... @@ -308,7 +310,6 @@ class UserLoginLogic
$is_amp = $amp_info ? $amp_info['amp_status'] : 0;
}
$info['is_amp'] = $is_amp;
//保存项目缓存
Cache::put('user-'.$info['project_id'],$project,12 * 3600);
return $this->success($info);
... ...
<?php
namespace App\Models\Project;
use App\Models\Base;
class ProjectServerBackup extends Base
{
protected $table = 'gl_project_server_backup';
const STATUS_NO = 0;
const STATUS_YES = 1;
}
... ...
... ... @@ -621,6 +621,13 @@ Route::middleware(['bloginauth'])->group(function () {
Route::any('/set_smtp', [\App\Http\Controllers\Bside\Subscribe\EmailController::class, 'set_smtp'])->name('subscribe_email_set_smtp');
Route::any('/group_send', [\App\Http\Controllers\Bside\Subscribe\EmailController::class, 'group_send'])->name('subscribe_email_group_send');
});
//网站评论
Route::prefix('comment')->group(function () {
Route::any('/', [\App\Http\Controllers\Bside\Comment\CommentController::class, 'lists'])->name('comment_lists');
Route::any('/info', [\App\Http\Controllers\Bside\Comment\CommentController::class, 'info'])->name('comment_info');
Route::any('/status', [\App\Http\Controllers\Bside\Comment\CommentController::class, 'status'])->name('comment_status');
});
});
//无需登录验证的路由组
Route::group([], function () {
... ...