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:
@@ -1,3 +1,3 @@
|
|||||||
LEGACY_WITHOUT_VERSION v0.3.6-alpha6
|
LEGACY_WITHOUT_VERSION v0.3.6-alpha7
|
||||||
v0.3.5 v0.3.6-alpha6
|
v0.3.5 v0.3.6-alpha7
|
||||||
v0.3.5.1 v0.3.6-alpha6
|
v0.3.5.1 v0.3.6-alpha7
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
||||||
|
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,12 +16,16 @@ const (
|
|||||||
userServiceConfigDirPath = "/etc/casaos"
|
userServiceConfigDirPath = "/etc/casaos"
|
||||||
userServiceConfigFilePath = "/etc/casaos/user-service.conf"
|
userServiceConfigFilePath = "/etc/casaos/user-service.conf"
|
||||||
userServiceName = "casaos-user-service.service"
|
userServiceName = "casaos-user-service.service"
|
||||||
|
userServiceNameShort = "user-service"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embedded ../../build/sysroot/etc/casaos/user-service.conf.sample
|
//go:embedded ../../build/sysroot/etc/casaos/user-service.conf.sample
|
||||||
var _userServiceConfigFileSample string
|
var _userServiceConfigFileSample string
|
||||||
|
|
||||||
var _logger *Logger
|
var (
|
||||||
|
_logger *Logger
|
||||||
|
_status *version.GlobalMigrationStatus
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
versionFlag := flag.Bool("v", false, "version")
|
versionFlag := flag.Bool("v", false, "version")
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
"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/config"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/encryption"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/encryption"
|
||||||
@@ -22,6 +23,16 @@ import (
|
|||||||
type migrationTool1 struct{}
|
type migrationTool1 struct{}
|
||||||
|
|
||||||
func (u *migrationTool1) IsMigrationNeeded() (bool, error) {
|
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 {
|
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||||
return false, nil
|
return false, nil
|
||||||
@@ -95,6 +106,8 @@ func (u *migrationTool1) Migrate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *migrationTool1) PostMigrate() error {
|
func (u *migrationTool1) PostMigrate() error {
|
||||||
|
defer _status.Done(common.Version)
|
||||||
|
|
||||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
"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/config"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
|
||||||
@@ -16,11 +17,21 @@ import (
|
|||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultDBPath = "/var/lib/casaos"
|
|
||||||
|
|
||||||
type migrationTool2 struct{}
|
type migrationTool2 struct{}
|
||||||
|
|
||||||
|
const defaultDBPath = "/var/lib/casaos"
|
||||||
|
|
||||||
func (u *migrationTool2) IsMigrationNeeded() (bool, error) {
|
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 {
|
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||||
return false, nil
|
return false, nil
|
||||||
@@ -120,6 +131,8 @@ func (u *migrationTool2) Migrate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *migrationTool2) PostMigrate() error {
|
func (u *migrationTool2) PostMigrate() error {
|
||||||
|
defer _status.Done(common.Version)
|
||||||
|
|
||||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ module github.com/IceWhaleTech/CasaOS-UserService
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2
|
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9
|
||||||
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5
|
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha6
|
||||||
github.com/gin-contrib/gzip v0.0.6
|
github.com/gin-contrib/gzip v0.0.6
|
||||||
github.com/gin-gonic/gin v1.8.1
|
github.com/gin-gonic/gin v1.8.1
|
||||||
github.com/satori/go.uuid v1.2.0
|
github.com/satori/go.uuid v1.2.0
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
|
|||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||||
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2 h1:l/f2wj7Xs1nbdhbio3ENzPT2cHuFIXuAhptqIIfndrg=
|
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9 h1:q4I/lSsCooxdd6LxinGy90y0n6V8EcaPBV1JCfpEnV4=
|
||||||
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2/go.mod h1:5sqNKg5cEH7IUnCklLSTrVoGx1dMBhm9DFDsCYVPvPQ=
|
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
|
||||||
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5 h1:aRDy7ug77eZN19vqRjH1N6HHYuzYUnwp9ByMwdfnmJw=
|
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha6 h1:3xTv3ACQItJph1EwTaq0qTThCIKodsU7Ej/ntLyXIMA=
|
||||||
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5/go.mod h1:v2d+VlVMF6i0XPGRJbmUt1b270J8kn1+pV5xmCJFrXk=
|
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha6/go.mod h1:hnZwGUzcOyiufMpVO7l3gu2gAm6Ws4TY4Nlj3kMshXA=
|
||||||
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
|
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
|
||||||
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
@@ -611,6 +611,8 @@ gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMT
|
|||||||
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||||
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
|
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
|
||||||
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||||
|
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||||
|
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -36,7 +35,7 @@ func MkDir(src string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
os.Chmod(src, 0777)
|
os.Chmod(src, 0o777)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -53,7 +52,7 @@ func IsNotExistCreateFile(src string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Exists(path string) bool {
|
func Exists(path string) bool {
|
||||||
_, err := os.Stat(path) //os.Stat获取文件信息
|
_, err := os.Stat(path) // os.Stat获取文件信息
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return os.IsExist(err)
|
return os.IsExist(err)
|
||||||
}
|
}
|
||||||
@@ -75,7 +74,7 @@ func ReadFullFile(path string) []byte {
|
|||||||
return []byte("")
|
return []byte("")
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
content, err := ioutil.ReadAll(file)
|
content, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte("")
|
return []byte("")
|
||||||
}
|
}
|
||||||
@@ -134,7 +133,7 @@ func WriteToPath(data []byte, path, name string) error {
|
|||||||
IsNotExistCreateFile(fullPath)
|
IsNotExistCreateFile(fullPath)
|
||||||
file, err := os.OpenFile(fullPath,
|
file, err := os.OpenFile(fullPath,
|
||||||
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
|
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
|
||||||
0666,
|
0o666,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+2
-2
@@ -2,7 +2,7 @@ package v1
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
json2 "encoding/json"
|
json2 "encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
url2 "net/url"
|
url2 "net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -418,7 +418,7 @@ func PostUserCustomConf(c *gin.Context) {
|
|||||||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data, _ := ioutil.ReadAll(c.Request.Body)
|
data, _ := io.ReadAll(c.Request.Body)
|
||||||
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id)
|
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id)
|
||||||
|
|
||||||
file.WriteToPath(data, filePath, name+".json")
|
file.WriteToPath(data, filePath, name+".json")
|
||||||
|
|||||||
Reference in New Issue
Block a user