2022-10-20 18:45:47 +08:00
package service
import (
2022-11-09 15:08:38 +08:00
"bufio"
2022-11-18 17:50:52 +08:00
"context"
2022-10-21 18:50:38 +08:00
"encoding/json"
"fmt"
2022-11-04 19:02:15 +08:00
"io/ioutil"
2022-10-27 23:09:39 +08:00
"os"
2022-10-25 18:34:33 +08:00
"os/exec"
2022-11-04 19:02:15 +08:00
"regexp"
2022-10-24 18:46:19 +08:00
"strconv"
2022-10-25 18:34:33 +08:00
"strings"
2022-10-24 18:46:19 +08:00
"time"
2022-10-21 18:50:38 +08:00
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-10-25 18:34:33 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
2023-03-07 16:44:16 +08:00
"github.com/1Panel-dev/1Panel/backend/buserr"
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/constant"
2022-10-27 23:09:39 +08:00
"github.com/1Panel-dev/1Panel/backend/global"
2022-12-23 18:00:19 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/common"
2022-11-04 19:02:15 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/compose"
2022-10-21 18:50:38 +08:00
_ "github.com/go-sql-driver/mysql"
2022-10-20 18:45:47 +08:00
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
type MysqlService struct { }
type IMysqlService interface {
2023-02-07 18:48:32 +08:00
SearchWithPage ( search dto . SearchWithPage ) ( int64 , interface { } , error )
2022-11-18 16:14:23 +08:00
ListDBName ( ) ( [ ] string , error )
2022-12-28 18:35:53 +08:00
Create ( ctx context . Context , req dto . MysqlDBCreate ) ( * model . DatabaseMysql , error )
2022-12-08 19:07:32 +08:00
ChangeAccess ( info dto . ChangeDBInfo ) error
ChangePassword ( info dto . ChangeDBInfo ) error
2022-11-18 16:14:23 +08:00
UpdateVariables ( updatas [ ] dto . MysqlVariablesUpdate ) error
2022-11-09 15:08:38 +08:00
UpdateConfByFile ( info dto . MysqlConfUpdateByFile ) error
2023-02-13 15:48:18 +08:00
UpdateDescription ( req dto . UpdateDescription ) error
2022-12-04 17:28:03 +08:00
DeleteCheck ( id uint ) ( [ ] string , error )
2022-12-27 16:30:25 +08:00
Delete ( ctx context . Context , req dto . MysqlDBDelete ) error
2022-11-18 16:14:23 +08:00
LoadStatus ( ) ( * dto . MysqlStatus , error )
LoadVariables ( ) ( * dto . MysqlVariables , error )
LoadBaseInfo ( ) ( * dto . DBBaseInfo , error )
2022-12-09 14:07:18 +08:00
LoadRemoteAccess ( ) ( bool , error )
2022-10-20 18:45:47 +08:00
}
func NewIMysqlService ( ) IMysqlService {
return & MysqlService { }
}
2023-02-07 18:48:32 +08:00
func ( u * MysqlService ) SearchWithPage ( search dto . SearchWithPage ) ( int64 , interface { } , error ) {
total , mysqls , err := mysqlRepo . Page ( search . Page , search . PageSize , commonRepo . WithLikeName ( search . Info ) )
2022-10-20 18:45:47 +08:00
var dtoMysqls [ ] dto . MysqlDBInfo
for _ , mysql := range mysqls {
var item dto . MysqlDBInfo
if err := copier . Copy ( & item , & mysql ) ; err != nil {
return 0 , nil , errors . WithMessage ( constant . ErrStructTransform , err . Error ( ) )
}
dtoMysqls = append ( dtoMysqls , item )
}
return total , dtoMysqls , err
}
2022-11-18 16:14:23 +08:00
func ( u * MysqlService ) ListDBName ( ) ( [ ] string , error ) {
mysqls , err := mysqlRepo . List ( )
2022-10-28 18:46:14 +08:00
var dbNames [ ] string
for _ , mysql := range mysqls {
dbNames = append ( dbNames , mysql . Name )
}
return dbNames , err
}
2023-01-03 18:32:02 +08:00
var formatMap = map [ string ] string {
"utf8" : "utf8_general_ci" ,
"utf8mb4" : "utf8mb4_general_ci" ,
"gbk" : "gbk_chinese_ci" ,
"big5" : "big5_chinese_ci" ,
}
2022-12-28 18:35:53 +08:00
func ( u * MysqlService ) Create ( ctx context . Context , req dto . MysqlDBCreate ) ( * model . DatabaseMysql , error ) {
if req . Username == "root" {
2022-12-21 15:54:34 +08:00
return nil , errors . New ( "Cannot set root as user name" )
2022-10-24 18:46:19 +08:00
}
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-11-09 15:08:38 +08:00
if err != nil {
2022-12-21 15:54:34 +08:00
return nil , err
2022-11-09 15:08:38 +08:00
}
2022-12-28 18:35:53 +08:00
mysql , _ := mysqlRepo . Get ( commonRepo . WithByName ( req . Name ) )
2022-10-20 18:45:47 +08:00
if mysql . ID != 0 {
2022-12-21 15:54:34 +08:00
return nil , constant . ErrRecordExist
2022-10-20 18:45:47 +08:00
}
2022-12-28 18:35:53 +08:00
if err := copier . Copy ( & mysql , & req ) ; err != nil {
2022-12-21 15:54:34 +08:00
return nil , errors . WithMessage ( constant . ErrStructTransform , err . Error ( ) )
2022-10-20 18:45:47 +08:00
}
2022-10-25 18:34:33 +08:00
2023-03-07 16:44:16 +08:00
createSql := fmt . Sprintf ( "create database `%s` default character set %s collate %s" , req . Name , req . Format , formatMap [ req . Format ] )
2023-01-03 18:32:02 +08:00
if err := excuteSql ( app . ContainerName , app . Password , createSql ) ; err != nil {
2023-03-07 16:44:16 +08:00
if strings . Contains ( err . Error ( ) , "ERROR 1007" ) {
return nil , buserr . New ( constant . ErrDatabaseIsExist )
}
2022-12-23 18:00:19 +08:00
return nil , err
2022-10-24 18:46:19 +08:00
}
2022-12-28 18:35:53 +08:00
tmpPermission := req . Permission
2023-03-07 16:44:16 +08:00
if err := excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "create user '%s'@'%s' identified by '%s';" , req . Username , tmpPermission , req . Password ) ) ; err != nil {
_ = excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "drop database `%s`" , req . Name ) )
if strings . Contains ( err . Error ( ) , "ERROR 1396" ) {
return nil , buserr . New ( constant . ErrUserIsExist )
}
2022-12-21 15:54:34 +08:00
return nil , err
2022-10-24 18:46:19 +08:00
}
2022-12-28 18:35:53 +08:00
grantStr := fmt . Sprintf ( "grant all privileges on `%s`.* to '%s'@'%s'" , req . Name , req . Username , tmpPermission )
2022-11-18 16:14:23 +08:00
if app . Version == "5.7.39" {
2022-12-28 18:35:53 +08:00
grantStr = fmt . Sprintf ( "%s identified by '%s' with grant option;" , grantStr , req . Password )
2022-10-24 18:46:19 +08:00
}
2022-10-25 18:34:33 +08:00
if err := excuteSql ( app . ContainerName , app . Password , grantStr ) ; err != nil {
2022-12-21 15:54:34 +08:00
return nil , err
2022-10-24 18:46:19 +08:00
}
2022-12-28 18:35:53 +08:00
global . LOG . Infof ( "create database %s successful!" , req . Name )
2022-11-18 16:14:23 +08:00
mysql . MysqlName = app . Name
2022-12-21 15:54:34 +08:00
if err := mysqlRepo . Create ( ctx , & mysql ) ; err != nil {
return nil , err
2022-10-20 18:45:47 +08:00
}
2022-12-21 15:54:34 +08:00
return & mysql , nil
2022-10-20 18:45:47 +08:00
}
2023-02-13 15:48:18 +08:00
func ( u * MysqlService ) UpdateDescription ( req dto . UpdateDescription ) error {
2022-12-24 13:31:30 +08:00
return mysqlRepo . Update ( req . ID , map [ string ] interface { } { "description" : req . Description } )
}
2022-12-04 17:28:03 +08:00
func ( u * MysqlService ) DeleteCheck ( id uint ) ( [ ] string , error ) {
var appInUsed [ ] string
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-12-04 17:28:03 +08:00
if err != nil {
return appInUsed , err
}
db , err := mysqlRepo . Get ( commonRepo . WithByID ( id ) )
if err != nil {
return appInUsed , err
}
2022-12-12 14:06:39 +08:00
apps , _ := appInstallResourceRepo . GetBy ( appInstallResourceRepo . WithLinkId ( app . ID ) , appInstallResourceRepo . WithResourceId ( db . ID ) )
2022-12-04 17:28:03 +08:00
for _ , app := range apps {
2022-12-04 19:20:36 +08:00
appInstall , _ := appInstallRepo . GetFirst ( commonRepo . WithByID ( app . AppInstallId ) )
2022-12-04 17:28:03 +08:00
if appInstall . ID != 0 {
appInUsed = append ( appInUsed , appInstall . Name )
}
}
return appInUsed , nil
}
2022-12-27 16:30:25 +08:00
func ( u * MysqlService ) Delete ( ctx context . Context , req dto . MysqlDBDelete ) error {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-12-26 14:47:08 +08:00
if err != nil && ! req . ForceDelete {
2022-10-24 18:46:19 +08:00
return err
}
2022-10-25 18:34:33 +08:00
2022-12-26 14:47:08 +08:00
db , err := mysqlRepo . Get ( commonRepo . WithByID ( req . ID ) )
if err != nil && ! req . ForceDelete {
2022-10-24 18:46:19 +08:00
return err
}
2023-03-08 23:58:42 +08:00
if err := excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "drop user if exists '%s'@'%s'" , db . Username , db . Permission ) ) ; err != nil && ! req . ForceDelete {
2022-12-04 17:28:03 +08:00
return err
}
2022-12-26 14:47:08 +08:00
if err := excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "drop database if exists `%s`" , db . Name ) ) ; err != nil && ! req . ForceDelete {
2022-12-04 17:28:03 +08:00
return err
2022-10-20 18:45:47 +08:00
}
2022-12-28 18:35:53 +08:00
global . LOG . Info ( "execute delete database sql successful, now start to drop uploads and records" )
2022-12-09 14:07:18 +08:00
2023-03-02 13:54:07 +08:00
uploadDir := fmt . Sprintf ( "%s/1panel/uploads/database/mysql/%s/%s" , global . CONF . System . BaseDir , app . Name , db . Name )
2022-12-09 14:07:18 +08:00
if _ , err := os . Stat ( uploadDir ) ; err == nil {
_ = os . RemoveAll ( uploadDir )
}
2022-12-26 14:47:08 +08:00
if req . DeleteBackup {
localDir , err := loadLocalDir ( )
if err != nil && ! req . ForceDelete {
return err
}
backupDir := fmt . Sprintf ( "%s/database/mysql/%s/%s" , localDir , db . MysqlName , db . Name )
if _ , err := os . Stat ( backupDir ) ; err == nil {
_ = os . RemoveAll ( backupDir )
}
2022-12-28 18:35:53 +08:00
global . LOG . Infof ( "delete database %s-%s backups successful" , app . Name , db . Name )
2022-12-09 14:07:18 +08:00
}
2023-02-21 19:06:24 +08:00
_ = backupRepo . DeleteRecord ( ctx , commonRepo . WithByType ( "mysql" ) , commonRepo . WithByName ( app . Name ) , backupRepo . WithByDetailName ( db . Name ) )
2022-12-09 14:07:18 +08:00
2022-12-27 16:30:25 +08:00
_ = mysqlRepo . Delete ( ctx , commonRepo . WithByID ( db . ID ) )
2022-10-24 18:46:19 +08:00
return nil
2022-10-20 18:45:47 +08:00
}
2022-10-21 18:50:38 +08:00
2022-12-08 19:07:32 +08:00
func ( u * MysqlService ) ChangePassword ( info dto . ChangeDBInfo ) error {
2022-10-25 18:34:33 +08:00
var (
mysql model . DatabaseMysql
err error
)
if info . ID != 0 {
mysql , err = mysqlRepo . Get ( commonRepo . WithByID ( info . ID ) )
if err != nil {
return err
}
2022-10-24 18:46:19 +08:00
}
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-10-24 18:46:19 +08:00
if err != nil {
return err
2022-10-21 18:50:38 +08:00
}
2022-12-08 19:07:32 +08:00
passwordChangeCMD := fmt . Sprintf ( "set password for '%s'@'%s' = password('%s')" , mysql . Username , mysql . Permission , info . Value )
if app . Version != "5.7.39" {
passwordChangeCMD = fmt . Sprintf ( "ALTER USER '%s'@'%s' IDENTIFIED WITH mysql_native_password BY '%s';" , mysql . Username , mysql . Permission , info . Value )
}
if info . ID != 0 {
2022-12-21 12:21:27 +08:00
appRess , _ := appInstallResourceRepo . GetBy ( appInstallResourceRepo . WithLinkId ( app . ID ) , appInstallResourceRepo . WithResourceId ( mysql . ID ) )
for _ , appRes := range appRess {
appInstall , err := appInstallRepo . GetFirst ( commonRepo . WithByID ( appRes . AppInstallId ) )
if err != nil {
return err
}
appModel , err := appRepo . GetFirst ( commonRepo . WithByID ( appInstall . AppId ) )
if err != nil {
return err
}
2022-12-28 18:35:53 +08:00
global . LOG . Infof ( "start to update mysql password used by app %s-%s" , appModel . Key , appInstall . Name )
2022-12-23 18:00:19 +08:00
if err := updateInstallInfoInDB ( appModel . Key , appInstall . Name , "user-password" , true , info . Value ) ; err != nil {
2022-12-21 12:21:27 +08:00
return err
}
}
2022-12-08 19:07:32 +08:00
if err := excuteSql ( app . ContainerName , app . Password , passwordChangeCMD ) ; err != nil {
return err
}
2022-12-28 18:35:53 +08:00
global . LOG . Info ( "excute password change sql successful" )
2022-12-08 19:07:32 +08:00
_ = mysqlRepo . Update ( mysql . ID , map [ string ] interface { } { "password" : info . Value } )
return nil
}
hosts , err := excuteSqlForRows ( app . ContainerName , app . Password , "select host from mysql.user where user='root';" )
if err != nil {
return err
}
for _ , host := range hosts {
if host == "%" || host == "localhost" {
passwordRootChangeCMD := fmt . Sprintf ( "set password for 'root'@'%s' = password('%s')" , host , info . Value )
if app . Version != "5.7.39" {
2022-12-28 18:35:53 +08:00
passwordRootChangeCMD = fmt . Sprintf ( "alter user 'root'@'%s' identified with mysql_native_password BY '%s';" , host , info . Value )
2022-12-08 19:07:32 +08:00
}
if err := excuteSql ( app . ContainerName , app . Password , passwordRootChangeCMD ) ; err != nil {
2022-10-25 18:34:33 +08:00
return err
}
}
2022-12-08 19:07:32 +08:00
}
2022-12-21 12:21:27 +08:00
if err := updateInstallInfoInDB ( "mysql" , "" , "password" , false , info . Value ) ; err != nil {
2022-12-12 19:04:45 +08:00
return err
}
2022-12-21 12:21:27 +08:00
if err := updateInstallInfoInDB ( "phpmyadmin" , "" , "password" , true , info . Value ) ; err != nil {
2022-12-12 19:04:45 +08:00
return err
}
2022-12-08 19:07:32 +08:00
return nil
}
func ( u * MysqlService ) ChangeAccess ( info dto . ChangeDBInfo ) error {
var (
mysql model . DatabaseMysql
err error
)
if info . ID != 0 {
mysql , err = mysqlRepo . Get ( commonRepo . WithByID ( info . ID ) )
2022-10-25 18:34:33 +08:00
if err != nil {
2022-10-24 18:46:19 +08:00
return err
}
}
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-12-08 19:07:32 +08:00
if err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if info . ID == 0 {
mysql . Name = "*"
mysql . Username = "root"
mysql . Permission = "%"
mysql . Password = app . Password
}
if info . Value != mysql . Permission {
if err := excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "drop user if exists '%s'@'%s'" , mysql . Username , mysql . Permission ) ) ; err != nil {
return err
}
if info . ID == 0 {
return nil
}
}
if err := excuteSql ( app . ContainerName , app . Password , fmt . Sprintf ( "create user if not exists '%s'@'%s' identified by '%s';" , mysql . Username , info . Value , mysql . Password ) ) ; err != nil {
2022-10-24 18:46:19 +08:00
return err
}
2023-03-17 19:41:35 +08:00
grantStr := fmt . Sprintf ( "grant all privileges on `%s`.* to '%s'@'%s'" , mysql . Name , mysql . Username , info . Value )
2022-11-18 16:14:23 +08:00
if app . Version == "5.7.39" {
2022-10-25 18:34:33 +08:00
grantStr = fmt . Sprintf ( "%s identified by '%s' with grant option;" , grantStr , mysql . Password )
2022-10-24 18:46:19 +08:00
}
2022-10-25 18:34:33 +08:00
if err := excuteSql ( app . ContainerName , app . Password , grantStr ) ; err != nil {
2022-10-24 18:46:19 +08:00
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql ( app . ContainerName , app . Password , "flush privileges" ) ; err != nil {
2022-10-24 18:46:19 +08:00
return err
}
2022-10-25 18:34:33 +08:00
if info . ID == 0 {
return nil
}
2022-10-24 18:46:19 +08:00
_ = mysqlRepo . Update ( mysql . ID , map [ string ] interface { } { "permission" : info . Value } )
return nil
}
2022-11-09 15:08:38 +08:00
func ( u * MysqlService ) UpdateConfByFile ( info dto . MysqlConfUpdateByFile ) error {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-11-09 15:08:38 +08:00
if err != nil {
return err
}
2022-11-18 16:14:23 +08:00
path := fmt . Sprintf ( "%s/mysql/%s/conf/my.cnf" , constant . AppInstallDir , app . Name )
2022-11-09 15:08:38 +08:00
file , err := os . OpenFile ( path , os . O_WRONLY | os . O_TRUNC , 0640 )
if err != nil {
return err
}
defer file . Close ( )
write := bufio . NewWriter ( file )
_ , _ = write . WriteString ( info . File )
write . Flush ( )
2022-11-18 16:14:23 +08:00
if _ , err := compose . Restart ( fmt . Sprintf ( "%s/mysql/%s/docker-compose.yml" , constant . AppInstallDir , app . Name ) ) ; err != nil {
2022-11-09 15:08:38 +08:00
return err
}
return nil
}
2022-11-18 16:14:23 +08:00
func ( u * MysqlService ) UpdateVariables ( updatas [ ] dto . MysqlVariablesUpdate ) error {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-10-25 11:41:19 +08:00
if err != nil {
return err
}
2022-11-04 19:02:15 +08:00
var files [ ] string
2022-10-25 11:41:19 +08:00
2022-11-18 16:14:23 +08:00
path := fmt . Sprintf ( "%s/mysql/%s/conf/my.cnf" , constant . AppInstallDir , app . Name )
2022-11-04 19:02:15 +08:00
lineBytes , err := ioutil . ReadFile ( path )
if err != nil {
2022-10-25 11:41:19 +08:00
return err
2022-11-04 19:02:15 +08:00
}
2022-12-21 16:21:28 +08:00
files = strings . Split ( string ( lineBytes ) , "\n" )
2022-12-08 18:09:14 +08:00
group := "[mysqld]"
2022-11-04 19:02:15 +08:00
for _ , info := range updatas {
2022-12-09 14:07:18 +08:00
if app . Version != "5.7.39" {
if info . Param == "query_cache_size" {
continue
}
}
2022-12-23 18:00:19 +08:00
if _ , ok := info . Value . ( float64 ) ; ok {
files = updateMyCnf ( files , group , info . Param , common . LoadSizeUnit ( info . Value . ( float64 ) ) )
2022-12-20 15:23:48 +08:00
} else {
files = updateMyCnf ( files , group , info . Param , info . Value )
}
2022-10-25 11:41:19 +08:00
}
2022-12-12 14:06:39 +08:00
file , err := os . OpenFile ( path , os . O_WRONLY | os . O_TRUNC , 0666 )
2022-11-04 19:02:15 +08:00
if err != nil {
2022-10-25 11:41:19 +08:00
return err
}
2022-11-04 19:02:15 +08:00
defer file . Close ( )
_ , err = file . WriteString ( strings . Join ( files , "\n" ) )
if err != nil {
2022-10-25 11:41:19 +08:00
return err
}
2022-11-18 16:14:23 +08:00
if _ , err := compose . Restart ( fmt . Sprintf ( "%s/mysql/%s/docker-compose.yml" , constant . AppInstallDir , app . Name ) ) ; err != nil {
2022-10-25 11:41:19 +08:00
return err
}
return nil
}
2022-11-18 16:14:23 +08:00
func ( u * MysqlService ) LoadBaseInfo ( ) ( * dto . DBBaseInfo , error ) {
2022-10-25 18:34:33 +08:00
var data dto . DBBaseInfo
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-10-21 18:50:38 +08:00
if err != nil {
return nil , err
}
2022-11-04 19:02:15 +08:00
data . ContainerName = app . ContainerName
2022-10-25 18:34:33 +08:00
data . Name = app . Name
data . Port = int64 ( app . Port )
2022-10-21 18:50:38 +08:00
2022-12-09 14:07:18 +08:00
return & data , nil
}
func ( u * MysqlService ) LoadRemoteAccess ( ) ( bool , error ) {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-12-09 14:07:18 +08:00
if err != nil {
return false , err
}
2022-10-25 18:34:33 +08:00
hosts , err := excuteSqlForRows ( app . ContainerName , app . Password , "select host from mysql.user where user='root';" )
if err != nil {
2022-12-09 14:07:18 +08:00
return false , err
2022-10-25 18:34:33 +08:00
}
for _ , host := range hosts {
if host == "%" {
2022-12-09 14:07:18 +08:00
return true , nil
2022-10-25 18:34:33 +08:00
}
}
2022-12-09 14:07:18 +08:00
return false , nil
2022-10-25 18:34:33 +08:00
}
2022-11-18 16:14:23 +08:00
func ( u * MysqlService ) LoadVariables ( ) ( * dto . MysqlVariables , error ) {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-10-25 18:34:33 +08:00
if err != nil {
return nil , err
}
variableMap , err := excuteSqlForMaps ( app . ContainerName , app . Password , "show global variables;" )
2022-10-24 18:46:19 +08:00
if err != nil {
return nil , err
}
var info dto . MysqlVariables
2022-10-21 18:50:38 +08:00
arr , err := json . Marshal ( variableMap )
if err != nil {
return nil , err
}
_ = json . Unmarshal ( arr , & info )
return & info , nil
}
2022-11-18 16:14:23 +08:00
func ( u * MysqlService ) LoadStatus ( ) ( * dto . MysqlStatus , error ) {
2022-12-21 12:21:27 +08:00
app , err := appInstallRepo . LoadBaseInfo ( "mysql" , "" )
2022-10-21 18:50:38 +08:00
if err != nil {
return nil , err
}
2022-10-25 18:34:33 +08:00
statusMap , err := excuteSqlForMaps ( app . ContainerName , app . Password , "show global status;" )
2022-10-21 18:50:38 +08:00
if err != nil {
return nil , err
}
2022-10-25 18:34:33 +08:00
2022-10-21 18:50:38 +08:00
var info dto . MysqlStatus
2022-10-25 18:34:33 +08:00
arr , err := json . Marshal ( statusMap )
2022-10-21 18:50:38 +08:00
if err != nil {
return nil , err
}
_ = json . Unmarshal ( arr , & info )
2022-10-24 18:46:19 +08:00
2022-10-25 18:34:33 +08:00
if value , ok := statusMap [ "Run" ] ; ok {
2022-10-24 18:46:19 +08:00
uptime , _ := strconv . Atoi ( value )
info . Run = time . Unix ( time . Now ( ) . Unix ( ) - int64 ( uptime ) , 0 ) . Format ( "2006-01-02 15:04:05" )
} else {
2022-10-25 18:34:33 +08:00
if value , ok := statusMap [ "Uptime" ] ; ok {
2022-10-24 18:46:19 +08:00
uptime , _ := strconv . Atoi ( value )
info . Run = time . Unix ( time . Now ( ) . Unix ( ) - int64 ( uptime ) , 0 ) . Format ( "2006-01-02 15:04:05" )
}
}
2022-10-25 18:34:33 +08:00
info . File = "OFF"
info . Position = "OFF"
rows , err := excuteSqlForRows ( app . ContainerName , app . Password , "show master status;" )
if err != nil {
return nil , err
}
if len ( rows ) > 2 {
itemValue := strings . Split ( rows [ 1 ] , "\t" )
if len ( itemValue ) > 2 {
info . File = itemValue [ 0 ]
info . Position = itemValue [ 1 ]
}
}
return & info , nil
}
func excuteSqlForMaps ( containerName , password , command string ) ( map [ string ] string , error ) {
2023-02-15 18:05:23 +08:00
cmd := exec . Command ( "docker" , "exec" , containerName , "mysql" , "-uroot" , "-p" + password , "-e" , command )
stdout , err := cmd . CombinedOutput ( )
2022-10-27 23:09:39 +08:00
stdStr := strings . ReplaceAll ( string ( stdout ) , "mysql: [Warning] Using a password on the command line interface can be insecure.\n" , "" )
if err != nil || strings . HasPrefix ( string ( stdStr ) , "ERROR " ) {
return nil , errors . New ( stdStr )
2022-10-24 18:46:19 +08:00
}
2022-10-25 18:34:33 +08:00
rows := strings . Split ( stdStr , "\n" )
rowMap := make ( map [ string ] string )
for _ , v := range rows {
itemRow := strings . Split ( v , "\t" )
if len ( itemRow ) == 2 {
rowMap [ itemRow [ 0 ] ] = itemRow [ 1 ]
2022-10-24 18:46:19 +08:00
}
}
2022-10-25 18:34:33 +08:00
return rowMap , nil
}
func excuteSqlForRows ( containerName , password , command string ) ( [ ] string , error ) {
2023-02-15 18:05:23 +08:00
cmd := exec . Command ( "docker" , "exec" , containerName , "mysql" , "-uroot" , "-p" + password , "-e" , command )
stdout , err := cmd . CombinedOutput ( )
2022-10-25 18:34:33 +08:00
stdStr := strings . ReplaceAll ( string ( stdout ) , "mysql: [Warning] Using a password on the command line interface can be insecure.\n" , "" )
2022-10-27 23:09:39 +08:00
if err != nil || strings . HasPrefix ( string ( stdStr ) , "ERROR " ) {
return nil , errors . New ( stdStr )
}
2022-10-25 18:34:33 +08:00
return strings . Split ( stdStr , "\n" ) , nil
}
func excuteSql ( containerName , password , command string ) error {
2023-02-15 18:05:23 +08:00
cmd := exec . Command ( "docker" , "exec" , containerName , "mysql" , "-uroot" , "-p" + password , "-e" , command )
stdout , err := cmd . CombinedOutput ( )
2022-10-25 18:34:33 +08:00
stdStr := strings . ReplaceAll ( string ( stdout ) , "mysql: [Warning] Using a password on the command line interface can be insecure.\n" , "" )
2022-10-27 23:09:39 +08:00
if err != nil || strings . HasPrefix ( string ( stdStr ) , "ERROR " ) {
return errors . New ( stdStr )
2022-10-25 18:34:33 +08:00
}
return nil
2022-10-21 18:50:38 +08:00
}
2022-10-28 18:46:14 +08:00
2022-11-04 19:02:15 +08:00
func updateMyCnf ( oldFiles [ ] string , group string , param string , value interface { } ) [ ] string {
isOn := false
2022-11-09 15:08:38 +08:00
hasGroup := false
2022-11-04 19:02:15 +08:00
hasKey := false
regItem , _ := regexp . Compile ( ` \[*\] ` )
var newFiles [ ] string
2022-11-09 15:08:38 +08:00
i := 0
2022-11-04 19:02:15 +08:00
for _ , line := range oldFiles {
2022-11-09 15:08:38 +08:00
i ++
2022-11-04 19:02:15 +08:00
if strings . HasPrefix ( line , group ) {
isOn = true
2022-11-09 15:08:38 +08:00
hasGroup = true
2022-11-04 19:02:15 +08:00
newFiles = append ( newFiles , line )
continue
}
if ! isOn {
newFiles = append ( newFiles , line )
continue
}
2023-03-07 18:20:52 +08:00
if strings . HasPrefix ( line , param + "=" ) || strings . HasPrefix ( line , "# " + param + "=" ) {
2022-11-04 19:02:15 +08:00
newFiles = append ( newFiles , fmt . Sprintf ( "%s=%v" , param , value ) )
hasKey = true
continue
}
2022-11-09 15:08:38 +08:00
if regItem . Match ( [ ] byte ( line ) ) || i == len ( oldFiles ) {
isOn = false
if ! hasKey {
newFiles = append ( newFiles , fmt . Sprintf ( "%s=%v" , param , value ) )
}
2022-11-04 19:02:15 +08:00
newFiles = append ( newFiles , line )
continue
}
2022-11-09 15:08:38 +08:00
newFiles = append ( newFiles , line )
2022-11-04 19:02:15 +08:00
}
2022-11-09 15:08:38 +08:00
if ! hasGroup {
2022-11-04 19:02:15 +08:00
newFiles = append ( newFiles , group + "\n" )
newFiles = append ( newFiles , fmt . Sprintf ( "%s=%v\n" , param , value ) )
}
return newFiles
}