-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add migration from versions 0.3.3, 0.3.4, 0.3.5 (#3)
- Loading branch information
Showing
20 changed files
with
358 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
build/usr/share/casaos/migration/script.d/01-migrate-user-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
1 change: 1 addition & 0 deletions
1
build/usr/share/casaos/migration/service.d/user-service/from-0.3.4.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from-0.3.sh |
Empty file.
4 changes: 4 additions & 0 deletions
4
build/usr/share/casaos/setup/script.d/01-setup-user-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
1 change: 1 addition & 0 deletions
1
build/usr/share/casaos/setup/service.d/user-service/debian/bullseye/setup-user-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../setup-user-service.sh |
Empty file.
1 change: 1 addition & 0 deletions
1
build/usr/share/casaos/setup/service.d/user-service/ubuntu/jammy/setup-user-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../setup-user-service.sh |
1 change: 1 addition & 0 deletions
1
build/usr/share/casaos/setup/service.d/user-service/ubuntu/setup-user-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../debian/setup-user-service.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package common | ||
|
||
const Version = "0.3.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.