You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

slurmdb_user.go 4.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package slurmer
  2. /*
  3. #cgo LDFLAGS: -lslurmdb
  4. #include <stdio.h>
  5. #include <slurm/slurm.h>
  6. #include <slurm/slurmdb.h>
  7. #include <memory.h>
  8. #include <malloc.h>
  9. typedef struct user_info_msg {
  10. uint32_t record_count;
  11. slurmdb_user_rec_t *user_array;
  12. } user_info_msg_t;
  13. typedef struct slurmdb_user_rec{
  14. uint16_t admin_level;
  15. List assoc_list;
  16. List coord_accts;
  17. char *default_acct;
  18. char *default_wckey;
  19. char *name;
  20. char *old_name;
  21. uint32_t uid;
  22. List wckey_list;
  23. } slurmdb_user_rec_pcm;
  24. struct user_info_msg get_user_info() {
  25. struct user_info_msg userinfo;
  26. List userList = NULL;
  27. slurmdb_user_cond_t *user_cond = NULL;
  28. void *db_conn;
  29. db_conn = slurmdb_connection_get();
  30. userList = slurmdb_users_get(db_conn, user_cond);
  31. slurmdb_connection_close(&db_conn);
  32. slurmdb_user_rec_t *rec = NULL;
  33. ListIterator itr = slurm_list_iterator_create(userList);
  34. int i = 0;
  35. uint32_t length;
  36. length = slurm_list_count(userList);
  37. userinfo.record_count = length;
  38. userinfo.user_array = malloc(length * sizeof(slurmdb_user_rec_t));
  39. while ((rec = slurm_list_next(itr))) {
  40. userinfo.user_array[i] = *rec;
  41. i++;
  42. }
  43. return userinfo;
  44. }
  45. struct slurmdb_user_rec *user_from_list(struct user_info_msg *list, int i) {
  46. return (struct slurmdb_user_rec *) &list->user_array[i];
  47. }
  48. */
  49. import "C"
  50. import (
  51. pbslurm "code.gitlink.org.cn/JCCE/PCM.git/adaptor/pcm_slurm/gen/idl"
  52. ssh "code.gitlink.org.cn/JCCE/PCM.git/common/ssh"
  53. "context"
  54. "strings"
  55. )
  56. type UserInfoMsg struct {
  57. LastUpdate int64
  58. RecordCount uint32
  59. UserInfoList []pbslurm.UserInfo
  60. }
  61. func UserDescriptorConvertCToGo(cStruct *C.struct_slurmdb_user_rec) pbslurm.UserInfo {
  62. var goStruct pbslurm.UserInfo
  63. goStruct.Name = C.GoString(cStruct.name)
  64. return goStruct
  65. }
  66. func GetUserInfo() UserInfoMsg {
  67. var goUserBuffer UserInfoMsg
  68. cUserBuffer := C.get_user_info()
  69. goUserBuffer.RecordCount = uint32(cUserBuffer.record_count)
  70. goUserBuffer.UserInfoList = make([]pbslurm.UserInfo, cUserBuffer.record_count, cUserBuffer.record_count)
  71. for i := uint32(0); i < goUserBuffer.RecordCount; i++ {
  72. user := C.user_from_list(&cUserBuffer, C.int(i))
  73. goUser := UserDescriptorConvertCToGo(user)
  74. goUserBuffer.UserInfoList[i] = goUser
  75. }
  76. return goUserBuffer
  77. }
  78. func (slurmStruct SlurmStruct) ListUsers(ctx context.Context, req *pbslurm.ListUsersReq) (*pbslurm.ListUsersResp, error) {
  79. userList := GetUserInfo()
  80. resp := pbslurm.ListUsersResp{}
  81. for _, user := range userList.UserInfoList {
  82. userInfoResult := user
  83. //userInfoResult.Name = user.Name
  84. resp.UserInfos = append(resp.UserInfos, &userInfoResult)
  85. }
  86. return &resp, nil
  87. }
  88. func (slurmStruct SlurmStruct) GetUser(ctx context.Context, req *pbslurm.GetUserReq) (*pbslurm.GetUserResp, error) {
  89. userList := GetUserInfo()
  90. resp := pbslurm.GetUserResp{}
  91. for _, user := range userList.UserInfoList {
  92. if strings.Contains(user.Name, req.UserName) {
  93. userInfoResult := user
  94. resp.UserInfo = append(resp.UserInfo, &userInfoResult)
  95. }
  96. }
  97. return &resp, nil
  98. }
  99. func (slurmStruct SlurmStruct) AddUser(ctx context.Context, req *pbslurm.AddUserReq) (*pbslurm.AddUserResp, error) {
  100. cmd := "/usr/local/bin/sacctmgr add user "
  101. cmd = cmd + req.Names
  102. if len(req.Accounts) != 0 {
  103. cmd = cmd + " Accounts=" + req.Accounts
  104. }
  105. if len(req.AdminLevel) != 0 {
  106. cmd = cmd + " AdminLevel=" + req.AdminLevel
  107. }
  108. if len(req.Clusters) != 0 {
  109. cmd = cmd + " Clusters=" + req.Clusters
  110. }
  111. if len(req.DefaultAccount) != 0 {
  112. cmd = cmd + " DefaultAccount=" + req.DefaultAccount
  113. }
  114. if len(req.DefaultQos) != 0 {
  115. cmd = cmd + " DefaultQOS=" + req.DefaultQos
  116. }
  117. if len(req.DefaultWckey) != 0 {
  118. cmd = cmd + " DefaultWCKey=" + req.DefaultWckey
  119. }
  120. if len(req.FairShare) != 0 {
  121. cmd = cmd + " Fairshare=" + req.FairShare
  122. }
  123. if len(req.MaxCpuMins) != 0 {
  124. cmd = cmd + " MaxCPUMins=" + req.MaxCpuMins
  125. }
  126. if len(req.MaxCpus) != 0 {
  127. cmd = cmd + " MaxCPUs=" + req.MaxCpus
  128. }
  129. if len(req.MaxJobs) != 0 {
  130. cmd = cmd + " MaxJobs=" + req.MaxJobs
  131. }
  132. if len(req.MaxNodes) != 0 {
  133. cmd = cmd + " MaxNodes=" + req.MaxNodes
  134. }
  135. if len(req.MaxSubmitJobs) != 0 {
  136. cmd = cmd + " MaxSubmitJobs=" + req.MaxSubmitJobs
  137. }
  138. if len(req.MaxWall) != 0 {
  139. cmd = cmd + " MaxWall=" + req.MaxWall
  140. }
  141. if len(req.Partitions) != 0 {
  142. cmd = cmd + " Partitions=" + req.Partitions
  143. }
  144. if len(req.QosLevel) != 0 {
  145. cmd = cmd + " QosLevel=" + req.QosLevel
  146. }
  147. cmd = cmd + " -i"
  148. result := ssh.ExecCommand(cmd)
  149. resp := pbslurm.AddUserResp{}
  150. resp.Result = result
  151. return &resp, nil
  152. }
  153. func (slurmStruct SlurmStruct) DeleteUser(ctx context.Context, req *pbslurm.DeleteUserReq) (*pbslurm.DeleteUserResp, error) {
  154. cmd := "/usr/local/bin/sacctmgr delete user "
  155. cmd = cmd + req.Names
  156. cmd = cmd + " -i"
  157. result := ssh.ExecCommand(cmd)
  158. resp := pbslurm.DeleteUserResp{}
  159. resp.Result = result
  160. return &resp, nil
  161. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.