From df0f0159443be324dfc616acb322b29f25e0a1c4 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 13 Oct 2022 16:09:19 -0400 Subject: [PATCH 1/2] wip --- route/route.go | 3 --- route/v1/system.go | 61 +++++----------------------------------------- service/system.go | 42 +++++++++++++------------------ 3 files changed, 23 insertions(+), 83 deletions(-) diff --git a/route/route.go b/route/route.go index a876849..19f2b3b 100644 --- a/route/route.go +++ b/route/route.go @@ -146,9 +146,6 @@ func InitRouter() *gin.Engine { // v1SysGroup.GET("/disk", v1.GetSystemDiskInfo) // v1SysGroup.GET("/network", v1.GetSystemNetInfo) - v1SysGroup.PUT("/usb-auto-mount", v1.PutSystemUSBAutoMount) ///sys/usb/:status - v1SysGroup.GET("/usb-auto-mount", v1.GetSystemUSBAutoMount) ///sys/usb/status - v1SysGroup.GET("/server-info", nil) v1SysGroup.PUT("/server-info", nil) v1SysGroup.GET("/apps-state", v1.GetSystemAppsStatus) diff --git a/route/v1/system.go b/route/v1/system.go index 95674e7..9872ee3 100644 --- a/route/v1/system.go +++ b/route/v1/system.go @@ -164,51 +164,6 @@ func PostKillCasaOS(c *gin.Context) { os.Exit(0) } -// @Summary Turn off usb auto-mount -// @Produce application/json -// @Accept application/json -// @Tags sys -// @Security ApiKeyAuth -// @Success 200 {string} string "ok" -// @Router /sys/usb/off [put] -func PutSystemUSBAutoMount(c *gin.Context) { - js := make(map[string]string) - c.ShouldBind(&js) - status := js["state"] - if status == "on" { - service.MyService.System().UpdateUSBAutoMount("True") - service.MyService.System().ExecUSBAutoMountShell("True") - } else { - service.MyService.System().UpdateUSBAutoMount("False") - service.MyService.System().ExecUSBAutoMountShell("False") - } - c.JSON(common_err.SUCCESS, - model.Result{ - Success: common_err.SUCCESS, - Message: common_err.GetMsg(common_err.SUCCESS), - }) -} - -// @Summary Turn off usb auto-mount -// @Produce application/json -// @Accept application/json -// @Tags sys -// @Security ApiKeyAuth -// @Success 200 {string} string "ok" -// @Router /sys/usb [get] -func GetSystemUSBAutoMount(c *gin.Context) { - state := "True" - if config.ServerInfo.USBAutoMount == "False" { - state = "False" - } - c.JSON(common_err.SUCCESS, - model.Result{ - Success: common_err.SUCCESS, - Message: common_err.GetMsg(common_err.SUCCESS), - Data: state, - }) -} - func GetSystemAppsStatus(c *gin.Context) { systemAppList := service.MyService.App().GetSystemAppList() appList := []model2.MyAppList{} @@ -225,12 +180,12 @@ func GetSystemAppsStatus(c *gin.Context) { Id: v.ID, Port: v.Labels["web"], Index: v.Labels["index"], - //Order: m.Labels["order"], + // Order: m.Labels["order"], Image: v.Image, Latest: false, - //Type: m.Labels["origin"], - //Slogan: m.Slogan, - //Rely: m.Rely, + // Type: m.Labels["origin"], + // Slogan: m.Slogan, + // Rely: m.Rely, Host: v.Labels["host"], Protocol: v.Labels["protocol"], }) @@ -251,7 +206,6 @@ func GetSystemAppsStatus(c *gin.Context) { // @Success 200 {string} string "ok" // @Router /sys/hardware/info [get] func GetSystemHardwareInfo(c *gin.Context) { - data := make(map[string]string, 1) data["drive_model"] = service.MyService.System().GetDeviceTree() c.JSON(common_err.SUCCESS, @@ -270,7 +224,7 @@ func GetSystemHardwareInfo(c *gin.Context) { // @Success 200 {string} string "ok" // @Router /sys/utilization [get] func GetSystemUtilization(c *gin.Context) { - var data = make(map[string]interface{}) + data := make(map[string]interface{}) cpu := service.MyService.System().GetCpuPercent() num := service.MyService.System().GetCpuCoreNum() cpuData := make(map[string]interface{}) @@ -282,7 +236,7 @@ func GetSystemUtilization(c *gin.Context) { data["cpu"] = cpuData data["mem"] = service.MyService.System().GetMemInfo() - //拼装网络信息 + // 拼装网络信息 netList := service.MyService.System().GetNetInfo() newNet := []model.IOCountersStat{} nets := service.MyService.System().GetNet(true) @@ -313,7 +267,6 @@ func GetSystemUtilization(c *gin.Context) { // @Success 200 {string} string "ok" // @Router /sys/socket/port [get] func GetSystemSocketPort(c *gin.Context) { - c.JSON(common_err.SUCCESS, model.Result{ Success: common_err.SUCCESS, @@ -336,7 +289,6 @@ func GetSystemCupInfo(c *gin.Context) { data["percent"] = cpu data["num"] = num c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data}) - } // @Summary get mem info @@ -349,7 +301,6 @@ func GetSystemCupInfo(c *gin.Context) { func GetSystemMemInfo(c *gin.Context) { mem := service.MyService.System().GetMemInfo() c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: mem}) - } // @Summary get disk info diff --git a/service/system.go b/service/system.go index 8717e4c..b7feef8 100644 --- a/service/system.go +++ b/service/system.go @@ -30,8 +30,6 @@ type SystemService interface { UpdateAssist() UpSystemPort(port string) GetTimeZone() string - UpdateUSBAutoMount(state string) - ExecUSBAutoMountShell(state string) UpAppOrderFile(str, id string) GetAppOrderFile(id string) []byte GetNet(physics bool) []string @@ -53,14 +51,7 @@ type SystemService interface { GetCPUTemperature() int GetCPUPower() map[string]string } -type systemService struct { -} - -func (s *systemService) UpdateUSBAutoMount(state string) { - config.ServerInfo.USBAutoMount = state - config.Cfg.Section("server").Key("USBAutoMount").SetValue(state) - config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath) -} +type systemService struct{} func (c *systemService) MkdirAll(path string) (int, error) { _, err := os.Stat(path) @@ -76,8 +67,8 @@ func (c *systemService) MkdirAll(path string) (int, error) { } return common_err.SERVICE_ERROR, err } -func (c *systemService) RenameFile(oldF, newF string) (int, error) { +func (c *systemService) RenameFile(oldF, newF string) (int, error) { _, err := os.Stat(newF) if err == nil { return common_err.DIR_ALREADY_EXISTS, nil @@ -92,6 +83,7 @@ func (c *systemService) RenameFile(oldF, newF string) (int, error) { } return common_err.SERVICE_ERROR, err } + func (c *systemService) CreateFile(path string) (int, error) { _, err := os.Stat(path) if err == nil { @@ -104,9 +96,11 @@ func (c *systemService) CreateFile(path string) (int, error) { } return common_err.SERVICE_ERROR, err } + func (c *systemService) GetDeviceTree() string { return command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;GetDeviceTree") } + func (c *systemService) GetSysInfo() host.InfoStat { info, _ := host.Info() return *info @@ -128,9 +122,7 @@ func (c *systemService) GetNetState(name string) string { } func (c *systemService) GetDirPathOne(path string) (m model.Path) { - f, err := os.Stat(path) - if err != nil { return } @@ -175,6 +167,7 @@ func (c *systemService) GetDirPath(path string) []model.Path { } return dirs } + func (c *systemService) GetCpuInfo() []cpu.InfoStat { info, _ := cpu.Info() return info @@ -207,6 +200,7 @@ func (c *systemService) GetNetInfo() []net.IOCountersStat { parts, _ := net.IOCounters(true) return parts } + func (c *systemService) GetNet(physics bool) []string { t := "1" if physics { @@ -220,11 +214,12 @@ func (s *systemService) UpdateSystemVersion(version string) { os.Remove(config.AppInfo.LogPath + "/upgrade.log") } file.CreateFile(config.AppInfo.LogPath + "/upgrade.log") - //go command2.OnlyExec("curl -fsSL https://raw.githubusercontent.com/LinkLeong/casaos-alpha/main/update.sh | bash") + // go command2.OnlyExec("curl -fsSL https://raw.githubusercontent.com/LinkLeong/casaos-alpha/main/update.sh | bash") go command2.OnlyExec("curl -fsSL https://raw.githubusercontent.com/IceWhaleTech/get/main/update.sh | bash") - //s.log.Error(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version) - //s.log.Error(command2.ExecResultStr(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)) + // s.log.Error(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version) + // s.log.Error(command2.ExecResultStr(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)) } + func (s *systemService) UpdateAssist() { command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/assist.sh") } @@ -233,14 +228,6 @@ func (s *systemService) GetTimeZone() string { return command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;GetTimeZone") } -func (s *systemService) ExecUSBAutoMountShell(state string) { - if state == "False" { - command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Stop_Auto") - } else { - command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Start_Auto") - } -} - func (s *systemService) GetSystemConfigDebug() []string { return command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/helper.sh ;GetSysInfo") } @@ -248,9 +235,11 @@ func (s *systemService) GetSystemConfigDebug() []string { func (s *systemService) UpAppOrderFile(str, id string) { file.WriteToPath([]byte(str), config.AppInfo.DBPath+"/"+id, "app_order.json") } + func (s *systemService) GetAppOrderFile(id string) []byte { return file.ReadFullFile(config.AppInfo.UserDataPath + "/" + id + "/app_order.json") } + func (s *systemService) UpSystemPort(port string) { if len(port) > 0 && port != config.ServerInfo.HttpPort { config.Cfg.Section("server").Key("HttpPort").SetValue(port) @@ -258,6 +247,7 @@ func (s *systemService) UpSystemPort(port string) { } config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath) } + func (s *systemService) GetCasaOSLogs(lineNumber int) string { file, err := os.Open(filepath.Join(config.AppInfo.LogPath, fmt.Sprintf("%s.%s", config.AppInfo.LogSaveName, @@ -294,8 +284,8 @@ func GetDeviceAllIP() []string { func (s *systemService) IsServiceRunning(name string) bool { status := command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;CheckServiceStatus smbd") return strings.TrimSpace(status) == "running" - } + func (s *systemService) GetCPUTemperature() int { outPut := "" if file.Exists("/sys/class/thermal/thermal_zone0/temp") { @@ -313,6 +303,7 @@ func (s *systemService) GetCPUTemperature() int { } return celsius } + func (s *systemService) GetCPUPower() map[string]string { data := make(map[string]string, 2) data["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10) @@ -323,6 +314,7 @@ func (s *systemService) GetCPUPower() map[string]string { } return data } + func NewSystemService() SystemService { return &systemService{} } From 8908c399698e7d95f78bf648c91289715590f9e7 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 13 Oct 2022 19:33:18 -0400 Subject: [PATCH 2/2] wip --- .../{migration-036.go => migration-sample.go} | 14 -------------- 1 file changed, 14 deletions(-) rename cmd/migration-tool/{migration-036.go => migration-sample.go} (73%) diff --git a/cmd/migration-tool/migration-036.go b/cmd/migration-tool/migration-sample.go similarity index 73% rename from cmd/migration-tool/migration-036.go rename to cmd/migration-tool/migration-sample.go index b6d5410..c45858d 100644 --- a/cmd/migration-tool/migration-036.go +++ b/cmd/migration-tool/migration-sample.go @@ -11,18 +11,13 @@ package main import ( - "strings" - interfaces "github.com/IceWhaleTech/CasaOS-Common" "github.com/IceWhaleTech/CasaOS-Common/utils/version" - "github.com/IceWhaleTech/CasaOS/pkg/config" - "github.com/IceWhaleTech/CasaOS/service" ) type migrationTool struct{} func (u *migrationTool) IsMigrationNeeded() (bool, error) { - majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion() if err != nil { if err == version.ErrLegacyVersionNotFound { @@ -46,22 +41,13 @@ func (u *migrationTool) IsMigrationNeeded() (bool, error) { _logger.Info("Migration is needed for a CasaOS version 0.3.5 and older...") return true, nil - } func (u *migrationTool) PreMigrate() error { - return nil } func (u *migrationTool) Migrate() error { - - if service.MyService.System().GetSysInfo().KernelArch == "aarch64" && config.ServerInfo.USBAutoMount != "True" && strings.Contains(service.MyService.System().GetDeviceTree(), "Raspberry Pi") { - service.MyService.System().UpdateUSBAutoMount("False") - service.MyService.System().ExecUSBAutoMountShell("False") - } - - _logger.Info("update done") return nil }