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

View File

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

View File

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