EmergencyRenewSite.php
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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;
}
}