Bladeren bron

add send notify function

LinkLeong 2 jaren geleden
bovenliggende
commit
9f2e8dae6f

+ 1 - 0
build/scripts/migration/service.d/casaos/migration.list

@@ -1,3 +1,4 @@
 LEGACY_WITHOUT_VERSION v0.3.6
 LEGACY_WITHOUT_VERSION v0.3.6
 v0.3.5 v0.3.6
 v0.3.5 v0.3.6
 v0.3.5.1 v0.3.6
 v0.3.5.1 v0.3.6
+v0.3.6 v0.3.7

+ 0 - 10
cmd/migration-tool/log.go

@@ -1,13 +1,3 @@
-/*
- * @Author: LinkLeong link@icewhale.org
- * @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
 package main
 
 
 import (
 import (

+ 1 - 0
cmd/migration-tool/main.go

@@ -80,6 +80,7 @@ func main() {
 
 
 	migrationTools := []interfaces.MigrationTool{
 	migrationTools := []interfaces.MigrationTool{
 		NewMigrationToolFor_035(),
 		NewMigrationToolFor_035(),
+		NewMigrationToolFor_036(),
 	}
 	}
 
 
 	var selectedMigrationTool interfaces.MigrationTool
 	var selectedMigrationTool interfaces.MigrationTool

+ 5 - 5
cmd/migration-tool/migration-034-035.go

@@ -25,9 +25,9 @@ import (
 	"github.com/IceWhaleTech/CasaOS/service"
 	"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()
 	majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion()
 	if err != nil {
 	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
 	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") {
 	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().UpdateUSBAutoMount("False")
@@ -173,7 +173,7 @@ func (u *migrationTool) Migrate() error {
 	return nil
 	return nil
 }
 }
 
 
-func (u *migrationTool) PostMigrate() error {
+func (u *migrationTool036) PostMigrate() error {
 	return nil
 	return nil
 }
 }
 
 

+ 74 - 0
cmd/migration-tool/migration-036.go

@@ -0,0 +1,74 @@
+/*
+ * @Author: LinkLeong link@icewhale.org
+ * @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{}
+}

+ 67 - 0
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
+}

+ 13 - 1
main.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
+	"path/filepath"
 	"time"
 	"time"
 
 
 	"github.com/IceWhaleTech/CasaOS-Gateway/common"
 	"github.com/IceWhaleTech/CasaOS-Gateway/common"
@@ -12,10 +13,12 @@ import (
 	"github.com/IceWhaleTech/CasaOS/pkg/cache"
 	"github.com/IceWhaleTech/CasaOS/pkg/cache"
 	"github.com/IceWhaleTech/CasaOS/pkg/config"
 	"github.com/IceWhaleTech/CasaOS/pkg/config"
 	"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
 	"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
+	"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
 	"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
 	"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
 	"github.com/IceWhaleTech/CasaOS/route"
 	"github.com/IceWhaleTech/CasaOS/route"
 	"github.com/IceWhaleTech/CasaOS/service"
 	"github.com/IceWhaleTech/CasaOS/service"
 	"github.com/IceWhaleTech/CasaOS/types"
 	"github.com/IceWhaleTech/CasaOS/types"
+	"go.uber.org/zap"
 
 
 	"github.com/robfig/cron"
 	"github.com/robfig/cron"
 	"gorm.io/gorm"
 	"gorm.io/gorm"
@@ -107,7 +110,7 @@ func main() {
 	if err != nil {
 	if err != nil {
 		panic(err)
 		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 {
 	for _, v := range routers {
 		err = service.MyService.Gateway().CreateRoute(&common.Route{
 		err = service.MyService.Gateway().CreateRoute(&common.Route{
 			Path:   "/v1/" + v,
 			Path:   "/v1/" + v,
@@ -141,6 +144,15 @@ func main() {
 	// 	MaxHeaderBytes: 1 << 20,
 	// 	MaxHeaderBytes: 1 << 20,
 	// }
 	// }
 	// s.ListenAndServe()
 	// 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)
 	err = http.Serve(listener, r)
 	if err != nil {
 	if err != nil {
 		panic(err)
 		panic(err)

+ 8 - 1
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/socket-port", v1.GetSystemSocketPort) //sys/socket_port
 	r.GET("/v1/sys/version/check", v1.GetSystemCheckVersion)
 	r.GET("/v1/sys/version/check", v1.GetSystemCheckVersion)
-
+	r.GET("/ping", func(ctx *gin.Context) {
+		ctx.String(200, "pong")
+	})
 	v1Group := r.Group("/v1")
 	v1Group := r.Group("/v1")
 
 
 	v1Group.Use(jwt2.JWT())
 	v1Group.Use(jwt2.JWT())
@@ -238,6 +240,11 @@ func InitRouter() *gin.Engine {
 				v1SharesGroup.GET("/status", v1.GetSambaStatus)
 				v1SharesGroup.GET("/status", v1.GetSambaStatus)
 			}
 			}
 		}
 		}
+		v1NotifyGroup := v1Group.Group("/notify")
+		v1NotifyGroup.Use()
+		{
+			v1NotifyGroup.POST("/:path", v1.PostNotifyMssage)
+		}
 	}
 	}
 	return r
 	return r
 }
 }

+ 2 - 2
route/v1/docker.go

@@ -38,7 +38,7 @@ var upgrader = websocket.Upgrader{
 	HandshakeTimeout: time.Duration(time.Second * 5),
 	HandshakeTimeout: time.Duration(time.Second * 5),
 }
 }
 
 
-//打开docker的terminal
+// 打开docker的terminal
 func DockerTerminal(c *gin.Context) {
 func DockerTerminal(c *gin.Context) {
 	col := c.DefaultQuery("cols", "100")
 	col := c.DefaultQuery("cols", "100")
 	row := c.DefaultQuery("rows", "30")
 	row := c.DefaultQuery("rows", "30")
@@ -877,7 +877,7 @@ func UpdateSetting(c *gin.Context) {
 	if err != nil {
 	if err != nil {
 		service.MyService.Docker().DockerContainerUpdateName(m.ContainerName, id)
 		service.MyService.Docker().DockerContainerUpdateName(m.ContainerName, id)
 		service.MyService.Docker().DockerContainerStart(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
 		return
 	}
 	}
 	//		echo -e "hellow\nworld" >>
 	//		echo -e "hellow\nworld" >>

+ 16 - 0
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)})
+}

+ 16 - 0
service/notify.go

@@ -37,12 +37,28 @@ type NotifyServer interface {
 	SendInstallAppBySocket(app notify.Application)
 	SendInstallAppBySocket(app notify.Application)
 	SendAllHardwareStatusBySocket(disk model2.Summary, list []model2.DriveUSB, mem map[string]interface{}, cpu map[string]interface{}, netList []model2.IOCountersStat)
 	SendAllHardwareStatusBySocket(disk model2.Summary, list []model2.DriveUSB, mem map[string]interface{}, cpu map[string]interface{}, netList []model2.IOCountersStat)
 	SendStorageBySocket(message notify.StorageMessage)
 	SendStorageBySocket(message notify.StorageMessage)
+	SendNotify(path string, message map[string]interface{})
 }
 }
 
 
 type notifyServer struct {
 type notifyServer struct {
 	db *gorm.DB
 	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) {
 func (i *notifyServer) SendStorageBySocket(message notify.StorageMessage) {
 	body := make(map[string]interface{})
 	body := make(map[string]interface{})
 	body["data"] = message
 	body["data"] = message