安装过程显示更详细内容

This commit is contained in:
GoEdgeLab
2022-11-11 21:48:00 +08:00
parent 1a1b937a63
commit 6c40df361c
4 changed files with 69 additions and 22 deletions

View File

@@ -27,10 +27,11 @@ func main() {
app.Version(teaconst.Version) app.Version(teaconst.Version)
app.Product(teaconst.ProductName) app.Product(teaconst.ProductName)
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon|issues]") app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon|issues]")
app.On("setup", func() { app.On("setup", func() {
var setupCmd = setup.NewSetupFromCmd() var setupCmd = setup.NewSetupFromCmd()
err := setupCmd.Run() err := setupCmd.Run()
result := maps.Map{} var result = maps.Map{}
if err != nil { if err != nil {
result["isOk"] = false result["isOk"] = false
result["error"] = err.Error() result["error"] = err.Error()

View File

@@ -22,6 +22,8 @@ type Setup struct {
// 要返回的数据 // 要返回的数据
AdminNodeId string AdminNodeId string
AdminNodeSecret string AdminNodeSecret string
logFp *os.File
} }
func NewSetup(config *Config) *Setup { func NewSetup(config *Config) *Setup {
@@ -31,15 +33,15 @@ func NewSetup(config *Config) *Setup {
} }
func NewSetupFromCmd() *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 { for _, arg := range args {
index := strings.Index(arg, "=") var index = strings.Index(arg, "=")
if index <= 0 { if index <= 0 {
continue continue
} }
value := arg[index+1:] var value = arg[index+1:]
value = strings.Trim(value, "\"'") value = strings.Trim(value, "\"'")
switch arg[:index] { switch arg[:index] {
case "-api-node-protocol": 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 { func (this *Setup) Run() error {
@@ -73,7 +86,7 @@ func (this *Setup) Run() error {
} }
// 执行SQL // 执行SQL
config := &dbs.Config{} var config = &dbs.Config{}
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml")) configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil { if err != nil {
return err return err
@@ -91,14 +104,22 @@ func (this *Setup) Run() error {
return errors.New("can not find database config for env '" + Tea.Env + "'") 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) err = executor.Run(false)
if err != nil { if err != nil {
return err return err
} }
// Admin节点信息 // Admin节点信息
apiTokenDAO := models.NewApiTokenDAO() var apiTokenDAO = models.NewApiTokenDAO()
token, err := apiTokenDAO.FindEnabledTokenWithRole(nil, "admin") token, err := apiTokenDAO.FindEnabledTokenWithRole(nil, "admin")
if err != nil { if err != nil {
return err return err
@@ -110,7 +131,7 @@ func (this *Setup) Run() error {
this.AdminNodeSecret = token.Secret this.AdminNodeSecret = token.Secret
// 检查API节点 // 检查API节点
dao := models.NewAPINodeDAO() var dao = models.NewAPINodeDAO()
apiNodeId, err := dao.FindEnabledAPINodeIdWithAddr(nil, this.config.APINodeProtocol, this.config.APINodeHost, this.config.APINodePort) apiNodeId, err := dao.FindEnabledAPINodeIdWithAddr(nil, this.config.APINodeProtocol, this.config.APINodeHost, this.config.APINodePort)
if err != nil { if err != nil {
return err return err
@@ -175,7 +196,7 @@ func (this *Setup) Run() error {
} }
// 保存配置 // 保存配置
apiConfig := &configs.APIConfig{ var apiConfig = &configs.APIConfig{
NodeId: apiNode.UniqueId, NodeId: apiNode.UniqueId,
Secret: apiNode.Secret, Secret: apiNode.Secret,
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"io"
"regexp" "regexp"
"runtime" "runtime"
"sort" "sort"
@@ -56,12 +57,17 @@ type sqlItem struct {
} }
type SQLDump struct { type SQLDump struct {
logWriter io.Writer
} }
func NewSQLDump() *SQLDump { func NewSQLDump() *SQLDump {
return &SQLDump{} return &SQLDump{}
} }
func (this *SQLDump) SetLogWriter(logWriter io.Writer) {
this.logWriter = logWriter
}
// Dump 导出数据 // Dump 导出数据
func (this *SQLDump) Dump(db *dbs.DB, includingRecords bool) (result *SQLDumpResult, err error) { func (this *SQLDump) Dump(db *dbs.DB, includingRecords bool) (result *SQLDumpResult, err error) {
result = &SQLDumpResult{} result = &SQLDumpResult{}
@@ -248,7 +254,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
var op = "+ table " + newTable.Name var op = "+ table " + newTable.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
if len(newTable.Records) == 0 { if len(newTable.Records) == 0 {
execSQL(newTable.Definition) execSQL(newTable.Definition)
@@ -267,7 +273,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
var op = "+ " + newTable.Name + " " + newField.Name var op = "+ " + newTable.Name + " " + newField.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD `" + newField.Name + "` " + newField.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD `" + newField.Name + "` " + newField.Definition)
if err != nil { if err != nil {
@@ -277,7 +283,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
var op = "* " + newTable.Name + " " + newField.Name var op = "* " + newTable.Name + " " + newField.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + newTable.Name + " MODIFY `" + newField.Name + "` " + newField.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " MODIFY `" + newField.Name + "` " + newField.Definition)
if err != nil { 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 var op = "+ index " + newTable.Name + " " + newIndex.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition)
if err != nil { 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 var op = "* index " + newTable.Name + " " + newIndex.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + newTable.Name + " DROP KEY " + newIndex.Name) _, err = db.Exec("ALTER TABLE " + newTable.Name + " DROP KEY " + newIndex.Name)
if err != nil { 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 var op = "- index " + oldTable.Name + " " + oldIndex.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP KEY " + oldIndex.Name) _, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP KEY " + oldIndex.Name)
if err != nil { 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 var op = "- field " + oldTable.Name + " " + oldField.Name
ops = append(ops, op) ops = append(ops, op)
if showLog { if showLog {
fmt.Println(op) this.log(op)
} }
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP COLUMN `" + oldField.Name + "`") _, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP COLUMN `" + oldField.Name + "`")
if err != nil { if err != nil {
@@ -385,7 +391,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
if one == nil { if one == nil {
ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
if showLog { if showLog {
fmt.Println("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", ")) this.log("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
} }
var params = []string{} var params = []string{}
var args = []string{} var args = []string{}
@@ -406,7 +412,7 @@ func (this *SQLDump) applyQueue(db *dbs.DB, newResult *SQLDumpResult, showLog bo
} else if !record.ValuesEquals(one) { } else if !record.ValuesEquals(one) {
ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
if showLog { if showLog {
fmt.Println("* record " + newTable.Name + " " + strings.Join(valueStrings, ", ")) this.log("* record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
} }
var args = []string{} var args = []string{}
var values = []any{} var values = []any{}
@@ -535,3 +541,12 @@ func (this *SQLDump) tryCreateIndex(err error, db *dbs.DB, tableName string, ind
return err return err
} }
// 打印操作日志
func (this *SQLDump) log(message string) {
if this.logWriter != nil {
_, _ = this.logWriter.Write([]byte(message + "\n"))
} else {
fmt.Println(message)
}
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io"
"os" "os"
"time" "time"
) )
@@ -23,6 +24,7 @@ var LatestSQLResult = &SQLDumpResult{}
// SQLExecutor 安装或升级SQL执行器 // SQLExecutor 安装或升级SQL执行器
type SQLExecutor struct { type SQLExecutor struct {
dbConfig *dbs.DBConfig dbConfig *dbs.DBConfig
logWriter io.Writer
} }
func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor { func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
@@ -33,7 +35,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
func NewSQLExecutorFromCmd() (*SQLExecutor, error) { func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
// 执行SQL // 执行SQL
config := &dbs.Config{} var config = &dbs.Config{}
configData, err := os.ReadFile(Tea.ConfigFile("db.yaml")) configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil { if err != nil {
return nil, err return nil, err
@@ -45,6 +47,10 @@ func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
return NewSQLExecutor(config.DBs[Tea.Env]), nil return NewSQLExecutor(config.DBs[Tea.Env]), nil
} }
func (this *SQLExecutor) SetLogWriter(logWriter io.Writer) {
this.logWriter = logWriter
}
func (this *SQLExecutor) Run(showLog bool) error { func (this *SQLExecutor) Run(showLog bool) error {
db, err := dbs.NewInstanceFromConfig(this.dbConfig) db, err := dbs.NewInstanceFromConfig(this.dbConfig)
if err != nil { if err != nil {
@@ -56,6 +62,10 @@ func (this *SQLExecutor) Run(showLog bool) error {
}() }()
var sqlDump = NewSQLDump() var sqlDump = NewSQLDump()
sqlDump.SetLogWriter(this.logWriter)
if this.logWriter != nil {
showLog = true
}
_, err = sqlDump.Apply(db, LatestSQLResult, showLog) _, err = sqlDump.Apply(db, LatestSQLResult, showLog)
if err != nil { if err != nil {
return err return err