Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remount if the configuration file already exists #877

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/sysroot/usr/lib/systemd/system/casaos.service
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[Unit]
After=casaos-message-bus.service
After=rclone.service
ConditionFileNotEmpty=/etc/casaos/casaos.conf
Description=CasaOS Main Service

Expand Down
12 changes: 0 additions & 12 deletions build/sysroot/usr/share/casaos/shell/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ GetLocalJoinNetworks() {
zerotier-cli listnetworks -j
}

#移除挂载点,删除已挂在的文件夹
UMountPorintAndRemoveDir() {
DEVICE=$1
MOUNT_POINT=$(mount | grep ${DEVICE} | awk '{ print $3 }')
if [[ -z ${MOUNT_POINT} ]]; then
${log} "Warning: ${DEVICE} is not mounted"
else
umount -lf ${DEVICE}
/bin/rmdir "${MOUNT_POINT}"
fi
}

#格式化fat32磁盘
#param 需要格式化的目录 /dev/sda1
#param 格式
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/labstack/echo/v4 v4.10.0
github.com/maruel/natural v1.1.0
github.com/mholt/archiver/v3 v3.5.1
github.com/moby/sys/mount v0.3.3
github.com/moby/sys/mountinfo v0.6.2
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
Expand All @@ -41,6 +42,7 @@ require (
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.5.0
golang.org/x/oauth2 v0.3.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gorm.io/gorm v1.24.3
gotest.tools v2.2.0+incompatible
)
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOj
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
github.com/moby/sys/mount v0.3.3 h1:fX1SVkXFJ47XWDoeFW4Sq7PdQJnV2QIDZAqjNqgEjUs=
github.com/moby/sys/mount v0.3.3/go.mod h1:PBaEorSNTLG5t/+4EgukEQVlAvVEc6ZjTySwKdqp5K0=
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -321,6 +323,7 @@ golang.org/x/oauth2 v0.3.0 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func main() {
route.V2APIPath,
route.V2DocPath,
route.V3FilePath,
route.V4DirPath,
}
for _, apiPath := range routers {
err = service.MyService.Gateway().CreateRoute(&model.Route{
Expand Down
7 changes: 4 additions & 3 deletions route/periodical.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ func SendAllHardwareStatusBySocket() {

body["sys_net"] = newNet
systemTempMap := service.MyService.Notify().GetSystemTempMap()
for k, v := range systemTempMap {
body[k] = v
}
systemTempMap.Range(func(key, value interface{}) bool {
body[key.(string)] = value
return true
})
service.MyService.Notify().SendNotify("casaos:system:utilization", body)
}

Expand Down
16 changes: 4 additions & 12 deletions route/v1/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ func GetRecoverStorage(c *gin.Context) {
username = fileutil.NameAccumulation(username, "/mnt")
dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 {
c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
service.MyService.Storage().CheckAndMountByName(username)
notify["status"] = "warn"
notify["message"] = "The same configuration has been added"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username)
}
dmap := make(map[string]string)
dmap["client_id"] = add.ClientID
Expand Down Expand Up @@ -115,12 +111,8 @@ func GetRecoverStorage(c *gin.Context) {
username = fileutil.NameAccumulation(username, "/mnt")
dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 {
c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
service.MyService.Storage().CheckAndMountByName(username)
notify["status"] = "warn"
notify["message"] = "The same configuration has been added"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username)
}
dmap := make(map[string]string)
dmap["client_id"] = add.AppKey
Expand Down
9 changes: 8 additions & 1 deletion route/v1/samba.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"regexp"
"strings"

"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
"go.uber.org/zap"

"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/samba"
Expand Down Expand Up @@ -203,7 +205,12 @@ func DeleteSambaConnections(c *gin.Context) {
return
}
for _, v := range mountPointList {
service.MyService.Connections().UnmountSmaba(v.Path)
err := service.MyService.Connections().UnmountSmaba(v.Path)
if err != nil {
logger.Error("unmount smaba error", zap.Error(err), zap.Any("path", v.Path))
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
}
}
dir, _ := ioutil.ReadDir(connection.MountPoint)
if len(dir) == 0 {
Expand Down
8 changes: 5 additions & 3 deletions route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ func GetSystemUtilization(c *gin.Context) {
}

data["net"] = newNet
for k, v := range service.MyService.Notify().GetSystemTempMap() {
data[k] = v
}
systemMap := service.MyService.Notify().GetSystemTempMap()
systemMap.Range(func(key, value interface{}) bool {
data[key.(string)] = value
return true
})
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
}

Expand Down
4 changes: 2 additions & 2 deletions route/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func InitV2Router() http.Handler {

e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
// return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
return true
return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
//return true
},
ParseTokenFunc: func(token string, c echo.Context) (interface{}, error) {
claims, code := jwt.Validate(token)
Expand Down
8 changes: 4 additions & 4 deletions service/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
"github.com/IceWhaleTech/CasaOS/service/model"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
"github.com/moby/sys/mount"
"gorm.io/gorm"
)

Expand All @@ -26,7 +27,7 @@ type ConnectionsService interface {
DeleteConnection(id string)
UpdateConnection(connection *model2.ConnectionsDBModel)
MountSmaba(username, host, directory, port, mountPoint, password string) string
UnmountSmaba(mountPoint string) string
UnmountSmaba(mountPoint string) error
}

type connectionsStruct struct {
Expand Down Expand Up @@ -59,9 +60,8 @@ func (s *connectionsStruct) MountSmaba(username, host, directory, port, mountPoi
str := command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;MountCIFS " + username + " " + host + " " + directory + " " + port + " " + mountPoint + " " + password)
return str
}
func (s *connectionsStruct) UnmountSmaba(mountPoint string) string {
str := command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;UMountPorintAndRemoveDir " + mountPoint)
return str
func (s *connectionsStruct) UnmountSmaba(mountPoint string) error {
return mount.Unmount(mountPoint)
}

func NewConnectionsService(db *gorm.DB) ConnectionsService {
Expand Down
12 changes: 7 additions & 5 deletions service/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
"go.uber.org/zap"
"golang.org/x/sync/syncmap"

socketio "github.com/googollee/go-socket.io"
"github.com/gorilla/websocket"
Expand All @@ -36,17 +37,18 @@ type NotifyServer interface {
//SendInstallAppBySocket(app notifyCommon.Application)
SendNotify(name string, message map[string]interface{})
SettingSystemTempData(message map[string]interface{})
GetSystemTempMap() map[string]interface{}
GetSystemTempMap() syncmap.Map
}

type notifyServer struct {
db *gorm.DB
SystemTempMap map[string]interface{}
SystemTempMap syncmap.Map //[string]interface{}
}

func (i *notifyServer) SettingSystemTempData(message map[string]interface{}) {
for k, v := range message {
i.SystemTempMap[k] = v
i.SystemTempMap.Store(k, v)
//i.SystemTempMap[k] = v
}
}

Expand Down Expand Up @@ -340,10 +342,10 @@ func SendMeg() {
// }

// }
func (i *notifyServer) GetSystemTempMap() map[string]interface{} {
func (i *notifyServer) GetSystemTempMap() syncmap.Map {
return i.SystemTempMap
}

func NewNotifyService(db *gorm.DB) NotifyServer {
return &notifyServer{db: db, SystemTempMap: make(map[string]interface{})}
return &notifyServer{db: db, SystemTempMap: syncmap.Map{}}
}