goploy/controller/ServerController.go

475 lines
15 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package controller
import (
2021-09-18 16:35:50 +08:00
"bytes"
2021-07-09 20:46:12 +08:00
"github.com/pkg/sftp"
2020-08-04 14:28:25 +08:00
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
2021-12-21 16:49:17 +08:00
"github.com/zhenorzz/goploy/response"
2020-08-04 14:28:25 +08:00
"github.com/zhenorzz/goploy/utils"
2021-12-22 10:59:12 +08:00
"io"
2021-02-14 14:22:22 +08:00
"io/ioutil"
2021-12-21 16:49:17 +08:00
"net/http"
2021-07-09 20:46:12 +08:00
"strconv"
2021-09-18 16:35:50 +08:00
"strings"
2020-08-04 14:28:25 +08:00
)
// Server struct
type Server Controller
2021-12-21 16:49:17 +08:00
func (s Server) Routes() []core.Route {
return []core.Route{
core.NewRoute("/server/getList", http.MethodGet, s.GetList),
core.NewRoute("/server/getTotal", http.MethodGet, s.GetTotal),
core.NewRoute("/server/getOption", http.MethodGet, s.GetOption),
core.NewRoute("/server/getPublicKey", http.MethodGet, s.GetPublicKey),
core.NewRoute("/server/check", http.MethodPost, s.Check).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/add", http.MethodPost, s.Add).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/edit", http.MethodPut, s.Edit).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/toggle", http.MethodPut, s.Toggle).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/downloadFile", http.MethodGet, s.DownloadFile).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/uploadFile", http.MethodPost, s.UploadFile).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/report", http.MethodGet, s.Report).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/getAllMonitor", http.MethodGet, s.GetAllMonitor),
core.NewRoute("/server/addMonitor", http.MethodPost, s.AddMonitor).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/editMonitor", http.MethodPut, s.EditMonitor).Roles(core.RoleAdmin, core.RoleManager),
core.NewRoute("/server/deleteMonitor", http.MethodDelete, s.DeleteMonitor).Roles(core.RoleAdmin, core.RoleManager),
}
}
func (Server) GetList(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
serverList, err := model.Server{NamespaceID: gp.Namespace.ID}.GetList(pagination)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2020-09-26 11:45:44 +08:00
Data: struct {
Servers model.Servers `json:"list"`
}{Servers: serverList},
}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (Server) GetTotal(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
total, err := model.Server{NamespaceID: gp.Namespace.ID}.GetTotal()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2020-09-26 11:45:44 +08:00
Data: struct {
Total int64 `json:"total"`
}{Total: total},
}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (Server) GetOption(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
serverList, err := model.Server{NamespaceID: gp.Namespace.ID}.GetAll()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2020-09-26 11:45:44 +08:00
Data: struct {
Servers model.Servers `json:"list"`
}{Servers: serverList},
}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (Server) GetPublicKey(gp *core.Goploy) core.Response {
2021-11-09 14:12:28 +08:00
publicKeyPath := gp.URLQuery.Get("path")
2021-02-14 14:22:22 +08:00
2021-11-09 14:12:28 +08:00
contentByte, err := ioutil.ReadFile(publicKeyPath + ".pub")
2021-02-14 14:22:22 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-02-14 14:22:22 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-06-22 19:59:39 +08:00
Data: struct {
Key string `json:"key"`
}{Key: string(contentByte)},
2021-02-14 14:22:22 +08:00
}
}
2021-12-21 16:49:17 +08:00
func (Server) Check(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2021-02-07 15:43:02 +08:00
IP string `json:"ip" validate:"required,ip|hostname"`
Port int `json:"port" validate:"min=0,max=65535"`
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password" validate:"max=255"`
2020-08-04 14:28:25 +08:00
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-09-18 16:35:50 +08:00
if Conn, err := utils.DialSSH(reqData.Owner, reqData.Password, reqData.Path, reqData.IP, reqData.Port); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-18 16:35:50 +08:00
} else {
_ = Conn.Close()
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{Message: "Connected"}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (s Server) Add(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
Name string `json:"name" validate:"required"`
2021-02-26 16:45:13 +08:00
NamespaceID int64 `json:"namespaceId" validate:"gte=0"`
IP string `json:"ip" validate:"ip|hostname"`
2020-08-04 14:28:25 +08:00
Port int `json:"port" validate:"min=0,max=65535"`
2021-02-07 15:43:02 +08:00
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password"`
2020-08-04 14:28:25 +08:00
Description string `json:"description" validate:"max=255"`
}
2020-09-26 11:45:44 +08:00
2020-08-04 14:28:25 +08:00
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
id, err := model.Server{
2021-02-26 16:45:13 +08:00
NamespaceID: reqData.NamespaceID,
2020-08-04 14:28:25 +08:00
Name: reqData.Name,
IP: reqData.IP,
Port: reqData.Port,
Owner: reqData.Owner,
2021-02-07 15:43:02 +08:00
Path: reqData.Path,
Password: reqData.Password,
2020-08-04 14:28:25 +08:00
Description: reqData.Description,
2021-12-30 10:44:59 +08:00
OSInfo: s.getOSInfo(reqData.Owner, reqData.Password, reqData.Path, reqData.IP, reqData.Port),
2020-08-04 14:28:25 +08:00
}.AddRow()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2020-09-26 11:45:44 +08:00
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (s Server) Edit(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
2021-02-26 16:45:13 +08:00
NamespaceID int64 `json:"namespaceId" validate:"gte=0"`
2020-08-04 14:28:25 +08:00
Name string `json:"name" validate:"required"`
2021-02-04 11:15:43 +08:00
IP string `json:"ip" validate:"required,ip|hostname"`
2020-08-04 14:28:25 +08:00
Port int `json:"port" validate:"min=0,max=65535"`
2021-02-07 15:43:02 +08:00
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password" validate:"max=255"`
2020-08-04 14:28:25 +08:00
Description string `json:"description" validate:"max=255"`
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
err := model.Server{
ID: reqData.ID,
2021-02-26 16:45:13 +08:00
NamespaceID: reqData.NamespaceID,
2020-08-04 14:28:25 +08:00
Name: reqData.Name,
IP: reqData.IP,
Port: reqData.Port,
Owner: reqData.Owner,
2021-02-07 15:43:02 +08:00
Path: reqData.Path,
Password: reqData.Password,
2020-08-04 14:28:25 +08:00
Description: reqData.Description,
2021-12-30 10:44:59 +08:00
OSInfo: s.getOSInfo(reqData.Owner, reqData.Password, reqData.Path, reqData.IP, reqData.Port),
2020-08-04 14:28:25 +08:00
}.EditRow()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
func (Server) Toggle(gp *core.Goploy) core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2021-06-22 19:59:39 +08:00
ID int64 `json:"id" validate:"gt=0"`
State int8 `json:"state" validate:"oneof=0 1"`
2020-08-04 14:28:25 +08:00
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-05-25 16:12:08 +08:00
if err := (model.Server{ID: reqData.ID, State: reqData.State}).ToggleRow(); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{}
2020-08-04 14:28:25 +08:00
}
2021-07-09 20:46:12 +08:00
2021-09-29 15:59:42 +08:00
// DownloadFile sftp download file
2021-12-21 16:49:17 +08:00
func (Server) DownloadFile(gp *core.Goploy) core.Response {
2021-07-09 20:46:12 +08:00
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: "invalid server id"}
2021-07-09 20:46:12 +08:00
}
server, err := (model.Server{ID: id}).GetData()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-07-09 20:46:12 +08:00
}
client, err := utils.DialSSH(server.Owner, server.Password, server.Path, server.IP, server.Port)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-07-09 20:46:12 +08:00
}
2021-12-21 16:49:17 +08:00
return response.SftpFile{Filename: gp.URLQuery.Get("file"), Client: client}
2021-07-09 20:46:12 +08:00
}
2021-09-18 16:35:50 +08:00
2021-09-29 15:59:42 +08:00
// UploadFile sftp upload file
2021-12-21 16:49:17 +08:00
func (Server) UploadFile(gp *core.Goploy) core.Response {
2022-01-08 21:35:57 +08:00
type ReqData struct {
ID int64 `schema:"id" validate:"gt=0"`
FilePath string `schema:"filePath" validate:"required"`
2021-09-29 15:59:42 +08:00
}
2022-01-08 21:35:57 +08:00
var reqData ReqData
if err := decodeQuery(gp.URLQuery, &reqData); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
server, err := (model.Server{ID: reqData.ID}).GetData()
2021-09-29 15:59:42 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
file, fileHandler, err := gp.Request.FormFile("file")
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
defer file.Close()
client, err := utils.DialSSH(server.Owner, server.Password, server.Path, server.IP, server.Port)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
defer client.Close()
sftpClient, err := sftp.NewClient(client)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
defer sftpClient.Close()
2022-01-08 21:35:57 +08:00
remoteFile, err := sftpClient.Create(reqData.FilePath + "/" + fileHandler.Filename)
2021-09-29 15:59:42 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
2021-12-22 10:59:12 +08:00
defer remoteFile.Close()
2021-09-29 15:59:42 +08:00
2021-12-22 10:59:12 +08:00
_, err = io.Copy(remoteFile, file)
2021-09-29 15:59:42 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-09-29 15:59:42 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{}
2021-09-29 15:59:42 +08:00
}
2021-12-21 16:49:17 +08:00
func (Server) Report(gp *core.Goploy) core.Response {
2022-01-08 21:35:57 +08:00
type ReqData struct {
ServerID int64 `schema:"serverId" validate:"gt=0"`
Type int `schema:"type" validate:"gt=0"`
2022-01-10 10:02:25 +08:00
DatetimeRange string `schema:"datetimeRange" validate:"required"`
2021-11-09 14:12:28 +08:00
}
2022-01-08 21:35:57 +08:00
var reqData ReqData
if err := decodeQuery(gp.URLQuery, &reqData); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-09 14:12:28 +08:00
}
2022-01-08 21:35:57 +08:00
datetimeRange := strings.Split(reqData.DatetimeRange, ",")
2021-11-09 14:12:28 +08:00
if len(datetimeRange) != 2 {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: "invalid datetime range"}
2021-11-09 14:12:28 +08:00
}
2022-01-08 21:35:57 +08:00
serverAgentLogs, err := (model.ServerAgentLog{ServerID: reqData.ServerID, Type: reqData.Type}).GetListBetweenTime(datetimeRange[0], datetimeRange[1])
2021-11-09 14:12:28 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-09 14:12:28 +08:00
}
type Flag struct {
Count int
Curr int
}
flagMap := map[string]Flag{}
for _, log := range serverAgentLogs {
if _, ok := flagMap[log.Item]; !ok {
flagMap[log.Item] = Flag{}
}
flagMap[log.Item] = Flag{Count: flagMap[log.Item].Count + 1}
}
serverAgentMap := map[string]model.ServerAgentLogs{}
for _, log := range serverAgentLogs {
flagMap[log.Item] = Flag{
Count: flagMap[log.Item].Count,
Curr: flagMap[log.Item].Curr + 1,
}
step := flagMap[log.Item].Count / 60
if flagMap[log.Item].Count <= 60 ||
flagMap[log.Item].Curr%step == 0 ||
flagMap[log.Item].Count-1 == flagMap[log.Item].Curr {
serverAgentMap[log.Item] = append(serverAgentMap[log.Item], log)
}
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-11-09 14:12:28 +08:00
Data: struct {
ServerAgentMap map[string]model.ServerAgentLogs `json:"map"`
}{ServerAgentMap: serverAgentMap},
}
}
2021-12-21 16:49:17 +08:00
func (Server) GetAllMonitor(gp *core.Goploy) core.Response {
2021-11-20 15:51:19 +08:00
serverID, err := strconv.ParseInt(gp.URLQuery.Get("serverId"), 10, 64)
serverMonitorList, err := model.ServerMonitor{ServerID: serverID}.GetAll()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-11-20 15:51:19 +08:00
Data: struct {
List model.ServerMonitors `json:"list"`
}{List: serverMonitorList},
}
}
2021-12-21 16:49:17 +08:00
func (s Server) AddMonitor(gp *core.Goploy) core.Response {
2021-11-20 15:51:19 +08:00
type ReqData struct {
ServerID int64 `json:"serverId" validate:"required"`
Item string `json:"item" validate:"required"`
Formula string `json:"formula" validate:"required"`
Operator string `json:"operator" validate:"required"`
Value string `json:"value" validate:"required"`
GroupCycle int `json:"groupCycle" validate:"required"`
LastCycle int `json:"lastCycle" validate:"required"`
SilentCycle int `json:"silentCycle" validate:"required"`
StartTime string `json:"startTime" validate:"required,len=5"`
EndTime string `json:"endTime" validate:"required,len=5"`
NotifyType uint8 `json:"notifyType" validate:"gt=0"`
NotifyTarget string `json:"notifyTarget" validate:"required"`
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
id, err := model.ServerMonitor{
ServerID: reqData.ServerID,
Item: reqData.Item,
Formula: reqData.Formula,
Operator: reqData.Operator,
Value: reqData.Value,
GroupCycle: reqData.GroupCycle,
LastCycle: reqData.LastCycle,
SilentCycle: reqData.SilentCycle,
StartTime: reqData.StartTime,
EndTime: reqData.EndTime,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
}.AddRow()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-11-20 15:51:19 +08:00
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
}
2021-12-21 16:49:17 +08:00
func (s Server) EditMonitor(gp *core.Goploy) core.Response {
2021-11-20 15:51:19 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"required"`
Item string `json:"item" validate:"required"`
Formula string `json:"formula" validate:"required"`
Operator string `json:"operator" validate:"required"`
Value string `json:"value" validate:"required"`
GroupCycle int `json:"groupCycle" validate:"required"`
LastCycle int `json:"lastCycle" validate:"required"`
SilentCycle int `json:"silentCycle" validate:"required"`
StartTime string `json:"startTime" validate:"required,len=5"`
EndTime string `json:"endTime" validate:"required,len=5"`
NotifyType uint8 `json:"notifyType" validate:"gt=0"`
NotifyTarget string `json:"notifyTarget" validate:"required"`
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
err := model.ServerMonitor{
ID: reqData.ID,
Item: reqData.Item,
Formula: reqData.Formula,
Operator: reqData.Operator,
Value: reqData.Value,
GroupCycle: reqData.GroupCycle,
LastCycle: reqData.LastCycle,
SilentCycle: reqData.SilentCycle,
StartTime: reqData.StartTime,
EndTime: reqData.EndTime,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
}.EditRow()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{}
2021-11-20 15:51:19 +08:00
}
2021-12-21 16:49:17 +08:00
func (s Server) DeleteMonitor(gp *core.Goploy) core.Response {
2021-11-20 15:51:19 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"required"`
}
var reqData ReqData
2022-01-08 21:35:57 +08:00
if err := decodeJson(gp.Body, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
err := model.ServerMonitor{
ID: reqData.ID,
}.DeleteRow()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-11-20 15:51:19 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{}
2021-11-20 15:51:19 +08:00
}
2021-09-18 16:35:50 +08:00
// version|cpu cores|mem
2021-12-30 10:44:59 +08:00
func (Server) getOSInfo(owner, password, path, ip string, port int) string {
2021-09-18 16:35:50 +08:00
osInfoScript := `cat /etc/os-release | grep "PRETTY_NAME" | awk -F\" '{print $2}' && cat /proc/cpuinfo | grep "processor" | wc -l && cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
2021-12-30 10:44:59 +08:00
client, err := utils.DialSSH(owner, password, path, ip, port)
if err != nil {
return ""
}
defer client.Close()
2021-09-18 16:35:50 +08:00
2021-12-30 10:44:59 +08:00
session, err := client.NewSession()
if err != nil {
return ""
}
defer session.Close()
2021-09-18 16:35:50 +08:00
2021-12-30 10:44:59 +08:00
var sshOutbuf, sshErrbuf bytes.Buffer
session.Stdout = &sshOutbuf
session.Stderr = &sshErrbuf
if err := session.Run(osInfoScript); err != nil {
2021-09-18 16:35:50 +08:00
return ""
}
2021-12-30 10:44:59 +08:00
2021-09-18 16:35:50 +08:00
// version|cpu cores|mem
2021-12-30 10:44:59 +08:00
return strings.Replace(strings.Trim(sshOutbuf.String(), "\n"), "\n", "|", -1)
2021-09-18 16:35:50 +08:00
}