2022-09-06 06:28:49 +00:00
|
|
|
/*
|
|
|
|
* @Author: LinkLeong link@icewhale.org
|
|
|
|
* @Date: 2022-08-23 18:09:11
|
|
|
|
* @LastEditors: LinkLeong
|
|
|
|
* @LastEditTime: 2022-08-31 14:17:51
|
|
|
|
* @FilePath: /CasaOS/cmd/migration-tool/main.go
|
|
|
|
* @Description:
|
|
|
|
* @Website: https://www.casaos.io
|
|
|
|
* Copyright (c) 2022 by icewhale, All Rights Reserved.
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
|
|
|
"github.com/IceWhaleTech/CasaOS-Gateway/common"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
casaosServiceName = "casaos.service"
|
|
|
|
)
|
|
|
|
|
2022-10-20 06:25:26 +00:00
|
|
|
var (
|
|
|
|
_logger *Logger
|
|
|
|
sqliteDB *gorm.DB
|
|
|
|
)
|
2022-09-06 06:28:49 +00:00
|
|
|
|
2022-10-20 06:25:26 +00:00
|
|
|
var (
|
|
|
|
configFlag = ""
|
|
|
|
dbFlag = ""
|
|
|
|
)
|
2022-09-06 06:28:49 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
config.InitSetup(configFlag)
|
|
|
|
|
|
|
|
if len(dbFlag) == 0 {
|
|
|
|
dbFlag = config.AppInfo.DBPath + "/db"
|
|
|
|
}
|
|
|
|
|
|
|
|
sqliteDB = sqlite.GetDb(dbFlag)
|
2022-10-20 06:25:26 +00:00
|
|
|
// gredis.GetRedisConn(config.RedisInfo),
|
2022-09-06 06:28:49 +00:00
|
|
|
|
2022-12-20 06:05:16 +00:00
|
|
|
service.MyService = service.NewService(sqliteDB, "", nil)
|
2022-09-06 06:28:49 +00:00
|
|
|
}
|
2022-10-20 06:25:26 +00:00
|
|
|
|
2022-09-06 06:28:49 +00:00
|
|
|
func main() {
|
|
|
|
versionFlag := flag.Bool("v", false, "version")
|
|
|
|
debugFlag := flag.Bool("d", true, "debug")
|
|
|
|
forceFlag := flag.Bool("f", true, "force")
|
|
|
|
flag.Parse()
|
|
|
|
_logger = NewLogger()
|
|
|
|
if *versionFlag {
|
|
|
|
fmt.Println(common.Version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *debugFlag {
|
|
|
|
_logger.DebugMode = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*forceFlag {
|
|
|
|
serviceEnabled, err := systemctl.IsServiceEnabled(casaosServiceName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serviceEnabled {
|
|
|
|
_logger.Info("%s is already enabled. If migration is still needed, try with -f.", casaosServiceName)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
migrationTools := []interfaces.MigrationTool{
|
2022-10-20 06:25:26 +00:00
|
|
|
// nothing to migrate from last version
|
2022-09-06 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var selectedMigrationTool interfaces.MigrationTool
|
|
|
|
|
|
|
|
// look for the right migration tool matching current version
|
|
|
|
for _, tool := range migrationTools {
|
|
|
|
migrationNeeded, err := tool.IsMigrationNeeded()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if migrationNeeded {
|
|
|
|
selectedMigrationTool = tool
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if selectedMigrationTool == nil {
|
|
|
|
_logger.Error("selectedMigrationTool is null")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := selectedMigrationTool.PreMigrate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := selectedMigrationTool.Migrate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-10-20 06:25:26 +00:00
|
|
|
if err := selectedMigrationTool.PostMigrate(); err != nil {
|
|
|
|
_logger.Error("Migration succeeded, but post-migration failed: %s", err)
|
|
|
|
}
|
2022-09-06 06:28:49 +00:00
|
|
|
}
|