EmergencyRecords.php
3.4 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
<?php
namespace App\Console\Commands\Domain;
use App\Models\Devops\ServersIp;
use App\Services\AlibabaCloudService;
use Illuminate\Console\Command;
class EmergencyRecords extends Command
{
protected $signature = 'emergency_records {server_id} {type}';
protected $description = '紧急修改域名解析';
public function handle()
{
//服务器id
$server_id = $this->argument('server_id');
//类型,1受灾,2恢复
$type = $this->argument('type');
//获取服务器的所有可用ip
$server_ip_model = new ServersIp();
$server_ip_list = $server_ip_model->where('servers_id', $server_id)->where('status', 0)->get();
if ($server_ip_list->count() > 0) {
foreach ($server_ip_list as $value) {
if (empty($value->ip) || empty($value->domain)) {
$this->output('ID ' . $value->id . ' 数据错误');
continue;
}
//获取解析记录
$domain_array = explode('.', $value->domain);
$domain_rr = $domain_array[0];
try {
$record = AlibabaCloudService::describeDomainRecords('globalso.com', $domain_rr);
} catch (\Exception $e) {
$this->output('主机记录 ' . $domain_rr . 'error:' . $e->getMessage());
continue;
}
$record_status_code = $record['statusCode'] ?? 0;
if ($record_status_code != 200) {
$this->output('获取主机记录 ' . $domain_rr . ' 解析数据失败');
continue;
}
$record_detail = $record['body']['DomainRecords']['Record'][0] ?? [];
if (!$record_detail) {
$this->output('主机记录 ' . $domain_rr . ' 解析数据不存在');
continue;
}
//目标ip跟解析记录当前ip一样的数据,不用修改
$target_ip = $type == 1 ? '43.153.1.240' : $value->ip;
if ($target_ip == $record_detail['Value']) {
$this->output('主机记录 ' . $domain_rr . ' 的值已为 ' . $target_ip);
continue;
}
//修改解析记录
$record_id = $record_detail['RecordId'];
$record_rr = $record_detail['RR'];
$record_type = $record_detail['Type'];
try {
$record_edit = AlibabaCloudService::updateDomainRecord($record_id, $record_rr, $record_type, $target_ip);
} catch (\Exception $e) {
$this->output('主机记录 ' . $domain_rr . 'error:' . $e->getMessage());
continue;
}
$record_edit_status_code = $record_edit['statusCode'] ?? 0;
if ($record_edit_status_code == 200) {
$this->output('修改主机记录 ' . $record_rr . ' 的值为 ' . $target_ip . ' 成功');
} else {
$this->output('修改主机记录 ' . $record_rr . ' 的值为 ' . $target_ip . ' 失败');
}
}
}
}
/**
* 输出处理日志
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}