diff --git a/internal/config/config.go b/internal/config/config.go index 428e3dad2..60dac8e71 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -36,6 +36,13 @@ type Config struct { OpenstackRpcConf zrpc.RpcClientConf OctopusRpcConf zrpc.RpcClientConf PcmCoreRpcConf zrpc.RpcClientConf + BlockChain struct { + ContractAddress string + FunctionName string + Url string + Type string + MemberName string + } MinioConf struct { Secret string diff --git a/internal/logic/cloud/commitgeneraltasklogic.go b/internal/logic/cloud/commitgeneraltasklogic.go index 0a101cea1..e07fc804c 100644 --- a/internal/logic/cloud/commitgeneraltasklogic.go +++ b/internal/logic/cloud/commitgeneraltasklogic.go @@ -11,10 +11,12 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/remoteUtil" "io" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml" + "k8s.io/apimachinery/pkg/util/json" kyaml "k8s.io/apimachinery/pkg/util/yaml" "strconv" "strings" @@ -55,7 +57,6 @@ func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) er logx.Info("commit success") } }() - //TODO adapter adapterId, _ := strconv.ParseUint(req.AdapterIds[0], 10, 64) var clusters []*models.CloudModel err := tx.Raw("SELECT * FROM `t_cluster` where adapter_id in ? and id in ?", req.AdapterIds, req.ClusterIds).Scan(&clusters).Error @@ -131,6 +132,19 @@ func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) er db := tx.Table("task").Create(&taskModel) db = tx.Table("task_cloud").Create(&taskClouds) db = tx.Table("t_notice").Create(¬iceInfo) + // 数据上链 + bytes, err := json.Marshal(taskModel) + if err != nil { + return err + } + remoteUtil.Evidence(remoteUtil.EvidenceParam{ + Url: l.svcCtx.Config.BlockChain.Url, + ContractAddress: l.svcCtx.Config.BlockChain.ContractAddress, + FunctionName: l.svcCtx.Config.BlockChain.FunctionName, + MemberName: l.svcCtx.Config.BlockChain.MemberName, + Type: l.svcCtx.Config.BlockChain.Type, + Args: []string{strconv.FormatInt(taskModel.Id, 10), string(bytes)}, + }) if db.Error != nil { logx.Errorf("Task creation failure, err: %v", db.Error) return errors.New("task creation failure") diff --git a/internal/logic/hpc/commithpctasklogic.go b/internal/logic/hpc/commithpctasklogic.go index 790ba6bda..7cc2cdd13 100644 --- a/internal/logic/hpc/commithpctasklogic.go +++ b/internal/logic/hpc/commithpctasklogic.go @@ -6,7 +6,9 @@ import ( "github.com/go-resty/resty/v2" clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/remoteUtil" v1 "gitlink.org.cn/JointCloud/pcm-hpc/routers/v1" + "k8s.io/apimachinery/pkg/util/json" "strconv" "time" @@ -116,7 +118,16 @@ func (l *CommitHpcTaskLogic) CommitHpcTask(req *types.CommitHpcTaskReq) (resp *t resp = &types.CommitHpcTaskResp{ JobId: string(jobId), } - + // 数据上链 + bytes, _ := json.Marshal(taskModel) + remoteUtil.Evidence(remoteUtil.EvidenceParam{ + Url: l.svcCtx.Config.BlockChain.Url, + ContractAddress: l.svcCtx.Config.BlockChain.ContractAddress, + FunctionName: l.svcCtx.Config.BlockChain.FunctionName, + MemberName: l.svcCtx.Config.BlockChain.MemberName, + Type: l.svcCtx.Config.BlockChain.Type, + Args: []string{strconv.FormatInt(taskModel.Id, 10), string(bytes)}, + }) return resp, nil } diff --git a/pkg/models/taskmodel_gen.go b/pkg/models/taskmodel_gen.go index 6f5baa881..e57c0f5db 100644 --- a/pkg/models/taskmodel_gen.go +++ b/pkg/models/taskmodel_gen.go @@ -35,22 +35,22 @@ type ( } Task struct { - Id int64 `db:"id"` // id - Name string `db:"name"` // 作业名称 - Description string `db:"description"` // 作业描述 - Status string `db:"status"` // 作业状态 - Strategy int64 `db:"strategy"` // 策略 - SynergyStatus int64 `db:"synergy_status"` // 协同状态(0-未协同、1-已协同) - CommitTime time.Time `db:"commit_time"` // 提交时间 - StartTime *time.Time `db:"start_time"` // 开始时间 - EndTime *time.Time `db:"end_time"` // 结束运行时间 - RunningTime int64 `db:"running_time"` // 已运行时间(单位秒) - YamlString string `db:"yaml_string"` - Result string `db:"result"` // 作业结果 - DeletedAt gorm.DeletedAt `gorm:"index"` - NsID string `db:"ns_id"` - AdapterTypeDict string `db:"adapter_type_dict"` //任务类型(对应字典表的值) - TaskTypeDict string `db:"task_type_dict"` + Id int64 `db:"id" json:"id"` // id + Name string `db:"name" json:"name"` // 作业名称 + Description string `db:"description" json:"description"` // 作业描述 + Status string `db:"status" json:"status"` // 作业状态 + Strategy int64 `db:"strategy" json:"strategy"` // 策略 + SynergyStatus int64 `db:"synergy_status" json:"synergyStatus"` // 协同状态(0-未协同、1-已协同) + CommitTime time.Time `db:"commit_time" json:"commitTime"` // 提交时间 + StartTime *time.Time `db:"start_time" json:"startTime"` // 开始时间 + EndTime *time.Time `db:"end_time" json:"endTime"` // 结束运行时间 + RunningTime int64 `db:"running_time" json:"runningTime"` // 已运行时间(单位秒) + YamlString string `db:"yaml_string" json:"yamlString"` + Result string `db:"result" json:"result"` // 作业结果 + DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` + NsID string `db:"ns_id" json:"nsID"` + AdapterTypeDict string `db:"adapter_type_dict" json:"adapterTypeDict"` //任务类型(对应字典表的值) + TaskTypeDict string `db:"task_type_dict" json:"taskTypeDict"` } ) diff --git a/pkg/utils/remoteUtil/BlockChain.go b/pkg/utils/remoteUtil/BlockChain.go new file mode 100644 index 000000000..cd95eea9f --- /dev/null +++ b/pkg/utils/remoteUtil/BlockChain.go @@ -0,0 +1,25 @@ +package remoteUtil + +import ( + "github.com/go-resty/resty/v2" +) + +type EvidenceParam struct { + Url string `json:"url"` + Type string `json:"type"` + MemberName string `json:"memberName"` + ContractAddress string `json:"contractAddress"` + FunctionName string `json:"functionName"` + Args []string `json:"args"` +} + +func Evidence(EvidenceParam EvidenceParam) error { + httpClient := resty.New().R() + _, err := httpClient.SetHeader("Content-Type", "application/json"). + SetBody(&EvidenceParam). + Post(EvidenceParam.Url) + if err != nil { + return err + } + return nil +}