mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 15:00:27 +08:00
安装过程显示更详细内容
This commit is contained in:
@@ -27,10 +27,11 @@ func main() {
|
||||
app.Version(teaconst.Version)
|
||||
app.Product(teaconst.ProductName)
|
||||
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon|issues]")
|
||||
|
||||
app.On("setup", func() {
|
||||
var setupCmd = setup.NewSetupFromCmd()
|
||||
err := setupCmd.Run()
|
||||
result := maps.Map{}
|
||||
var result = maps.Map{}
|
||||
if err != nil {
|
||||
result["isOk"] = false
|
||||
result["error"] = err.Error()
|
||||
|
||||
@@ -22,6 +22,8 @@ type Setup struct {
|
||||
// 要返回的数据
|
||||
AdminNodeId string
|
||||
AdminNodeSecret string
|
||||
|
||||
logFp *os.File
|
||||
}
|
||||
|
||||
func NewSetup(config *Config) *Setup {
|
||||
@@ -31,15 +33,15 @@ func NewSetup(config *Config) *Setup {
|
||||
}
|
||||
|
||||
func NewSetupFromCmd() *Setup {
|
||||
args := cmd.ParseArgs(strings.Join(os.Args[1:], " "))
|
||||
var args = cmd.ParseArgs(strings.Join(os.Args[1:], " "))
|
||||
|
||||
config := &Config{}
|
||||
var config = &Config{}
|
||||
for _, arg := range args {
|
||||
index := strings.Index(arg, "=")
|
||||
var index = strings.Index(arg, "=")
|
||||
if index <= 0 {
|
||||
continue
|
||||
}
|
||||
value := arg[index+1:]
|
||||
var value = arg[index+1:]
|
||||
value = strings.Trim(value, "\"'")
|
||||
switch arg[:index] {
|
||||
case "-api-node-protocol":
|
||||
@@ -51,7 +53,18 @@ func NewSetupFromCmd() *Setup {
|
||||
}
|
||||
}
|
||||
|
||||
return NewSetup(config)
|
||||
var setup = NewSetup(config)
|
||||
|
||||
// log writer
|
||||
var tmpDir = os.TempDir()
|
||||
if len(tmpDir) > 0 {
|
||||
fp, err := os.OpenFile(tmpDir+"/edge-install.log", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err == nil {
|
||||
setup.logFp = fp
|
||||
}
|
||||
}
|
||||
|
||||
return setup
|
||||
}
|
||||
|
||||
func (this *Setup) Run() error {
|
||||
@@ -73,7 +86,7 @@ func (this *Setup) Run() error {
|
||||
}
|
||||
|
||||
// 执行SQL
|
||||
config := &dbs.Config{}
|
||||
var config = &dbs.Config{}
|
||||
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -91,14 +104,22 @@ func (this *Setup) Run() error {
|
||||
return errors.New("can not find database config for env '" + Tea.Env + "'")
|
||||
}
|
||||
|
||||
executor := NewSQLExecutor(dbConfig)
|
||||
var executor = NewSQLExecutor(dbConfig)
|
||||
if this.logFp != nil {
|
||||
executor.SetLogWriter(this.logFp)
|
||||
|
||||
defer func() {
|
||||
_ = this.logFp.Close()
|
||||
_ = os.Remove(this.logFp.Name())
|
||||
}()
|
||||
}
|
||||
err = executor.Run(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Admin节点信息
|
||||
apiTokenDAO := models.NewApiTokenDAO()
|
||||
var apiTokenDAO = models.NewApiTokenDAO()
|
||||
token, err := apiTokenDAO.FindEnabledTokenWithRole(nil, "admin")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -110,7 +131,7 @@ func (this *Setup) Run() error {
|
||||
this.AdminNodeSecret = token.Secret
|
||||
|
||||
// 检查API节点
|
||||
dao := models.NewAPINodeDAO()
|
||||
var dao = models.NewAPINodeDAO()
|
||||
apiNodeId, err := dao.FindEnabledAPINodeIdWithAddr(nil, this.config.APINodeProtocol, this.config.APINodeHost, this.config.APINodePort)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -175,7 +196,7 @@ func (this *Setup) Run() error {
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
apiConfig := &configs.APIConfig{
|
||||
var apiConfig = &configs.APIConfig{
|
||||
NodeId: apiNode.UniqueId,
|
||||
Secret: apiNode.Secret,
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -56,12 +57,17 @@ type sqlItem struct {
|
||||
}
|
||||
|
||||
type SQLDump struct {
|
||||
logWriter io.Writer
|
||||
}
|
||||
|
||||
func NewSQLDump() *SQLDump {
|
||||
return &SQLDump{}
|
||||
}
|
||||
|
||||
func (this *SQLDump) SetLogWriter(logWriter io.Writer) {
|
||||
this.logWriter = logWriter
|
||||
}
|
||||
|
||||
// Dump 导出数据
|
||||
func (this *SQLDump) Dump(db *dbs.DB, includingRecords bool) (result *SQLDumpResult, err error) {
|
||||
result = &SQLDumpResult{}
|
||||
@@ -248,7 +254,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "+ table " + newTable.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
if len(newTable.Records) == 0 {
|
||||
execSQL(newTable.Definition)
|
||||
@@ -267,7 +273,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "+ " + newTable.Name + " " + newField.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD `" + newField.Name + "` " + newField.Definition)
|
||||
if err != nil {
|
||||
@@ -277,7 +283,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "* " + newTable.Name + " " + newField.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " MODIFY `" + newField.Name + "` " + newField.Definition)
|
||||
if err != nil {
|
||||
@@ -294,7 +300,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "+ index " + newTable.Name + " " + newIndex.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition)
|
||||
if err != nil {
|
||||
@@ -307,7 +313,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "* index " + newTable.Name + " " + newIndex.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " DROP KEY " + newIndex.Name)
|
||||
if err != nil {
|
||||
@@ -330,7 +336,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "- index " + oldTable.Name + " " + oldIndex.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP KEY " + oldIndex.Name)
|
||||
if err != nil {
|
||||
@@ -347,7 +353,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
var op = "- field " + oldTable.Name + " " + oldField.Name
|
||||
ops = append(ops, op)
|
||||
if showLog {
|
||||
fmt.Println(op)
|
||||
this.log(op)
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP COLUMN `" + oldField.Name + "`")
|
||||
if err != nil {
|
||||
@@ -385,7 +391,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
if one == nil {
|
||||
ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
|
||||
if showLog {
|
||||
fmt.Println("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
|
||||
this.log("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
|
||||
}
|
||||
var params = []string{}
|
||||
var args = []string{}
|
||||
@@ -406,7 +412,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
|
||||
} else if !record.ValuesEquals(one) {
|
||||
ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
|
||||
if showLog {
|
||||
fmt.Println("* record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
|
||||
this.log("* record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
|
||||
}
|
||||
var args = []string{}
|
||||
var values = []any{}
|
||||
@@ -535,3 +541,12 @@ func (this *SQLDump) tryCreateIndex(err error, db *dbs.DB, tableName string, ind
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 打印操作日志
|
||||
func (this *SQLDump) log(message string) {
|
||||
if this.logWriter != nil {
|
||||
_, _ = this.logWriter.Write([]byte(message + "\n"))
|
||||
} else {
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
@@ -22,7 +23,8 @@ var LatestSQLResult = &SQLDumpResult{}
|
||||
|
||||
// SQLExecutor 安装或升级SQL执行器
|
||||
type SQLExecutor struct {
|
||||
dbConfig *dbs.DBConfig
|
||||
dbConfig *dbs.DBConfig
|
||||
logWriter io.Writer
|
||||
}
|
||||
|
||||
func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
|
||||
@@ -33,7 +35,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
|
||||
|
||||
func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
|
||||
// 执行SQL
|
||||
config := &dbs.Config{}
|
||||
var config = &dbs.Config{}
|
||||
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -45,6 +47,10 @@ func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
|
||||
return NewSQLExecutor(config.DBs[Tea.Env]), nil
|
||||
}
|
||||
|
||||
func (this *SQLExecutor) SetLogWriter(logWriter io.Writer) {
|
||||
this.logWriter = logWriter
|
||||
}
|
||||
|
||||
func (this *SQLExecutor) Run(showLog bool) error {
|
||||
db, err := dbs.NewInstanceFromConfig(this.dbConfig)
|
||||
if err != nil {
|
||||
@@ -56,6 +62,10 @@ func (this *SQLExecutor) Run(showLog bool) error {
|
||||
}()
|
||||
|
||||
var sqlDump = NewSQLDump()
|
||||
sqlDump.SetLogWriter(this.logWriter)
|
||||
if this.logWriter != nil {
|
||||
showLog = true
|
||||
}
|
||||
_, err = sqlDump.Apply(db, LatestSQLResult, showLog)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user