Skip to content

Commit e7ebdc0

Browse files
authored
Merge pull request #48 from IceWhaleTech/dev
feat: Multiple updates
2 parents 90b9973 + 6c235d3 commit e7ebdc0

35 files changed

+777
-278
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ require (
4848
github.com/smartystreets/goconvey v1.6.4 // indirect
4949
github.com/swaggo/gin-swagger v1.3.0
5050
github.com/swaggo/swag v1.7.3
51-
github.com/tidwall/gjson v1.8.0
52-
github.com/tidwall/pretty v1.2.0 // indirect
51+
github.com/tidwall/gjson v1.10.2
52+
github.com/tidwall/sjson v1.2.3
5353
github.com/tklauser/go-sysconf v0.3.6 // indirect
5454
github.com/ugorji/go v1.2.6 // indirect
5555
go.opencensus.io v0.23.0 // indirect

go.sum

+6-5
Original file line numberDiff line numberDiff line change
@@ -741,13 +741,14 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG
741741
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
742742
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
743743
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
744-
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
745-
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
746-
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
747-
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
748-
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
744+
github.com/tidwall/gjson v1.10.2 h1:APbLGOM0rrEkd8WBw9C24nllro4ajFuJu0Sc9hRz8Bo=
745+
github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
746+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
747+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
749748
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
750749
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
750+
github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8=
751+
github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs=
751752
github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek=
752753
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
753754
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=

middleware/gin.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package middleware
22

33
import (
44
"fmt"
5-
"github.com/gin-gonic/gin"
65
"net/http"
6+
7+
"github.com/gin-gonic/gin"
78
)
89

910
func Cors() gin.HandlerFunc {
@@ -17,7 +18,7 @@ func Cors() gin.HandlerFunc {
1718
//服务器支持的所有跨域请求的方法
1819
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
1920
//允许跨域设置可以返回其他子段,可以自定义字段
20-
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session")
21+
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,Language")
2122
// 允许浏览器(客户端)可以解析的头部 (重要)
2223
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
2324
//设置缓存时间

model/disk.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type LSBLKModel struct {
2424
Tran string `json:"tran"`
2525
MinIO uint64 `json:"min-io"`
2626
UsedPercent float64 `json:"used_percent"`
27+
Serial string `json:"serial"`
2728
Children []LSBLKModel `json:"children"`
2829
//详情特有
2930
StartSector uint64 `json:"start_sector,omitempty"`

model/docker.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package model
2+
3+
type DockerStatsModel struct {
4+
Icon string `json:"icon"`
5+
Title string `json:"title"`
6+
Data interface{} `json:"data"`
7+
Pre interface{} `json:"pre"`
8+
}

model/sys_common.go

+4
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ type SystemConfig struct {
6868
SyncPort string `json:"sync_port"`
6969
SyncKey string `json:"sync_key"`
7070
}
71+
72+
type CasaOSGlobalVariables struct {
73+
AddApp bool
74+
}

pkg/config/init.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var ServerInfo = &model.ServerModel{}
3333

3434
var SystemConfigInfo = &model.SystemConfig{}
3535

36+
var CasaOSGlobalVariables = &model.CasaOSGlobalVariables{}
37+
3638
var Cfg *ini.File
3739

3840
//初始化设置,获取系统的部分信息。

pkg/utils/oasis_err/e.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
DIR_ALREADY_EXISTS = 20001
1717
FILE_ALREADY_EXISTS = 20002
1818
FILE_OR_DIR_EXISTS = 20003
19+
PORT_IS_OCCUPIED = 20004
1920

2021
//zerotier
2122
GET_TOKEN_ERROR = 30001
@@ -49,6 +50,7 @@ var MsgFlags = map[int]string{
4950
DIR_ALREADY_EXISTS: "Directory already exists",
5051
FILE_ALREADY_EXISTS: "File already exists",
5152
FILE_OR_DIR_EXISTS: "File or directory already exists",
53+
PORT_IS_OCCUPIED: "Port is occupied",
5254

5355
//zerotier
5456
GET_TOKEN_ERROR: "Get token error,Please log in to zerotier's official website to confirm whether the account is available",

route/init.go

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/IceWhaleTech/CasaOS/model/system_app"
1111
"github.com/IceWhaleTech/CasaOS/pkg/config"
1212
"github.com/IceWhaleTech/CasaOS/pkg/docker"
13+
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
1314
"github.com/IceWhaleTech/CasaOS/pkg/utils/env_helper"
1415
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
1516
"github.com/IceWhaleTech/CasaOS/pkg/utils/port"
@@ -20,6 +21,7 @@ import (
2021

2122
func InitFunction() {
2223
go checkSystemApp()
24+
Update2_3()
2325
}
2426

2527
var syncIsExistence = false
@@ -190,3 +192,10 @@ func checkSystemApp() {
190192
installSyncthing("44")
191193
}
192194
}
195+
func CheckSerialDiskMount() {
196+
// 检查挂载点重新挂载
197+
// 检查新硬盘是否有多个分区,如有多个分区需提示
198+
}
199+
func Update2_3() {
200+
command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/assist.sh")
201+
}

route/route.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ func InitRouter() *gin.Engine {
141141
{
142142
//获取我的已安装的列表
143143
v1AppGroup.GET("/mylist", v1.MyAppList)
144+
//
145+
v1AppGroup.GET("/usage", v1.AppUsageList)
144146
//app详情
145147
v1AppGroup.GET("/appinfo/:id", v1.AppInfo)
146148
//获取未安装的列表
@@ -192,6 +194,9 @@ func InitRouter() *gin.Engine {
192194
v1SysGroup.POST("/config", v1.PostSetSystemConfig)
193195
v1SysGroup.GET("/widget/config", v1.GetWidgetConfig)
194196
v1SysGroup.POST("/widget/config", v1.PostSetWidgetConfig)
197+
v1SysGroup.GET("/port", v1.GetCasaOSPort)
198+
v1SysGroup.PUT("/port", v1.PutCasaOSPort)
199+
v1SysGroup.POST("/kill", v1.PostKillCasaOS)
195200
}
196201
v1FileGroup := v1Group.Group("/file")
197202
v1FileGroup.Use()
@@ -221,14 +226,20 @@ func InitRouter() *gin.Engine {
221226
v1DiskGroup.POST("/format", v1.FormatDisk)
222227

223228
//添加分区
224-
v1DiskGroup.POST("/addpart", v1.AddPartition)
229+
v1DiskGroup.POST("/part", v1.AddPartition)
225230

226231
//获取可以格式化的内容
227232
v1DiskGroup.GET("/type", v1.FormatDiskType)
228233

229234
//删除分区
230235
v1DiskGroup.DELETE("/delpart", v1.RemovePartition)
231236

237+
//mount SATA disk
238+
v1DiskGroup.POST("/mount", v1.PostMountDisk)
239+
240+
//umount SATA disk
241+
v1DiskGroup.POST("/umount", v1.DeleteUmountDisk)
242+
232243
}
233244
v1ShareGroup := v1Group.Group("/share")
234245
v1ShareGroup.Use()

route/v1/app.go

+23
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ func MyAppList(c *gin.Context) {
103103
c.JSON(http.StatusOK, &model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
104104
}
105105

106+
// @Summary my app hardware usage list
107+
// @Produce application/json
108+
// @Accept application/json
109+
// @Tags app
110+
// @Security ApiKeyAuth
111+
// @Success 200 {string} string "ok"
112+
// @Router /app/usage [get]
113+
func AppUsageList(c *gin.Context) {
114+
list := service.MyService.App().GetHardwareUsage()
115+
c.JSON(http.StatusOK, &model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
116+
}
117+
106118
// @Summary 应用详情
107119
// @Produce application/json
108120
// @Accept application/json
@@ -212,3 +224,14 @@ func ShareAppFile(c *gin.Context) {
212224
content := service.MyService.OAPI().ShareAppFile(str)
213225
c.JSON(http.StatusOK, json.RawMessage(content))
214226
}
227+
228+
// @Summary Resource Usage
229+
// @Produce application/json
230+
// @Accept application/json
231+
// @Tags app
232+
// @Security ApiKeyAuth
233+
// @Success 200 {string} string "ok"
234+
// @Router /app/share [post]
235+
func AppListResourceUsage() {
236+
237+
}

route/v1/disk.go

+32-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package v1
22

33
import (
4+
"net/http"
5+
46
"github.com/IceWhaleTech/CasaOS/model"
57
"github.com/IceWhaleTech/CasaOS/pkg/utils/oasis_err"
68
"github.com/IceWhaleTech/CasaOS/service"
79
"github.com/gin-gonic/gin"
810
"github.com/shirou/gopsutil/v3/disk"
9-
"net/http"
10-
"strconv"
1111
)
1212

1313
// @Summary 获取磁盘列表
@@ -59,7 +59,7 @@ func GetPlugInDisk(c *gin.Context) {
5959
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: lst})
6060
}
6161

62-
// @Summary 获取磁盘列表
62+
// @Summary get disk list
6363
// @Produce application/json
6464
// @Accept application/json
6565
// @Tags disk
@@ -76,12 +76,12 @@ func GetPlugInDisks(c *gin.Context) {
7676
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: result})
7777
}
7878

79-
// @Summary 磁盘详情
79+
// @Summary disk detail
8080
// @Produce application/json
8181
// @Accept application/json
8282
// @Tags disk
8383
// @Security ApiKeyAuth
84-
// @Param path query string true "要获取的磁盘详情 例如/dev/sda"
84+
// @Param path query string true "for example /dev/sda"
8585
// @Success 200 {string} string "ok"
8686
// @Router /disk/info [get]
8787
func GetDiskInfo(c *gin.Context) {
@@ -93,7 +93,7 @@ func GetDiskInfo(c *gin.Context) {
9393
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: m})
9494
}
9595

96-
// @Summary 磁盘详情
96+
// @Summary format disk
9797
// @Produce application/json
9898
// @Accept multipart/form-data
9999
// @Tags disk
@@ -109,15 +109,9 @@ func FormatDisk(c *gin.Context) {
109109
if len(path) == 0 || len(t) == 0 {
110110
c.JSON(http.StatusOK, model.Result{Success: oasis_err.INVALID_PARAMS, Message: oasis_err.GetMsg(oasis_err.INVALID_PARAMS)})
111111
}
112-
113-
//删除挂载点
114-
service.MyService.Disk().UmountPointAndRemoveDir(path)
115-
116112
//格式化磁盘
117113
service.MyService.Disk().FormatDisk(path, t)
118114

119-
//重新挂载
120-
121115
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS)})
122116
}
123117

@@ -154,25 +148,43 @@ func RemovePartition(c *gin.Context) {
154148
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS)})
155149
}
156150

157-
// @Summary 添加分区
151+
// @Summary serial number
158152
// @Produce application/json
159153
// @Accept multipart/form-data
160154
// @Tags disk
161155
// @Security ApiKeyAuth
162156
// @Param path formData string true "磁盘路径 例如/dev/sda"
163-
// @Param size formData string true "需要分区容量大小(MB)"
164-
// @Param num formData string true "磁盘符号"
157+
// @Param serial formData string true "serial"
165158
// @Success 200 {string} string "ok"
166159
// @Router /disk/addpart [post]
167160
func AddPartition(c *gin.Context) {
168161
path := c.PostForm("path")
169-
size, _ := strconv.Atoi(c.DefaultPostForm("size", "0"))
170-
num := c.DefaultPostForm("num", "9")
171-
if len(path) == 0 {
162+
serial := c.PostForm("serial")
163+
if len(path) == 0 || len(serial) == 0 {
172164
c.JSON(http.StatusOK, model.Result{Success: oasis_err.INVALID_PARAMS, Message: oasis_err.GetMsg(oasis_err.INVALID_PARAMS)})
165+
return
173166
}
167+
service.MyService.Disk().AddPartition(path)
168+
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS)})
169+
}
170+
171+
func PostMountDisk(c *gin.Context) {
172+
// for example: path=/dev/sda1
173+
path := c.PostForm("path")
174+
//执行挂载目录
175+
service.MyService.Disk().MountDisk(path, "volume")
176+
//添加到数据库
177+
178+
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS)})
179+
}
180+
181+
func DeleteUmountDisk(c *gin.Context) {
182+
183+
// for example: path=/dev/sda1
184+
path := c.PostForm("path")
185+
service.MyService.Disk().UmountPointAndRemoveDir(path)
186+
187+
//删除数据库记录
174188

175-
//size*1024*1024/512
176-
service.MyService.Disk().AddPartition(path, num, uint64(size*1024*2))
177189
c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS)})
178190
}

route/v1/docker.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package v1
22

33
import (
44
"bytes"
5-
"encoding/json"
65
json2 "encoding/json"
76
"net/http"
87
"reflect"
@@ -11,6 +10,7 @@ import (
1110
"time"
1211

1312
"github.com/IceWhaleTech/CasaOS/model"
13+
"github.com/IceWhaleTech/CasaOS/pkg/config"
1414
"github.com/IceWhaleTech/CasaOS/pkg/docker"
1515
upnp2 "github.com/IceWhaleTech/CasaOS/pkg/upnp"
1616
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
@@ -421,9 +421,12 @@ func InstallApp(c *gin.Context) {
421421
rely := model.MapStrings{}
422422

423423
copier.Copy(&rely, &relyMap)
424-
for i := 0; i < len(m.Volumes); i++ {
425-
m.Volumes[i].Path = docker.GetDir(id, m.Volumes[i].ContainerPath)
424+
if m.Origin != "custom" {
425+
for i := 0; i < len(m.Volumes); i++ {
426+
m.Volumes[i].Path = docker.GetDir(id, m.Volumes[i].ContainerPath)
427+
}
426428
}
429+
427430
portsStr, _ := json2.Marshal(m.Ports)
428431
envsStr, _ := json2.Marshal(m.Envs)
429432
volumesStr, _ := json2.Marshal(m.Volumes)
@@ -463,6 +466,7 @@ func InstallApp(c *gin.Context) {
463466
// m.PortMap = m.Port
464467
//}
465468
service.MyService.App().SaveContainer(md)
469+
config.CasaOSGlobalVariables.AddApp = true
466470

467471
}()
468472

@@ -677,7 +681,7 @@ func UnInstallApp(c *gin.Context) {
677681
}
678682

679683
//step:删除容器
680-
err = service.MyService.Docker().DockerContainerRemove(appId)
684+
err = service.MyService.Docker().DockerContainerRemove(appId, false)
681685
if err != nil {
682686
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.UNINSTALL_APP_ERROR, Message: oasis_err2.GetMsg(oasis_err2.UNINSTALL_APP_ERROR), Data: err.Error()})
683687
return
@@ -908,16 +912,10 @@ func UpdateSetting(c *gin.Context) {
908912
//如果容器端口均未修改,这不进行处理
909913
portsStr, _ := json2.Marshal(m.Ports)
910914

911-
list := []model.PathMap{}
912-
json.Unmarshal([]byte(appInfo.Volumes), &list)
913-
for i := 0; i < len(list); i++ {
914-
list[i].Path = docker.GetDir(id, list[i].ContainerPath)
915-
}
916915
envsStr, _ := json2.Marshal(m.Envs)
917916
volumesStr, _ := json2.Marshal(m.Volumes)
918917
devicesStr, _ := json2.Marshal(m.Devices)
919-
listStr, _ := json2.Marshal(list)
920-
if !reflect.DeepEqual(string(portsStr), appInfo.Ports) || !reflect.DeepEqual(string(envsStr), appInfo.Envs) || !reflect.DeepEqual(volumesStr, listStr) || m.PortMap != appInfo.PortMap || m.NetworkModel != appInfo.NetModel {
918+
if !reflect.DeepEqual(string(portsStr), appInfo.Ports) || !reflect.DeepEqual(string(envsStr), appInfo.Envs) || !reflect.DeepEqual(string(volumesStr), appInfo.Volumes) || m.PortMap != appInfo.PortMap || m.NetworkModel != appInfo.NetModel {
921919

922920
var newUUid = uuid.NewV4().String()
923921
var err error
@@ -934,7 +932,7 @@ func UpdateSetting(c *gin.Context) {
934932
return
935933
}
936934

937-
err = service.MyService.Docker().DockerContainerRemove(id)
935+
err = service.MyService.Docker().DockerContainerRemove(id, true)
938936
if err != nil {
939937
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
940938
return

0 commit comments

Comments
 (0)