EmergencyRecords.php 3.2 KB
<?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');

        try {
            //获取服务器的所有可用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];
                    $record = AlibabaCloudService::describeDomainRecords('globalso.com', $domain_rr);
                    $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'];

                    $record_edit = AlibabaCloudService::updateDomainRecord($record_id, $record_rr, $record_type, $target_ip);
                    $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 . ' 失败');
                    }
                }
            }
        } catch (\Exception $e) {
            $this->output($e->getMessage());
        }
    }

    /**
     * 输出处理日志
     * @param $message
     */
    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
    }
}