Skip to content

Commit

Permalink
add migration from versions 0.3.3, 0.3.4, 0.3.5 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Aug 12, 2022
1 parent 52d3842 commit 0b66fd8
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Install dependencies for cross-compiling
run: |
sudo apt update
sudo apt-get --no-install-recommends --yes install \
libc6-dev-amd64-cross \
gcc-10-aarch64-linux-gnu libc6-dev-arm64-cross \
gcc-10-arm-linux-gnueabihf libc6-dev-armhf-cross
-
name: Checkout
uses: actions/checkout@v2
Expand Down
71 changes: 68 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,84 @@ before:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- binary: build/usr/bin/casaos-user-service
- id: casaos-user-service-amd64
binary: build/usr/bin/casaos-user-service
env:
- CGO_ENABLED=0
- CGO_ENABLED=1
- CC=x86_64-linux-gnu-gcc-10
goos:
- linux
goarch:
- amd64
- id: casaos-user-service-arm64
binary: build/usr/bin/casaos-user-service
env:
- CGO_ENABLED=1
- CC=aarch64-linux-gnu-gcc-10
goos:
- linux
goarch:
- arm64
- id: casaos-user-service-arm-7
binary: build/usr/bin/casaos-user-service
env:
- CGO_ENABLED=1
- CC=arm-linux-gnueabihf-gcc-10
goos:
- linux
goarch:
- arm
goarm:
- 7
- id: casaos-user-service-migration-tool-amd64
binary: build/usr/bin/casaos-user-service-migration-tool
main: ./cmd/migration-tool
env:
- CGO_ENABLED=1
- CC=x86_64-linux-gnu-gcc-10
goos:
- linux
goarch:
- amd64
- id: casaos-user-service-migration-tool-arm64
binary: build/usr/bin/casaos-user-service-migration-tool
main: ./cmd/migration-tool
env:
- CGO_ENABLED=1
- CC=aarch64-linux-gnu-gcc-10
goos:
- linux
goarch:
- arm64
- id: casaos-user-service-migration-tool-arm-7
binary: build/usr/bin/casaos-user-service-migration-tool
main: ./cmd/migration-tool
env:
- CGO_ENABLED=1
- CC=arm-linux-gnueabihf-gcc-10
goos:
- linux
goarch:
- arm
goarm:
- 7
archives:
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}"
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}"
id: casaos-user-service
builds:
- casaos-user-service-amd64
- casaos-user-service-arm64
- casaos-user-service-arm-7
replacements:
arm: arm-7
files:
- build/**/*
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}"
id: casaos-user-service-migration-tool
builds:
- casaos-user-service-migration-tool-amd64
- casaos-user-service-migration-tool-arm64
- casaos-user-service-migration-tool-arm-7
replacements:
arm: arm-7
files:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

__get_existing_version() {
local version

echo "$version"
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

set -e

Empty file.
37 changes: 37 additions & 0 deletions cmd/migration-tool/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"log"
"os"
)

type Logger struct {
DebugMode bool

_debug *log.Logger
_info *log.Logger
_error *log.Logger
}

func NewLogger() *Logger {
return &Logger{
DebugMode: false,
_debug: log.New(os.Stdout, "DEBUG: ", 0),
_info: log.New(os.Stdout, "", 0),
_error: log.New(os.Stderr, "ERROR: ", 0),
}
}

func (l *Logger) Debug(format string, v ...interface{}) {
if l.DebugMode {
l._debug.Printf(format, v...)
}
}

func (l *Logger) Info(format string, v ...interface{}) {
l._info.Printf(format, v...)
}

func (l *Logger) Error(format string, v ...interface{}) {
l._error.Printf(format, v...)
}
90 changes: 90 additions & 0 deletions cmd/migration-tool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"flag"
"fmt"
"os"

interfaces "github.com/IceWhaleTech/CasaOS-Common"
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
"github.com/IceWhaleTech/CasaOS-UserService/common"
)

const (
userServiceConfigSampleFilePath = "/etc/casaos/user-service.conf.sample"
userServiceName = "casaos-user-service.service"
)

var _logger *Logger

func main() {
versionFlag := flag.Bool("v", false, "version")
debugFlag := flag.Bool("d", true, "debug")
forceFlag := flag.Bool("f", false, "force")
flag.Parse()

if *versionFlag {
fmt.Println(common.Version)
os.Exit(0)
}

_logger = NewLogger()

if os.Getuid() != 0 {
_logger.Info("Root privileges are required to run this program.")
os.Exit(1)
}

if *debugFlag {
_logger.DebugMode = true
}

if !*forceFlag {
serviceEnabled, err := systemctl.IsServiceEnabled(userServiceName)
if err != nil {
_logger.Error("Failed to check if %s is enabled", userServiceName)
panic(err)
}

if serviceEnabled {
_logger.Info("%s is already enabled. If migration is still needed, try with -f.", userServiceName)
os.Exit(1)
}
}

migrationTools := []interfaces.MigrationTool{
NewMigrationToolFor033_034_035(),
}

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.Info("No migration to proceed.")
return
}

if err := selectedMigrationTool.PreMigrate(); err != nil {
panic(err)
}

if err := selectedMigrationTool.Migrate(); err != nil {
panic(err)
}

if err := selectedMigrationTool.PostMigrate(); err != nil {
panic(err)
}
}
97 changes: 97 additions & 0 deletions cmd/migration-tool/migration-033-034-035.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
interfaces "github.com/IceWhaleTech/CasaOS-Common"
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
"gopkg.in/ini.v1"
)

type migrationTool struct{}

func (u *migrationTool) IsMigrationNeeded() (bool, error) {
_logger.Info("Checking if migration is needed for CasaoS version between 0.3.3 and 0.3.5...")

minorVersion, err := version.DetectMinorVersion()
if err != nil {
return false, err
}

if minorVersion != 3 {
return false, nil
}

// this is the best way to tell if CasaOS version is between 0.3.3 and 0.3.5
isUserDataInDatabase, err := version.IsUserDataInDatabase()
if err != nil {
return false, err
}

if !isUserDataInDatabase {
return false, nil
}

return true, nil
}

func (u *migrationTool) PreMigrate() error {
_logger.Info("Copying %s to %s if it doesn't exist...", userServiceConfigSampleFilePath, config.UserServiceConfigFilePath)
if err := file.CopySingleFile(userServiceConfigSampleFilePath, config.UserServiceConfigFilePath, "skip"); err != nil {
return err
}
return nil
}

func (u *migrationTool) Migrate() error {
_logger.Info("Loading legacy %s...", version.LegacyCasaOSConfigFilePath)
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
if err != nil {
return err
}

// LogPath
logPath, err := legacyConfigFile.Section("app").GetKey("LogPath")
if err != nil {
return err
}

// LogFileExt
logFileExt, err := legacyConfigFile.Section("app").GetKey("LogFileExt")
if err != nil {
return err
}

// DBPath
dbPath, err := legacyConfigFile.Section("app").GetKey("DBPath")
if err != nil {
return err
}

// UserDataPath
userDataPath, err := legacyConfigFile.Section("app").GetKey("UserDataPath")
if err != nil {
return err
}

_logger.Info("Updating %s with settings from legacy configuration...", config.UserServiceConfigFilePath)
config.InitSetup(config.UserServiceConfigFilePath)

config.AppInfo.LogPath = logPath.Value()
config.AppInfo.LogSaveName = logSaveName.Value()
config.AppInfo.LogFileExt = logFileExt.Value()
config.AppInfo.DBPath = dbPath.Value()
config.AppInfo.UserDataPath = userDataPath.Value()

config.SaveSetup(config.UserServiceConfigFilePath)

return nil
}

func (u *migrationTool) PostMigrate() error {
return nil
}

func NewMigrationToolFor033_034_035() interfaces.MigrationTool {
return &migrationTool{}
}
3 changes: 3 additions & 0 deletions common/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package common

const Version = "0.3.5"
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ module github.com/IceWhaleTech/CasaOS-UserService
go 1.18

require (
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220804224534-081d69c201cc
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220811230555-229797f76426
github.com/IceWhaleTech/CasaOS-Gateway v0.0.0-20220804231126-285796241a3b
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/satori/go.uuid v1.2.0
github.com/tidwall/gjson v1.14.1
go.uber.org/zap v1.21.0
gopkg.in/ini.v1 v1.66.6
gopkg.in/ini.v1 v1.67.0
gorm.io/driver/sqlite v1.3.6
gorm.io/gorm v1.23.8
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down
Loading

0 comments on commit 0b66fd8

Please sign in to comment.