add send notify function
This commit is contained in:
parent
466350dd21
commit
d14381e6a2
11 changed files with 202 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
import (
|
||||
|
|
|
@ -80,6 +80,7 @@ func main() {
|
|||
|
||||
migrationTools := []interfaces.MigrationTool{
|
||||
NewMigrationToolFor_035(),
|
||||
NewMigrationToolFor_036(),
|
||||
}
|
||||
|
||||
var selectedMigrationTool interfaces.MigrationTool
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
74
cmd/migration-tool/migration-036.go
Normal file
74
cmd/migration-tool/migration-036.go
Normal file
|
@ -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
common/notify.go
Normal file
67
common/notify.go
Normal file
|
@ -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 ¬ifyService{
|
||||
address: address,
|
||||
}, nil
|
||||
}
|
14
main.go
14
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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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" >>
|
||||
|
|
16
route/v1/notiry.go
Normal file
16
route/v1/notiry.go
Normal file
|
@ -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)})
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue