mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-10-06 19:59:42 +07:00
add migration versioning (#12)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
)
|
||||
|
||||
@@ -15,12 +16,16 @@ const (
|
||||
userServiceConfigDirPath = "/etc/casaos"
|
||||
userServiceConfigFilePath = "/etc/casaos/user-service.conf"
|
||||
userServiceName = "casaos-user-service.service"
|
||||
userServiceNameShort = "user-service"
|
||||
)
|
||||
|
||||
//go:embedded ../../build/sysroot/etc/casaos/user-service.conf.sample
|
||||
var _userServiceConfigFileSample string
|
||||
|
||||
var _logger *Logger
|
||||
var (
|
||||
_logger *Logger
|
||||
_status *version.GlobalMigrationStatus
|
||||
)
|
||||
|
||||
func main() {
|
||||
versionFlag := flag.Bool("v", false, "version")
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/encryption"
|
||||
@@ -22,6 +23,16 @@ import (
|
||||
type migrationTool1 struct{}
|
||||
|
||||
func (u *migrationTool1) IsMigrationNeeded() (bool, error) {
|
||||
if status, err := version.GetGlobalMigrationStatus(userServiceNameShort); err == nil {
|
||||
_status = status
|
||||
if status.LastMigratedVersion != "" {
|
||||
_logger.Info("Last migrated version: %s", status.LastMigratedVersion)
|
||||
if r, err := version.Compare(status.LastMigratedVersion, common.Version); err == nil {
|
||||
return r < 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||
return false, nil
|
||||
@@ -95,6 +106,8 @@ func (u *migrationTool1) Migrate() error {
|
||||
}
|
||||
|
||||
func (u *migrationTool1) PostMigrate() error {
|
||||
defer _status.Done(common.Version)
|
||||
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
|
||||
@@ -16,11 +17,21 @@ import (
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const defaultDBPath = "/var/lib/casaos"
|
||||
|
||||
type migrationTool2 struct{}
|
||||
|
||||
const defaultDBPath = "/var/lib/casaos"
|
||||
|
||||
func (u *migrationTool2) IsMigrationNeeded() (bool, error) {
|
||||
if status, err := version.GetGlobalMigrationStatus(userServiceNameShort); err == nil {
|
||||
_status = status
|
||||
if status.LastMigratedVersion != "" {
|
||||
_logger.Info("Last migrated version: %s", status.LastMigratedVersion)
|
||||
if r, err := version.Compare(status.LastMigratedVersion, common.Version); err == nil {
|
||||
return r < 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||
return false, nil
|
||||
@@ -120,6 +131,8 @@ func (u *migrationTool2) Migrate() error {
|
||||
}
|
||||
|
||||
func (u *migrationTool2) PostMigrate() error {
|
||||
defer _status.Done(common.Version)
|
||||
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user