2023-07-19 16:25:41 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-05-09 21:50:24 +08:00
|
|
|
"bytes"
|
2024-03-04 16:35:05 +08:00
|
|
|
"compress/gzip"
|
2023-07-19 16:25:41 +08:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-03-04 16:35:05 +08:00
|
|
|
"errors"
|
2023-07-19 16:25:41 +08:00
|
|
|
"fmt"
|
2023-07-25 17:08:13 +08:00
|
|
|
"os"
|
2023-08-01 11:20:16 +08:00
|
|
|
"os/exec"
|
2024-03-04 16:35:05 +08:00
|
|
|
"path"
|
2023-07-19 16:25:41 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-07-25 17:08:13 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2024-05-22 18:32:47 +08:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2024-03-04 16:35:05 +08:00
|
|
|
"github.com/docker/docker/client"
|
2023-07-19 16:25:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Remote struct {
|
2024-04-23 17:59:00 +08:00
|
|
|
Type string
|
2023-07-25 17:08:13 +08:00
|
|
|
Client *sql.DB
|
2023-08-31 17:22:12 +08:00
|
|
|
Database string
|
2023-07-25 17:08:13 +08:00
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Address string
|
|
|
|
Port uint
|
2023-11-29 10:46:07 +08:00
|
|
|
|
|
|
|
SSL bool
|
|
|
|
RootCert string
|
|
|
|
ClientKey string
|
|
|
|
ClientCert string
|
|
|
|
SkipVerify bool
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 17:08:13 +08:00
|
|
|
func NewRemote(db Remote) *Remote {
|
|
|
|
return &db
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Create(info CreateInfo) error {
|
|
|
|
createSql := fmt.Sprintf("create database `%s` default character set %s collate %s", info.Name, info.Format, formatMap[info.Format])
|
|
|
|
if err := r.ExecSQL(createSql, info.Timeout); err != nil {
|
2023-09-08 23:10:17 +08:00
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "error 1007") {
|
2023-07-19 16:25:41 +08:00
|
|
|
return buserr.New(constant.ErrDatabaseIsExist)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:32:11 +08:00
|
|
|
if err := r.CreateUser(info, true); err != nil {
|
2023-08-04 18:20:04 +08:00
|
|
|
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
2023-07-19 16:25:41 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:32:11 +08:00
|
|
|
func (r *Remote) CreateUser(info CreateInfo, withDeleteDB bool) error {
|
2023-07-19 16:25:41 +08:00
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("create user %s identified by '%s';", user, info.Password), info.Timeout); err != nil {
|
2023-09-07 21:36:10 +08:00
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "error 1396") {
|
|
|
|
return buserr.New(constant.ErrUserIsExist)
|
|
|
|
}
|
2023-08-21 11:32:11 +08:00
|
|
|
if withDeleteDB {
|
|
|
|
_ = r.Delete(DeleteInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Permission: info.Permission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300})
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-09-14 10:58:14 +08:00
|
|
|
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", info.Name, user)
|
2023-07-19 16:25:41 +08:00
|
|
|
if info.Name == "*" {
|
2023-09-14 10:58:14 +08:00
|
|
|
grantStr = fmt.Sprintf("grant all privileges on *.* to %s", user)
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(info.Version, "5.7") || strings.HasPrefix(info.Version, "5.6") {
|
|
|
|
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, info.Password)
|
2023-09-14 10:58:14 +08:00
|
|
|
} else {
|
|
|
|
grantStr = grantStr + " with grant option;"
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
if err := r.ExecSQL(grantStr, info.Timeout); err != nil {
|
2023-08-21 11:32:11 +08:00
|
|
|
if withDeleteDB {
|
|
|
|
_ = r.Delete(DeleteInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Permission: info.Permission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300})
|
|
|
|
}
|
2023-07-19 16:25:41 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Delete(info DeleteInfo) error {
|
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
if strings.HasPrefix(info.Version, "5.6") {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop user %s", user), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop user if exists %s", user), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(info.Name) != 0 {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !info.ForceDelete {
|
|
|
|
global.LOG.Info("execute delete database sql successful, now start to drop uploads and records")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ChangePassword(info PasswordChangeInfo) error {
|
2023-07-20 17:51:57 +08:00
|
|
|
if info.Username != "root" {
|
2023-07-19 16:25:41 +08:00
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 17:51:57 +08:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
passwordChangeSql := fmt.Sprintf("set password for %s = password('%s')", user, info.Password)
|
|
|
|
if !strings.HasPrefix(info.Version, "5.7") && !strings.HasPrefix(info.Version, "5.6") {
|
2023-09-01 12:28:12 +08:00
|
|
|
passwordChangeSql = fmt.Sprintf("ALTER USER %s IDENTIFIED BY '%s';", user, info.Password)
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
if err := r.ExecSQL(passwordChangeSql, info.Timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := r.ExecSQLForHosts(info.Timeout)
|
|
|
|
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.Password)
|
|
|
|
if !strings.HasPrefix(info.Version, "5.7") && !strings.HasPrefix(info.Version, "5.6") {
|
2023-08-29 10:50:15 +08:00
|
|
|
passwordRootChangeCMD = fmt.Sprintf("alter user 'root'@'%s' identified by '%s';", host, info.Password)
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
if err := r.ExecSQL(passwordRootChangeCMD, info.Timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ChangeAccess(info AccessChangeInfo) error {
|
2023-07-20 17:51:57 +08:00
|
|
|
if info.Username == "root" {
|
2023-07-19 16:25:41 +08:00
|
|
|
info.OldPermission = "%"
|
|
|
|
info.Name = "*"
|
2023-08-07 11:52:11 +08:00
|
|
|
info.Password = r.Password
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
|
|
|
if info.Permission != info.OldPermission {
|
|
|
|
if err := r.Delete(DeleteInfo{
|
|
|
|
Version: info.Version,
|
2023-07-20 17:51:57 +08:00
|
|
|
Username: info.Username,
|
2023-07-19 16:25:41 +08:00
|
|
|
Permission: info.OldPermission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-20 17:51:57 +08:00
|
|
|
if info.Username == "root" {
|
2023-07-19 16:25:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-07-21 18:28:45 +08:00
|
|
|
if err := r.CreateUser(CreateInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Password: info.Password,
|
|
|
|
Permission: info.Permission,
|
|
|
|
Timeout: info.Timeout,
|
2023-08-21 11:32:11 +08:00
|
|
|
}, false); err != nil {
|
2023-07-20 17:51:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
2023-07-25 17:08:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 14:32:23 +08:00
|
|
|
func (r *Remote) Backup(info BackupInfo) error {
|
2023-07-25 17:08:13 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(info.TargetDir) {
|
|
|
|
if err := os.MkdirAll(info.TargetDir, os.ModePerm); err != nil {
|
2023-07-27 14:32:23 +08:00
|
|
|
return fmt.Errorf("mkdir %s failed, err: %v", info.TargetDir, err)
|
2023-07-25 17:08:13 +08:00
|
|
|
}
|
|
|
|
}
|
2024-05-23 16:41:05 +08:00
|
|
|
outfile, err := os.OpenFile(path.Join(info.TargetDir, info.FileName), os.O_RDWR|os.O_CREATE, 0755)
|
2024-05-23 23:16:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open file %s failed, err: %v", path.Join(info.TargetDir, info.FileName), err)
|
2024-05-23 16:41:05 +08:00
|
|
|
}
|
2024-05-23 23:16:50 +08:00
|
|
|
defer outfile.Close()
|
2024-04-23 17:59:00 +08:00
|
|
|
dumpCmd := "mysqldump"
|
|
|
|
if r.Type == constant.AppMariaDB {
|
|
|
|
dumpCmd = "mariadb-dump"
|
|
|
|
}
|
|
|
|
global.LOG.Infof("start to %s | gzip > %s.gzip", dumpCmd, info.TargetDir+"/"+info.FileName)
|
2024-03-04 16:35:05 +08:00
|
|
|
image, err := loadImage(info.Type, info.Version)
|
2023-11-29 10:46:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-23 17:59:00 +08:00
|
|
|
backupCmd := fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c '%s -h %s -P %d -u%s -p%s %s --default-character-set=%s %s'",
|
|
|
|
image, dumpCmd, r.Address, r.Port, r.User, r.Password, sslSkip(info.Version, r.Type), info.Format, info.Name)
|
2024-03-04 16:35:05 +08:00
|
|
|
|
2024-05-30 15:25:15 +08:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(backupCmd, r.Password, "******"))
|
2024-03-04 16:35:05 +08:00
|
|
|
cmd := exec.Command("bash", "-c", backupCmd)
|
2024-05-09 21:50:24 +08:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
2024-03-04 16:35:05 +08:00
|
|
|
|
|
|
|
gzipCmd := exec.Command("gzip", "-cf")
|
|
|
|
gzipCmd.Stdin, _ = cmd.StdoutPipe()
|
|
|
|
gzipCmd.Stdout = outfile
|
2024-05-09 21:50:24 +08:00
|
|
|
|
2024-03-04 16:35:05 +08:00
|
|
|
_ = gzipCmd.Start()
|
2024-05-09 21:50:24 +08:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("handle backup database failed, err: %v", stderr.String())
|
|
|
|
}
|
2024-03-04 16:35:05 +08:00
|
|
|
_ = gzipCmd.Wait()
|
2023-07-27 14:32:23 +08:00
|
|
|
return nil
|
2023-07-25 17:08:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Recover(info RecoverInfo) error {
|
2024-03-04 16:35:05 +08:00
|
|
|
fi, _ := os.Open(info.SourceFile)
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
image, err := loadImage(info.Type, info.Version)
|
2023-11-29 10:46:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-23 17:59:00 +08:00
|
|
|
recoverCmd := fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c '%s -h %s -P %d -u%s -p%s %s --default-character-set=%s %s'",
|
|
|
|
image, r.Type, r.Address, r.Port, r.User, r.Password, sslSkip(info.Version, r.Type), info.Format, info.Name)
|
2024-03-04 16:35:05 +08:00
|
|
|
|
2024-05-30 15:25:15 +08:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(recoverCmd, r.Password, "******"))
|
2024-03-04 16:35:05 +08:00
|
|
|
cmd := exec.Command("bash", "-c", recoverCmd)
|
|
|
|
|
|
|
|
if strings.HasSuffix(info.SourceFile, ".gz") {
|
|
|
|
gzipFile, err := os.Open(info.SourceFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer gzipFile.Close()
|
|
|
|
gzipReader, err := gzip.NewReader(gzipFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer gzipReader.Close()
|
|
|
|
cmd.Stdin = gzipReader
|
|
|
|
} else {
|
|
|
|
cmd.Stdin = fi
|
2023-07-25 17:08:13 +08:00
|
|
|
}
|
2024-03-04 16:35:05 +08:00
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
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 errors.New(stdStr)
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
2024-03-04 16:35:05 +08:00
|
|
|
|
2023-07-19 16:25:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:07:27 +08:00
|
|
|
func (r *Remote) SyncDB(version string) ([]SyncDBInfo, error) {
|
|
|
|
var datas []SyncDBInfo
|
2023-08-16 23:36:11 +08:00
|
|
|
rows, err := r.Client.Query("select schema_name, default_character_set_name from information_schema.SCHEMATA")
|
2023-07-27 16:07:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var dbName, charsetName string
|
|
|
|
if err = rows.Scan(&dbName, &charsetName); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
2023-10-25 11:01:27 +08:00
|
|
|
if dbName == "information_schema" || dbName == "mysql" || dbName == "performance_schema" || dbName == "sys" || dbName == "__recycle_bin__" || dbName == "recycle_bin" {
|
2023-07-27 16:07:27 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataItem := SyncDBInfo{
|
2023-09-16 13:16:15 +08:00
|
|
|
Name: dbName,
|
|
|
|
From: "remote",
|
|
|
|
MysqlName: r.Database,
|
|
|
|
Format: charsetName,
|
2023-07-27 16:07:27 +08:00
|
|
|
}
|
2023-08-16 23:36:11 +08:00
|
|
|
userRows, err := r.Client.Query("select user,host from mysql.db where db = ?", dbName)
|
2023-07-27 16:07:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var permissionItem []string
|
|
|
|
isLocal := true
|
|
|
|
i := 0
|
|
|
|
for userRows.Next() {
|
|
|
|
var user, host string
|
|
|
|
if err = userRows.Scan(&user, &host); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
if user == "root" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
dataItem.Username = user
|
|
|
|
}
|
|
|
|
if dataItem.Username == user && host == "%" {
|
|
|
|
isLocal = false
|
|
|
|
dataItem.Permission = "%"
|
|
|
|
} else if dataItem.Username == user && host != "localhost" {
|
|
|
|
isLocal = false
|
|
|
|
permissionItem = append(permissionItem, host)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if len(dataItem.Username) == 0 {
|
|
|
|
dataItem.Permission = "%"
|
|
|
|
} else {
|
|
|
|
if isLocal {
|
|
|
|
dataItem.Permission = "localhost"
|
|
|
|
}
|
|
|
|
if len(dataItem.Permission) == 0 {
|
|
|
|
dataItem.Permission = strings.Join(permissionItem, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
datas = append(datas, dataItem)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:41 +08:00
|
|
|
func (r *Remote) Close() {
|
|
|
|
_ = r.Client.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ExecSQL(command string, timeout uint) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if _, err := r.Client.ExecContext(ctx, command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return buserr.New(constant.ErrExecTimeOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ExecSQLForHosts(timeout uint) ([]string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
results, err := r.Client.QueryContext(ctx, "select host from mysql.user where user='root';")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return nil, buserr.New(constant.ErrExecTimeOut)
|
|
|
|
}
|
|
|
|
var rows []string
|
|
|
|
for results.Next() {
|
|
|
|
var host string
|
|
|
|
if err := results.Scan(&host); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rows = append(rows, host)
|
|
|
|
}
|
|
|
|
return rows, nil
|
|
|
|
}
|
2024-03-04 16:35:05 +08:00
|
|
|
|
|
|
|
func loadImage(dbType, version string) (string, error) {
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
2024-05-30 15:25:15 +08:00
|
|
|
return "", err
|
2024-03-04 16:35:05 +08:00
|
|
|
}
|
2024-05-22 18:32:47 +08:00
|
|
|
images, err := cli.ImageList(context.Background(), image.ListOptions{})
|
2024-03-04 16:35:05 +08:00
|
|
|
if err != nil {
|
2024-05-30 15:25:15 +08:00
|
|
|
return "", err
|
2024-03-04 16:35:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, image := range images {
|
|
|
|
for _, tag := range image.RepoTags {
|
|
|
|
if !strings.HasPrefix(tag, dbType+":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if dbType == "mariadb" && strings.HasPrefix(tag, "mariadb:") {
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(version, "5.6") && strings.HasPrefix(tag, "mysql:5.6") {
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(version, "5.7") && strings.HasPrefix(tag, "mysql:5.7") {
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(version, "8.") && strings.HasPrefix(tag, "mysql:8.") {
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 15:25:15 +08:00
|
|
|
return loadVersion(dbType, version), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadVersion(dbType string, version string) string {
|
|
|
|
if dbType == "mariadb" {
|
|
|
|
return "mariadb:11.3.2 "
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(version, "5.6") {
|
|
|
|
return "mysql:5.6.51"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(version, "5.7") {
|
|
|
|
return "mysql:5.7.44"
|
2024-03-04 16:35:05 +08:00
|
|
|
}
|
2024-05-30 15:25:15 +08:00
|
|
|
return "mysql:8.2.0"
|
2024-03-04 16:35:05 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 17:59:00 +08:00
|
|
|
func sslSkip(version, dbType string) string {
|
|
|
|
if dbType == constant.AppMariaDB || strings.HasPrefix(version, "5.6") || strings.HasPrefix(version, "5.7") {
|
2024-03-04 16:35:05 +08:00
|
|
|
return "--skip-ssl"
|
|
|
|
}
|
|
|
|
return "--ssl-mode=DISABLED"
|
|
|
|
}
|