2021-09-26 02:35:02 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-09 11:02:41 +00:00
|
|
|
"encoding/json"
|
2022-05-05 05:46:55 +00:00
|
|
|
"fmt"
|
2021-12-09 11:02:41 +00:00
|
|
|
"io"
|
2021-12-06 09:08:36 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"runtime"
|
2021-11-03 09:07:46 +00:00
|
|
|
"strings"
|
2021-12-06 09:08:36 +00:00
|
|
|
"sync"
|
2021-11-03 09:07:46 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
2021-09-27 06:17:36 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
|
2021-09-27 06:17:36 +00:00
|
|
|
model2 "github.com/IceWhaleTech/CasaOS/service/model"
|
2021-09-26 02:35:02 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
client2 "github.com/docker/docker/client"
|
|
|
|
"github.com/pkg/errors"
|
2022-05-13 10:12:26 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2022-06-08 10:19:45 +00:00
|
|
|
"go.uber.org/zap"
|
2021-09-26 02:35:02 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppService interface {
|
2022-05-05 05:46:55 +00:00
|
|
|
GetMyList(index, size int, position bool) (*[]model2.MyAppList, *[]model2.MyAppList)
|
2021-09-26 02:35:02 +00:00
|
|
|
SaveContainer(m model2.AppListDBModel)
|
|
|
|
GetUninstallInfo(id string) model2.AppListDBModel
|
2022-05-05 05:46:55 +00:00
|
|
|
DeleteApp(id string)
|
2022-07-22 03:02:11 +00:00
|
|
|
GetContainerInfo(id string) (types.Container, error)
|
2021-09-26 02:35:02 +00:00
|
|
|
GetAppDBInfo(id string) model2.AppListDBModel
|
|
|
|
UpdateApp(m model2.AppListDBModel)
|
2022-07-22 03:02:11 +00:00
|
|
|
GetSimpleContainerInfo(id string) (types.Container, error)
|
2021-11-25 11:41:25 +00:00
|
|
|
DelAppConfigDir(path string)
|
2022-05-13 10:12:26 +00:00
|
|
|
GetSystemAppList() []types.Container
|
2022-07-22 03:02:11 +00:00
|
|
|
GetHardwareUsageStream()
|
2021-12-09 11:02:41 +00:00
|
|
|
GetHardwareUsage() []model.DockerStatsModel
|
|
|
|
GetAppStats(id string) string
|
2022-03-09 08:37:03 +00:00
|
|
|
GetAllDBApps() []model2.AppListDBModel
|
2022-05-05 05:46:55 +00:00
|
|
|
ImportApplications(casaApp bool)
|
|
|
|
CheckNewImage()
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type appStruct struct {
|
2022-06-08 10:19:45 +00:00
|
|
|
db *gorm.DB
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
func (a *appStruct) CheckNewImage() {
|
|
|
|
list := MyService.Docker().DockerContainerList()
|
|
|
|
for _, v := range list {
|
|
|
|
inspect, err := MyService.Docker().DockerImageInfo(strings.Split(v.Image, ":")[0])
|
|
|
|
if err != nil {
|
|
|
|
NewVersionApp[v.ID] = inspect.ID
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if inspect.ID == v.ImageID {
|
|
|
|
delete(NewVersionApp, v.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
NewVersionApp[v.ID] = inspect.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func (a *appStruct) ImportApplications(casaApp bool) {
|
|
|
|
if casaApp {
|
|
|
|
list := MyService.App().GetAllDBApps()
|
|
|
|
for _, app := range list {
|
|
|
|
info, err := MyService.Docker().DockerContainerInfo(app.CustomId)
|
|
|
|
if err != nil {
|
|
|
|
MyService.App().DeleteApp(app.CustomId)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//info.NetworkSettings
|
|
|
|
info.Config.Labels["casaos"] = "casaos"
|
|
|
|
info.Config.Labels["web"] = app.PortMap
|
|
|
|
info.Config.Labels["icon"] = app.Icon
|
|
|
|
info.Config.Labels["desc"] = app.Description
|
2022-05-13 10:12:26 +00:00
|
|
|
info.Config.Labels["index"] = app.Index
|
|
|
|
info.Config.Labels["custom_id"] = app.CustomId
|
2022-05-05 05:46:55 +00:00
|
|
|
info.Name = app.Title
|
|
|
|
container_id, err := MyService.Docker().DockerContainerCopyCreate(info)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
MyService.App().DeleteApp(app.CustomId)
|
|
|
|
MyService.Docker().DockerContainerStop(app.CustomId)
|
|
|
|
MyService.Docker().DockerContainerRemove(app.CustomId, false)
|
|
|
|
MyService.Docker().DockerContainerStart(container_id)
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list := MyService.Docker().DockerContainerList()
|
|
|
|
for _, app := range list {
|
|
|
|
info, err := MyService.Docker().DockerContainerInfo(app.ID)
|
|
|
|
if err != nil || info.Config.Labels["casaos"] == "casaos" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info.Config.Labels["casaos"] = "casaos"
|
|
|
|
info.Config.Labels["web"] = ""
|
|
|
|
info.Config.Labels["icon"] = ""
|
|
|
|
info.Config.Labels["desc"] = ""
|
2022-05-13 10:12:26 +00:00
|
|
|
info.Config.Labels["index"] = ""
|
|
|
|
info.Config.Labels["custom_id"] = uuid.NewV4().String()
|
2022-05-05 05:46:55 +00:00
|
|
|
|
|
|
|
_, err = MyService.Docker().DockerContainerCopyCreate(info)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
// allcontainer := MyService.Docker().DockerContainerList()
|
|
|
|
// for _, app := range allcontainer {
|
|
|
|
// info, err := MyService.Docker().DockerContainerInfo(app.ID)
|
|
|
|
// if err != nil {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// MyService.Docker().DockerContainerStop(app.ID)
|
|
|
|
// MyService.Docker().DockerContainerRemove(app.ID, false)
|
|
|
|
// //info.NetworkSettings
|
|
|
|
// info.Config.Labels["custom_id"] = uuid.NewV4().String()
|
|
|
|
// container_id, err := MyService.Docker().DockerContainerCopyCreate(info)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// MyService.Docker().DockerContainerStart(container_id)
|
|
|
|
//}
|
2022-05-05 05:46:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
//获取我的应用列表
|
2022-05-05 05:46:55 +00:00
|
|
|
func (a *appStruct) GetMyList(index, size int, position bool) (*[]model2.MyAppList, *[]model2.MyAppList) {
|
2021-12-29 08:42:20 +00:00
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv, client2.WithTimeout(time.Second*5))
|
2021-09-26 02:35:02 +00:00
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to init client", zap.Any("err", err))
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
defer cli.Close()
|
2022-05-05 05:46:55 +00:00
|
|
|
// fts := filters.NewArgs()
|
|
|
|
// fts.Add("label", "casaos=casaos")
|
|
|
|
//fts.Add("label", "casaos")
|
|
|
|
//fts.Add("casaos", "casaos")
|
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
2021-09-26 02:35:02 +00:00
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to get container_list", zap.Any("err", err))
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
//获取本地数据库应用
|
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
unTranslation := []model2.MyAppList{}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
|
|
|
list := []model2.MyAppList{}
|
2021-11-09 10:57:50 +00:00
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
for _, m := range containers {
|
2022-05-05 05:46:55 +00:00
|
|
|
if m.Labels["casaos"] == "casaos" {
|
2022-07-22 03:02:11 +00:00
|
|
|
|
|
|
|
_, newVersion := NewVersionApp[m.ID]
|
|
|
|
name := strings.ReplaceAll(m.Names[0], "/", "")
|
|
|
|
icon := m.Labels["icon"]
|
|
|
|
if len(m.Labels["name"]) > 0 {
|
|
|
|
name = m.Labels["name"]
|
|
|
|
}
|
2022-05-13 10:12:26 +00:00
|
|
|
if m.Labels["origin"] == "system" {
|
2022-07-22 03:02:11 +00:00
|
|
|
name = strings.Split(m.Image, ":")[0]
|
|
|
|
if len(strings.Split(name, "/")) > 1 {
|
|
|
|
icon = "https://icon.casaos.io/main/all/" + strings.Split(name, "/")[1] + ".png"
|
|
|
|
}
|
2022-05-13 10:12:26 +00:00
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
|
2021-11-09 10:57:50 +00:00
|
|
|
list = append(list, model2.MyAppList{
|
2022-07-22 03:02:11 +00:00
|
|
|
Name: name,
|
|
|
|
Icon: icon,
|
2022-05-05 05:46:55 +00:00
|
|
|
State: m.State,
|
2022-05-13 10:12:26 +00:00
|
|
|
CustomId: m.Labels["custom_id"],
|
|
|
|
Id: m.ID,
|
2022-05-05 05:46:55 +00:00
|
|
|
Port: m.Labels["web"],
|
|
|
|
Index: m.Labels["index"],
|
2022-05-13 10:12:26 +00:00
|
|
|
//Order: m.Labels["order"],
|
2022-07-22 03:02:11 +00:00
|
|
|
Image: m.Image,
|
|
|
|
Latest: newVersion,
|
|
|
|
//Type: m.Labels["origin"],
|
2022-05-05 05:46:55 +00:00
|
|
|
//Slogan: m.Slogan,
|
2021-11-09 10:57:50 +00:00
|
|
|
//Rely: m.Rely,
|
2022-06-08 10:19:45 +00:00
|
|
|
Host: m.Labels["host"],
|
|
|
|
Protocol: m.Labels["protocol"],
|
2021-11-09 10:57:50 +00:00
|
|
|
})
|
2022-05-05 05:46:55 +00:00
|
|
|
} else {
|
|
|
|
unTranslation = append(unTranslation, model2.MyAppList{
|
2022-07-22 03:02:11 +00:00
|
|
|
Name: strings.ReplaceAll(m.Names[0], "/", ""),
|
|
|
|
Icon: "",
|
|
|
|
State: m.State,
|
|
|
|
CustomId: m.ID,
|
|
|
|
Id: m.ID,
|
|
|
|
Port: "",
|
|
|
|
Latest: false,
|
|
|
|
Host: "",
|
|
|
|
Protocol: "",
|
|
|
|
Image: m.Image,
|
2022-05-05 05:46:55 +00:00
|
|
|
})
|
2021-11-09 10:57:50 +00:00
|
|
|
}
|
2022-05-05 05:46:55 +00:00
|
|
|
}
|
2021-11-09 10:57:50 +00:00
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
//lMap := make(map[string]interface{})
|
|
|
|
// for _, dbModel := range lm {
|
|
|
|
// if position {
|
|
|
|
// if dbModel.Position {
|
|
|
|
// lMap[dbModel.ContainerId] = dbModel
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// lMap[dbModel.ContainerId] = dbModel
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// for _, container := range containers {
|
|
|
|
|
|
|
|
// if lMap[container.ID] != nil && container.Labels["origin"] != "system" {
|
|
|
|
// m := lMap[container.ID].(model2.AppListDBModel)
|
|
|
|
// if len(m.Label) == 0 {
|
|
|
|
// m.Label = m.Title
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // info, err := cli.ContainerInspect(context.Background(), container.ID)
|
|
|
|
// // var tm string
|
|
|
|
// // if err != nil {
|
|
|
|
// // tm = time.Now().String()
|
|
|
|
// // } else {
|
|
|
|
// // tm = info.State.StartedAt
|
|
|
|
// //}
|
|
|
|
// list = append(list, model2.MyAppList{
|
|
|
|
// Name: m.Label,
|
|
|
|
// Icon: m.Icon,
|
|
|
|
// State: container.State,
|
|
|
|
// CustomId: strings.ReplaceAll(container.Names[0], "/", ""),
|
|
|
|
// Port: m.PortMap,
|
|
|
|
// Index: m.Index,
|
|
|
|
// //UpTime: tm,
|
|
|
|
// Image: m.Image,
|
|
|
|
// Slogan: m.Slogan,
|
|
|
|
// //Rely: m.Rely,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
return &list, &unTranslation
|
2021-11-09 10:57:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//system application list
|
2022-05-13 10:12:26 +00:00
|
|
|
func (a *appStruct) GetSystemAppList() []types.Container {
|
2021-11-09 10:57:50 +00:00
|
|
|
//获取docker应用
|
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv)
|
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to init client", zap.Any("err", err))
|
2021-11-09 10:57:50 +00:00
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
fts := filters.NewArgs()
|
|
|
|
fts.Add("label", "origin=system")
|
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: fts})
|
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to get container_list", zap.Any("err", err))
|
2021-11-09 10:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//获取本地数据库应用
|
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
// var lm []model2.AppListDBModel
|
|
|
|
// a.db.Table(model2.CONTAINERTABLENAME).Select("title,icon,port_map,`index`,container_id,position,label,slogan,image,volumes").Find(&lm)
|
2021-11-09 10:57:50 +00:00
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
//list := []model2.MyAppList{}
|
|
|
|
//lMap := make(map[string]interface{})
|
|
|
|
// for _, dbModel := range lm {
|
|
|
|
// lMap[dbModel.ContainerId] = dbModel
|
|
|
|
// }
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-05-13 10:12:26 +00:00
|
|
|
return containers
|
2021-09-26 02:35:02 +00:00
|
|
|
|
|
|
|
}
|
2022-03-09 08:37:03 +00:00
|
|
|
func (a *appStruct) GetAllDBApps() []model2.AppListDBModel {
|
|
|
|
var lm []model2.AppListDBModel
|
2022-05-05 05:46:55 +00:00
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Select("custom_id,title,icon,container_id,label,slogan,image,port_map").Find(&lm)
|
2022-03-09 08:37:03 +00:00
|
|
|
return lm
|
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
|
|
|
//获取我的应用列表
|
2022-07-22 03:02:11 +00:00
|
|
|
func (a *appStruct) GetContainerInfo(id string) (types.Container, error) {
|
2021-09-26 02:35:02 +00:00
|
|
|
//获取docker应用
|
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv)
|
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to init client", zap.Any("err", err))
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
filters := filters.NewArgs()
|
2022-07-22 03:02:11 +00:00
|
|
|
filters.Add("id", id)
|
2021-09-26 02:35:02 +00:00
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: filters})
|
|
|
|
if err != nil {
|
2022-06-08 10:19:45 +00:00
|
|
|
loger.Error("Failed to get container_list", zap.Any("err", err))
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(containers) > 0 {
|
|
|
|
return containers[0], nil
|
|
|
|
}
|
|
|
|
return types.Container{}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (a *appStruct) GetSimpleContainerInfo(id string) (types.Container, error) {
|
2021-09-26 02:35:02 +00:00
|
|
|
//获取docker应用
|
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv)
|
|
|
|
if err != nil {
|
|
|
|
return types.Container{}, err
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
filters := filters.NewArgs()
|
2022-07-22 03:02:11 +00:00
|
|
|
filters.Add("id", id)
|
2021-09-26 02:35:02 +00:00
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: filters})
|
2021-12-03 08:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.Container{}, err
|
|
|
|
}
|
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
if len(containers) > 0 {
|
|
|
|
return containers[0], nil
|
|
|
|
}
|
|
|
|
return types.Container{}, errors.New("container not existent")
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取我的应用列表
|
|
|
|
func (a *appStruct) GetAppDBInfo(id string) model2.AppListDBModel {
|
|
|
|
var m model2.AppListDBModel
|
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Where("custom_id = ?", id).First(&m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
//根据容器id获取镜像名称
|
|
|
|
func (a *appStruct) GetUninstallInfo(id string) model2.AppListDBModel {
|
|
|
|
var m model2.AppListDBModel
|
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Select("image,version,enable_upnp,ports,envs,volumes,origin").Where("custom_id = ?", id).First(&m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
//创建容器成功后保存容器
|
|
|
|
func (a *appStruct) SaveContainer(m model2.AppListDBModel) {
|
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Create(&m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appStruct) UpdateApp(m model2.AppListDBModel) {
|
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Save(&m)
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:41:25 +00:00
|
|
|
func (a *appStruct) DelAppConfigDir(path string) {
|
2022-06-29 03:09:58 +00:00
|
|
|
command.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;DelAppConfigDir " + path)
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
func (a *appStruct) DeleteApp(id string) {
|
2021-09-26 02:35:02 +00:00
|
|
|
a.db.Table(model2.CONTAINERTABLENAME).Where("custom_id = ?", id).Delete(&model2.AppListDBModel{})
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:14:01 +00:00
|
|
|
var dataStats sync.Map
|
2021-12-09 11:02:41 +00:00
|
|
|
|
|
|
|
var isFinish bool = false
|
2021-12-06 09:08:36 +00:00
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
func (a *appStruct) GetAppStats(id string) string {
|
2021-12-06 09:08:36 +00:00
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv)
|
|
|
|
if err != nil {
|
2021-12-09 11:02:41 +00:00
|
|
|
return ""
|
2021-12-06 09:08:36 +00:00
|
|
|
}
|
|
|
|
defer cli.Close()
|
2021-12-09 11:02:41 +00:00
|
|
|
con, err := cli.ContainerStats(context.Background(), id, false)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
defer con.Body.Close()
|
|
|
|
c, _ := ioutil.ReadAll(con.Body)
|
|
|
|
return string(c)
|
|
|
|
}
|
2021-12-06 09:08:36 +00:00
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
func (a *appStruct) GetHardwareUsage() []model.DockerStatsModel {
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
stream := true
|
2021-12-09 11:02:41 +00:00
|
|
|
for !isFinish {
|
2022-07-22 03:02:11 +00:00
|
|
|
if stream {
|
|
|
|
stream = false
|
2021-12-09 11:02:41 +00:00
|
|
|
go func() {
|
2022-07-22 03:02:11 +00:00
|
|
|
a.GetHardwareUsageStream()
|
2021-12-09 11:02:41 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
runtime.Gosched()
|
2021-12-06 09:08:36 +00:00
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
list := []model.DockerStatsModel{}
|
|
|
|
|
2021-12-10 09:14:01 +00:00
|
|
|
dataStats.Range(func(key, value interface{}) bool {
|
|
|
|
list = append(list, value.(model.DockerStatsModel))
|
|
|
|
return true
|
|
|
|
})
|
2021-12-09 11:02:41 +00:00
|
|
|
return list
|
2021-12-06 09:08:36 +00:00
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (a *appStruct) GetHardwareUsageStream() {
|
2021-12-06 09:08:36 +00:00
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
cli, err := client2.NewClientWithOpts(client2.FromEnv)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-12-06 09:08:36 +00:00
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
defer cli.Close()
|
2021-12-06 09:08:36 +00:00
|
|
|
|
2021-12-09 11:02:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
2022-05-05 05:46:55 +00:00
|
|
|
fts := filters.NewArgs()
|
|
|
|
fts.Add("label", "casaos=casaos")
|
2022-06-15 02:22:27 +00:00
|
|
|
//fts.Add("status", "running")
|
2022-07-22 03:02:11 +00:00
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: fts})
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("Failed to get container_list", zap.Any("err", err))
|
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-07-22 03:02:11 +00:00
|
|
|
if i%10 == 0 {
|
|
|
|
containers, err = cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: fts})
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("Failed to get container_list", zap.Any("err", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 06:17:36 +00:00
|
|
|
if config.CasaOSGlobalVariables.AppChange {
|
|
|
|
config.CasaOSGlobalVariables.AppChange = false
|
2021-12-10 10:08:02 +00:00
|
|
|
dataStats.Range(func(key, value interface{}) bool {
|
|
|
|
dataStats.Delete(key)
|
|
|
|
return true
|
|
|
|
})
|
2021-12-09 11:02:41 +00:00
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
|
2022-06-15 11:01:26 +00:00
|
|
|
var temp sync.Map
|
2021-12-09 11:02:41 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-05-05 05:46:55 +00:00
|
|
|
for _, v := range containers {
|
2022-06-15 02:22:27 +00:00
|
|
|
if v.State != "running" {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
wg.Add(1)
|
2022-05-05 05:46:55 +00:00
|
|
|
go func(v types.Container, i int) {
|
2021-12-09 11:02:41 +00:00
|
|
|
defer wg.Done()
|
2022-09-06 06:28:49 +00:00
|
|
|
stats, err := cli.ContainerStatsOneShot(ctx, v.ID)
|
2021-12-09 11:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decode := json.NewDecoder(stats.Body)
|
|
|
|
var data interface{}
|
|
|
|
if err := decode.Decode(&data); err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
2022-05-05 05:46:55 +00:00
|
|
|
m, _ := dataStats.Load(v.ID)
|
2021-12-09 11:02:41 +00:00
|
|
|
dockerStats := model.DockerStatsModel{}
|
2021-12-10 09:14:01 +00:00
|
|
|
if m != nil {
|
2022-07-22 03:02:11 +00:00
|
|
|
dockerStats.Previous = m.(model.DockerStatsModel).Data
|
2021-12-10 09:14:01 +00:00
|
|
|
}
|
2021-12-09 11:02:41 +00:00
|
|
|
dockerStats.Data = data
|
2022-05-05 05:46:55 +00:00
|
|
|
dockerStats.Icon = v.Labels["icon"]
|
2022-05-13 10:12:26 +00:00
|
|
|
dockerStats.Title = strings.ReplaceAll(v.Names[0], "/", "")
|
2022-06-14 10:10:22 +00:00
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
// @tiger - 不建议直接把依赖的数据结构封装返回。
|
|
|
|
// 如果依赖的数据结构有变化,应该在这里适配或者保存,这样更加对客户端负责
|
2022-06-15 11:01:26 +00:00
|
|
|
temp.Store(v.ID, dockerStats)
|
2021-12-10 08:33:39 +00:00
|
|
|
if i == 99 {
|
|
|
|
stats.Body.Close()
|
|
|
|
}
|
2021-12-10 09:14:01 +00:00
|
|
|
}(v, i)
|
2021-12-09 11:02:41 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
2022-06-15 11:01:26 +00:00
|
|
|
dataStats = temp
|
2021-12-09 11:02:41 +00:00
|
|
|
isFinish = true
|
2022-06-15 11:01:26 +00:00
|
|
|
|
2022-06-14 10:10:22 +00:00
|
|
|
time.Sleep(time.Second * 1)
|
2021-12-09 11:02:41 +00:00
|
|
|
}
|
|
|
|
isFinish = false
|
|
|
|
cancel()
|
2021-12-06 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
func NewAppService(db *gorm.DB) AppService {
|
|
|
|
return &appStruct{db: db}
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|