作者 刘锟

amp批量建站脚本

@@ -6,10 +6,16 @@ use App\Helper\Arr; @@ -6,10 +6,16 @@ use App\Helper\Arr;
6 use App\Models\Collect\CollectTask; 6 use App\Models\Collect\CollectTask;
7 use App\Models\Com\UpdateLog; 7 use App\Models\Com\UpdateLog;
8 use App\Models\Com\UpdateVisit; 8 use App\Models\Com\UpdateVisit;
  9 +use App\Models\Devops\ServerConfig;
  10 +use App\Models\Domain\DomainInfo;
9 use App\Models\Product\Product; 11 use App\Models\Product\Product;
  12 +use App\Models\Project\Project;
10 use App\Services\ProjectServer; 13 use App\Services\ProjectServer;
  14 +use App\Utils\HttpUtils;
  15 +use GuzzleHttp\Exception\GuzzleException;
11 use Illuminate\Console\Command; 16 use Illuminate\Console\Command;
12 use Illuminate\Support\Facades\DB; 17 use Illuminate\Support\Facades\DB;
  18 +use Symfony\Component\Process\Process;
13 19
14 class Temp extends Command 20 class Temp extends Command
15 { 21 {
@@ -30,17 +36,106 @@ class Temp extends Command @@ -30,17 +36,106 @@ class Temp extends Command
30 36
31 public function handle() 37 public function handle()
32 { 38 {
33 - $project = ProjectServer::useProject(626);  
34 - if ($project) {  
35 - CollectTask::select(['id', 'language'])->where('status', 0)->chunk(1000, function ($query) { 39 + $domain_model = new DomainInfo();
  40 + $server_model = new ServerConfig();
  41 + $project_model = new Project();
36 42
37 - foreach ($query as $item) {  
38 - $item->domain = 'lecusostreetlight.quanqiusou.cn/' . $item->language;  
39 - $item->save(); 43 + $domain_list = $domain_model->list(['domain' => ['like', 'www.%']], 'id', ['id', 'domain', 'project_id'], 'asc');
  44 + foreach ($domain_list as $info) {
  45 + $this->output('domain:' . $info['domain'] . ',开始');
  46 +
  47 + $project_info = $project_model->read(['id' => $info['project_id']], 'serve_id');
  48 + if ($project_info === false) {
  49 + $this->output('获取项目数据失败');
  50 + continue;
  51 + }
  52 +
  53 + $server_info = $server_model->read(['id' => $project_info['serve_id']], ['init_domain', 'host']);
  54 + if ($server_info === false) {
  55 + $this->output('获取服务器数据失败');
  56 + continue;
  57 + }
  58 +
  59 + $domain_array = parse_url($info['domain']);
  60 + $host = $domain_array['host'] ?? $domain_array['path'];
  61 + $host_array = explode('.', $host);
  62 + if (count($host_array) <= 2) {
  63 + array_unshift($host_array, 'm');
  64 + } else {
  65 + $host_array[0] = 'm';
  66 + }
  67 + $amp_domain = implode('.', $host_array);
  68 + if (!$this->check_cname($amp_domain, $server_info)) {
  69 + $this->output('AMP站点域名' . $amp_domain . '未解析至目标服务器');
  70 + continue;
  71 + }
  72 +
  73 + $api_url = 'http://' . $server_info['init_domain'] . '/api/createSiteAmp';
  74 + $api_param = [
  75 + 'domain' => $info['domain'],
  76 + 'private_key' => '',
  77 + 'cert' => ''
  78 + ];
  79 +
  80 + try {
  81 + $rs = HttpUtils::get($api_url, $api_param);
  82 + $rs = json_decode($rs, true);
  83 + if (isset($rs['status']) && $rs['status'] == 200) {
  84 + $this->output('创建AMP站点成功');
  85 + } else {
  86 + $this->output($rs['message'] ?? '');
  87 + continue;
40 } 88 }
41 - }); 89 + } catch (\Exception | GuzzleException $e) {
  90 + errorLog('创建AMP站点', $api_param, $e);
  91 + $this->output('创建AMP站点失败');
  92 + continue;
  93 + }
  94 +
  95 + $data = [
  96 + 'amp_status' => 1,
  97 + 'amp_type' => 1,
  98 + ];
  99 + $domain_model->edit($data, ['id' => $info['id']]);
42 } 100 }
43 - DB::disconnect('custom_mysql'); 101 +
44 echo '成功' . PHP_EOL; 102 echo '成功' . PHP_EOL;
45 } 103 }
  104 +
  105 + public function check_cname($domain, $server_info)
  106 + {
  107 + $checkA = false;
  108 + $checkCname = false;
  109 +
  110 + $process = new Process(['nslookup', '-qt=a', $domain]);
  111 + $process->run();
  112 + $output = explode(PHP_EOL, $process->getOutput());
  113 + foreach ($output as $line) {
  114 + if ($line) {
  115 + $checkA = strpos($line, $server_info['host']) !== false;
  116 + if ($checkA) {
  117 + return $domain;
  118 + }
  119 + }
  120 + }
  121 +
  122 + //是否cname
  123 + $process = new Process(['nslookup', '-qt=cname', $domain]);
  124 + $process->run();
  125 + $output = explode(PHP_EOL, $process->getOutput());
  126 + foreach ($output as $line) {
  127 + if ($line) {
  128 + $checkCname = (strpos($line, $server_info['init_domain']) !== false);
  129 + if ($checkCname) {
  130 + return $domain;
  131 + }
  132 + }
  133 + }
  134 + return false;
  135 + }
  136 +
  137 + public function output($msg)
  138 + {
  139 + echo $msg . PHP_EOL;
  140 + }
46 } 141 }
@@ -42,7 +42,7 @@ class HttpUtils @@ -42,7 +42,7 @@ class HttpUtils
42 public static function get($url, $data, $headers = []) 42 public static function get($url, $data, $headers = [])
43 { 43 {
44 LogUtils::info("HttpUtils-GET请求URL:" . $url); 44 LogUtils::info("HttpUtils-GET请求URL:" . $url);
45 - $response = Http::timeout(20)->withHeaders($headers)->get($url, $data); 45 + $response = Http::timeout(60)->withHeaders($headers)->get($url, $data);
46 self::checkSuccess($response); 46 self::checkSuccess($response);
47 return $response->getBody()->getContents(); 47 return $response->getBody()->getContents();
48 } 48 }
@@ -50,7 +50,7 @@ class HttpUtils @@ -50,7 +50,7 @@ class HttpUtils
50 public static function post($url, $data, $headers = []) 50 public static function post($url, $data, $headers = [])
51 { 51 {
52 LogUtils::info("HttpUtils-POST请求URL:" . $url); 52 LogUtils::info("HttpUtils-POST请求URL:" . $url);
53 - $response = Http::timeout(20)->withHeaders($headers)->post($url, $data); 53 + $response = Http::timeout(60)->withHeaders($headers)->post($url, $data);
54 self::checkSuccess($response); 54 self::checkSuccess($response);
55 return $response->getBody()->getContents(); 55 return $response->getBody()->getContents();
56 } 56 }