Skip to content

CasaOS - A simple, easy-to-use, elegant open-source Personal Cloud system.

add send notify function

LinkLeongd14381e6a249
diff --git a/build/scripts/migration/service.d/casaos/migration.list b/build/scripts/migration/service.d/casaos/migration.list
index 2035e42..d3ce03f 100644
--- a/build/scripts/migration/service.d/casaos/migration.list
+++ b/build/scripts/migration/service.d/casaos/migration.list
@@ -1,3 +1,4 @@
LEGACY_WITHOUT_VERSION v0.3.6
v0.3.5 v0.3.6
v0.3.5.1 v0.3.6
+v0.3.6 v0.3.7
diff --git a/cmd/migration-tool/log.go b/cmd/migration-tool/log.go
index e20da69..ed69983 100644
--- a/cmd/migration-tool/log.go
+++ b/cmd/migration-tool/log.go
@@ -1,13 +1,3 @@
-/*
- * @Author: LinkLeong [email protected]
- * @Date: 2022-08-30 22:15:30
- * @LastEditors: LinkLeong
- * @LastEditTime: 2022-08-30 22:15:47
- * @FilePath: /CasaOS/cmd/migration-tool/log.go
- * @Description:
- * @Website: https://www.casaos.io
- * Copyright (c) 2022 by icewhale, All Rights Reserved.
- */
package main
import (
diff --git a/cmd/migration-tool/main.go b/cmd/migration-tool/main.go
index a09d5b8..5f7c60b 100644
--- a/cmd/migration-tool/main.go
+++ b/cmd/migration-tool/main.go
@@ -80,6 +80,7 @@ func main() {
migrationTools := []interfaces.MigrationTool{
NewMigrationToolFor_035(),
+ NewMigrationToolFor_036(),
}
var selectedMigrationTool interfaces.MigrationTool
diff --git a/cmd/migration-tool/migration-034-035.go b/cmd/migration-tool/migration-034-035.go
index 4fa2ed3..3dfed60 100644
--- a/cmd/migration-tool/migration-034-035.go
+++ b/cmd/migration-tool/migration-034-035.go
@@ -25,9 +25,9 @@ import (
"github.com/IceWhaleTech/CasaOS/service"
)
-type migrationTool struct{}
+type migrationTool036 struct{}
-func (u *migrationTool) IsMigrationNeeded() (bool, error) {
+func (u *migrationTool036) IsMigrationNeeded() (bool, error) {
majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion()
if err != nil {
@@ -55,12 +55,12 @@ func (u *migrationTool) IsMigrationNeeded() (bool, error) {
}
-func (u *migrationTool) PreMigrate() error {
+func (u *migrationTool036) PreMigrate() error {
return nil
}
-func (u *migrationTool) Migrate() error {
+func (u *migrationTool036) 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")
@@ -173,7 +173,7 @@ func (u *migrationTool) Migrate() error {
return nil
}
-func (u *migrationTool) PostMigrate() error {
+func (u *migrationTool036) PostMigrate() error {
return nil
}
diff --git a/cmd/migration-tool/migration-036.go b/cmd/migration-tool/migration-036.go
new file mode 100644
index 0000000..b6d5410
--- /dev/null
+++ b/cmd/migration-tool/migration-036.go
@@ -0,0 +1,74 @@
+/*
+ * @Author: LinkLeong [email protected]
+ * @Date: 2022-08-24 17:36:00
+ * @LastEditors: LinkLeong
+ * @LastEditTime: 2022-09-05 11:24:27
+ * @FilePath: /CasaOS/cmd/migration-tool/migration-034-035.go
+ * @Description:
+ * @Website: https://www.casaos.io
+ * Copyright (c) 2022 by icewhale, All Rights Reserved.
+ */
+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 {
+ return false, nil
+ }
+
+ return false, err
+ }
+
+ if majorVersion > 0 {
+ return false, nil
+ }
+
+ if minorVersion > 3 {
+ return false, nil
+ }
+
+ if minorVersion == 3 && patchVersion > 5 {
+ return false, nil
+ }
+
+ _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
+}
+
+func (u *migrationTool) PostMigrate() error {
+ return nil
+}
+
+func NewMigrationToolFor_036() interfaces.MigrationTool {
+ return &migrationTool{}
+}
diff --git a/common/notify.go b/common/notify.go
new file mode 100644
index 0000000..4a003d0
--- /dev/null
+++ b/common/notify.go
@@ -0,0 +1,67 @@
+package common
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+const (
+ CasaOSURLFilename = "casaos.url"
+ APICasaOSNotify = "/v1/notiry"
+)
+
+type NotifyService interface {
+ SendNotify(path string, message map[string]interface{}) error
+}
+type notifyService struct {
+ address string
+}
+
+func (n *notifyService) SendNotify(path string, message map[string]interface{}) error {
+
+ url := strings.TrimSuffix(n.address, "/") + "/" + APICasaOSNotify + "/" + path
+ body, err := json.Marshal(message)
+ if err != nil {
+ return err
+ }
+ response, err := http.Post(url, "application/json", bytes.NewBuffer(body))
+ if err != nil {
+ return err
+ }
+
+ if response.StatusCode != http.StatusCreated {
+ return errors.New("failed to send notify (status code: " + fmt.Sprint(response.StatusCode) + ")")
+ }
+ return nil
+
+}
+
+func NewNotifyService(runtimePath string) (NotifyService, error) {
+ casaosAddressFile := filepath.Join(runtimePath, CasaOSURLFilename)
+
+ buf, err := os.ReadFile(casaosAddressFile)
+ if err != nil {
+ return nil, err
+ }
+
+ address := string(buf)
+
+ response, err := http.Get(address + "/ping")
+ if err != nil {
+ return nil, err
+ }
+
+ if response.StatusCode != 200 {
+ return nil, errors.New("failed to ping casaos service")
+ }
+
+ return &notifyService{
+ address: address,
+ }, nil
+}
diff --git a/main.go b/main.go
index b616e52..6ff7af8 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/http"
+ "path/filepath"
"time"
"github.com/IceWhaleTech/CasaOS-Gateway/common"
@@ -12,10 +13,12 @@ import (
"github.com/IceWhaleTech/CasaOS/pkg/cache"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
+ "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
"github.com/IceWhaleTech/CasaOS/route"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/IceWhaleTech/CasaOS/types"
+ "go.uber.org/zap"
"github.com/robfig/cron"
"gorm.io/gorm"
@@ -107,7 +110,7 @@ func main() {
if err != nil {
panic(err)
}
- routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "disks", "storage", "samba"}
+ routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "disks", "storage", "samba", "notify"}
for _, v := range routers {
err = service.MyService.Gateway().CreateRoute(&common.Route{
Path: "/v1/" + v,
@@ -141,6 +144,15 @@ func main() {
// MaxHeaderBytes: 1 << 20,
// }
// s.ListenAndServe()
+ urlFilePath := filepath.Join(config.CommonInfo.RuntimePath, "casaos.url")
+ err = file.CreateFileAndWriteContent(urlFilePath, "http://"+listener.Addr().String())
+ if err != nil {
+ loger.Error("Management service is listening...",
+ zap.Any("address", listener.Addr().String()),
+ zap.Any("filepath", urlFilePath),
+ )
+ }
+
err = http.Serve(listener, r)
if err != nil {
panic(err)
diff --git a/route/route.go b/route/route.go
index 2ce2dc3..39d011b 100644
--- a/route/route.go
+++ b/route/route.go
@@ -40,7 +40,9 @@ func InitRouter() *gin.Engine {
r.GET("/v1/sys/socket-port", v1.GetSystemSocketPort) //sys/socket_port
r.GET("/v1/sys/version/check", v1.GetSystemCheckVersion)
-
+ r.GET("/ping", func(ctx *gin.Context) {
+ ctx.String(200, "pong")
+ })
v1Group := r.Group("/v1")
v1Group.Use(jwt2.JWT())
@@ -238,6 +240,11 @@ func InitRouter() *gin.Engine {
v1SharesGroup.GET("/status", v1.GetSambaStatus)
}
}
+ v1NotifyGroup := v1Group.Group("/notify")
+ v1NotifyGroup.Use()
+ {
+ v1NotifyGroup.POST("/:path", v1.PostNotifyMssage)
+ }
}
return r
}
diff --git a/route/v1/docker.go b/route/v1/docker.go
index 05ebddf..8c1254f 100644
--- a/route/v1/docker.go
+++ b/route/v1/docker.go
@@ -877,7 +877,7 @@ func UpdateSetting(c *gin.Context) {
if err != nil {
service.MyService.Docker().DockerContainerUpdateName(m.ContainerName, id)
service.MyService.Docker().DockerContainerStart(id)
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
+ c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
return
}
// echo -e "hellow\nworld" >>
diff --git a/route/v1/notiry.go b/route/v1/notiry.go
new file mode 100644
index 0000000..c818aab
--- /dev/null
+++ b/route/v1/notiry.go
@@ -0,0 +1,16 @@
+package v1
+
+import (
+ "github.com/IceWhaleTech/CasaOS/model"
+ "github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
+ "github.com/IceWhaleTech/CasaOS/service"
+ "github.com/gin-gonic/gin"
+)
+
+func PostNotifyMssage(c *gin.Context) {
+ path := c.Param("path")
+ message := make(map[string]interface{})
+ c.ShouldBind(&message)
+ service.MyService.Notify().SendNotify(path, message)
+ c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
+}
diff --git a/service/notify.go b/service/notify.go
index 3981e55..154d45a 100644
--- a/service/notify.go
+++ b/service/notify.go
@@ -37,12 +37,28 @@ type NotifyServer interface {
SendInstallAppBySocket(app notify.Application)
SendAllHardwareStatusBySocket(disk model2.Summary, list []model2.DriveUSB, mem map[string]interface{}, cpu map[string]interface{}, netList []model2.IOCountersStat)
SendStorageBySocket(message notify.StorageMessage)
+ SendNotify(path string, message map[string]interface{})
}
type notifyServer struct {
db *gorm.DB
}
+func (i *notifyServer) SendNotify(path string, message map[string]interface{}) {
+
+ msg := gosf.Message{}
+ msg.Body = message
+ msg.Success = true
+ msg.Text = path
+
+ notify := notify.Message{}
+ notify.Path = path
+ notify.Msg = msg
+
+ NotifyMsg <- notify
+
+}
+
func (i *notifyServer) SendStorageBySocket(message notify.StorageMessage) {
body := make(map[string]interface{})
body["data"] = message