Skip to content

Commit

Permalink
add migration versioning (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Sep 1, 2022
1 parent 6c73a4b commit 38fee52
Showing 8 changed files with 51 additions and 19 deletions.
6 changes: 3 additions & 3 deletions build/scripts/migration/service.d/user-service/migration.list
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
LEGACY_WITHOUT_VERSION v0.3.6-alpha6
v0.3.5 v0.3.6-alpha6
v0.3.5.1 v0.3.6-alpha6
LEGACY_WITHOUT_VERSION v0.3.6-alpha7
v0.3.5 v0.3.6-alpha7
v0.3.5.1 v0.3.6-alpha7
7 changes: 6 additions & 1 deletion cmd/migration-tool/main.go
Original file line number Diff line number Diff line change
@@ -8,19 +8,24 @@ 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"
)

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")
13 changes: 13 additions & 0 deletions cmd/migration-tool/migration_032_and_older.go
Original file line number Diff line number Diff line change
@@ -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
17 changes: 15 additions & 2 deletions cmd/migration-tool/migration_033_034_035.go
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ module github.com/IceWhaleTech/CasaOS-UserService
go 1.19

require (
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha6
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/satori/go.uuid v1.2.0
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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 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/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2 h1:l/f2wj7Xs1nbdhbio3ENzPT2cHuFIXuAhptqIIfndrg=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220831042533-e51a41247ea2/go.mod h1:5sqNKg5cEH7IUnCklLSTrVoGx1dMBhm9DFDsCYVPvPQ=
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5 h1:aRDy7ug77eZN19vqRjH1N6HHYuzYUnwp9ByMwdfnmJw=
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha5/go.mod h1:v2d+VlVMF6i0XPGRJbmUt1b270J8kn1+pV5xmCJFrXk=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9 h1:q4I/lSsCooxdd6LxinGy90y0n6V8EcaPBV1JCfpEnV4=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha6 h1:3xTv3ACQItJph1EwTaq0qTThCIKodsU7Ej/ntLyXIMA=
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/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
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.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
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-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
9 changes: 4 additions & 5 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package file

import (
"io"
"io/ioutil"
"os"
"path"
"strings"
@@ -36,7 +35,7 @@ func MkDir(src string) error {
if err != nil {
return err
}
os.Chmod(src, 0777)
os.Chmod(src, 0o777)

return nil
}
@@ -53,7 +52,7 @@ func IsNotExistCreateFile(src string) error {
}

func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
_, err := os.Stat(path) // os.Stat获取文件信息
if err != nil {
return os.IsExist(err)
}
@@ -75,7 +74,7 @@ func ReadFullFile(path string) []byte {
return []byte("")
}
defer file.Close()
content, err := ioutil.ReadAll(file)
content, err := io.ReadAll(file)
if err != nil {
return []byte("")
}
@@ -134,7 +133,7 @@ func WriteToPath(data []byte, path, name string) error {
IsNotExistCreateFile(fullPath)
file, err := os.OpenFile(fullPath,
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
0666,
0o666,
)
if err != nil {
return err
4 changes: 2 additions & 2 deletions route/v1/user.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ package v1

import (
json2 "encoding/json"
"io/ioutil"
"io"
"net/http"
url2 "net/url"
"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)})
return
}
data, _ := ioutil.ReadAll(c.Request.Body)
data, _ := io.ReadAll(c.Request.Body)
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id)

file.WriteToPath(data, filePath, name+".json")

0 comments on commit 38fee52

Please sign in to comment.