作者 赵彬吉
... ... @@ -2,29 +2,124 @@
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';
protected $signature = 'emergency_relieve {server_id}';
protected $description = '危机解除,恢复项目服务器';
public function handle()
{
$backup_list = ProjectServerBackup::where('status', ProjectServerBackup::STATUS_NO)->get();
//需要恢复的服务器
$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();
$project_model = new Project();
if ($backup_list->count() > 0) {
foreach ($backup_list as $item) {
$project_model->edit(['serve_id' => $item->serve_id], ['id' => $item->project_id]);
$item->status = ProjectServerBackup::STATUS_YES;
$item->save();
$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:' . $item->project_id . ',恢复成功');
$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;
}
/**
... ...
... ... @@ -13,18 +13,19 @@ use Symfony\Component\Process\Process;
class EmergencyRenewSite extends Command
{
protected $signature = 'emergency_renew_site';
protected $signature = 'emergency_renew_site {server_id}';
protected $description = '紧急重建站点';
public function handle()
{
//目标服务器
$target_server_id = 1;
$target_server = ServersIp::select(['id', 'ip', 'domain'])->where('servers_id', $target_server_id)->first()->toArray();
$server_ip_model = new ServersIp();
$target_server = $server_ip_model->select(['id', 'ip', 'domain'])->where('servers_id', $target_server_id)->first()->toArray();
//受灾服务器
$server_ids = [9, 13];
$server_ip_ids = ServersIp::whereIn('servers_id', $server_ids)->get()->pluck('id')->toArray();
$server_id = $this->argument('server_id');;
$server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();
//获取所有受灾项目
$project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();
... ... @@ -32,6 +33,7 @@ class EmergencyRenewSite extends Command
$create_model = new DomainCreateTask();
$notify_model = new Notify();
$backup_model = new ProjectServerBackup();
if ($project_list->count() > 0) {
foreach ($project_list as $value) {
$domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
if (!$domain_info) {
... ... @@ -42,6 +44,7 @@ class EmergencyRenewSite extends Command
//判断域名是否已经解析到目标服务器
if (!$this->check_cname($domain_info['domain'], $target_server)) {
$this->output($domain_info['domain'] . ' | 未解析到目标服务器');
continue;
}
//获取站点其他域名
... ... @@ -87,9 +90,9 @@ class EmergencyRenewSite extends Command
//备份项目原始服务器
$backup_info = $backup_model->read(['project_id' => $value->id, 'status' => ProjectServerBackup::STATUS_NO], ['id']);
if ($backup_info) {
$backup_model->edit(['serve_id' => $value->serve_id], ['id' => $backup_info['id']]);
$backup_model->edit(['server_id' => $server_id, 'server_ip_id' => $value->serve_id], ['id' => $backup_info['id']]);
} else {
$backup_model->add(['project_id' => $value->id, 'serve_id' => $value->serve_id]);
$backup_model->add(['project_id' => $value->id, 'server_id' => $server_id, 'server_ip_id' => $value->serve_id]);
}
//更改项目服务器
... ... @@ -99,6 +102,7 @@ class EmergencyRenewSite extends Command
$this->output($domain_info['domain'] . ' | success');
}
}
}
/**
* 验证是否cname或者A记录解析到目标服务器
... ...
<?php
/**
* @remark :
* @name :SyncImage.php
* @author :lyh
* @method :post
* @time :2024/9/11 10:39
*/
namespace App\Console\Commands\Sync;
use App\Models\File\Image;
use App\Models\File\ImageSetting;
use App\Services\UpyunService;
use Illuminate\Console\Command;
use Qcloud\Cos\Client;
class SyncImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_shuiying_image';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步图片与文件';
// public function handle(){
// $str = $this->getProjectConfig(501);
// $imageModel = new Image();
// $lists = $imageModel->list(['project_id'=>501]);
// $domain = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com';//cos域名
// foreach ($lists as $k => $v){
// if($v['path'] == '/upload/p/501/image_product/2024-09/6569ac3a212aa39368.png'){
// continue;
// }
// $url = $domain . $v['path'].'?'.$str;
// echo date('Y-m-d H:i:s') . '水印路径:'. $url .',主键id:'. $v['id'] . PHP_EOL;
// $this->coverOriginalImage($url,$v['path']);
// }
// return true;
// }
public function handle(){
$data = [];
$domain = 'https://ecdn6-nc.globalso.com/';
$imageModel = new Image();
$lists = $imageModel->list(['project_id'=>501]);
foreach ($lists as $k => $v){
if($v['path'] == '/upload/p/501/image_product/2024-09/6569ac3a212aa39368.png'){
continue;
}
$url = $domain . $v['path'];
echo date('Y-m-d H:i:s') . '刷新路径:'. $url .',主键id:'. $v['id'] . PHP_EOL;
$data[] = $url;
}
$yunService = new UpyunService();
return $yunService->preheatPush($data);
}
/**
* @remark :添加水印后保存图片(覆盖/非覆盖的文件未存入数据库)
* @name :uploadImages
* @author :lyh
* @method :post
* @time :2024/8/19 17:06
*/
public function coverOriginalImage($url,$cdnUrl){
// 获取水印后的图片内容
$imageContent = file_get_contents($url);
// 使用 COS SDK 将图片重新上传并覆盖原图
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
// 上传并覆盖原图
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径
'Body' => $imageContent,
]);
return $cos['cdn'].$cdnUrl;
}
/**
* @remark :获取图片配置
* @name :getProjectConfig
* @author :lyh
* @method :post
* @time :2024/8/24 11:03
*/
public function getProjectConfig($project_id = 0){
$str = '';
$imageSettingModel = new ImageSetting();
$settingInfo = $imageSettingModel->read(['project_id'=>$project_id]);
if($settingInfo !== false){
if($settingInfo['status'] == 1 && !empty($settingInfo['image_data'])){
$image_data = json_decode($settingInfo['image_data'],true);
foreach ($image_data as $k => $v){
if (str_starts_with($v, "image/")) {
$v = 'image/'.urlSafeBase64Encode(substr($v, strlen("image/")));
}
$image_data[$k] = $v;
}
$str = 'watermark/1/'.implode('/',$image_data);
return $str;
}
if($settingInfo['status'] == 2 && !empty($settingInfo['str_data'])){
$str_data = json_decode($settingInfo['str_data'],true);
foreach ($str_data as $k => $v){
$arr = explode('/',$v);
if ($arr[0] == 'text') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
if ($arr[0] == 'font') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
if ($arr[0] == 'fill') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
$str_data[$k] = $v;
}
$str = 'watermark/2/'.implode('/',$str_data);
return $str;
}
}
return $str;
}
}
... ...
... ... @@ -24,7 +24,7 @@ class CommentController extends BaseController
*/
public function lists(){
$commentModel = new Comment();
$lists = $commentModel->list();
$lists = $commentModel->lists($this->map,$this->page,$this->row);
$this->response('success',Code::SUCCESS,$lists);
}
... ...
... ... @@ -47,7 +47,7 @@ class ProductController extends BaseController
*/
public function index(Product $product)
{
$filed = ['id', 'project_id', 'title', 'sort' ,'thumb' ,'product_type' , 'route' , 'intro' , 'content' ,
$filed = ['id', 'project_id', 'title', 'sort' ,'thumb' ,'product_type' , 'route' ,
'category_id', 'keyword_id', 'status', 'created_uid', 'is_upgrade' ,'created_at', 'updated_at','six_read'];
$this->order = 'sort';
$query = $product->orderBy($this->order ,'desc')->orderBy('id','desc');
... ...