作者 刘锟

合并分支 'akun' 到 'master'

Akun



查看合并请求 !730
<?php
namespace App\Console\Commands\Domain;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
class CreateSite extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create_domain_site';
/**
* The console command description.
*
* @var string
*/
protected $description = '创建域名站点';
public function handle()
{
$model = new DomainCreateTask();
$task = $model->read(['status' => DomainCreateTask::STATUS_UN]);
if ($task) {
$model->edit(['status' => DomainCreateTask::STATUS_ING], ['id' => $task['id']]);
$re = $this->editDomainBt($task['domain_id'], $task['type']);
if (is_array($re)) {
$model->edit(['status' => DomainCreateTask::STATUS_FAL, 'error_msg' => $re[1]], ['id' => $task['id']]);
} else {
$model->edit(['status' => DomainCreateTask::STATUS_SUC], ['id' => $task['id']]);
}
}
}
protected function editDomainBt($domain_id, $type)
{
//获取域名数据
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['id' => $domain_id]);
if ($domain_info === false) {
return [false, '获取域名数据失败'];
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
if ($project_info === false) {
return [false, '获取项目数据失败'];
}
//获取服务器数据
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
if ($serversIpInfo === false) {
return [false, '获取服务器数据失败1'];
}
$serverModel = new Servers();
$serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);
if ($serverInfo === false) {
return [false, '获取服务器数据失败2'];
}
if ($type == 1) {
/*****************编辑主站*******************/
if ($domain_info['type'] == 2) {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/setSsl';
$api_param = [
'domain' => $domain_info['domain'],
'private_key' => $domain_info['private_key'],
'cert' => $domain_info['private_cert'],
'rewrite' => $domain_info['extend_config'],
'other_domain' => $domain_info['other_domain'],
'is_https' => $domain_info['is_https'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
} else {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/applySsl';
$api_param = [
'domain' => $domain_info['domain'],
'rewrite' => $domain_info['extend_config'],
'other_domain' => $domain_info['other_domain'],
'is_https' => $domain_info['is_https'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
}
try {
$rs = HttpUtils::get($api_url, $api_param, [], 180);
$rs = json_decode($rs, true);
if (isset($rs['status']) && $rs['status'] == 200) {
$this->output($domain_info['domain'] . ',主站创建成功');
} else {
return [false, $rs['message'] ?? '未知错误'];
}
} catch (\Exception | GuzzleException $e) {
return [false, $e->getMessage()];
}
} else {
/*****************编辑amp站*******************/
if ($domain_info['amp_status']) {
$api_url_amp = 'http://' . $serverInfo['init_domain'] . '/api/createSiteAmp';
$api_param_amp = [
'domain' => $domain_info['domain'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
if ($domain_info['amp_type'] == 2) {
$api_param_amp['private_key'] = $domain_info['amp_private_key'];
$api_param_amp['cert'] = $domain_info['amp_private_cert'];
}
try {
$rs_amp = HttpUtils::get($api_url_amp, $api_param_amp, [], 180);
$rs_amp = json_decode($rs_amp, true);
if (isset($rs_amp['status']) && $rs_amp['status'] == 200) {
$this->output($domain_info['domain'] . ',amp站创建成功');
} else {
return [false, $rs['message'] ?? '未知错误'];
}
} catch (\Exception | GuzzleException $e_amp) {
return [false, $e_amp->getMessage()];
}
}
}
return true;
}
/**
* 输出处理日志
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}
... ... @@ -4,21 +4,12 @@ namespace App\Http\Logic\Aside\Domain;
use App\Http\Logic\Aside\BaseLogic;
use App\Jobs\EditAmpDomainBt;
use App\Jobs\EditCustomDomainBt;
use App\Jobs\EditDomainBt;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Project\CountryCustom;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use Symfony\Component\Process\Process;
class DomainInfoLogic extends BaseLogic
... ... @@ -152,98 +143,6 @@ class DomainInfoLogic extends BaseLogic
}
/**
* 编辑网站证书
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2023/10/17 11:52
*/
public function setDomainSsl($initDomain,$domain,$rewrite,$other_domain,$is_https)
{
if($this->param['type'] == 2){
if(empty($this->param['key'])){
$this->fail('证书KEY值不能为空');
}
if(empty($this->param['cert'])){
$this->fail('证书cert值不能为空');
}
$api_url = 'http://'.$initDomain.'/api/setSsl';
$api_param = [
'domain' => $domain,
'private_key' => $this->param['key'],
'cert' => $this->param['cert'],
'rewrite'=>$rewrite,
'other_domain'=>$other_domain,
'is_https' => $is_https
];
}else{
$api_url = 'http://'.$initDomain.'/api/applySsl';
$api_param = [
'domain' => $domain,
'rewrite'=>$rewrite,
'other_domain'=>$other_domain,
'is_https' => $is_https
];
}
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if(isset($rs['status']) && $rs['status'] == 200){
return $this->success();
}else{
$this->fail($rs['message']??'');
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建站点', $api_param, $e);
$this->fail('编辑证书失败');
}
return $this->success();
}
/**
* 编辑amp网站证书
* @param $initDomain
* @param $domain
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2024/02/22 14:58
*/
public function setAmpDomainSsl($initDomain,$domain)
{
if($this->param['amp_type'] == 2){
if(empty($this->param['amp_key'])){
$this->fail('AMP站点证书KEY值不能为空');
}
if(empty($this->param['amp_cert'])){
$this->fail('AMP站点证书cert值不能为空');
}
}
$api_url = 'http://'.$initDomain.'/api/createSiteAmp';
$api_param = [
'domain' => $domain,
'private_key' => $this->param['amp_key']??'',
'cert' => $this->param['amp_cert']??''
];
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if(isset($rs['status']) && $rs['status'] == 200){
return $this->success();
}else{
$this->fail($rs['message']??'');
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建AMP站点', $api_param, $e);
$this->fail('编辑AMP站点证书失败');
}
return $this->success();
}
/**
* @remark :保存证书相关配置
* @name :sslSave
* @author :lyh
... ... @@ -340,12 +239,6 @@ class DomainInfoLogic extends BaseLogic
}
}
// //主站生成证书
// EditDomainBt::dispatch($this->param['id']);
// //amp站生成证书
// if($data['amp_status']){
// EditAmpDomainBt::dispatch($this->param['id']);
// }
return $this->success();
}
... ... @@ -460,9 +353,6 @@ class DomainInfoLogic extends BaseLogic
'type' => DomainCreateTask::TYPE_CUSTOM
]);
}
// //创建站点,设置证书
// EditCustomDomainBt::dispatch($id);
}
return $this->success();
... ...
<?php
namespace App\Jobs;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class EditAmpDomainBt implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1; // 可配置任务重试次数
protected $domain_id;
/**
* Create a new job instance.
*
* @param $domain_id
*/
public function __construct($domain_id)
{
$this->domain_id = $domain_id;
}
/**
* Execute the job.
*
* @return bool
*/
public function handle()
{
//获取域名数据
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['id' => $this->domain_id]);
if ($domain_info === false) {
return $this->output($domain_info['domain'] . ':获取域名数据失败');
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
if ($project_info === false) {
return $this->output($domain_info['domain'] . ':获取项目数据失败');
}
//获取服务器数据
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
if ($serversIpInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
$serverModel = new Servers();
$serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);
if ($serverInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
//编辑amp站
$api_url_amp = 'http://' . $serverInfo['init_domain'] . '/api/createSiteAmp';
$api_param_amp = [
'domain' => $domain_info['domain'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
if ($domain_info['amp_type'] == 2) {
$api_param_amp['private_key'] = $domain_info['amp_private_key'];
$api_param_amp['cert'] = $domain_info['amp_private_cert'];
}
try {
$rs_amp = HttpUtils::get($api_url_amp, $api_param_amp);
$rs_amp = json_decode($rs_amp, true);
if (isset($rs_amp['status']) && $rs_amp['status'] == 200) {
$this->output($domain_info['domain'] . ':amp站编辑成功');
} else {
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . ($rs_amp['message'] ?? ''));
}
} catch (\Exception | GuzzleException $e_amp) {
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . $e_amp->getMessage());
}
return true;
}
/**
* 输出处理日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
public function failed(\Exception $exception)
{
return $this->output($exception->getMessage());
}
}
<?php
namespace App\Jobs;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Project\CountryCustom;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class EditCustomDomainBt implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1; // 可配置任务重试次数
protected $domain_id;
/**
* Create a new job instance.
*
* @param $domain_id
*/
public function __construct($domain_id)
{
$this->domain_id = $domain_id;
}
/**
* Execute the job.
*
* @return bool
*/
public function handle()
{
//获取域名数据
$domain_model = new CountryCustom();
$domain_info = $domain_model->read(['id' => $this->domain_id]);
if ($domain_info === false) {
return $this->output($domain_info['custom_domain'] . ':获取域名数据失败');
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
if ($project_info === false) {
return $this->output($domain_info['custom_domain'] . ':获取项目数据失败');
}
//获取服务器数据
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
if ($serversIpInfo === false) {
return $this->output($domain_info['custom_domain'] . ':获取服务器数据失败');
}
$serverModel = new Servers();
$serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);
//编辑站点
if ($domain_info['type'] == 2) {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/setSsl';
$api_param = [
'domain' => $domain_info['custom_domain'],
'private_key' => $domain_info['private_key'],
'cert' => $domain_info['private_cert'],
'rewrite' => [],
'other_domain' => [],
'is_https' => 1
];
} else {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/applySsl';
$api_param = [
'domain' => $domain_info['custom_domain'],
'rewrite' => [],
'other_domain' => [],
'is_https' => 1
];
}
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if (isset($rs['status']) && $rs['status'] == 200) {
$this->output($domain_info['custom_domain'] . ':站点编辑成功');
} else {
$this->output($domain_info['custom_domain'] . ':站点编辑失败,原因:' . ($rs['message'] ?? ''));
}
} catch (\Exception | GuzzleException $e) {
$this->output($domain_info['custom_domain'] . ':站点编辑失败,原因:' . $e->getMessage());
}
return true;
}
/**
* 输出处理日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
public function failed(\Exception $exception)
{
return $this->output($exception->getMessage());
}
}
<?php
namespace App\Jobs;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class EditDomainBt implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1; // 可配置任务重试次数
protected $domain_id;
/**
* Create a new job instance.
*
* @param $domain_id
*/
public function __construct($domain_id)
{
$this->domain_id = $domain_id;
}
/**
* Execute the job.
*
* @return bool
*/
public function handle()
{
//获取域名数据
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['id' => $this->domain_id]);
if ($domain_info === false) {
return $this->output($domain_info['domain'] . ':获取域名数据失败');
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
if ($project_info === false) {
return $this->output($domain_info['domain'] . ':获取项目数据失败');
}
//获取服务器数据
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
if ($serversIpInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
$serverModel = new Servers();
$serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);
if ($serverInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
//编辑主站
if ($domain_info['type'] == 2) {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/setSsl';
$api_param = [
'domain' => $domain_info['domain'],
'private_key' => $domain_info['private_key'],
'cert' => $domain_info['private_cert'],
'rewrite' => $domain_info['extend_config'],
'other_domain' => $domain_info['other_domain'],
'is_https' => $domain_info['is_https'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
} else {
$api_url = 'http://' . $serverInfo['init_domain'] . '/api/applySsl';
$api_param = [
'domain' => $domain_info['domain'],
'rewrite' => $domain_info['extend_config'],
'other_domain' => $domain_info['other_domain'],
'is_https' => $domain_info['is_https'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
}
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if (isset($rs['status']) && $rs['status'] == 200) {
$this->output($domain_info['domain'] . ':主站编辑成功');
} else {
$this->output($domain_info['domain'] . ':主站编辑失败,原因:' . ($rs['message'] ?? ''));
}
} catch (\Exception | GuzzleException $e) {
$this->output($domain_info['domain'] . ':主站编辑失败,原因:' . $e->getMessage());
}
return true;
}
/**
* 输出处理日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
public function failed(\Exception $exception)
{
return $this->output($exception->getMessage());
}
}