作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +
  6 +use App\Models\Devops\DevopsTaskLog;
  7 +use App\Models\Project;
  8 +use Illuminate\Console\Command;
  9 +use App\Models\Devops\DevopsTask as DevopsTaskModel;
  10 +
  11 +/**
  12 + * Class DevopsTask
  13 + * @package App\Console\Commands
  14 + * @author zbj
  15 + * @date 2023/4/25
  16 + */
  17 +class DevopsTask extends Command
  18 +{
  19 + /**
  20 + * The name and signature of the console command.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $signature = 'devops_task';
  25 +
  26 + /**
  27 + * The console command description.
  28 + *
  29 + * @var string
  30 + */
  31 + protected $description = '运维任务执行';
  32 +
  33 + /**
  34 + * Create a new command instance.
  35 + *
  36 + * @return void
  37 + */
  38 + public function __construct()
  39 + {
  40 + parent::__construct();
  41 + }
  42 +
  43 + /**
  44 + * @return bool
  45 + */
  46 + public function handle()
  47 + {
  48 + while (true){
  49 + $tasks = DevopsTaskModel::where('status', DevopsTaskModel::STATUS_PENDING)->get();
  50 + foreach ($tasks as $task){
  51 + echo "Start task " . $task->id . PHP_EOL;
  52 + if($task->type == DevopsTaskModel::TYPE_MYSQL){
  53 + $this->updateTable($task);
  54 + }
  55 + echo "End task " . $task->id . PHP_EOL;
  56 + }
  57 + sleep(10);
  58 + }
  59 + }
  60 +
  61 + public function updateTable($task){
  62 + $projects = Project::all();
  63 + foreach ($projects as $project){
  64 + echo "project " . $project->id;
  65 + $log = DevopsTaskLog::addLog($task->id, $project->id);
  66 + if($log->status == DevopsTaskModel::STATUS_ACTIVE){
  67 + continue;
  68 + }
  69 + if(!$project->mysqlConfig){
  70 + $log->status = DevopsTaskLog::STATUS_ERROR;
  71 + $log->remark = '未配置数据库';
  72 + $log->save();
  73 + continue;
  74 + }
  75 + //DB类是单例模式,生命周期内修改配置不会生效
  76 + $conn = new \mysqli(
  77 + $project->mysqlConfig->host,
  78 + $project->mysqlConfig->user,
  79 + $project->mysqlConfig->password,
  80 + $project->databaseName(),
  81 + $project->mysqlConfig->port,
  82 + );
  83 + $res = $conn->query($task->sql);
  84 +
  85 + $log->status = $res ? DevopsTaskLog::STATUS_ACTIVE : DevopsTaskLog::STATUS_ERROR;
  86 + $log->remark = $res ? '成功' : 'sql执行失败';
  87 + $log->save();
  88 + echo '-->' . $log->remark . PHP_EOL;
  89 + }
  90 + $task->status = DevopsTaskModel::STATUS_ACTIVE;
  91 + $task->save();
  92 + }
  93 +}
1 -<?php  
2 -/**  
3 - * Created by PhpStorm.  
4 - * User: zhl  
5 - * Date: 2023/4/19  
6 - * Time: 15:04  
7 - */  
8 -namespace App\Console\Commands;  
9 -  
10 -use App\Models\Project;  
11 -use App\Services\ProjectServer;  
12 -use Illuminate\Console\Command;  
13 -use Illuminate\Support\Facades\DB;  
14 -  
15 -/**  
16 - * Class ProjectDatabaseUpdate  
17 - * @package App\Console\Commands  
18 - */  
19 -class ProjectDatabaseUpdate extends Command  
20 -{  
21 - /**  
22 - * The name and signature of the console command.  
23 - *  
24 - * @var string  
25 - */  
26 - protected $signature = 'project:database_update';  
27 -  
28 - /**  
29 - * The console command description.  
30 - *  
31 - * @var string  
32 - */  
33 - protected $description = '项目数据库更新';  
34 -  
35 - /**  
36 - * Create a new command instance.  
37 - *  
38 - * @return void  
39 - */  
40 - public function __construct()  
41 - {  
42 - parent::__construct();  
43 - }  
44 -  
45 - /**  
46 - * @return bool  
47 - */  
48 - public function handle()  
49 - {  
50 - #TODO 未处理更新 更新对象(base, project) 更新内容  
51 - return true;  
52 - }  
53 -  
54 - /**  
55 - * 主库内容更新  
56 - * @param $sql  
57 - * @return array  
58 - */  
59 - public function baseUpdate($sql)  
60 - {  
61 - return DB::select($sql);  
62 - }  
63 -  
64 - /**  
65 - * 客户数据表更新  
66 - * @param $sql  
67 - * @return bool  
68 - */  
69 - public function projectDatabaseUpdate($sql)  
70 - {  
71 - $project = Project::get();  
72 - foreach ($project as $val) {  
73 - ProjectServer::useProject($val->id);  
74 - DB::connection('custom_mysql')->select($sql);  
75 - }  
76 - return true;  
77 - }  
78 -  
79 - /**  
80 - * 指定项目数据库更新  
81 - * @param $project_id  
82 - * @param $sql  
83 - */  
84 - public function appointProjectUpdate($project_id, $sql)  
85 - {  
86 - ProjectServer::useProject($project_id);  
87 - DB::connection('custom_mysql')->select($sql);  
88 - }  
89 -}  
1 -<?php  
2 -/**  
3 - * Created by PhpStorm.  
4 - * User: zhl  
5 - * Date: 2023/4/12  
6 - * Time: 15:33  
7 - */  
8 -namespace App\Console\Commands;  
9 -  
10 -use App\Models\Project;  
11 -use App\Services\ProjectServer;  
12 -use Illuminate\Console\Command;  
13 -use Illuminate\Support\Facades\DB;  
14 -use Illuminate\Support\Facades\Schema;  
15 -  
16 -/**  
17 - * Class ProjectInitDatabase  
18 - * @package App\Console\Commands  
19 - */  
20 -class ProjectInit extends Command  
21 -{  
22 - /**  
23 - * The name and signature of the console command.  
24 - *  
25 - * @var string  
26 - */  
27 - protected $signature = 'project:init';  
28 -  
29 - /**  
30 - * The console command description.  
31 - *  
32 - * @var string  
33 - */  
34 - protected $description = '项目数据库初始化';  
35 -  
36 - protected $connect = null;  
37 -  
38 - /**  
39 - * Create a new command instance.  
40 - *  
41 - * @return void  
42 - */  
43 - public function __construct()  
44 - {  
45 - parent::__construct();  
46 - }  
47 -  
48 - /**  
49 - * @return bool  
50 - */  
51 - public function handle()  
52 - {  
53 - #TODO 通过项目ID获取项目部署数据库配置, 创建数据库, 同步数据表  
54 - $project_id = 102;  
55 - $project = Project::getProjectById($project_id);  
56 - if (empty($project) || empty($project->mysqlConfig()))  
57 - return true;  
58 - $this->initDatabase($project);  
59 -  
60 - return true;  
61 - }  
62 -  
63 - /**  
64 - * @param Project $project  
65 - * @return bool  
66 - */  
67 - public function initDatabase($project)  
68 - {  
69 - $create_flag = $this->createDatabase($project);  
70 -  
71 - if (!$create_flag) {  
72 - // 创建数据库失败 添加通知以及再次处理  
73 - }  
74 - // 设置 database.connections.custom_mysql 数据  
75 - ProjectServer::useProject($project->id);  
76 -  
77 - // TODO 创建对应库 初始化数据表  
78 - $this->initTable();  
79 - return true;  
80 - }  
81 -  
82 - /**  
83 - * @return bool  
84 - */  
85 - public function initTable()  
86 - {  
87 - $database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();  
88 -  
89 - $table = Schema::connection('custom_tmp_mysql')->getAllTables();  
90 - $table = array_column($table, 'Tables_in_' . $database_name);  
91 - foreach ($table as $v) {  
92 - $has_table = Schema::connection('custom_mysql')->hasTable($v);  
93 - if ($has_table)  
94 - continue;  
95 -  
96 - $connection = DB::connection('custom_tmp_mysql');  
97 - $sql = $connection->getDoctrineSchemaManager()  
98 - ->getDatabasePlatform()  
99 - ->getCreateTableSQL($connection->getDoctrineSchemaManager()->listTableDetails($v));  
100 -  
101 - DB::connection('custom_mysql')->select($sql[0]);  
102 - }  
103 - return true;  
104 - }  
105 -  
106 - /**  
107 - * 创建数据库  
108 - * 链接mysql 查询数据库是否存在 创建数据库  
109 - * @param Project $project  
110 - * @return bool|\mysqli_result|null  
111 - */  
112 - public function createDatabase($project)  
113 - {  
114 - # 该方法需要:composer require parity-bit/laravel-db-commands  
115 -// $result = Artisan::call('db:create', [  
116 -// '--database' => $database_name,  
117 -// ]);  
118 -//  
119 -// return $result;  
120 -  
121 - if ($this->connect)  
122 - return $this->connect;  
123 -  
124 - //连接到 MySQL 服务器  
125 - $servername = $project->mysqlConfig()->host;  
126 - $username = $project->mysqlConfig()->user;  
127 - $password = $project->mysqlConfig()->password;  
128 - $conn = new \mysqli($servername, $username, $password);  
129 - //检查连接是否成功  
130 - if ($conn->connect_error) {  
131 - die("连接失败: " . $conn->connect_error);  
132 - }  
133 - $this->connect = $conn;  
134 -// $result = $conn->query('SHOW DATABASES LIKE \'' . $database_name . '\';');  
135 -// if ($result)  
136 -// return true;  
137 - $result = $conn->query('CREATE DATABASE ' . $project->databaseName() . ';');  
138 - return $result;  
139 - }  
140 -}  
1 <?php 1 <?php
2 2
3 -namespace App\Console\Commands; 3 +namespace App\Console\Commands\Test;
4 4
5 5
6 6
@@ -38,16 +38,6 @@ class BaseController extends Controller @@ -38,16 +38,6 @@ class BaseController extends Controller
38 $this->get_param(); 38 $this->get_param();
39 } 39 }
40 40
41 - /**  
42 - * admin端用渲染 不走接口  
43 - * @return mixed  
44 - * @author zbj  
45 - * @date 2023/4/19  
46 - */  
47 - public function manage(){  
48 - return Session::get('manage');  
49 - }  
50 -  
51 41
52 /** 42 /**
53 * 成功返回 43 * 成功返回
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 2
3 namespace App\Http\Controllers\Aside\Devops; 3 namespace App\Http\Controllers\Aside\Devops;
4 4
5 -use App\Http\Controllers\Bside\BaseController; 5 +use App\Http\Controllers\Aside\BaseController;
6 6
7 use App\Http\Logic\Aside\Devops\ServerConfigLogic; 7 use App\Http\Logic\Aside\Devops\ServerConfigLogic;
8 use App\Http\Requests\Aside\Devops\ServerConfigRequest; 8 use App\Http\Requests\Aside\Devops\ServerConfigRequest;
@@ -31,7 +31,7 @@ class LoginController extends BaseController @@ -31,7 +31,7 @@ class LoginController extends BaseController
31 31
32 return $this->success(); 32 return $this->success();
33 } 33 }
34 - if($this->manage()){ 34 + if($logic->manage()){
35 return redirect(route('admin.home.white')); 35 return redirect(route('admin.home.white'));
36 } 36 }
37 return view('admin.login'); 37 return view('admin.login');
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
3 namespace App\Http\Controllers\Aside; 3 namespace App\Http\Controllers\Aside;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 -use App\Http\Controllers\Bside\BaseController;  
7 6
8 use App\Models\Project as ProjectModel; 7 use App\Models\Project as ProjectModel;
9 8
@@ -6,8 +6,9 @@ namespace App\Http\Logic\Aside\Devops; @@ -6,8 +6,9 @@ namespace App\Http\Logic\Aside\Devops;
6 6
7 use App\Http\Logic\Aside\BaseLogic; 7 use App\Http\Logic\Aside\BaseLogic;
8 use App\Http\Logic\Aside\Project\ProjectLogic; 8 use App\Http\Logic\Aside\Project\ProjectLogic;
  9 +use App\Models\Devops\DevopsTask;
9 use App\Models\Project; 10 use App\Models\Project;
10 -use App\Models\ServerConfig; 11 +use App\Models\Devops\ServerConfig;
11 use App\Services\ProjectServer; 12 use App\Services\ProjectServer;
12 use Illuminate\Support\Facades\DB; 13 use Illuminate\Support\Facades\DB;
13 14
@@ -29,25 +30,29 @@ class ServerConfigLogic extends BaseLogic @@ -29,25 +30,29 @@ class ServerConfigLogic extends BaseLogic
29 30
30 public function save($param) 31 public function save($param)
31 { 32 {
32 - $project = ProjectServer::useProject($param['project_id']); 33 + $project_logic = new ProjectLogic();
  34 + $project = $project_logic->getCacheInfo($param['project_id']);
33 if(!$project){ 35 if(!$project){
34 - $this->fail('项目不存在或者已经删'); 36 + $this->fail('项目不存在或者已经删');
35 } 37 }
36 DB::beginTransaction(); 38 DB::beginTransaction();
37 try { 39 try {
  40 + //保存配置
38 $res = parent::save($param); 41 $res = parent::save($param);
39 42
  43 + //关联到项目
40 $data['id'] = $param['project_id']; 44 $data['id'] = $param['project_id'];
41 if ($param['type'] == ServerConfig::TYPE_SERVER) { 45 if ($param['type'] == ServerConfig::TYPE_SERVER) {
42 $data['serve_id'] = $res['id']; 46 $data['serve_id'] = $res['id'];
43 }else{ 47 }else{
44 $data['mysql_id'] = $res['id']; 48 $data['mysql_id'] = $res['id'];
45 } 49 }
46 -  
47 - (new ProjectLogic())->save($data); 50 + $project_logic->save($data);
48 51
49 //初始化数据库 52 //初始化数据库
50 if ($param['type'] == ServerConfig::TYPE_MYSQL) { 53 if ($param['type'] == ServerConfig::TYPE_MYSQL) {
  54 + //切换数据库配置
  55 + $project = ProjectServer::useProject($param['project_id']);
51 //创建数据库 56 //创建数据库
52 ProjectServer::createDatabase($project); 57 ProjectServer::createDatabase($project);
53 //创建表 58 //创建表
@@ -73,9 +78,8 @@ class ServerConfigLogic extends BaseLogic @@ -73,9 +78,8 @@ class ServerConfigLogic extends BaseLogic
73 public function updateTable($param){ 78 public function updateTable($param){
74 $project = ProjectServer::useProject($param['project_id']); 79 $project = ProjectServer::useProject($param['project_id']);
75 if(!$project){ 80 if(!$project){
76 - $this->fail('项目不存在或者已经删'); 81 + $this->fail('项目不存在或数据库未配置');
77 } 82 }
78 - ProjectServer::useProject($param['project_id']);  
79 DB::connection('custom_mysql')->statement($param['sql']); 83 DB::connection('custom_mysql')->statement($param['sql']);
80 return $this->success(); 84 return $this->success();
81 } 85 }
@@ -100,19 +104,7 @@ class ServerConfigLogic extends BaseLogic @@ -100,19 +104,7 @@ class ServerConfigLogic extends BaseLogic
100 * @date 2023/4/24 104 * @date 2023/4/24
101 */ 105 */
102 public function updateAllTable($param){ 106 public function updateAllTable($param){
103 - $projects = Project::all();  
104 - foreach ($projects as $project){  
105 - //DB类是单例模式,不能用哈 切换配置不会生效  
106 - $conn = new \mysqli(  
107 - $project->mysqlConfig->host,  
108 - $project->mysqlConfig->user,  
109 - $project->mysqlConfig->password,  
110 - $project->databaseName(),  
111 - $project->mysqlConfig->port,  
112 - );  
113 -  
114 - $conn->query($param['sql']);  
115 - } 107 + DevopsTask::addTask($param['sql']);
116 return $this->success(); 108 return $this->success();
117 } 109 }
118 } 110 }
@@ -48,4 +48,12 @@ class LoginLogic extends BaseLogic @@ -48,4 +48,12 @@ class LoginLogic extends BaseLogic
48 Session::forget('manage'); 48 Session::forget('manage');
49 return redirect(route('admin.login')); 49 return redirect(route('admin.login'));
50 } 50 }
  51 +
  52 + public static function manage($field = ''){
  53 + $manage = Session::get('manage');
  54 + if($field){
  55 + return $manage[$field] ?? '';
  56 + }
  57 + return $manage;
  58 + }
51 } 59 }
@@ -5,6 +5,7 @@ namespace App\Http\Middleware\Bside; @@ -5,6 +5,7 @@ namespace App\Http\Middleware\Bside;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Models\User\ProjectMenu; 6 use App\Models\User\ProjectMenu;
7 use App\Models\User\ProjectRole as ProjectRoleModel; 7 use App\Models\User\ProjectRole as ProjectRoleModel;
  8 +use App\Services\ProjectServer;
8 use Closure; 9 use Closure;
9 use Illuminate\Http\Request; 10 use Illuminate\Http\Request;
10 use Illuminate\Support\Facades\Cache; 11 use Illuminate\Support\Facades\Cache;
@@ -29,7 +30,10 @@ class LoginAuthMiddleware @@ -29,7 +30,10 @@ class LoginAuthMiddleware
29 return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']); 30 return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
30 } 31 }
31 // 设置数据信息 32 // 设置数据信息
32 -// ProjectServer::useProject($info['project_id']); 33 + $project = ProjectServer::useProject($info['project_id']);
  34 + if($project){
  35 + return response(['code'=>Code::USER_ERROR,'msg'=>'数据库未配置']);
  36 + }
33 //操作权限设置 37 //操作权限设置
34 $projectRoleModel = new ProjectRoleModel(); 38 $projectRoleModel = new ProjectRoleModel();
35 $role_info = $projectRoleModel->read(['id'=>$info['role_id']]); 39 $role_info = $projectRoleModel->read(['id'=>$info['role_id']]);
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 2
3 namespace App\Http\Requests\Aside\Devops; 3 namespace App\Http\Requests\Aside\Devops;
4 4
5 -use App\Models\ServerConfig; 5 +use App\Models\Devops\ServerConfig;
6 use Illuminate\Foundation\Http\FormRequest; 6 use Illuminate\Foundation\Http\FormRequest;
7 use Illuminate\Validation\Rule; 7 use Illuminate\Validation\Rule;
8 8
  1 +<?php
  2 +
  3 +namespace App\Models\Devops;
  4 +
  5 +use App\Http\Logic\Aside\LoginLogic;
  6 +use App\Models\Base;
  7 +
  8 +/**
  9 + * 运维任务
  10 + * Class DevopsTask
  11 + * @package App\Models\Devops
  12 + * @author zbj
  13 + * @date 2023/4/25
  14 + */
  15 +class DevopsTask extends Base
  16 +{
  17 + /**
  18 + * @var string
  19 + */
  20 + protected $table = 'gl_devops_task';
  21 +
  22 +
  23 + /**
  24 + * 1:数据库更新, 2:代码更新
  25 + */
  26 + const TYPE_MYSQL = 1;
  27 + const TYPE_CODE = 2;
  28 +
  29 + const STATUS_PENDING = 0;
  30 + const STATUS_ACTIVE = 1;
  31 +
  32 + /**
  33 + * @param $sql
  34 + * @param int $type
  35 + * @return mixed
  36 + * @author zbj
  37 + * @date 2023/4/25
  38 + */
  39 + public static function addTask($sql, int $type = self::TYPE_MYSQL){
  40 + $model = new static();
  41 + $model->type = $type;
  42 + $model->sql = $sql;
  43 + $model->manage_id = intval(LoginLogic::manage('id'));
  44 + $model->save();
  45 + return $model->id;
  46 + }
  47 +
  48 +
  49 +}
  1 +<?php
  2 +
  3 +namespace App\Models\Devops;
  4 +
  5 +use App\Http\Logic\Aside\LoginLogic;
  6 +use App\Models\Base;
  7 +
  8 +/**
  9 + * 运维任务
  10 + * Class DevopsTask
  11 + * @package App\Models\Devops
  12 + * @author zbj
  13 + * @date 2023/4/25
  14 + */
  15 +class DevopsTaskLog extends Base
  16 +{
  17 + /**
  18 + * @var string
  19 + */
  20 + protected $table = 'gl_devops_task_log';
  21 +
  22 + const STATUS_PENDING = 0;
  23 + const STATUS_ACTIVE = 1;
  24 + const STATUS_ERROR = 2;
  25 +
  26 + /**
  27 + * @param $task_id
  28 + * @param $project_id
  29 + * @return mixed
  30 + * @author zbj
  31 + * @date 2023/4/25
  32 + */
  33 + public static function addLog($task_id, $project_id)
  34 + {
  35 + $log = self::where('task_id', $task_id)->where('project_id', $project_id)->first();
  36 + if (!$log) {
  37 + $log = new self();
  38 + $log->task_id = $task_id;
  39 + $log->project_id = $project_id;
  40 + $log->save();
  41 + }
  42 + return $log;
  43 + }
  44 +
  45 +
  46 +}
@@ -6,7 +6,9 @@ @@ -6,7 +6,9 @@
6 * Time: 10:04 6 * Time: 10:04
7 */ 7 */
8 8
9 -namespace App\Models; 9 +namespace App\Models\Devops;
  10 +
  11 +use App\Models\Base;
10 12
11 /** 13 /**
12 * 服务账户信息 14 * 服务账户信息
@@ -27,6 +27,10 @@ class ProjectServer extends BaseService @@ -27,6 +27,10 @@ class ProjectServer extends BaseService
27 $project = Project::getProjectById($project_id); 27 $project = Project::getProjectById($project_id);
28 if (empty($project)) 28 if (empty($project))
29 return false; 29 return false;
  30 +
  31 + if(!$project->mysqlConfig){
  32 + return false;
  33 + }
30 // 设置 database.connections.custom_mysql 配置 34 // 设置 database.connections.custom_mysql 配置
31 config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]); 35 config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]);
32 config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]); 36 config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]);