Skip to content

Commit

Permalink
Pull Submit
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong committed Dec 6, 2021
1 parent 52bd22a commit 2508a4e
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 41 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ require (
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.3
github.com/tidwall/gjson v1.8.0
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/gjson v1.10.2
github.com/tidwall/sjson v1.2.3
github.com/tklauser/go-sysconf v0.3.6 // indirect
github.com/ugorji/go v1.2.6 // indirect
go.opencensus.io v0.23.0 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,14 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/gjson v1.10.2 h1:APbLGOM0rrEkd8WBw9C24nllro4ajFuJu0Sc9hRz8Bo=
github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8=
github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs=
github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek=
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/oasis_err/e.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
DIR_ALREADY_EXISTS = 20001
FILE_ALREADY_EXISTS = 20002
FILE_OR_DIR_EXISTS = 20003
PORT_IS_OCCUPIED = 20004

//zerotier
GET_TOKEN_ERROR = 30001
Expand Down Expand Up @@ -49,6 +50,7 @@ var MsgFlags = map[int]string{
DIR_ALREADY_EXISTS: "Directory already exists",
FILE_ALREADY_EXISTS: "File already exists",
FILE_OR_DIR_EXISTS: "File or directory already exists",
PORT_IS_OCCUPIED: "Port is occupied",

//zerotier
GET_TOKEN_ERROR: "Get token error,Please log in to zerotier's official website to confirm whether the account is available",
Expand Down
4 changes: 4 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func InitRouter() *gin.Engine {
{
//获取我的已安装的列表
v1AppGroup.GET("/mylist", v1.MyAppList)
//
v1AppGroup.GET("/usage", v1.AppUsageList)
//app详情
v1AppGroup.GET("/appinfo/:id", v1.AppInfo)
//获取未安装的列表
Expand Down Expand Up @@ -192,6 +194,8 @@ func InitRouter() *gin.Engine {
v1SysGroup.POST("/config", v1.PostSetSystemConfig)
v1SysGroup.GET("/widget/config", v1.GetWidgetConfig)
v1SysGroup.POST("/widget/config", v1.PostSetWidgetConfig)
v1SysGroup.PUT("/port", v1.PutCasaOSPort)
v1SysGroup.POST("/kill", v1.PostKillCasaOS)
}
v1FileGroup := v1Group.Group("/file")
v1FileGroup.Use()
Expand Down
23 changes: 23 additions & 0 deletions route/v1/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func MyAppList(c *gin.Context) {
c.JSON(http.StatusOK, &model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
}

// @Summary my app hardware usage list
// @Produce application/json
// @Accept application/json
// @Tags app
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/usage [get]
func AppUsageList(c *gin.Context) {
list := service.MyService.App().GetHardwareUsage()
c.JSON(http.StatusOK, &model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
}

// @Summary 应用详情
// @Produce application/json
// @Accept application/json
Expand Down Expand Up @@ -212,3 +224,14 @@ func ShareAppFile(c *gin.Context) {
content := service.MyService.OAPI().ShareAppFile(str)
c.JSON(http.StatusOK, json.RawMessage(content))
}

// @Summary Resource Usage
// @Produce application/json
// @Accept application/json
// @Tags app
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /app/share [post]
func AppListResourceUsage() {

}
13 changes: 7 additions & 6 deletions route/v1/disk.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package v1

import (
"net/http"
"strconv"

"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/utils/oasis_err"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/v3/disk"
"net/http"
"strconv"
)

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

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

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

// @Summary 磁盘详情
// @Summary format disk
// @Produce application/json
// @Accept multipart/form-data
// @Tags disk
Expand Down
13 changes: 3 additions & 10 deletions route/v1/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"bytes"
"encoding/json"
json2 "encoding/json"
"net/http"
"reflect"
Expand Down Expand Up @@ -677,7 +676,7 @@ func UnInstallApp(c *gin.Context) {
}

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

list := []model.PathMap{}
json.Unmarshal([]byte(appInfo.Volumes), &list)
for i := 0; i < len(list); i++ {
list[i].Path = docker.GetDir(id, list[i].ContainerPath)
}
envsStr, _ := json2.Marshal(m.Envs)
volumesStr, _ := json2.Marshal(m.Volumes)
devicesStr, _ := json2.Marshal(m.Devices)
listStr, _ := json2.Marshal(list)
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 {
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 {

var newUUid = uuid.NewV4().String()
var err error
Expand All @@ -934,7 +927,7 @@ func UpdateSetting(c *gin.Context) {
return
}

err = service.MyService.Docker().DockerContainerRemove(id)
err = service.MyService.Docker().DockerContainerRemove(id, true)
if err != nil {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR)})
return
Expand Down
51 changes: 50 additions & 1 deletion route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"

"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/utils/oasis_err"
port2 "github.com/IceWhaleTech/CasaOS/pkg/utils/port"
"github.com/IceWhaleTech/CasaOS/pkg/utils/version"
"github.com/IceWhaleTech/CasaOS/service"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
Expand Down Expand Up @@ -80,7 +82,7 @@ func GetCasaOSErrorLogs(c *gin.Context) {
// @Produce application/json
// @Accept multipart/form-data
// @Tags sys
// @Param file formData file true "用户头像"
// @Param config formData string true "config json string"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/changhead [post]
Expand Down Expand Up @@ -135,6 +137,42 @@ func PostSetWidgetConfig(c *gin.Context) {
})
}

// @Summary edit casaos server port
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Param port formData file true "用户头像"
// @Success 200 {string} string "ok"
// @Router /sys/widget/config [post]
func PutCasaOSPort(c *gin.Context) {
port, err := strconv.Atoi(c.PostForm("port"))
if err != nil {
c.JSON(http.StatusOK,
model.Result{
Success: oasis_err.ERROR,
Message: err.Error(),
})
return
}

isAvailable := port2.IsPortAvailable(port, "tcp")
if !isAvailable {
c.JSON(http.StatusOK,
model.Result{
Success: oasis_err.PORT_IS_OCCUPIED,
Message: oasis_err.GetMsg(oasis_err.PORT_IS_OCCUPIED),
})
return
}
service.MyService.System().UpSystemPort(strconv.Itoa(port))
c.JSON(http.StatusOK,
model.Result{
Success: oasis_err.SUCCESS,
Message: oasis_err.GetMsg(oasis_err.SUCCESS),
})
}

// @Summary 检查是否进入引导状态
// @Produce application/json
// @Accept application/json
Expand All @@ -156,3 +194,14 @@ func GetGuideCheck(c *gin.Context) {
Data: data,
})
}

// @Summary active killing casaos
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/kill [post]
func PostKillCasaOS(c *gin.Context) {
os.Exit(0)
}
59 changes: 58 additions & 1 deletion service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package service

import (
"context"
"io/ioutil"
"runtime"
"strings"
"sync"
"time"

"github.com/IceWhaleTech/CasaOS/pkg/config"
Expand All @@ -13,6 +16,7 @@ import (
"github.com/docker/docker/api/types/filters"
client2 "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"gorm.io/gorm"
)

Expand All @@ -27,6 +31,7 @@ type AppService interface {
GetSimpleContainerInfo(name string) (types.Container, error)
DelAppConfigDir(path string)
GetSystemAppList() *[]model2.MyAppList
GetHardwareUsage() []string
}

type appStruct struct {
Expand All @@ -48,7 +53,6 @@ func (a *appStruct) GetMyList(index, size int, position bool) *[]model2.MyAppLis
if err != nil {
a.log.Error("获取docker容器失败", "app.getmylist", "line:42", err)
}

//获取本地数据库应用

var lm []model2.AppListDBModel
Expand Down Expand Up @@ -231,6 +235,59 @@ func (a *appStruct) RemoveContainerById(id string) {
a.db.Table(model2.CONTAINERTABLENAME).Where("custom_id = ?", id).Delete(&model2.AppListDBModel{})
}

func (a *appStruct) GetHardwareUsage() []string {

var dataStr []string
cli, err := client2.NewClientWithOpts(client2.FromEnv)
if err != nil {
return dataStr
}
defer cli.Close()

lock := &sync.Mutex{}
var lm []model2.AppListDBModel
var count = 0
a.db.Table(model2.CONTAINERTABLENAME).Select("title,icon,container_id").Find(&lm)
for _, v := range lm {
go func(lock *sync.Mutex, id, title, icon string) {
stats, err := cli.ContainerStats(context.Background(), id, false)
if err != nil {
lock.Lock()
count++
lock.Unlock()
return
}
defer stats.Body.Close()
statsByte, err := ioutil.ReadAll(stats.Body)
if err != nil {
lock.Lock()
count++
lock.Unlock()
return
}
lock.Lock()
statsByte, _ = sjson.SetBytes(statsByte, "icon", icon)
statsByte, _ = sjson.SetBytes(statsByte, "title", title)
dataStr = append(dataStr, string(statsByte))
count++
lock.Unlock()
}(lock, v.ContainerId, v.Title, v.Icon)
}

for {
lock.Lock()
c := count
lock.Unlock()

runtime.Gosched()
if c == len(lm) {
break
}
}
return dataStr

}

// init install
func Init() {

Expand Down
6 changes: 3 additions & 3 deletions service/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (d *diskService) GetDiskInfoByPath(path string) *disk.UsageStat {
return diskInfo
}

//获取磁盘信息
//get disk details
func (d *diskService) LSBLK() []model.LSBLKModel {
str := command2.ExecLSBLK()
if str == nil {
Expand All @@ -111,7 +111,7 @@ func (d *diskService) LSBLK() []model.LSBLKModel {

var health = true
for _, i := range m {
if i.Children != nil {
if i.Type != "loop" {
fsused = 0
for _, child := range i.Children {
if child.RM {
Expand All @@ -134,7 +134,7 @@ func (d *diskService) LSBLK() []model.LSBLKModel {
i.Children = c
if fsused > 0 {
i.UsedPercent, err = strconv.ParseFloat(fmt.Sprintf("%.4f", float64(fsused)/float64(i.Size)), 64)
fmt.Println(err)
d.log.Fatal("diskservice_lsblk_fsused", err)
}
n = append(n, i)
health = true
Expand Down
Loading

0 comments on commit 2508a4e

Please sign in to comment.