EmergencyRelieve.php
4.7 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
<?php
namespace App\Console\Commands\Domain;
use App\Models\Com\Notify;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Models\Project\ProjectServerBackup;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class EmergencyRelieve extends Command
{
protected $signature = 'emergency_relieve {server_id}';
protected $description = '危机解除,恢复项目服务器';
public function handle()
{
//需要恢复的服务器
$server_id = $this->argument('server_id');
//获取所有需要恢复的项目
$backup_model = new ProjectServerBackup();
$backup_list = $backup_model->where('server_id', $server_id)->where('status', ProjectServerBackup::STATUS_NO)->get();
if ($backup_list->count() > 0) {
$project_model = new Project();
$domain_model = new DomainInfo();
$create_model = new DomainCreateTask();
$notify_model = new Notify();
$server_ip_model = new ServersIp();
foreach ($backup_list as $value) {
$domain_info = $domain_model->read(['project_id' => $value->project_id, 'status' => 1], ['id', 'domain']);
if (!$domain_info) {
//过滤未绑定正式域名的项目
continue;
}
//判断域名是否已经解析到目标服务器
$target_server = $server_ip_model->select(['id', 'ip', 'domain'])->where('id', $value->server_ip_id)->first()->toArray();
if (!$this->check_cname($domain_info['domain'], $target_server)) {
$this->output($domain_info['domain'] . ' | 未解析到目标服务器');
continue;
}
//创建目标服务器建站任务
$map_create = [
'type' => DomainCreateTask::TYPE_MAIN,
'server_id' => $value->server_id,
'project_id' => $value->project_id,
'domain_id' => $domain_info['id'],
'status' => DomainCreateTask::STATUS_UN,
];
$task_info = $create_model->read($map_create, ['id']);
if (!$task_info) {
$create_model->add($map_create);
}
//创建目标服务器站点页面生成任务
$map_notify = [
'type' => Notify::TYPE_MASTER,
'server_id' => $value->server_id,
'project_id' => $value->project_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'] = 10;
$notify_model->add($map_notify);
}
//更改项目服务器
$project_model->edit(['serve_id' => $value->server_ip_id], ['id' => $value->project_id]);
//更改恢复状态
$value->status = ProjectServerBackup::STATUS_YES;
$value->save();
$this->output('项目ID:' . $value->project_id . ' | 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;
}
}