添加productInfo并将productId改为uuid

This commit is contained in:
Pan Qiancheng 2025-04-29 10:32:47 +08:00
parent c35fe7fd82
commit 68d4d63a92
3 changed files with 42 additions and 24 deletions

View File

@ -1,6 +1,8 @@
package machine
import (
"bash_go_service/shared/pkg/logger"
"bash_go_service/shared/pkg/utils"
"bufio"
"fmt"
"os"
@ -10,22 +12,28 @@ import (
)
type Info struct {
Hostname string
OS string
Kernel string
CPU string
Memory string
GPU string
Hostname string `json:"hostname"`
OS string `json:"os"`
Kernel string `json:"kernel"`
CPU string `json:"cpu"`
Memory string `json:"memory"`
GPU string `json:"gpu"`
ProductInfo utils.ProductInfo `json:"productInfo"`
}
func CollectMachineInfo() *Info {
productInfo, err := utils.GetProductInfo()
if err != nil {
logger.Debug("cannot get product info: %v", err)
}
return &Info{
Hostname: getHostname(),
OS: getOSInfo(),
Kernel: getKernelVersion(),
CPU: getCPUInfo(),
Memory: getMemoryInfo(),
GPU: getGPUInfo(),
Hostname: getHostname(),
OS: getOSInfo(),
Kernel: getKernelVersion(),
CPU: getCPUInfo(),
Memory: getMemoryInfo(),
GPU: getGPUInfo(),
ProductInfo: *productInfo,
}
}

View File

@ -4,6 +4,7 @@ import (
"bash_go_service/shared/pkg/client"
"bash_go_service/shared/pkg/constants"
"bash_go_service/shared/pkg/logger"
"bash_go_service/shared/pkg/utils"
"context"
"encoding/binary"
"net"
@ -16,12 +17,13 @@ import (
)
type ExecParams struct {
Filename string `json:"filename"`
Pwd string `json:"pwd"`
Args []string `json:"args"`
Env []string `json:"env"`
LogPath string `json:"logPath"`
Error string `json:"error"`
Filename string `json:"filename"`
Pwd string `json:"pwd"`
Args []string `json:"args"`
Env []string `json:"env"`
LogPath string `json:"logPath"`
Error string `json:"error"`
ProductInfo utils.ProductInfo `json:"productInfo"`
}
type UnixSocketTask struct {
@ -169,6 +171,14 @@ func (t *UnixSocketTask) handleConnection(conn net.Conn) {
params.Error = string(context)
}
info, err := utils.GetProductInfo()
if err != nil {
logger.Error("GetProductInfo error: %v", err)
params.ProductInfo = utils.ProductInfo{}
} else {
params.ProductInfo = *info
}
// 在HTTP调用前发送[start]标记
// 在HTTP调用前发送[start]标记
t.writeMessage(conn, "[sthttp]")
@ -216,7 +226,7 @@ func (t *UnixSocketTask) handleConnection(conn net.Conn) {
}
}
err := client.PostToStream(constants.QuestionStreamApi, params, nil, handleQuestion)
err = client.PostToStream(constants.QuestionStreamApi, params, nil, handleQuestion)
if err != nil {
logger.Error("PostToStream error: %v", err)

View File

@ -8,25 +8,25 @@ import (
)
type ProductInfo struct {
BashProductId string `json:"productId"`
BashUUID string `json:"bash_uuid"`
BashInstanceId string `json:"instanceId"`
}
func GetProductInfo() (*ProductInfo, error) {
envs := os.Environ()
var productId, instanceId string
var uuid, instanceId string
for _, env := range envs {
if strings.HasPrefix(env, constants.ProductEnvName) {
productId = strings.TrimPrefix(env, constants.ProductEnvName+"=")
uuid = strings.TrimPrefix(env, constants.ProductEnvName+"=")
} else if strings.HasPrefix(env, constants.BashInstanceId) {
instanceId = strings.TrimPrefix(env, constants.BashInstanceId+"=")
}
}
if productId == "" {
if uuid == "" {
return nil, fmt.Errorf("product ID not found in environment variables")
}
return &ProductInfo{
BashProductId: productId,
BashUUID: uuid,
BashInstanceId: instanceId,
}, nil
}