diff --git a/adaptor/pcm_slurm/idl/slurmdb_cluster.proto b/adaptor/pcm_slurm/idl/slurmdb_cluster.proto index 9322df4b..2c90c29d 100644 --- a/adaptor/pcm_slurm/idl/slurmdb_cluster.proto +++ b/adaptor/pcm_slurm/idl/slurmdb_cluster.proto @@ -22,3 +22,48 @@ message ClusterInfo{ AssociationInfo root_assoc = 12 ; int32 rpc_version = 13 ; } + +message ListClustersReq{ + SlurmVersion slurm_version = 1; +} + +message ListClustersResp { + repeated ClusterInfo cluster_infos = 1; +} + +message GetClusterReq{ + SlurmVersion slurm_version = 1; + string cluster_name = 2; +} + +message GetClusterResp { + repeated ClusterInfo cluster_infos = 1; +} + +message AddClusterReq{ + SlurmVersion slurm_version = 1; + string name =2; + string nodes =3; + string control_port = 4 ; + string cpu_count = 5 ; + string dimensions = 6 ; + string dim_size = 7 ; + string flags = 8 ; + string classification = 9 ; + string control_host = 10 ; + string plugin_id_select = 11 ; + string rpc_version = 13 ; +} + +message AddClusterResp { + string result = 1; +} + +message DeleteClusterReq{ + SlurmVersion slurm_version = 1; + string names=2; +} + +message DeleteClusterResp { + string result = 1; +} \ No newline at end of file diff --git a/adaptor/pcm_slurm/service/slurmdb_cluster.go b/adaptor/pcm_slurm/service/slurmdb_cluster.go new file mode 100644 index 00000000..a84d082f --- /dev/null +++ b/adaptor/pcm_slurm/service/slurmdb_cluster.go @@ -0,0 +1,169 @@ +package slurmer + +/* +#cgo LDFLAGS: -lslurmdb + +#include +#include +#include +#include +#include + +typedef struct cluster_info_msg { + uint32_t record_count; + slurmdb_cluster_rec_t *cluster_array; +} cluster_info_msg_t; + +typedef struct slurmdb_cluster_rec{ + char *name; +} slurmdb_cluster_rec_pcm; + +struct cluster_info_msg get_cluster_info() { + struct cluster_info_msg clusterinfo; + List clusterList = NULL; + slurmdb_cluster_cond_t *cluster_cond = NULL; + void *db_conn; + db_conn = slurmdb_connection_get(); + clusterList = slurmdb_clusters_get(db_conn, cluster_cond); + slurmdb_connection_close(&db_conn); + + slurmdb_cluster_rec_t *rec = NULL; + ListIterator itr = slurm_list_iterator_create(clusterList); + int i = 0; + uint32_t length; + length = slurm_list_count(clusterList); + clusterinfo.record_count = length; + clusterinfo.cluster_array = malloc(length * sizeof(slurmdb_cluster_rec_t)); + while ((rec = slurm_list_next(itr))) { + clusterinfo.cluster_array[i] = *rec; + i++; + } + return clusterinfo; +} + +struct slurmdb_cluster_rec *cluster_from_list(struct cluster_info_msg *list, int i) { + return (struct slurmdb_cluster_rec *) &list->cluster_array[i]; +} + +*/ +import "C" +import ( + pbslurm "code.gitlink.org.cn/JCCE/PCM.git/adaptor/pcm_slurm/gen/idl" + "code.gitlink.org.cn/JCCE/PCM.git/common/ssh" + "context" + "strings" +) + +type ClusterInfoMsg struct { + Last_update int64 + Record_count uint32 + ClusterInfoList []pbslurm.ClusterInfo +} + +func Cluster_descriptor_convert_c_to_go(c_struct *C.struct_slurmdb_cluster_rec) pbslurm.ClusterInfo { + var go_struct pbslurm.ClusterInfo + go_struct.Name = C.GoString(c_struct.name) + return go_struct +} + +func GetClusterInfo() ClusterInfoMsg { + var go_cluster_buffer ClusterInfoMsg + c_cluster_buffer := C.get_cluster_info() + go_cluster_buffer.Record_count = uint32(c_cluster_buffer.record_count) + go_cluster_buffer.ClusterInfoList = make([]pbslurm.ClusterInfo, c_cluster_buffer.record_count, c_cluster_buffer.record_count) + + for i := uint32(0); i < go_cluster_buffer.Record_count; i++ { + cluster := C.cluster_from_list(&c_cluster_buffer, C.int(i)) + go_cluster := Cluster_descriptor_convert_c_to_go(cluster) + go_cluster_buffer.ClusterInfoList[i] = go_cluster + } + return go_cluster_buffer +} + +func (slurmStruct SlurmStruct) GetAllClusters(ctx context.Context, req *pbslurm.ListClustersReq) (*pbslurm.ListClustersResp, error) { + + clusterList := GetClusterInfo() + + resp := pbslurm.ListClustersResp{} + for _, cluster := range clusterList.ClusterInfoList { + clusterInfoResult := pbslurm.ClusterInfo{} + clusterInfoResult = cluster + resp.ClusterInfos = append(resp.ClusterInfos, &clusterInfoResult) + } + return &resp, nil +} + +func (slurmStruct SlurmStruct) DeleteCluster(ctx context.Context, req *pbslurm.DeleteClusterReq) (*pbslurm.DeleteClusterResp, error) { + + cmd := "usr/local/bin/sacctmgr delete cluster" + cmd = cmd + req.Names + cmd = cmd + "-i" + + result := ssh.ExecCommand(cmd) + //var go_cluster_buffer ClusterInfoMsg + resp := pbslurm.DeleteClusterResp{} + resp.Result = result + return &resp, nil + +} + +func (slurmStruct SlurmStruct) AddCluster(ctx context.Context, req *pbslurm.AddClusterReq) (*pbslurm.AddClusterResp, error) { + + cmd := "/usr/local/bin/sacctmgr add cluster " + + cmd = cmd + req.Name + if len(req.Nodes) != 0 { + cmd = cmd + " Nodes=" + req.Nodes + } + if len(req.Classification) != 0 { + cmd = cmd + " Classification=" + req.Classification + } + if len(req.ControlHost) != 0 { + cmd = cmd + " ControlHost=\"" + req.ControlHost + "\"" + } + if len(req.ControlPort) != 0 { + cmd = cmd + " ControlPort=" + req.ControlPort + } + if len(req.CpuCount) != 0 { + cmd = cmd + " CpuCount=" + req.CpuCount + } + if len(req.Dimensions) != 0 { + cmd = cmd + " Dimensions=" + req.Dimensions + } + if len(req.DimSize) != 0 { + cmd = cmd + " DimSize=" + req.DimSize + } + if len(req.Flags) != 0 { + cmd = cmd + " Flags=" + req.Flags + } + if len(req.PluginIdSelect) != 0 { + cmd = cmd + " GrpNodes=" + req.PluginIdSelect + } + if len(req.RpcVersion) != 0 { + cmd = cmd + " RpcVersion=" + req.RpcVersion + } + + cmd = cmd + " -i" + + result := ssh.ExecCommand(cmd) + + resp := pbslurm.AddClusterResp{} + resp.Result = result + + return &resp, nil +} + +func (slurmStruct SlurmStruct) GetCluster(ctx context.Context, req *pbslurm.GetClusterReq) (*pbslurm.GetClusterResp, error) { + + clusterList := GetClusterInfo() + + resp := pbslurm.GetClusterResp{} + for _, cluster := range clusterList.ClusterInfoList { + clusterInfoResult := cluster + if strings.Contains(cluster.Name, req.ClusterName) { + resp.ClusterInfos = append(resp.ClusterInfos, &clusterInfoResult) + } + } + + return &resp, nil +}