diff --git a/backend-service/pkg/machine/info.go b/backend-service/pkg/machine/info.go index 2b10589..0aa6ffe 100644 --- a/backend-service/pkg/machine/info.go +++ b/backend-service/pkg/machine/info.go @@ -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, } } diff --git a/backend-service/pkg/service/socket.go b/backend-service/pkg/service/socket.go index 298e3fd..4c51c36 100644 --- a/backend-service/pkg/service/socket.go +++ b/backend-service/pkg/service/socket.go @@ -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) diff --git a/shared/pkg/utils/product.go b/shared/pkg/utils/product.go index 18d0eea..8400d21 100644 --- a/shared/pkg/utils/product.go +++ b/shared/pkg/utils/product.go @@ -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 }