Skip to content

Commit

Permalink
Storage Manager
Browse files Browse the repository at this point in the history
Fix the app store classification problem
Fix the application market classification problem
  • Loading branch information
LinkLeong committed Jan 20, 2022
1 parent 9675eff commit fcb906a
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 120 deletions.
3 changes: 3 additions & 0 deletions model/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ type LSBLKModel struct {
Health string `json:"health"`
HotPlug bool `json:"hotplug"`
FSUsed string `json:"fsused"`
Temperature int `json:"temperature"`
Tran string `json:"tran"`
MinIO uint64 `json:"min-io"`
UsedPercent float64 `json:"used_percent"`
Serial string `json:"serial"`
Children []LSBLKModel `json:"children"`
//详情特有
StartSector uint64 `json:"start_sector,omitempty"`
Rota bool `json:"rota"` //true(hhd) false(ssd)
DiskType string `json:"disk_type"`
EndSector uint64 `json:"end_sector,omitempty"`
}
69 changes: 69 additions & 0 deletions model/smartctl_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package model

//
type SmartctlA struct {
Smartctl struct {
Version []int `json:"version"`
SvnRevision string `json:"svn_revision"`
PlatformInfo string `json:"platform_info"`
BuildInfo string `json:"build_info"`
Argv []string `json:"argv"`
ExitStatus int `json:"exit_status"`
} `json:"smartctl"`
Device struct {
Name string `json:"name"`
InfoName string `json:"info_name"`
Type string `json:"type"`
Protocol string `json:"protocol"`
} `json:"device"`
ModelName string `json:"model_name"`
SerialNumber string `json:"serial_number"`
FirmwareVersion string `json:"firmware_version"`
UserCapacity struct {
Blocks int `json:"blocks"`
Bytes int64 `json:"bytes"`
} `json:"user_capacity"`
SmartStatus struct {
Passed bool `json:"passed"`
} `json:"smart_status"`
AtaSmartData struct {
OfflineDataCollection struct {
Status struct {
Value int `json:"value"`
String string `json:"string"`
} `json:"status"`
CompletionSeconds int `json:"completion_seconds"`
} `json:"offline_data_collection"`
SelfTest struct {
Status struct {
Value int `json:"value"`
String string `json:"string"`
Passed bool `json:"passed"`
} `json:"status"`
PollingMinutes struct {
Short int `json:"short"`
Extended int `json:"extended"`
Conveyance int `json:"conveyance"`
} `json:"polling_minutes"`
} `json:"self_test"`
Capabilities struct {
Values []int `json:"values"`
ExecOfflineImmediateSupported bool `json:"exec_offline_immediate_supported"`
OfflineIsAbortedUponNewCmd bool `json:"offline_is_aborted_upon_new_cmd"`
OfflineSurfaceScanSupported bool `json:"offline_surface_scan_supported"`
SelfTestsSupported bool `json:"self_tests_supported"`
ConveyanceSelfTestSupported bool `json:"conveyance_self_test_supported"`
SelectiveSelfTestSupported bool `json:"selective_self_test_supported"`
AttributeAutosaveEnabled bool `json:"attribute_autosave_enabled"`
ErrorLoggingSupported bool `json:"error_logging_supported"`
GpLoggingSupported bool `json:"gp_logging_supported"`
} `json:"capabilities"`
} `json:"ata_smart_data"`
PowerOnTime struct {
Hours int `json:"hours"`
} `json:"power_on_time"`
PowerCycleCount int `json:"power_cycle_count"`
Temperature struct {
Current int `json:"current"`
} `json:"temperature"`
}
21 changes: 21 additions & 0 deletions pkg/utils/command/command_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package command

import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os/exec"
"time"
)

func OnlyExec(cmdStr string) {
Expand Down Expand Up @@ -85,7 +87,26 @@ func ExecLSBLK() []byte {
func ExecLSBLKByPath(path string) []byte {
output, err := exec.Command("lsblk", path, "-O", "-J", "-b").Output()
if err != nil {
fmt.Println("lsblk", err)
return nil
}
return output
}

//exec smart
func ExecSmartCTLByPath(path string) []byte {
timeout := 3
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
output, err := exec.CommandContext(ctx, "smartctl", "-a", path, "-j").Output()
if err != nil {
fmt.Println("smartctl", err)
return nil
}
return output
}

func ExecEnabledSMART(path string) {

exec.Command("smartctl", "-s on", path).Output()
}
40 changes: 27 additions & 13 deletions pkg/utils/oasis_err/e.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const (
//zerotier
GET_TOKEN_ERROR = 30001

//disk
NAME_NOT_AVAILABLE = 40001
DISK_NEEDS_FORMAT = 40002
DISK_BUSYING = 40003
REMOVE_MOUNT_POINT_ERROR = 40004
FORMAT_ERROR = 40005

//app
UNINSTALL_APP_ERROR = 50001
PULL_IMAGE_ERROR = 50002
Expand All @@ -37,34 +44,41 @@ const (
var MsgFlags = map[int]string{
SUCCESS: "ok",
ERROR: "fail",
INVALID_PARAMS: "Invalid params",
ERROR_AUTH_TOKEN: "error auth token",
INVALID_PARAMS: "Parameters Error",
ERROR_AUTH_TOKEN: "Error auth token",

//user
PWD_INVALID: "Password invalid",
PWD_INVALID: "Invalid password",
PWD_IS_EMPTY: "Password is empty",
PWD_INVALID_OLD: "Old Password invalid",
ACCOUNT_LOCK: "Account Lock",
PWD_INVALID_OLD: "Invalid old password",
ACCOUNT_LOCK: "Account is locked",

//system
DIR_ALREADY_EXISTS: "Directory already exists",
DIR_ALREADY_EXISTS: "Folder already exists",
FILE_ALREADY_EXISTS: "File already exists",
FILE_OR_DIR_EXISTS: "File or directory already exists",
FILE_OR_DIR_EXISTS: "File or folder 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",

//app
UNINSTALL_APP_ERROR: "uninstall app error",
PULL_IMAGE_ERROR: "pull image error",
DEVICE_NOT_EXIST: "device not exist",
UNINSTALL_APP_ERROR: "Error uninstalling app",
PULL_IMAGE_ERROR: "Error pulling image",
DEVICE_NOT_EXIST: "Device does not exist",

//disk
NAME_NOT_AVAILABLE: "Name not available",
DISK_NEEDS_FORMAT: "Drive needs to be formatted",
REMOVE_MOUNT_POINT_ERROR: "Failed to remove mount point",
DISK_BUSYING: "Drive is busy",
FORMAT_ERROR: "Formatting failed, please check if the directory is occupied",

//
FILE_DOES_NOT_EXIST: "file does not exist",
FILE_DOES_NOT_EXIST: "File does not exist",

FILE_READ_ERROR: "file read error",
SHORTCUTS_URL_ERROR: "url error",
FILE_READ_ERROR: "File read error",
SHORTCUTS_URL_ERROR: "URL error",
}

//获取错误信息
Expand Down
46 changes: 31 additions & 15 deletions route/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package route
import (
"encoding/json"
"encoding/xml"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -34,10 +35,9 @@ func installSyncthing(appId string) {
m := model.CustomizationPostData{}
var dockerImage string
var dockerImageVersion string

appInfo = service.MyService.OAPI().GetServerAppInfo(appId)

appInfo = service.MyService.OAPI().GetServerAppInfo(appId, "system")
dockerImage = appInfo.Image
dockerImageVersion = appInfo.ImageVersion

if len(appInfo.ImageVersion) == 0 {
dockerImageVersion = "latest"
Expand Down Expand Up @@ -84,9 +84,9 @@ func installSyncthing(appId string) {
err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog)
if err != nil {
//pull image error
fmt.Println("pull image error", err, dockerImage, dockerImageVersion)
return
}

for !service.MyService.Docker().IsExistImage(dockerImage + ":" + dockerImageVersion) {
time.Sleep(time.Second)
}
Expand All @@ -101,8 +101,8 @@ func installSyncthing(appId string) {
m.Volumes = appInfo.Volumes

containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel)

if err != nil {
fmt.Println("container create error", err)
// create container error
return
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func checkSystemApp() {
path := ""
for _, i := range paths {
if i.ContainerPath == "/config" {
path = docker.GetDir(v.CustomId, i.Path) + "config.xml"
path = docker.GetDir(v.CustomId, i.Path) + "/config.xml"
for i := 0; i < 10; i++ {
if file.CheckNotExist(path) {
time.Sleep(1 * time.Second)
Expand All @@ -197,21 +197,37 @@ func CheckSerialDiskMount() {

list := service.MyService.Disk().LSBLK()
mountPoint := make(map[string]string, len(dbList))

//remount
for _, v := range dbList {
mountPoint[v.Path] = v.MountPoint
}
for _, v := range list {
command.ExecEnabledSMART(v.Path)
if v.Children != nil {
for _, h := range v.Children {
mountPoint[h.MountPoint] = "1"
}
}
}
if len(h.MountPoint) == 0 && len(v.Children) == 1 && h.FsType == "ext4" {
if m, ok := mountPoint[h.Path]; ok {
//mount point check
volume := m
if !file.CheckNotExist(m) {
for i := 0; file.CheckNotExist(volume); i++ {
volume = m + strconv.Itoa(i+1)
}
}
service.MyService.Disk().MountDisk(h.Path, volume)
if volume != m {
ms := model2.SerialDisk{}
ms.Serial = v.Serial
service.MyService.Disk().UpdateMountPoint(ms)
}

//remount
for _, item := range dbList {
if _, ok := mountPoint[item.MountPoint]; !ok {
service.MyService.Disk().MountDisk(item.Path, item.MountPoint)
}
}
}
}
}
service.MyService.Disk().RemoveLSBLKCache()
command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;AutoRemoveUnuseDir")

}
func Update2_3() {
Expand Down
22 changes: 11 additions & 11 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,30 @@ func InitRouter() *gin.Engine {
v1DiskGroup.Use()
{
v1DiskGroup.GET("/check", v1.GetDiskCheck)
//获取磁盘列表
v1DiskGroup.GET("/list", v1.GetPlugInDisk)

v1DiskGroup.GET("/list", v1.GetDiskList)

//获取磁盘详情
v1DiskGroup.GET("/info", v1.GetDiskInfo)

//格式化磁盘
//format storage
v1DiskGroup.POST("/format", v1.FormatDisk)

//添加分区
v1DiskGroup.POST("/part", v1.AddPartition)
// add storage
v1DiskGroup.POST("/storage", v1.AddPartition)

//mount SATA disk
v1DiskGroup.POST("/mount", v1.PostMountDisk)

//umount sata disk
v1DiskGroup.POST("/umount", v1.PostDiskUmount)

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

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

//mount SATA disk
v1DiskGroup.POST("/mount", v1.PostMountDisk)

//umount SATA disk
v1DiskGroup.POST("/umount", v1.PostDiskUmount)
v1DiskGroup.DELETE("/remove/:id", v1.DeleteDisk)
}
v1ShareGroup := v1Group.Group("/share")
v1ShareGroup.Use()
Expand Down
38 changes: 19 additions & 19 deletions route/v1/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ func AppList(c *gin.Context) {
categoryId := c.DefaultQuery("category_id", "0")
key := c.DefaultQuery("key", "")
recommend, list, community := service.MyService.OAPI().GetServerList(index, size, t, categoryId, key)
for i := 0; i < len(recommend); i++ {
ct, _ := service.MyService.Docker().DockerListByImage(recommend[i].Image, recommend[i].ImageVersion)
if ct != nil {
list[i].State = ct.State
}
}
for i := 0; i < len(list); i++ {
ct, _ := service.MyService.Docker().DockerListByImage(list[i].Image, list[i].ImageVersion)
if ct != nil {
list[i].State = ct.State
}
}
for i := 0; i < len(community); i++ {
ct, _ := service.MyService.Docker().DockerListByImage(community[i].Image, community[i].ImageVersion)
if ct != nil {
list[i].State = ct.State
}
}
// for i := 0; i < len(recommend); i++ {
// ct, _ := service.MyService.Docker().DockerListByImage(recommend[i].Image, recommend[i].ImageVersion)
// if ct != nil {
// recommend[i].State = ct.State
// }
// }
// for i := 0; i < len(list); i++ {
// ct, _ := service.MyService.Docker().DockerListByImage(list[i].Image, list[i].ImageVersion)
// if ct != nil {
// list[i].State = ct.State
// }
// }
// for i := 0; i < len(community); i++ {
// ct, _ := service.MyService.Docker().DockerListByImage(community[i].Image, community[i].ImageVersion)
// if ct != nil {
// community[i].State = ct.State
// }
// }
data := make(map[string]interface{}, 3)
data["recommend"] = recommend
data["list"] = list
Expand Down Expand Up @@ -137,7 +137,7 @@ func AppUsageList(c *gin.Context) {
func AppInfo(c *gin.Context) {

id := c.Param("id")
info := service.MyService.OAPI().GetServerAppInfo(id)
info := service.MyService.OAPI().GetServerAppInfo(id, "")
if info.NetworkModel != "host" {
for i := 0; i < len(info.Ports); i++ {
if p, _ := strconv.Atoi(info.Ports[i].ContainerPort); port2.IsPortAvailable(p, info.Ports[i].Protocol) {
Expand Down
Loading

0 comments on commit fcb906a

Please sign in to comment.